diff mbox

PR c++/51473 - ICE with invalid auto

Message ID m34nx212ok.fsf@redhat.com
State New
Headers show

Commit Message

Dodji Seketeli Dec. 15, 2011, 11:42 a.m. UTC
Hello,

It seems to me that [dcl.spec.auto]/1

    The auto type-specifier signifies that the type of a variable
    being declared shall be deduced from its initializer or that a
    function declarator shall include a trailing-return-type.

and the subsequent paragraphs of that section up to [dcl.spec.auto]/5

    A program that uses auto in a context not explicitly allowed in
    this section is ill-formed.

imply that it is not possible to have an 'auto' specifier in a
simple-declaration that has no declarator.

Yet in the example the patch below, G++ doesn't diagnose that and as
it goes further in analyzing that example, it encounters
inconsistencies that lead to an ICE.

Thus I am proposing to error out early in this case.

Bootstrapped and tested on x86_64-unknown-linux-gnu against trunk.


gcc/cp/

	PR c++/51473
	* decl.c (check_tag_decl): Error out on auto specifier with no
	declarator.

gcc/testsuite/

	PR c++/51473
	* g++.dg/cpp0x/auto30.C: New test.
---
 gcc/cp/decl.c                       |    6 ++++++
 gcc/testsuite/g++.dg/cpp0x/auto30.C |    9 +++++++++
 2 files changed, 15 insertions(+), 0 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/auto30.C

Comments

Jason Merrill Dec. 15, 2011, 1:55 p.m. UTC | #1
OK.

Jason
diff mbox

Patch

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 5a4e027..0f0931e 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -4140,6 +4140,12 @@  check_tag_decl (cp_decl_specifier_seq *declspecs)
     error_p = true;
   if (declared_type == NULL_TREE && ! saw_friend && !error_p)
     permerror (input_location, "declaration does not declare anything");
+  else if (declared_type != NULL_TREE && type_uses_auto (declared_type))
+    {
+      error ("%<auto%> can only be specified for variables "
+	     "or function declarations");
+      return error_mark_node;
+    }
   /* Check for an anonymous union.  */
   else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
 	   && TYPE_ANONYMOUS_P (declared_type))
diff --git a/gcc/testsuite/g++.dg/cpp0x/auto30.C b/gcc/testsuite/g++.dg/cpp0x/auto30.C
new file mode 100644
index 0000000..d26e290
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/auto30.C
@@ -0,0 +1,9 @@ 
+// Origin PR c++/51473
+// { dg-options "-std=c++11" }
+
+struct A
+{
+    auto friend struct B; // { dg-error "multiple types|can only be specified|friend" }
+};
+
+auto int; // { dg-error "multiple types|can only be specified for variables" }