https://github.com/kodlan updated https://github.com/llvm/llvm-project/pull/220110
>From e60c6d8666d650f07d788fda26b2eef8d8a49969 Mon Sep 17 00:00:00 2001 From: Stanislav Bardyuk <[email protected]> Date: Tue, 1 Sep 2026 17:50:54 +0000 Subject: [PATCH] [Clang] Fix crash classifying a dependent call to a non-callable value Clang asserts in Type::castAs<FunctionType> (via CallExpr::getCallReturnType) when a template argument contains a call whose callee has already been substituted to a plain value while an argument is still dependent, e.g. template <auto V> struct W { static constexpr auto value = V; template <class T> auto f(T) -> W<value(T::value)>; }; template struct W<42>; When W<42> is instantiated, the callee "value" becomes an int but T::value is still dependent, so BuildCallExpr creates a dependent CallExpr (the MayBeFunctionType guard from #139246 intentionally lets DeclRefExpr callees through so the error is deferred to instantiation). Checking the template argument against the auto NTTP then classifies the expression, and getCallReturnType has no case for a type-dependent call whose callee type is not a function, so it hits the castAs assertion. Handle it the same way the record-callee (#68078) and dependent-builtin (#210524) cases already are: return DependentTy. The check goes after the pointer-stripping branch so a callee of pointer-to-non-function type (same crash, e.g. p(T::value) with const int *p) is covered too. The invalid call is still diagnosed when the member actually gets instantiated (as a substitution failure), and the accepted-and-deferred behavior matches GCC. Fixes #218323 --- clang/docs/ReleaseNotes.md | 4 +++ clang/lib/AST/Expr.cpp | 3 ++ clang/test/SemaTemplate/fun-template-def.cpp | 33 ++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index bf295981710ac..32434c258b5f3 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -614,6 +614,10 @@ features cannot lower the translation-unit ABI level; in a token that was lexed and cached before the first occurrence was parsed. (#GH214128) +- Fixed a crash when classifying a dependent call whose callee has already + been substituted to a value of non-callable type, such as a call to a + non-type template parameter member in a trailing return type. (#GH218323) + #### Bug Fixes to AST Handling - Fixed a non-deterministic ordering of unused local typedefs that made diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp index 6ce0a29aa3bd7..9ec62537f29f0 100644 --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -1656,6 +1656,9 @@ QualType CallExpr::getCallReturnType(const ASTContext &Ctx) const { return Ctx.DependentTy; } + if (isTypeDependent() && !CalleeType->isFunctionType()) + return Ctx.DependentTy; + const FunctionType *FnType = CalleeType->castAs<FunctionType>(); return FnType->getReturnType(); } diff --git a/clang/test/SemaTemplate/fun-template-def.cpp b/clang/test/SemaTemplate/fun-template-def.cpp index b0d0580d5ba80..c61d9be3d3228 100644 --- a/clang/test/SemaTemplate/fun-template-def.cpp +++ b/clang/test/SemaTemplate/fun-template-def.cpp @@ -121,6 +121,39 @@ template struct S<1>; // expected-note {{in instantiation}} } +namespace GH218323 { +template <auto V> struct W { + static constexpr auto value = V; + template <class T> auto f(T) -> W<value(T::value)>; +}; +template struct W<42>; + +template <auto V> struct WPtr { + static constexpr int arr[1] = {V}; + static constexpr const int *p = arr; + template <class T> auto f(T) -> WPtr<p(T::value)>; +}; +template struct WPtr<42>; + +template <auto V> +struct ConstantWrapper { + static constexpr auto value = V; + template <class... Ts> + constexpr auto operator()(Ts... args) const -> ConstantWrapper<value(Ts::value...)> { + return {}; + } +}; + +struct Plus { + template <class T, class U> + constexpr auto operator()(T&& t, U&& u) const -> decltype(static_cast<T&&>(t) + static_cast<U&&>(u)) { + return static_cast<T&&>(t) + static_cast<U&&>(u); + } +}; + +constexpr auto cwv = ConstantWrapper<Plus{}>{}(ConstantWrapper<42>{}, ConstantWrapper<17>{}); +static_assert(cwv.value == 59, ""); +} #endif #if __cplusplus >= 201702L _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
