This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch] Fix PR c++/27804: ICE with invalid const variable


The C++ frontend crashes on the following invalid code snippet:

  bool foo()
  {
    const int i = X;
    return i > 0;
  }

bug.cc: In function 'bool foo()':
bug.cc:3: error: 'X' was not declared in this scope
bug.cc:4: internal compiler error: tree check: expected class 'type', have 'exceptional' (error_mark) in ocp_convert, at cp/cvt.c:630
Please submit a full bug report, [etc.]

Instead of adding another check for error_mark_nodes in ocp_convert
the following patch avoids the generation of the error_mark_node
that causes the trouble. The comment above constant_value_1 reads:

  /* If DECL is a scalar enumeration constant or variable with a
     constant initializer, return the initializer (or, its initializers,
     recursively); otherwise, return DECL.  If INTEGRAL_P, the
     initializer is only returned if DECL is an integral
     constant-expression.  */

However, we have the following test in the function:

  if (init == error_mark_node)
    return error_mark_node;

The patch fixes the ICE by returning decl instead.

This also improves the error messages given for g++.dg/init/member1.C:
Instead of

  member1.C:15: error: '<expression error>' is not a valid template argument
  for type 'int' because it is a non-constant expression

we then get

  member1.C:15: error: 'C<int>::i' is not a valid template argument for
  type 'int' because it is a non-constant expression

Bootstrapped and regtested on x86_64-unknown-linux-gnu.
Ok for mainline?

Regards,
Volker

:ADDPATCH C++:


2006-06-02  Volker Reichelt  <reichelt@igpm.rwth-aachen.de>

	PR c++/27804
	* init.c (constant_value_1): Return decl instead of error_mark_node
	for invalid initializers.

===================================================================
--- gcc/gcc/cp/init.c	2006-06-01 12:45:03 +0200
+++ gcc/gcc/cp/init.c	2006-06-01 12:43:05 +0200
@@ -1483,7 +1483,7 @@ constant_value_1 (tree decl, bool integr
 	  init = DECL_INITIAL (decl);
 	}
       if (init == error_mark_node)
-	return error_mark_node;
+	return decl;
       if (!init
 	  || !TREE_TYPE (init)
 	  || (integral_p
===================================================================

2006-06-02  Volker Reichelt  <reichelt@igpm.rwth-aachen.de>

	PR c++/27804
	* g++.dg/init/const4.C: New test.
	* g++.dg/other/fold1.C: Adjust error-marker.
	* g++.dg/init/member1.C: Add error-marker.

===================================================================
--- gcc/gcc/testsuite/g++.dg/init/const4.C	2003-09-23 19:59:22 +0200
+++ gcc/gcc/testsuite/g++.dg/init/const4.C	2006-06-01 12:48:17 +0200
@@ -0,0 +1,8 @@
+// PR c++/27804
+// { dg-do compile }
+
+bool foo()
+{
+  const int i = X;  // { dg-error "not declared" }
+  return i > 0;
+}
===================================================================
--- gcc/gcc/testsuite/g++.dg/other/fold1.C	(revision 114293)
+++ gcc/gcc/testsuite/g++.dg/other/fold1.C	(working copy)
@@ -4,5 +4,5 @@
 struct A
 {
     static const int i = i;  // { dg-error "not declared" }
-    int x[i];                // { dg-error "variable-size array" }
+    int x[i];                // { dg-error "integral constant-expression" }
 };
===================================================================
--- gcc/gcc/testsuite/g++.dg/init/member1.C	(revision 114293)
+++ gcc/gcc/testsuite/g++.dg/init/member1.C	(working copy)
@@ -11,7 +11,7 @@
 template<typename T> struct C
 {
   static const int i = A<T>::i;  // { dg-error "incomplete" }
-  static const int j = i;
+  static const int j = i;  // { dg-error "non-constant expression" }
   B<j> b;  // { dg-error "not a valid template arg" }
 };
 
===================================================================



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]