From d0a7b563beac4a5fbf6abd1fd3c2215836996045 Mon Sep 17 00:00:00 2001 From: Richard Trieu Date: Wed, 30 Mar 2016 22:23:00 +0000 Subject: [PATCH] Fix Clang crash with template type diffing. Fixes https://llvm.org/bugs/show_bug.cgi?id=27129 which is crash involving type aliases and template type diffing. Template arguments for type aliases and template arguments for the underlying desugared type may not have one-to-one relations, which could mess us the attempt to get more information from the desugared type. For type aliases, ignore the iterator over the desugared type. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@264940 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AST/ASTDiagnostic.cpp | 11 ++++++--- test/Misc/diag-template-diffing.cpp | 37 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/lib/AST/ASTDiagnostic.cpp b/lib/AST/ASTDiagnostic.cpp index 6e8dfcb937..658d850b12 100644 --- a/lib/AST/ASTDiagnostic.cpp +++ b/lib/AST/ASTDiagnostic.cpp @@ -990,19 +990,22 @@ class TemplateDiff { } }; + bool UseDesugaredIterator; InternalIterator SugaredIterator; InternalIterator DesugaredIterator; public: TSTiterator(ASTContext &Context, const TemplateSpecializationType *TST) - : SugaredIterator(TST), + : UseDesugaredIterator(TST->isSugared() && !TST->isTypeAlias()), + SugaredIterator(TST), DesugaredIterator( GetTemplateSpecializationType(Context, TST->desugar())) {} /// &operator++ - Increment the iterator to the next template argument. TSTiterator &operator++() { ++SugaredIterator; - ++DesugaredIterator; + if (UseDesugaredIterator) + ++DesugaredIterator; return *this; } @@ -1024,11 +1027,13 @@ class TemplateDiff { /// hasDesugaredTA - Returns true if there is another TemplateArgument /// available. bool hasDesugaredTA() const { - return !DesugaredIterator.isEnd(); + return UseDesugaredIterator && !DesugaredIterator.isEnd(); } /// getDesugaredTA - Returns the desugared TemplateArgument. reference getDesugaredTA() const { + assert(UseDesugaredIterator && + "Desugared TemplateArgument should not be used."); return *DesugaredIterator; } }; diff --git a/test/Misc/diag-template-diffing.cpp b/test/Misc/diag-template-diffing.cpp index 5e1caa5c31..90bcf6b2d1 100644 --- a/test/Misc/diag-template-diffing.cpp +++ b/test/Misc/diag-template-diffing.cpp @@ -1421,6 +1421,43 @@ B> b4 = B<>(); // CHECK-ELIDE-NOTREE: error: no viable conversion from 'B>' to 'B>' } +namespace TypeAlias { + +template class vector {}; + +template class Point; +template using Polygon = vector>; + +void foo(Polygon<3, float>); +void bar() { foo(Polygon<2, float>()); } + +// CHECK-ELIDE-NOTREE: error: no matching function for call to 'foo' +// CHECK-ELIDE-NOTREE: note: candidate function not viable: no known conversion from 'Polygon<2, [...]>' to 'Polygon<3, [...]>' for 1st argument + +enum class X { + X1, + X2, +}; + +template struct EnumToType; + +template <> struct EnumToType { using type = int; }; + +template <> struct EnumToType { using type = double; }; + + +template using VectorType = vector::type>; + +template void D(const VectorType&); + +void run() { + D(VectorType()); +} +// CHECK-ELIDE-NOTREE: error: no matching function for call to 'D' +// CHECK-ELIDE-NOTREE: note: candidate function [with x = TypeAlias::X::X1] not viable: no known conversion from 'VectorType' to 'const VectorType<(TypeAlias::X)0>' for 1st argument + +} + // CHECK-ELIDE-NOTREE: {{[0-9]*}} errors generated. // CHECK-NOELIDE-NOTREE: {{[0-9]*}} errors generated. // CHECK-ELIDE-TREE: {{[0-9]*}} errors generated. -- 2.40.0