From 06721bb8b4a95efaf0d0b2d6d1bd2256f8e24c60 Mon Sep 17 00:00:00 2001 From: Matheus Izvekov Date: Fri, 29 Sep 2023 02:22:50 +0200 Subject: [PATCH] [clang] implement common sugared type of inst-dependent DecltypeType (#67739) While a DecltypeType node itself is not uniqued, an instantiation dependent DecltypeType will have a DependentDecltypeType as an underlying type, which is uniqued. In that case, there can be non-identical non-sugar DecltypeTypes nodes which nonetheless represent the same type. Fixes https://github.com/llvm/llvm-project/issues/67603 --- clang/docs/ReleaseNotes.rst | 3 +++ clang/lib/AST/ASTContext.cpp | 9 ++++++++- clang/test/SemaCXX/sugar-common-types.cpp | 11 +++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 1d74c492845a..09302040a351 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -266,6 +266,9 @@ Bug Fixes in This Version (`#64836 `_) - Clang now allows an ``_Atomic`` qualified integer in a switch statement. Fixes (`#65557 `_) +- Fixes crash when trying to obtain the common sugared type of + `decltype(instantiation-dependent-expr)`. + Fixes (`#67603 `_) Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index 57aaa05b1d81..5ce0b54166e0 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -12705,7 +12705,6 @@ static QualType getCommonNonSugarTypeNode(ASTContext &Ctx, const Type *X, #define SUGAR_FREE_TYPE(Class) UNEXPECTED_TYPE(Class, "sugar-free") SUGAR_FREE_TYPE(Builtin) - SUGAR_FREE_TYPE(Decltype) SUGAR_FREE_TYPE(DeducedTemplateSpecialization) SUGAR_FREE_TYPE(DependentBitInt) SUGAR_FREE_TYPE(Enum) @@ -12935,6 +12934,14 @@ static QualType getCommonNonSugarTypeNode(ASTContext &Ctx, const Type *X, TY->getTemplateName()), As, X->getCanonicalTypeInternal()); } + case Type::Decltype: { + const auto *DX = cast(X), *DY = cast(Y); + assert(DX->isDependentType()); + assert(DY->isDependentType()); + assert(Ctx.hasSameExpr(DX->getUnderlyingExpr(), DY->getUnderlyingExpr())); + // As Decltype is not uniqued, building a common type would be wasteful. + return QualType(DX, 0); + } case Type::DependentName: { const auto *NX = cast(X), *NY = cast(Y); diff --git a/clang/test/SemaCXX/sugar-common-types.cpp b/clang/test/SemaCXX/sugar-common-types.cpp index 4a8ff2addb66..e1c7578a66b9 100644 --- a/clang/test/SemaCXX/sugar-common-types.cpp +++ b/clang/test/SemaCXX/sugar-common-types.cpp @@ -142,3 +142,14 @@ namespace PR61419 { extern const pair p; id t = false ? p.first : p.second; } // namespace PR61419 + +namespace GH67603 { + template using A = long; + template void h() { + using C = B; + using D = B; + N t = 0 ? A() : A(); + // expected-error@-1 {{rvalue of type 'A' (aka 'long')}} + } + template void h(); +} // namespace GH67603