public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* C++ PATCH for c++/84374 - diagnose invalid uses of decltype(auto)
@ 2019-09-08 16:47 Marek Polacek
  2019-09-09 21:00 ` Jason Merrill
  0 siblings, 1 reply; 4+ messages in thread
From: Marek Polacek @ 2019-09-08 16:47 UTC (permalink / raw)
  To: GCC Patches, Jason Merrill

[dcl.type.auto.deduct]/5 "If the placeholder-type-specifier is of the
form type-constraintopt decltype(auto), T shall be the placeholder alone."

So, only plain decltype(auto) is allowed.  But we aren't diagnosing it in
function declarations, because do_auto_deduction is never called for those.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2019-09-08  Marek Polacek  <polacek@redhat.com>

	PR c++/84374 - diagnose invalid uses of decltype(auto).
	* decl.c (grokdeclarator): Diagnose wrong usage of decltype(auto) in
	a function declaration.

	* g++.dg/cpp1y/auto-fn57.C: New test.

diff --git gcc/cp/decl.c gcc/cp/decl.c
index 88e2c3beb2b..b1777730934 100644
--- gcc/cp/decl.c
+++ gcc/cp/decl.c
@@ -11560,6 +11560,14 @@ grokdeclarator (const cp_declarator *declarator,
 				      "cannot have deduced return type");
 			    virtualp = false;
 			  }
+			else if (is_auto (auto_node)
+				 && AUTO_IS_DECLTYPE (auto_node)
+				 && type != auto_node)
+			  {
+			    error_at (typespec_loc, "%qT as type rather than "
+				      "plain %<decltype(auto)%>", type);
+			    return error_mark_node;
+			  }
 		      }
 		    else if (!is_auto (type) && sfk != sfk_conversion)
 		      {
@@ -11580,6 +11588,16 @@ grokdeclarator (const cp_declarator *declarator,
 				    "invalid use of %<decltype(auto)%>");
 			return error_mark_node;
 		      }
+		    else if (tree a = type_uses_auto (late_return_type))
+		      {
+			if (AUTO_IS_DECLTYPE (a) && a != late_return_type)
+			  {
+			    error_at (typespec_loc, "%qT as type rather than "
+				      "plain %<decltype(auto)%>",
+				      late_return_type);
+			    return error_mark_node;
+			  }
+		      }
 		    tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
 		    if (!tmpl)
 		      if (tree late_auto = type_uses_auto (late_return_type))
diff --git gcc/testsuite/g++.dg/cpp1y/auto-fn57.C gcc/testsuite/g++.dg/cpp1y/auto-fn57.C
new file mode 100644
index 00000000000..e58df187df5
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp1y/auto-fn57.C
@@ -0,0 +1,18 @@
+// PR c++/84374 - diagnose invalid uses of decltype(auto).
+// { dg-do compile { target c++14 } }
+
+auto l = [](auto* r)->decltype(auto)* { return r; }; // { dg-error "as type rather than plain" }
+auto m = [](auto* r)->decltype(auto)& { return *r; }; // { dg-error "as type rather than plain" }
+
+decltype(auto)* f(); // { dg-error "as type rather than plain" }
+decltype(auto)& f2(); // { dg-error "as type rather than plain" }
+decltype(auto)* f3() { return 42; } // { dg-error "as type rather than plain" }
+decltype(auto)& f4() { return 42; } // { dg-error "as type rather than plain" }
+
+
+class C {
+  decltype(auto)* g(); // { dg-error "as type rather than plain" }
+  decltype(auto)& g2(); // { dg-error "as type rather than plain" }
+  decltype(auto)* g3() { } // { dg-error "as type rather than plain" }
+  decltype(auto)& g4() { } // { dg-error "as type rather than plain" }
+};

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: C++ PATCH for c++/84374 - diagnose invalid uses of decltype(auto)
  2019-09-08 16:47 C++ PATCH for c++/84374 - diagnose invalid uses of decltype(auto) Marek Polacek
@ 2019-09-09 21:00 ` Jason Merrill
  2019-09-10  0:04   ` Marek Polacek
  0 siblings, 1 reply; 4+ messages in thread
From: Jason Merrill @ 2019-09-09 21:00 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches

On 9/8/19 11:47 AM, Marek Polacek wrote:
> [dcl.type.auto.deduct]/5 "If the placeholder-type-specifier is of the
> form type-constraintopt decltype(auto), T shall be the placeholder alone."
> 
> So, only plain decltype(auto) is allowed.  But we aren't diagnosing it in
> function declarations, because do_auto_deduction is never called for those.
> 
> Bootstrapped/regtested on x86_64-linux, ok for trunk?
> 
> 2019-09-08  Marek Polacek  <polacek@redhat.com>
> 
> 	PR c++/84374 - diagnose invalid uses of decltype(auto).
> 	* decl.c (grokdeclarator): Diagnose wrong usage of decltype(auto) in
> 	a function declaration.
> 
> 	* g++.dg/cpp1y/auto-fn57.C: New test.
> 
> diff --git gcc/cp/decl.c gcc/cp/decl.c
> index 88e2c3beb2b..b1777730934 100644
> --- gcc/cp/decl.c
> +++ gcc/cp/decl.c
> @@ -11560,6 +11560,14 @@ grokdeclarator (const cp_declarator *declarator,
>   				      "cannot have deduced return type");
>   			    virtualp = false;
>   			  }
> +			else if (is_auto (auto_node)
> +				 && AUTO_IS_DECLTYPE (auto_node)
> +				 && type != auto_node)
> +			  {
> +			    error_at (typespec_loc, "%qT as type rather than "
> +				      "plain %<decltype(auto)%>", type);
> +			    return error_mark_node;
> +			  }
>   		      }
>   		    else if (!is_auto (type) && sfk != sfk_conversion)
>   		      {
> @@ -11580,6 +11588,16 @@ grokdeclarator (const cp_declarator *declarator,
>   				    "invalid use of %<decltype(auto)%>");
>   			return error_mark_node;
>   		      }
> +		    else if (tree a = type_uses_auto (late_return_type))
> +		      {
> +			if (AUTO_IS_DECLTYPE (a) && a != late_return_type)
> +			  {
> +			    error_at (typespec_loc, "%qT as type rather than "
> +				      "plain %<decltype(auto)%>",
> +				      late_return_type);
> +			    return error_mark_node;
> +			  }
> +		      }

Maybe check this in one place, after splice_late_return_type?

Jason

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: C++ PATCH for c++/84374 - diagnose invalid uses of decltype(auto)
  2019-09-09 21:00 ` Jason Merrill
