diff mbox series

C++ PATCH for c++/88216, ICE with class type in non-type template parameter

Message ID 20181210195215.GP21364@redhat.com
State New
Headers show
Series C++ PATCH for c++/88216, ICE with class type in non-type template parameter | expand

Commit Message

Marek Polacek Dec. 10, 2018, 7:52 p.m. UTC
Here we were crashing in cxx_eval_constant_expression:
 4600           /* We can only get here in checking mode via
 4601              build_non_dependent_expr,  because any expression that
 4602              calls or takes the address of the function will have
 4603              pulled a FUNCTION_DECL out of the COMPONENT_REF.  */
 4604           gcc_checking_assert (ctx->quiet || errorcount);
 4605           *non_constant_p = true;
 4606           return t;
 4607         }
where we got via 
 6661   if (!TREE_CONSTANT (expr))
 6662     {
 6663       if ((complain & tf_error)
 6664           && require_rvalue_constant_expression (expr))
 6665         cxx_constant_value (expr);
 6666       return error_mark_node;
 6667     }
because the expr wasn't constant.  The fix would be to add error() I think.

But the testcase is valid, so we need (also) something else.  This problem
occurs when using a class type non-type template parameter as a template
argument, because the TREE_CONSTANT check in get_template_parm_object will
fail--we're in a template and build_converted_constant_expr didn't produce
a constant, because the expression contains template codes.  So I thought
it would make sense to just return and create the template parameter object
at instantiation.

But then I was running into sorry() in the demangler so I had to make tweaks
there too.  I don't know much about that code, so I hope I'm not doing anything
overly stoopid.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2018-12-10  Marek Polacek  <polacek@redhat.com>

	PR c++/88216 - ICE with class type in non-type template parameter.
	* mangle.c (write_expression): Handle TARGET_EXPR and
	VIEW_CONVERT_EXPR.
	* pt.c (get_template_parm_object): Just return for value dependent
	expressions.

	* g++.dg/cpp2a/nontype-class9.C: New test.

Comments

Jason Merrill Dec. 11, 2018, 3:48 p.m. UTC | #1
On 12/10/18 2:52 PM, Marek Polacek wrote:
> +  if (processing_template_decl && value_dependent_expression_p (expr))

You don't need to check processing_template_decl before 
value_dependent_expression_p.

I would lean toward checking for value-dependence in 
convert_nontype_argument, which already does that a lot.  Enough, 
actually, that perhaps we should remember the result in a local variable.

Jason
Marek Polacek Dec. 11, 2018, 9:05 p.m. UTC | #2
On Tue, Dec 11, 2018 at 10:48:17AM -0500, Jason Merrill wrote:
> On 12/10/18 2:52 PM, Marek Polacek wrote:
> > +  if (processing_template_decl && value_dependent_expression_p (expr))
> 
> You don't need to check processing_template_decl before
> value_dependent_expression_p.

Ok.
 
> I would lean toward checking for value-dependence in
> convert_nontype_argument, which already does that a lot.  Enough, actually,
> that perhaps we should remember the result in a local variable.

I've moved the call, but I didn't add a local variable for the result,
because I was afraid that, since EXPR is being modified on some of the
codepaths, its value-dependent-ness (is that a term?) may change.

Thanks,

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2018-12-11  Marek Polacek  <polacek@redhat.com>

	PR c++/88216 - ICE with class type in non-type template parameter.
	* mangle.c (write_expression): Handle TARGET_EXPR and
	VIEW_CONVERT_EXPR.
	* pt.c (convert_nontype_argument): Don't call
	get_template_parm_object for value-dependent expressions.

	* g++.dg/cpp2a/nontype-class9.C: New test.

