Fix diagnostic for static methods referencing fields from using decls

Previously we thought the instance member was a function, not a field,
and we'd say something silly like:
  t.cpp:4:27: error: call to non-static member function without an object argument
    static int f() { return n; }
                            ^

Noticed in PR21923.

llvm-svn: 224480
This commit is contained in:
Reid Kleckner 2014-12-18 00:42:51 +00:00
parent 323ddf97ba
commit ae628965c4
2 changed files with 12 additions and 2 deletions

View File

@ -90,7 +90,6 @@ enum IMAKind {
/// conservatively answer "yes", in which case some errors will simply
/// not be caught until template-instantiation.
static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef,
Scope *CurScope,
const LookupResult &R) {
assert(!R.empty() && (*R.begin())->isCXXClassMember());
@ -205,6 +204,9 @@ static void diagnoseInstanceReference(Sema &SemaRef,
SourceRange Range(Loc);
if (SS.isSet()) Range.setBegin(SS.getRange().getBegin());
// Look through using shadow decls and aliases.
Rep = Rep->getUnderlyingDecl();
DeclContext *FunctionLevelDC = SemaRef.getFunctionLevelDeclContext();
CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FunctionLevelDC);
CXXRecordDecl *ContextClass = Method ? Method->getParent() : nullptr;
@ -237,7 +239,7 @@ Sema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS,
SourceLocation TemplateKWLoc,
LookupResult &R,
const TemplateArgumentListInfo *TemplateArgs) {
switch (ClassifyImplicitMemberAccess(*this, CurScope, R)) {
switch (ClassifyImplicitMemberAccess(*this, R)) {
case IMA_Instance:
return BuildImplicitMemberExpr(SS, TemplateKWLoc, R, TemplateArgs, true);

View File

@ -255,3 +255,11 @@ namespace TypoCorrectTemplateMember {
using A::goobar; // expected-error {{no member named 'goobar' in 'TypoCorrectTemplateMember::A'; did you mean 'foobar'?}}
};
}
namespace use_instance_in_static {
struct A { int n; };
struct B : A {
using A::n;
static int f() { return n; } // expected-error {{invalid use of member 'n' in static member function}}
};
}