Index: include/clang/Sema/Sema.h =================================================================== --- include/clang/Sema/Sema.h +++ include/clang/Sema/Sema.h @@ -3692,6 +3692,9 @@ Expr *baseObjectExpr = nullptr, SourceLocation opLoc = SourceLocation()); + void DiagnoseInstanceReference(const CXXScopeSpec &SS, NamedDecl *Rep, + const DeclarationNameInfo &nameInfo); + ExprResult BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS, SourceLocation TemplateKWLoc, LookupResult &R, Index: lib/Sema/SemaExprMember.cpp =================================================================== --- lib/Sema/SemaExprMember.cpp +++ lib/Sema/SemaExprMember.cpp @@ -192,10 +192,8 @@ } /// Diagnose a reference to a field with no object available. -static void diagnoseInstanceReference(Sema &SemaRef, - const CXXScopeSpec &SS, - NamedDecl *Rep, - const DeclarationNameInfo &nameInfo) { +void Sema::DiagnoseInstanceReference(const CXXScopeSpec &SS, NamedDecl *Rep, + const DeclarationNameInfo &nameInfo) { SourceLocation Loc = nameInfo.getLoc(); SourceRange Range(Loc); if (SS.isSet()) Range.setBegin(SS.getRange().getBegin()); @@ -203,7 +201,7 @@ // Look through using shadow decls and aliases. Rep = Rep->getUnderlyingDecl(); - DeclContext *FunctionLevelDC = SemaRef.getFunctionLevelDeclContext(); + DeclContext *FunctionLevelDC = getFunctionLevelDeclContext(); CXXMethodDecl *Method = dyn_cast(FunctionLevelDC); CXXRecordDecl *ContextClass = Method ? Method->getParent() : nullptr; CXXRecordDecl *RepClass = dyn_cast(Rep->getDeclContext()); @@ -213,20 +211,19 @@ if (IsField && InStaticMethod) // "invalid use of member 'x' in static member function" - SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method) + Diag(Loc, diag::err_invalid_member_use_in_static_method) << Range << nameInfo.getName(); else if (ContextClass && RepClass && SS.isEmpty() && !InStaticMethod && !RepClass->Equals(ContextClass) && RepClass->Encloses(ContextClass)) // Unqualified lookup in a non-static member function found a member of an // enclosing class. - SemaRef.Diag(Loc, diag::err_nested_non_static_member_use) - << IsField << RepClass << nameInfo.getName() << ContextClass << Range; + Diag(Loc, diag::err_nested_non_static_member_use) + << IsField << RepClass << nameInfo.getName() << ContextClass << Range; else if (IsField) - SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use) - << nameInfo.getName() << Range; + Diag(Loc, diag::err_invalid_non_static_member_use) << nameInfo.getName() + << Range; else - SemaRef.Diag(Loc, diag::err_member_call_without_object) - << Range; + Diag(Loc, diag::err_member_call_without_object) << Range; } /// Builds an expression which might be an implicit member expression. @@ -260,7 +257,7 @@ case IMA_Error_StaticContext: case IMA_Error_Unrelated: - diagnoseInstanceReference(*this, SS, R.getRepresentativeDecl(), + DiagnoseInstanceReference(SS, R.getRepresentativeDecl(), R.getLookupNameInfo()); return ExprError(); } @@ -474,7 +471,7 @@ // If this is an implicit member access, use a different set of // diagnostics. if (!BaseExpr) - return diagnoseInstanceReference(SemaRef, SS, rep, nameInfo); + return SemaRef.DiagnoseInstanceReference(SS, rep, nameInfo); SemaRef.Diag(nameInfo.getLoc(), diag::err_qualified_member_of_unrelated) << SS.getRange() << rep << BaseType; Index: lib/Sema/TreeTransform.h =================================================================== --- lib/Sema/TreeTransform.h +++ lib/Sema/TreeTransform.h @@ -9128,8 +9128,18 @@ // If we have neither explicit template arguments, nor the template keyword, // it's a normal declaration name. - if (!Old->hasExplicitTemplateArgs() && !TemplateKWLoc.isValid()) + if (!Old->hasExplicitTemplateArgs() && !TemplateKWLoc.isValid()) { + // If an UnresolvedLookupExpr resolved to an instance member, that's an + // error. + NamedDecl *D = R.getAsSingle(); + if (D && D->isCXXInstanceMember()) { + SemaRef.DiagnoseInstanceReference(SS, D, Old->getNameInfo()); + R.clear(); + return ExprError(); + } + return getDerived().RebuildDeclarationNameExpr(SS, R, Old->requiresADL()); + } // If we have template arguments, rebuild them, then rebuild the // templateid expression. Index: test/SemaCXX/using-decl-1.cpp =================================================================== --- test/SemaCXX/using-decl-1.cpp +++ test/SemaCXX/using-decl-1.cpp @@ -327,3 +327,28 @@ using PR24033::st; // expected-error {{target of using declaration conflicts with declaration already in scope}} } } + +namespace pr21923 { +template struct Base { + int field; + void method(); +}; +template struct Derived : Base { + using Base::field; + using Base::method; + static void m_fn1() { + // expected-error@+1 {{invalid use of member 'field' in static member function}} + (void)field; + // expected-error@+1 {{invalid use of member 'field' in static member function}} + (void)&field; + // expected-error@+1 {{call to non-static member function without an object argument}} + (void)method; + // expected-error@+1 {{call to non-static member function without an object argument}} + (void)&method; + // expected-error@+1 {{call to non-static member function without an object argument}} + method(); + } +}; +// expected-note@+1 {{in instantiation of member function 'pr21923::Derived::m_fn1' requested here}} +template class Derived; +} Index: test/SemaTemplate/instantiate-using-decl.cpp =================================================================== --- test/SemaTemplate/instantiate-using-decl.cpp +++ test/SemaTemplate/instantiate-using-decl.cpp @@ -104,3 +104,37 @@ x.f(); } } + +namespace PR21923 { +struct A { + int member; + int method(); +}; +template +struct B : public T { + using T::member; + using T::method; + static void StaticFun() { + (void)B::member; // expected-error {{invalid use of member 'member' in static member function}} + (void)member; // expected-error {{invalid use of member 'member' in static member function}} + (void)&member; // expected-error {{invalid use of member 'member' in static member function}} + (void)method; // expected-error {{call to non-static member function without an object argument}} + (void)&method; // expected-error {{call to non-static member function without an object argument}} + method(); // expected-error {{call to non-static member function without an object argument}} + } + void InstanceFun() { + (void)member; + (void)B::member; + method(); + } +}; +template +struct C : public T { + using T::member; + using T::method; + static const int sdm1 = member; // expected-error {{invalid use of non-static data member 'member'}} + static const int sdm2 = method(); // expected-error {{call to non-static member function without an object argument}} +}; +template class B; // expected-note 1 {{requested here}} +template class C; // expected-note 1 {{requested here}} +}