diff mbox

[C++] Fix -Wunused-but-set-* on const var used as array size in template (PR c++/45588)

Message ID 20100908081051.GM1269@tyan-ft48-01.lab.bos.redhat.com
State New
Headers show

Commit Message

Jakub Jelinek Sept. 8, 2010, 8:10 a.m. UTC
Hi!

Attached are two alternative patches to fix the attached testcase.
For non-templates a similar issue has been fixed before by calling
mark_rvalue_use in compute_array_index_type, but, but for templates
the const var is changed for an INTEGER_CST already before it reaches
that function, by fold_decl_constant_value.  I have no idea when
fold_decl_constant_value actually needs to loop, perhaps just the
second patch should be sufficient (it is for the testcase, and also
for
template <int N>
void
foo ()
{
  const int i = S<N>::k;
  const int j = i;
  const int k = j;
  unsigned char a[k];
  bar (a);
}
foo body instead).

	Jakub
2010-09-08  Jakub Jelinek  <jakub@redhat.com>

	PR c++/45588
	* pt.c (fold_decl_constant_value): Call mark_rvalue_use
	before calling integral_constant_value.

	* g++.dg/warn/Wunused-var-15.C: New test.
2010-09-08  Jakub Jelinek  <jakub@redhat.com>

	PR c++/45588
	* pt.c (tsubst) <case INTEGER_TYPE>: Call mark_rvalue_use
	before calling fold_decl_constant_value.

	* g++.dg/warn/Wunused-var-15.C: New test.

--- gcc/cp/pt.c.jj	2010-09-06 08:41:35.000000000 +0200
+++ gcc/cp/pt.c	2010-09-08 09:54:39.000000000 +0200
@@ -10116,6 +10116,7 @@ tsubst (tree t, tree args, tsubst_flags_
 	    && !TREE_TYPE (max))
 	  TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
 
+	max = mark_rvalue_use (max);
 	max = fold_decl_constant_value (max);
 
 	/* If we're in a partial instantiation, preserve the magic NOP_EXPR
--- gcc/testsuite/g++.dg/warn/Wunused-var-15.C.jj	2010-09-08 09:32:24.000000000 +0200
+++ gcc/testsuite/g++.dg/warn/Wunused-var-15.C	2010-09-08 09:31:44.000000000 +0200
@@ -0,0 +1,29 @@
+// PR c++/45588
+// { dg-do compile }
+// { dg-options "-Wunused" }
+
+void bar (unsigned char *);
+
+template <int N>
+struct S
+{
+  static const int k = 6;
+};
+
+template <int N>
+const int S<N>::k;
+
+template <int N>
+void
+foo ()
+{
+  const int i = S<N>::k;
+  unsigned char a[i];
+  bar (a);
+}
+
+void
+baz ()
+{
+  foo<0> ();
+}

Comments

Jason Merrill Sept. 9, 2010, 2:12 a.m. UTC | #1
On 09/08/2010 04:10 AM, Jakub Jelinek wrote:
> Attached are two alternative patches to fix the attached testcase.
> For non-templates a similar issue has been fixed before by calling
> mark_rvalue_use in compute_array_index_type, but, but for templates
> the const var is changed for an INTEGER_CST already before it reaches
> that function, by fold_decl_constant_value.  I have no idea when
> fold_decl_constant_value actually needs to loop, perhaps just the
> second patch should be sufficient (it is for the testcase, and also

Let's go with the second patch.  My constexpr work is doing away with 
fold_decl_constant_value, anyway.

Jason
diff mbox

Patch

--- gcc/cp/pt.c.jj	2010-09-06 08:41:35.000000000 +0200
+++ gcc/cp/pt.c	2010-09-08 09:11:28.000000000 +0200
@@ -4854,6 +4854,7 @@  fold_decl_constant_value (tree expr)
   do
     {
       expr = fold_non_dependent_expr (const_expr);
+      expr = mark_rvalue_use (expr);
       const_expr = integral_constant_value (expr);
     }
   while (expr != const_expr);
--- gcc/testsuite/g++.dg/warn/Wunused-var-15.C.jj	2010-09-08 09:32:24.000000000 +0200
+++ gcc/testsuite/g++.dg/warn/Wunused-var-15.C	2010-09-08 09:31:44.000000000 +0200
@@ -0,0 +1,29 @@ 
+// PR c++/45588
+// { dg-do compile }
+// { dg-options "-Wunused" }
+
+void bar (unsigned char *);
+
+template <int N>
+struct S
+{
+  static const int k = 6;
+};
+
+template <int N>
+const int S<N>::k;
+
+template <int N>
+void
+foo ()
+{
+  const int i = S<N>::k;
+  unsigned char a[i];
+  bar (a);
+}
+
+void
+baz ()
+{
+  foo<0> ();
+}