From: Jason Merrill Date: Thu, 25 Feb 2021 21:47:53 +0000 (-0500) Subject: c++: Fix class NTTP constness handling [PR98810] X-Git-Tag: releases/gcc-10.3.0~252 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bf49d83570ddb4df7893c3d605f7fc89db13792d;p=thirdparty%2Fgcc.git c++: Fix class NTTP constness handling [PR98810] Here, when substituting still-dependent args into an alias template, we see a non-const type because the default argument is non-const, and is not a template parm object because it's still dependent. gcc/cp/ChangeLog: PR c++/98810 * pt.c (tsubst_copy) [VIEW_CONVERT_EXPR]: Add const to a class non-type template argument that needs it. gcc/testsuite/ChangeLog: PR c++/98810 * g++.dg/cpp2a/nontype-class-defarg1.C: New test. --- diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index fe45b7944cce..73bc06972ddf 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -16651,13 +16651,26 @@ tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl) tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); return build1 (code, type, op); } - else + else if (!CP_TYPE_CONST_P (TREE_TYPE (op))) { - gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op)) - || (TREE_CODE (op) == IMPLICIT_CONV_EXPR - && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op))); - return op; + /* The template argument is not const, presumably because + it is still dependent, and so not the const template parm + object. */ + tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); + gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p + (type, TREE_TYPE (op))); + if (TREE_CODE (op) == CONSTRUCTOR + || TREE_CODE (op) == IMPLICIT_CONV_EXPR) + { + /* Don't add a wrapper to these. */ + op = copy_node (op); + TREE_TYPE (op) = type; + } + else + /* Do add a wrapper otherwise. */ + op = build1 (code, type, op); } + return op; } /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */ else if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t)) diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class-defarg1.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class-defarg1.C new file mode 100644 index 000000000000..ddd64d6165ba --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class-defarg1.C @@ -0,0 +1,6 @@ +// PR c++/98810 +// { dg-do compile { target c++20 } } + +template struct a {}; +template s = a {}> using b = a ; +template constexpr auto g (const b &) { return true; }