diff mbox

[C++] Don't reject auto deduction of multiple variables if some initializers were type dependent (PR c++/78693)

Message ID 20170102201119.GM21933@tucnak
State New
Headers show

Commit Message

Jakub Jelinek Jan. 2, 2017, 8:11 p.m. UTC
Hi!

As the testcase shows, if some initializers are type dependent, auto_result
is still using auto, is not yet deduced and the deduction will happen during
instantiation.  So rejecting it due to inconsistent deduction when one
type is int and another type auto (or vice versa) is wrong, during
instantiation it could be still instantiated with initializers that are
valid.

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

Though, the PR also shows that we should do similar diagnostics about
inconsistent auto deduction at instantiation time, but we don't have
infrastructure for that (at tsubst time we don't know which variables
were declared together in a single declaration, nor the auto_result
for those vars where the auto deduction happened already during parsing).
So I think further work is needed on this PR.

2017-01-02  Jakub Jelinek  <jakub@redhat.com>

	PR c++/78693
	* parser.c (cp_parser_simple_declaration): Only complain about
	inconsistent auto deduction if auto_result doesn't use auto.

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


	Jakub

Comments

Jason Merrill Jan. 4, 2017, 9:21 p.m. UTC | #1
OK.

On Mon, Jan 2, 2017 at 3:11 PM, Jakub Jelinek <jakub@redhat.com> wrote:
> Hi!
>
> As the testcase shows, if some initializers are type dependent, auto_result
> is still using auto, is not yet deduced and the deduction will happen during
> instantiation.  So rejecting it due to inconsistent deduction when one
> type is int and another type auto (or vice versa) is wrong, during
> instantiation it could be still instantiated with initializers that are
> valid.
>
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
>
> Though, the PR also shows that we should do similar diagnostics about
> inconsistent auto deduction at instantiation time, but we don't have
> infrastructure for that (at tsubst time we don't know which variables
> were declared together in a single declaration, nor the auto_result
> for those vars where the auto deduction happened already during parsing).
> So I think further work is needed on this PR.
>
> 2017-01-02  Jakub Jelinek  <jakub@redhat.com>
>
>         PR c++/78693
>         * parser.c (cp_parser_simple_declaration): Only complain about
>         inconsistent auto deduction if auto_result doesn't use auto.
>
>         * g++.dg/cpp0x/pr78693.C: New test.
>
> --- gcc/cp/parser.c.jj  2017-01-01 12:45:44.000000000 +0100
> +++ gcc/cp/parser.c     2017-01-02 14:02:49.690859759 +0100
> @@ -12770,9 +12770,11 @@ cp_parser_simple_declaration (cp_parser*
>        if (cp_parser_error_occurred (parser))
>         goto done;
>
> -      if (auto_result)
> +      if (auto_result
> +         && (!processing_template_decl || !type_uses_auto (auto_result)))
>         {
> -         if (last_type && last_type != error_mark_node
> +         if (last_type
> +             && last_type != error_mark_node
>               && !same_type_p (auto_result, last_type))
>             {
>               /* If the list of declarators contains more than one declarator,
> --- gcc/testsuite/g++.dg/cpp0x/pr78693.C.jj     2017-01-02 14:05:45.494582539 +0100
> +++ gcc/testsuite/g++.dg/cpp0x/pr78693.C        2017-01-02 14:05:07.000000000 +0100
> @@ -0,0 +1,31 @@
> +// PR c++/78693
> +// { dg-do compile { target c++11 } }
> +
> +template <class T>
> +void
> +foo (T t)
> +{
> +  auto i = t, j = 1;           // { dg-bogus "inconsistent deduction" }
> +}
> +
> +template <class T>
> +void
> +bar (T t)
> +{
> +  auto i = 1, j = t, k = 2;    // { dg-bogus "inconsistent deduction" }
> +}
> +
> +template <class T, class U>
> +void
> +foo (T t, U u)
> +{
> +  auto i = t, j = u;           // { dg-bogus "inconsistent deduction" }
> +}
> +
> +void
> +foo ()
> +{
> +  foo (0);
> +  bar (0);
> +  foo (1, 2);
> +}
>
>         Jakub
diff mbox

Patch

--- gcc/cp/parser.c.jj	2017-01-01 12:45:44.000000000 +0100
+++ gcc/cp/parser.c	2017-01-02 14:02:49.690859759 +0100
@@ -12770,9 +12770,11 @@  cp_parser_simple_declaration (cp_parser*
       if (cp_parser_error_occurred (parser))
 	goto done;
 
-      if (auto_result)
+      if (auto_result
+	  && (!processing_template_decl || !type_uses_auto (auto_result)))
 	{
-	  if (last_type && last_type != error_mark_node
+	  if (last_type
+	      && last_type != error_mark_node
 	      && !same_type_p (auto_result, last_type))
 	    {
 	      /* If the list of declarators contains more than one declarator,
--- gcc/testsuite/g++.dg/cpp0x/pr78693.C.jj	2017-01-02 14:05:45.494582539 +0100
+++ gcc/testsuite/g++.dg/cpp0x/pr78693.C	2017-01-02 14:05:07.000000000 +0100
@@ -0,0 +1,31 @@ 
+// PR c++/78693
+// { dg-do compile { target c++11 } }
+
+template <class T>
+void
+foo (T t)
+{
+  auto i = t, j = 1;		// { dg-bogus "inconsistent deduction" }
+}
+
+template <class T>
+void
+bar (T t)
+{
+  auto i = 1, j = t, k = 2;	// { dg-bogus "inconsistent deduction" }
+}
+
+template <class T, class U>
+void
+foo (T t, U u)
+{
+  auto i = t, j = u;		// { dg-bogus "inconsistent deduction" }
+}
+
+void
+foo ()
+{
+  foo (0);
+  bar (0);
+  foo (1, 2);
+}