In this bug, when we see the invalid initializer we changed the type of
x to be error_mark_node. But we had already built an ARRAY_REF to it
using the incomplete type. Then the back end gets confused by this
invalid expression wrapped in a valid one.
Fixed by not messing with the type of x.
Tested x86_64-pc-linux-gnu, applying to trunk.
commit 1c880bb1328d3815435cf0695e180ab7fe1c47ec
Author: Jason Merrill <ja...@redhat.com>
Date: Fri Dec 14 11:36:43 2012 -0500
PR c++/42315
* decl.c (maybe_deduce_size_from_array_init): Don't change the
variable type.
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index cdda2f4..64bd4b5 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -4834,14 +4834,12 @@ maybe_deduce_size_from_array_init (tree decl, tree init)
if (failure == 1)
{
error ("initializer fails to determine size of %qD", decl);
- TREE_TYPE (decl) = error_mark_node;
}
else if (failure == 2)
{
if (do_default)
{
error ("array size missing in %qD", decl);
- TREE_TYPE (decl) = error_mark_node;
}
/* If a `static' var's size isn't known, make it extern as
well as static, so it does not get allocated. If it's not
@@ -4853,7 +4851,6 @@ maybe_deduce_size_from_array_init (tree decl, tree init)
else if (failure == 3)
{
error ("zero-size array %qD", decl);
- TREE_TYPE (decl) = error_mark_node;
}
}
diff --git a/gcc/testsuite/g++.dg/gomp/pr34964.C b/gcc/testsuite/g++.dg/gomp/pr34964.C
index f5995a6..a02faa2 100644
--- a/gcc/testsuite/g++.dg/gomp/pr34964.C
+++ b/gcc/testsuite/g++.dg/gomp/pr34964.C
@@ -2,5 +2,5 @@
// { dg-do compile }
// { dg-options "-fopenmp" }
-char x[] = 0; // { dg-error "initializer fails to determine size" }
+char x[] = 0; // { dg-error "initializer" }
#pragma omp threadprivate (x)
diff --git a/gcc/testsuite/g++.dg/init/array21.C b/gcc/testsuite/g++.dg/init/array21.C
index f41ce86..5438af1 100644
--- a/gcc/testsuite/g++.dg/init/array21.C
+++ b/gcc/testsuite/g++.dg/init/array21.C
@@ -2,6 +2,6 @@
void foo()
{
- const int x[] = 0; // { dg-error "size" }
+ const int x[] = 0; // { dg-error "initializer" }
++x;
}
diff --git a/gcc/testsuite/g++.dg/init/array32.C b/gcc/testsuite/g++.dg/init/array32.C
new file mode 100644
index 0000000..06b27a9
--- /dev/null
+++ b/gcc/testsuite/g++.dg/init/array32.C
@@ -0,0 +1,7 @@
+// PR c++/42315
+
+extern int x[];
+
+int i = x[0];
+
+int x[] = 0; // { dg-error "" }