diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 769e7999dac1..e826206be164 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -6759,8 +6759,15 @@ dependent_opaque_alias_p (const_tree t) { return (TYPE_P (t) && typedef_variant_p (t) - && any_dependent_type_attributes_p (DECL_ATTRIBUTES - (TYPE_NAME (t)))); + && (any_dependent_type_attributes_p (DECL_ATTRIBUTES + (TYPE_NAME (t))) + /* Treat a dependent decltype(lambda) alias as opaque so that we + don't prematurely strip it when used as a template argument. + Otherwise substitution into each occurrence of the (stripped) + alias would incorrectly yield a distinct lambda type. */ + || (TREE_CODE (t) == DECLTYPE_TYPE + && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == LAMBDA_EXPR + && !typedef_variant_p (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))))); } /* Return the number of innermost template parameters in TMPL. */ diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-uneval18.C b/gcc/testsuite/g++.dg/cpp2a/lambda-uneval18.C new file mode 100644 index 000000000000..b7d864c62453 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/lambda-uneval18.C @@ -0,0 +1,39 @@ +// PR c++/116714 +// PR c++/107390 +// { dg-do compile { target c++20 } } + +template +inline constexpr bool is_same_v = __is_same(T, U); + +template +struct is_same { static constexpr bool value = false; }; + +template +struct is_same { static constexpr bool value = true; }; + +template +void f() { + using type = decltype([]{}); + static_assert(is_same_v); + static_assert(is_same::value); +}; + +template +void g() { + using ty1 = decltype([]{}); + using ty2 = ty1; + static_assert(is_same_v); + static_assert(is_same::value); +}; + +template +void h() { + using ty1 = decltype([]{}); + using ty2 = decltype([]{}); + static_assert(!is_same_v); + static_assert(!is_same::value); +}; + +template void f(); +template void g(); +template void h();