Here, the problem ultimately seems to be that tsubst_copy_and_build, when called with empty args as we do during non-dependent expression folding, doesn't touch BASELINKs at all: it delegates to tsubst_copy which then immediately exits early due to the empty args. This means that the CAST_EXPR int(1) in the BASELINK A::condition<int(1)> never gets folded (as part of folding of the overall CALL_EXPR), which later causes us to crash when performing overload resolution of the rebuilt CALL_EXPR (which is in terms of this still-templated BASELINK).
This doesn't happen when condition() is a namespace-scope function because then condition<int(1)> is represented as a TEMPLATE_ID_EXPR rather than a BASELINK, which does get handled directly from tsubst_copy_and_build. This patch fixes this issue by having tsubst_copy_and_build handle BASELINK directly rather than delegating to tsubst_copy, so that it processes BASELINKS even when args is empty. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? gcc/cp/ChangeLog: PR c++/95468 * pt.c (tsubst_copy_and_build) <case BASELINK>: New case, copied over from tsubst_copy. gcc/testsuite/ChangeLog: PR c++/95468 * g++.dg/template/non-dependent15.C: New test. --- gcc/cp/pt.c | 5 +++++ gcc/testsuite/g++.dg/template/non-dependent15.C | 12 ++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 gcc/testsuite/g++.dg/template/non-dependent15.C diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 5102bf02d0f..5b2f43dc5c1 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -19856,6 +19856,11 @@ tsubst_copy_and_build (tree t, case SCOPE_REF: RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true, /*address_p=*/false)); + + case BASELINK: + return tsubst_baselink (t, current_nonlambda_class_type (), + args, complain, in_decl); + case ARRAY_REF: op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), args, complain, in_decl); diff --git a/gcc/testsuite/g++.dg/template/non-dependent15.C b/gcc/testsuite/g++.dg/template/non-dependent15.C new file mode 100644 index 00000000000..00dfe26d6ba --- /dev/null +++ b/gcc/testsuite/g++.dg/template/non-dependent15.C @@ -0,0 +1,12 @@ +// PR c++/95468 +// { dg-do compile { target c++11 } } + +struct A { + template <int N> + static constexpr int condition() { return N; } +}; + +template <int> struct B {}; + +template <class> +using T = B<A::condition<int(1)>()>; -- 2.30.0.452.gfb7fa4a1fd