Skip to content

Commit

Permalink
c++: class nttp ICE
Browse files Browse the repository at this point in the history
The new testcase from P2308 crashed trying to expand 'this' without an
object to refer to, because we stripped the TARGET_EXPR in
create_template_parm_object.  So let's leave it on for giving an error.

gcc/cp/ChangeLog:

	* pt.cc (create_template_parm_object): Pass TARGET_EXPR to
	cxx_constant_value.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/nontype-class64.C: New test.
  • Loading branch information
jicama committed Feb 8, 2024
1 parent 0535c20 commit 4797f01
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
5 changes: 3 additions & 2 deletions gcc/cp/pt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7308,14 +7308,15 @@ invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
static tree
create_template_parm_object (tree expr, tsubst_flags_t complain)
{
tree orig = expr;
if (TREE_CODE (expr) == TARGET_EXPR)
expr = TARGET_EXPR_INITIAL (expr);

if (!TREE_CONSTANT (expr))
{
if ((complain & tf_error)
&& require_rvalue_constant_expression (expr))
cxx_constant_value (expr);
&& require_rvalue_constant_expression (orig))
cxx_constant_value (orig);
return error_mark_node;
}
if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
Expand Down
23 changes: 23 additions & 0 deletions gcc/testsuite/g++.dg/cpp2a/nontype-class64.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Testcase from P2308R1
// { dg-do compile { target c++20 } }

template<auto n> struct B { /* ... */ };
B<5> b1; // OK, template parameter type is int
B<'a'> b2; // OK, template parameter type is char
B<2.5> b3; // OK, template parameter type is double
B<void(0)> b4; // { dg-error "void" }

template<int i> struct C { /* ... */ };
C<{ 42 }> c1; // OK

struct J1 {
J1 *self=this;
};
B<J1{}> j1; // { dg-error "not a constant expression" }

struct J2 {
J2 *self=this;
constexpr J2() {}
constexpr J2(const J2&) {}
};
B<J2{}> j2; // { dg-error "" }

0 comments on commit 4797f01

Please sign in to comment.