[C++ PATCH] Diagnose invalid auto fndecl without late return type or non-auto fndecl with late return type (PR c++/37967)

Jakub Jelinek jakub@redhat.com
Fri Oct 31 16:40:00 GMT 2008


Hi!

>From my reading auto foo (); is invalid, as auto for function decls is
only allowed for late return types.  Also, IMHO late return type requires
auto type and
"the type of the contained declarator-id in the declaration T D1 is
 `derived-declarator-type-list T,' T shall be the single type-specifier
 auto and the derived-declarator-type-list shall be empty."
IMHO means that even auto *foo () -> int; is invalid (correct me if
I'm wrong).  Ok for trunk?

BTW, I wonder whether
auto (*fp) () -> int;
is valid or not (I bet it is not), we ATM accept it (without or with
the following patch).  If it is invalid, we could diagnose that in
else if (declarator->u.function.late_return_type) after the
if (funcdecl_p).

2008-10-31  Jakub Jelinek  <jakub@redhat.com>

	PR c++/37967
	* decl.c (grokdeclarator): Diagnose auto function decl without
	late return type and late return type function decl where type
	is not auto.

	* g++.dg/cpp0x/auto8.C: New test.

--- gcc/cp/decl.c.jj	2008-10-31 15:36:20.000000000 +0100
+++ gcc/cp/decl.c	2008-10-31 16:26:58.000000000 +0100
@@ -8237,16 +8237,40 @@ grokdeclarator (const cp_declarator *dec
 	    /* Pick up the exception specifications.  */
 	    raises = declarator->u.function.exception_specification;
 
+	    /* Say it's a definition only for the CALL_EXPR
+	       closest to the identifier.  */
+	    funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
+
 	    /* Handle a late-specified return type.  */
+	    if (funcdecl_p)
+	      {
+		if (type_uses_auto (type))
+		  {
+		    if (!declarator->u.function.late_return_type)
+		      {
+			error ("%qs function uses auto type specifier without"
+			       " late return type", name);
+			return error_mark_node;
+		      }
+		    else if (!is_auto (type))
+		      {
+			error ("%qs function with late return type not using"
+			       " auto type specifier as its type", name);
+			return error_mark_node;
+		      }
+		  }
+		else if (declarator->u.function.late_return_type)
+		  {
+		    error ("%qs function with late return type not declared"
+			   " with auto type specifier", name);
+		    return error_mark_node;
+		  }
+	      }
 	    type = splice_late_return_type
 	      (type, declarator->u.function.late_return_type);
 	    if (type == error_mark_node)
 	      return error_mark_node;
 
-	    /* Say it's a definition only for the CALL_EXPR
-	       closest to the identifier.  */
-	    funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
-
 	    if (ctype == NULL_TREE
 		&& decl_context == FIELD
 		&& funcdecl_p
--- gcc/testsuite/g++.dg/cpp0x/auto8.C.jj	2008-10-31 16:07:15.000000000 +0100
+++ gcc/testsuite/g++.dg/cpp0x/auto8.C	2008-10-31 16:07:15.000000000 +0100
@@ -0,0 +1,16 @@
+// PR c++/37967
+// Negative test for auto
+// { dg-options "-std=c++0x" }
+
+auto f1 () -> int;
+auto f2 ();		// { dg-error "without late return type" }
+int f3 () -> int;	// { dg-error "with auto type specifier" }
+auto *f4 () -> int;	// { dg-error "not using auto" }
+
+struct A
+{
+  auto f5 () const -> int;
+  auto f6 ();		// { dg-error "without late return type" }
+  int f7 () -> int;	// { dg-error "with auto type specifier" }
+  auto *f8 () -> int;	// { dg-error "not using auto" }
+};

	Jakub



More information about the Gcc-patches mailing list