diff --git gcc/cp/mangle.c gcc/cp/mangle.c
index 64415894bc5..56247883010 100644
--- gcc/cp/mangle.c
+++ gcc/cp/mangle.c
@@ -2836,13 +2836,21 @@ write_expression (tree expr)
 {
   enum tree_code code = TREE_CODE (expr);
 
+  if (TREE_CODE (expr) == TARGET_EXPR)
+    {
+      expr = TARGET_EXPR_INITIAL (expr);
+      code = TREE_CODE (expr);
+    }
+
   /* Skip NOP_EXPR and CONVERT_EXPR.  They can occur when (say) a pointer
      argument is converted (via qualification conversions) to another type.  */
   while (CONVERT_EXPR_CODE_P (code)
 	 || location_wrapper_p (expr)
 	 /* Parentheses aren't mangled.  */
 	 || code == PAREN_EXPR
-	 || code == NON_LVALUE_EXPR)
+	 || code == NON_LVALUE_EXPR
+	 || (code == VIEW_CONVERT_EXPR
+	     && TREE_CODE (TREE_OPERAND (expr, 0)) == TEMPLATE_PARM_INDEX))
     {
       expr = TREE_OPERAND (expr, 0);
       code = TREE_CODE (expr);
diff --git gcc/cp/pt.c gcc/cp/pt.c
index 8560e588593..3b378ee9ff4 100644
--- gcc/cp/pt.c
+++ gcc/cp/pt.c
@@ -7123,7 +7123,8 @@ convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
     {
       /* Replace the argument with a reference to the corresponding template
 	 parameter object.  */
-      expr = get_template_parm_object (expr, complain);
+      if (!value_dependent_expression_p (expr))
+	expr = get_template_parm_object (expr, complain);
       if (expr == error_mark_node)
 	return NULL_TREE;
     }
diff --git gcc/testsuite/g++.dg/cpp2a/nontype-class9.C gcc/testsuite/g++.dg/cpp2a/nontype-class9.C
new file mode 100644
index 00000000000..737f712be47
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp2a/nontype-class9.C
@@ -0,0 +1,29 @@
+// PR c++/88216
+// { dg-do compile { target c++2a } }
+
+template <class T, class U> struct same;
+template <class T> struct same<T,T> {};
+
+struct T { };
+
+template <T t>
+struct U { };
+
+template <T t>
+void f (U<t>)
+{
+  same<T,decltype(t)> s;
+  same<const T&,decltype((t))> s2;
+}
+
+template<T t>
+U<t> u;
+
+T t;
+U<t> u2;
+
+void
+g ()
+{
+  f<t>(u2);
+}
Jason Merrill Dec. 13, 2018, 8:39 p.m. UTC | #3
On 12/11/18 4:05 PM, Marek Polacek wrote:
> On Tue, Dec 11, 2018 at 10:48:17AM -0500, Jason Merrill wrote:
>> On 12/10/18 2:52 PM, Marek Polacek wrote:
>>> +  if (processing_template_decl && value_dependent_expression_p (expr))
>>
>> You don't need to check processing_template_decl before
>> value_dependent_expression_p.
> 
> Ok.
>   
>> I would lean toward checking for value-dependence in
>> convert_nontype_argument, which already does that a lot.  Enough, actually,
>> that perhaps we should remember the result in a local variable.
> 
> I've moved the call, but I didn't add a local variable for the result,
> because I was afraid that, since EXPR is being modified on some of the
> codepaths, its value-dependent-ness (is that a term?) may change.

Any change would be a bug, but the patch is OK as is.

Jason
diff mbox series

Patch

diff --git gcc/cp/mangle.c gcc/cp/mangle.c
index 64415894bc5..56247883010 100644
--- gcc/cp/mangle.c
+++ gcc/cp/mangle.c
@@ -2836,13 +2836,21 @@  write_expression (tree expr)
 {
   enum tree_code code = TREE_CODE (expr);
 
+  if (TREE_CODE (expr) == TARGET_EXPR)
+    {
+      expr = TARGET_EXPR_INITIAL (expr);
+      code = TREE_CODE (expr);
+    }
+
   /* Skip NOP_EXPR and CONVERT_EXPR.  They can occur when (say) a pointer
      argument is converted (via qualification conversions) to another type.  */
   while (CONVERT_EXPR_CODE_P (code)
 	 || location_wrapper_p (expr)
 	 /* Parentheses aren't mangled.  */
 	 || code == PAREN_EXPR
-	 || code == NON_LVALUE_EXPR)
+	 || code == NON_LVALUE_EXPR
+	 || (code == VIEW_CONVERT_EXPR
+	     && TREE_CODE (TREE_OPERAND (expr, 0)) == TEMPLATE_PARM_INDEX))
     {
       expr = TREE_OPERAND (expr, 0);
       code = TREE_CODE (expr);
diff --git gcc/cp/pt.c gcc/cp/pt.c
index 8560e588593..878a354b193 100644
--- gcc/cp/pt.c
+++ gcc/cp/pt.c
@@ -6655,6 +6655,9 @@  invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
 static tree
 get_template_parm_object (tree expr, tsubst_flags_t complain)
 {
+  if (processing_template_decl && value_dependent_expression_p (expr))
+    return expr;
+
   if (TREE_CODE (expr) == TARGET_EXPR)
     expr = TARGET_EXPR_INITIAL (expr);
 
diff --git gcc/testsuite/g++.dg/cpp2a/nontype-class9.C gcc/testsuite/g++.dg/cpp2a/nontype-class9.C
new file mode 100644
index 00000000000..737f712be47
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp2a/nontype-class9.C
@@ -0,0 +1,29 @@ 
+// PR c++/88216
+// { dg-do compile { target c++2a } }
+
+template <class T, class U> struct same;
+template <class T> struct same<T,T> {};
+
+struct T { };
+
+template <T t>
+struct U { };
+
+template <T t>
+void f (U<t>)
+{
+  same<T,decltype(t)> s;
+  same<const T&,decltype((t))> s2;
+}
+
+template<T t>
+U<t> u;
+
+T t;
+U<t> u2;
+
+void
+g ()
+{
+  f<t>(u2);
+}