From 5fb68f064c4b5cae02946f3a86caafe1322b38bb Mon Sep 17 00:00:00 2001 From: Faisal Vali Date: Thu, 19 May 2016 02:28:21 +0000 Subject: [PATCH] Fix PR27601 by reverting [r267453] - Refactor traversal of bases in deduction of template parameters from base This reversal is being done with r267453's author's (i.e. Richard Smith's) permission. This fixes https://llvm.org/bugs/show_bug.cgi?id=27601 Also, per Richard's request the examples from the bug report have been added to our test suite. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@270016 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AST/CXXInheritance.cpp | 15 +++---- lib/Sema/SemaTemplateDeduction.cpp | 71 +++++++++++++++++++----------- test/SemaTemplate/deduction.cpp | 47 ++++++++++++++++++++ 3 files changed, 98 insertions(+), 35 deletions(-) diff --git a/lib/AST/CXXInheritance.cpp b/lib/AST/CXXInheritance.cpp index d1a2b435df..6a6ca76a01 100644 --- a/lib/AST/CXXInheritance.cpp +++ b/lib/AST/CXXInheritance.cpp @@ -137,7 +137,6 @@ CXXRecordDecl::isCurrentInstantiation(const DeclContext *CurContext) const { bool CXXRecordDecl::forallBases(ForallBasesCallback BaseMatches, bool AllowShortCircuit) const { SmallVector Queue; - llvm::SmallPtrSet Enqueued; const CXXRecordDecl *Record = this; bool AllMatches = true; @@ -159,14 +158,12 @@ bool CXXRecordDecl::forallBases(ForallBasesCallback BaseMatches, AllMatches = false; continue; } - - if (Enqueued.insert(Base).second) { - Queue.push_back(Base); - if (!BaseMatches(Base)) { - if (AllowShortCircuit) return false; - AllMatches = false; - continue; - } + + Queue.push_back(Base); + if (!BaseMatches(Base)) { + if (AllowShortCircuit) return false; + AllMatches = false; + continue; } } diff --git a/lib/Sema/SemaTemplateDeduction.cpp b/lib/Sema/SemaTemplateDeduction.cpp index f35dc30906..59dc1da49a 100644 --- a/lib/Sema/SemaTemplateDeduction.cpp +++ b/lib/Sema/SemaTemplateDeduction.cpp @@ -1454,35 +1454,54 @@ DeduceTemplateArgumentsByTypeMatch(Sema &S, // otherwise fail. If they yield more than one possible deduced A, the // type deduction fails. + // Reset the incorrectly deduced argument from above. + Deduced = DeducedOrig; + + // Use data recursion to crawl through the list of base classes. + // Visited contains the set of nodes we have already visited, while + // ToVisit is our stack of records that we still need to visit. + llvm::SmallPtrSet Visited; + SmallVector ToVisit; + ToVisit.push_back(RecordT); bool Successful = false; - RecordT->getAsCXXRecordDecl()->forallBases([&]( - const CXXRecordDecl *Base) { - // Start with a fresh copy of the old deduced arguments. - SmallVector DeducedBase(DeducedOrig.begin(), - DeducedOrig.end()); - - TemplateDeductionInfo BaseInfo(Info.getLocation()); - Sema::TemplateDeductionResult BaseResult = - DeduceTemplateArguments(S, TemplateParams, SpecParam, - S.Context.getRecordType(Base), - BaseInfo, DeducedBase); - - // If template argument deduction for this base was successful, - // note that we had some success. Otherwise, ignore any deductions - // from this base class. - if (BaseResult == Sema::TDK_Success) { - // FIXME: If we've already been successful, deduction should fail - // due to ambiguity. - Successful = true; - Deduced.swap(DeducedBase); - Info.Param = BaseInfo.Param; - Info.FirstArg = BaseInfo.FirstArg; - Info.SecondArg = BaseInfo.SecondArg; + while (!ToVisit.empty()) { + // Retrieve the next class in the inheritance hierarchy. + const RecordType *NextT = ToVisit.pop_back_val(); + + // If we have already seen this type, skip it. + if (!Visited.insert(NextT).second) + continue; + + // If this is a base class, try to perform template argument + // deduction from it. + if (NextT != RecordT) { + TemplateDeductionInfo BaseInfo(Info.getLocation()); + Sema::TemplateDeductionResult BaseResult = + DeduceTemplateArguments(S, TemplateParams, SpecParam, + QualType(NextT, 0), BaseInfo, Deduced); + + // If template argument deduction for this base was successful, + // note that we had some success. Otherwise, ignore any deductions + // from this base class. + if (BaseResult == Sema::TDK_Success) { + Successful = true; + DeducedOrig.clear(); + DeducedOrig.append(Deduced.begin(), Deduced.end()); + Info.Param = BaseInfo.Param; + Info.FirstArg = BaseInfo.FirstArg; + Info.SecondArg = BaseInfo.SecondArg; + } else + Deduced = DeducedOrig; } - // Keep going. - return true; - }); + // Visit base classes + CXXRecordDecl *Next = cast(NextT->getDecl()); + for (const auto &Base : Next->bases()) { + assert(Base.getType()->isRecordType() && + "Base class that isn't a record?"); + ToVisit.push_back(Base.getType()->getAs()); + } + } if (Successful) return Sema::TDK_Success; diff --git a/test/SemaTemplate/deduction.cpp b/test/SemaTemplate/deduction.cpp index 6826774a00..d024c31477 100644 --- a/test/SemaTemplate/deduction.cpp +++ b/test/SemaTemplate/deduction.cpp @@ -218,3 +218,50 @@ namespace NonDeducedNestedNameSpecifier { template int f(A, typename A::template B); int k = f(A(), 0); } + +namespace PR27601_RecursivelyInheritedBaseSpecializationsDeductionAmbiguity { +namespace ns1 { + +template struct B { }; +template struct B : B<> { }; +template struct D : B { }; + +template void f(B &) { } + +int main() { + D d; + f(d); +} +} //end ns1 + +namespace ns2 { + +template struct tup_impl; + +template struct tup_impl {}; // empty tail + +template +struct tup_impl : tup_impl { + using value_type = Head; + Head head; +}; + +template struct tup : tup_impl<0, Es...> {}; + +template +Head &get_helper(tup_impl &t) { + return t.head; +} + +template +Head const &get_helper(tup_impl const &t) { + return t.head; +} + +int main() { + tup t; + get_helper(t); + return 0; +} +} // end ns2 +} \ No newline at end of file -- 2.50.1