Index: lib/Sema/SemaTemplateInstantiateDecl.cpp =================================================================== --- lib/Sema/SemaTemplateInstantiateDecl.cpp +++ lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -4843,7 +4843,11 @@ NamedDecl *Result = nullptr; if (D->getDeclName()) { - DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName()); + DeclarationName Name = D->getDeclName(); + if (auto *DD = dyn_cast(D)) + Name = + SubstDeclarationNameInfo(DD->getNameInfo(), TemplateArgs).getName(); + DeclContext::lookup_result Found = ParentDC->lookup(Name); Result = findInstantiationOf(Context, D, Found.begin(), Found.end()); } else { // Since we don't have a name for the entity we're looking for, Index: lib/Sema/TreeTransform.h =================================================================== --- lib/Sema/TreeTransform.h +++ lib/Sema/TreeTransform.h @@ -8754,11 +8754,16 @@ // nested-name-qualifier (and therefore could do the lookup). NamedDecl *FirstQualifierInScope = nullptr; + DeclarationNameInfo MemberNameInfo = E->getMemberNameInfo(); + + if (isa(FoundDecl)) + MemberNameInfo = getDerived().TransformDeclarationNameInfo(MemberNameInfo); + return getDerived().RebuildMemberExpr(Base.get(), FakeOperatorLoc, E->isArrow(), QualifierLoc, TemplateKWLoc, - E->getMemberNameInfo(), + MemberNameInfo, Member, FoundDecl, (E->hasExplicitTemplateArgs() Index: test/SemaCXX/destructor.cpp =================================================================== --- test/SemaCXX/destructor.cpp +++ test/SemaCXX/destructor.cpp @@ -431,3 +431,23 @@ // The constructor definition should not have errors Invalid::~Invalid() {} + +namespace PR30361 { +template +struct C1 { + ~C1() {} + operator C1* () { return nullptr; } + void foo1(); +}; + +template +void C1::foo1() { + C1::operator C1*(); + C1::~C1(); +} + +void foo1() { + C1 x; + x.foo1(); +} +}