@ 2019-09-10  0:04   ` Marek Polacek
  2019-09-10  2:17     ` Jason Merrill
  0 siblings, 1 reply; 4+ messages in thread
From: Marek Polacek @ 2019-09-10  0:04 UTC (permalink / raw)
  To: Jason Merrill; +Cc: GCC Patches

On Mon, Sep 09, 2019 at 04:59:57PM -0400, Jason Merrill wrote:
> Maybe check this in one place, after splice_late_return_type?

That works!  So like this?

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2019-09-09  Marek Polacek  <polacek@redhat.com>

	PR c++/84374 - diagnose invalid uses of decltype(auto).
	* decl.c (grokdeclarator): Diagnose wrong usage of decltype(auto) in
	a function declaration.

	* g++.dg/cpp1y/auto-fn57.C: New test.

diff --git gcc/cp/decl.c gcc/cp/decl.c
index 88e2c3beb2b..dfcd7b16e6e 100644
--- gcc/cp/decl.c
+++ gcc/cp/decl.c
@@ -11681,6 +11681,16 @@ grokdeclarator (const cp_declarator *declarator,
 			  "allowed");
 		return error_mark_node;
 	      }
+	    /* Only plain decltype(auto) is allowed.  */
+	    if (tree a = type_uses_auto (type))
+	      {
+		if (AUTO_IS_DECLTYPE (a) && a != type)
+		  {
+		    error_at (typespec_loc, "%qT as type rather than "
+			      "plain %<decltype(auto)%>", type);
+		    return error_mark_node;
+		  }
+	      }
 
 	    if (ctype == NULL_TREE
 		&& decl_context == FIELD
diff --git gcc/testsuite/g++.dg/cpp1y/auto-fn57.C gcc/testsuite/g++.dg/cpp1y/auto-fn57.C
new file mode 100644
index 00000000000..e58df187df5
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp1y/auto-fn57.C
@@ -0,0 +1,18 @@
+// PR c++/84374 - diagnose invalid uses of decltype(auto).
+// { dg-do compile { target c++14 } }
+
+auto l = [](auto* r)->decltype(auto)* { return r; }; // { dg-error "as type rather than plain" }
+auto m = [](auto* r)->decltype(auto)& { return *r; }; // { dg-error "as type rather than plain" }
+
+decltype(auto)* f(); // { dg-error "as type rather than plain" }
+decltype(auto)& f2(); // { dg-error "as type rather than plain" }
+decltype(auto)* f3() { return 42; } // { dg-error "as type rather than plain" }
+decltype(auto)& f4() { return 42; } // { dg-error "as type rather than plain" }
+
+
+class C {
+  decltype(auto)* g(); // { dg-error "as type rather than plain" }
+  decltype(auto)& g2(); // { dg-error "as type rather than plain" }
+  decltype(auto)* g3() { } // { dg-error "as type rather than plain" }
+  decltype(auto)& g4() { } // { dg-error "as type rather than plain" }
+};

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: C++ PATCH for c++/84374 - diagnose invalid uses of decltype(auto)
  2019-09-10  0:04   ` Marek Polacek
@ 2019-09-10  2:17     ` Jason Merrill
  0 siblings, 0 replies; 4+ messages in thread
From: Jason Merrill @ 2019-09-10  2:17 UTC (permalink / raw)
  To: Marek Polacek; +Cc: GCC Patches

On 9/9/19 7:03 PM, Marek Polacek wrote:
> On Mon, Sep 09, 2019 at 04:59:57PM -0400, Jason Merrill wrote:
>> Maybe check this in one place, after splice_late_return_type?
> 
> That works!  So like this?
> 
> Bootstrapped/regtested on x86_64-linux, ok for trunk?

OK, thanks.

Jason

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-09-10  2:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-08 16:47 C++ PATCH for c++/84374 - diagnose invalid uses of decltype(auto) Marek Polacek
2019-09-09 21:00 ` Jason Merrill
2019-09-10  0:04   ` Marek Polacek
2019-09-10  2:17     ` Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).