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]

C++ PATCH: PR 19883


This patch fixes invalid error messages about non-constant array
bounds in templates.  We were trying to assess whether or not
dependent expressions were valid integral constant expressions at
template-declaration time, but that cannot be done until
template-instantiation time.  (Note that we do not actually do the
checks at instantiation time, so we are going to accept some
expressions that are not integral constant expressions in templates.
That's not a regression; we never did those checks.)

Tested on x86_64-unknown-linux-gnu, applied on the mainline.

--
Mark Mitchell
CodeSourcery, LLC
mark@codesourcery.com

2005-02-22  Mark Mitchell  <mark@codesourcery.com>

	PR c++/19883
	* parser.c (cp_parser_direct_declarator): Always complain about
	non-constant array bounds when in a function scope.
	* semantics.c (finish_id_expression): Do not mark dependent names
	as non-constant. 

2005-02-22  Mark Mitchell  <mark@codesourcery.com>

	PR c++/19883
	* g++.dg/parse/constant6.C: New test.

Index: testsuite/g++.dg/parse/constant6.C
===================================================================
RCS file: testsuite/g++.dg/parse/constant6.C
diff -N testsuite/g++.dg/parse/constant6.C
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- testsuite/g++.dg/parse/constant6.C	22 Feb 2005 21:36:54 -0000
***************
*** 0 ****
--- 1,23 ----
+ // PR c++/19883
+ 
+ template<typename T> struct A
+ {
+   static const T i = 1;
+   char a[int(i)];
+ };
+ 
+ template<int> struct B {};
+ 
+ template<typename T> struct C
+ {
+   static const T i = 2;
+   B<int(i)> a;
+ };
+ 
+ template< typename T, T N >
+ struct integral_c
+ {
+   static const T value = N;
+ 
+   typedef integral_c< T, static_cast<T>((value + 1)) > next;
+ };
Index: cp/parser.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/parser.c,v
retrieving revision 1.316
diff -c -5 -p -r1.316 parser.c
*** cp/parser.c	14 Feb 2005 17:50:42 -0000	1.316
--- cp/parser.c	22 Feb 2005 21:37:38 -0000
*************** cp_parser_direct_declarator (cp_parser* 
*** 11105,11123 ****
  						 &non_constant_p);
  	      if (!non_constant_p)
  		bounds = fold_non_dependent_expr (bounds);
  	      /* Normally, the array bound must be an integral constant
  		 expression.  However, as an extension, we allow VLAs
! 		 in function scopes.  And, we allow type-dependent
! 		 expressions in templates; sometimes we don't know for
! 		 sure whether or not something is a valid integral
! 		 constant expression until instantiation time.  (It
! 		 doesn't make sense to check for value-dependency, as
! 		 an expression is only value-dependent when it is a
! 		 constant expression.)  */  
! 	      else if (!type_dependent_expression_p (bounds)
! 		       && !at_function_scope_p ())
  		{
  		  error ("array bound is not an integer constant");
  		  bounds = error_mark_node;
  		}
  	    }
--- 11105,11116 ----
  						 &non_constant_p);
  	      if (!non_constant_p)
  		bounds = fold_non_dependent_expr (bounds);
  	      /* Normally, the array bound must be an integral constant
  		 expression.  However, as an extension, we allow VLAs
! 		 in function scopes.  */  
! 	      else if (!at_function_scope_p ())
  		{
  		  error ("array bound is not an integer constant");
  		  bounds = error_mark_node;
  		}
  	    }
Index: cp/semantics.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/semantics.c,v
retrieving revision 1.461
diff -c -5 -p -r1.461 semantics.c
*** cp/semantics.c	17 Feb 2005 16:42:04 -0000	1.461
--- cp/semantics.c	22 Feb 2005 21:37:41 -0000
*************** finish_id_expression (tree id_expression
*** 2627,2641 ****
  	    }
  	  /* A TEMPLATE_ID already contains all the information we
  	     need.  */
  	  if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
  	    return id_expression;
- 	  /* Since this name was dependent, the expression isn't
- 	     constant -- yet.  No error is issued because it might be
- 	     constant when things are instantiated.  */
- 	  if (integral_constant_expression_p)
- 	    *non_integral_constant_expression_p = true;
  	  *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
  	  /* If we found a variable, then name lookup during the
  	     instantiation will always resolve to the same VAR_DECL
  	     (or an instantiation thereof).  */
  	  if (TREE_CODE (decl) == VAR_DECL
--- 2627,2636 ----


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