Index: lib/Sema/SemaTemplateInstantiateDecl.cpp =================================================================== --- lib/Sema/SemaTemplateInstantiateDecl.cpp +++ lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -4846,7 +4846,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 @@ -2119,6 +2119,11 @@ NamedDecl *FirstQualifierInScope) { ExprResult BaseResult = getSema().PerformMemberExprBaseConversion(Base, isArrow); + DeclarationNameInfo DNI = MemberNameInfo; + + if (isa(FoundDecl)) + DNI = getDerived().TransformDeclarationNameInfo(MemberNameInfo); + if (!Member->getDeclName()) { // We have a reference to an unnamed field. This is always the // base of an anonymous struct/union member access, i.e. the @@ -2136,7 +2141,7 @@ Base = BaseResult.get(); ExprValueKind VK = isArrow ? VK_LValue : Base->getValueKind(); MemberExpr *ME = new (getSema().Context) - MemberExpr(Base, isArrow, OpLoc, Member, MemberNameInfo, + MemberExpr(Base, isArrow, OpLoc, Member, DNI, cast(Member)->getType(), VK, OK_Ordinary); return ME; } @@ -2149,7 +2154,7 @@ // FIXME: this involves duplicating earlier analysis in a lot of // cases; we should avoid this when possible. - LookupResult R(getSema(), MemberNameInfo, Sema::LookupMemberName); + LookupResult R(getSema(), DNI, Sema::LookupMemberName); R.addDecl(FoundDecl); R.resolveKind(); 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(); +} +}