c++: Fix poor diagnostic for array initializer [PR93710]

Message ID 20200212200535.459667-1-polacek@redhat.com
State New
Headers show
Series
  • c++: Fix poor diagnostic for array initializer [PR93710]
Related show

Commit Message

Marek Polacek Feb. 12, 2020, 8:05 p.m.
A small improvement for an error in build_user_type_conversion_1:
instead of

array-init1.C:11:1: error: conversion from ‘long int’ to ‘A’ is ambiguous
   11 | };
      | ^

we will print

array-init1.C:8:3: error: conversion from ‘long int’ to ‘A’ is ambiguous
    8 |   0L,
      |   ^~

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

2020-02-12  Marek Polacek  <polacek@redhat.com>

	PR c++/93710 - poor diagnostic for array initializer.
	* call.c (build_user_type_conversion_1): Use cp_expr_loc_or_input_loc
	for an error call.

	* g++.dg/diagnostic/array-init1.C: New test.
---
 gcc/cp/call.c                                 |  5 +++--
 gcc/testsuite/g++.dg/diagnostic/array-init1.C | 11 +++++++++++
 2 files changed, 14 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/diagnostic/array-init1.C


base-commit: 5bfc8303ffe2d86e938d45f13cd99a39469dac4f
-- 
Marek Polacek • Red Hat, Inc. • 300 A St, Boston, MA

Comments

Jason Merrill Feb. 13, 2020, 11:11 p.m. | #1
On 2/12/20 9:05 PM, Marek Polacek wrote:
> A small improvement for an error in build_user_type_conversion_1:

> instead of

> 

> array-init1.C:11:1: error: conversion from ‘long int’ to ‘A’ is ambiguous

>     11 | };

>        | ^

> 

> we will print

> 

> array-init1.C:8:3: error: conversion from ‘long int’ to ‘A’ is ambiguous

>      8 |   0L,

>        |   ^~

> 

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


OK.

> 2020-02-12  Marek Polacek  <polacek@redhat.com>

> 

> 	PR c++/93710 - poor diagnostic for array initializer.

> 	* call.c (build_user_type_conversion_1): Use cp_expr_loc_or_input_loc

> 	for an error call.

> 

> 	* g++.dg/diagnostic/array-init1.C: New test.

> ---

>   gcc/cp/call.c                                 |  5 +++--

>   gcc/testsuite/g++.dg/diagnostic/array-init1.C | 11 +++++++++++

>   2 files changed, 14 insertions(+), 2 deletions(-)

>   create mode 100644 gcc/testsuite/g++.dg/diagnostic/array-init1.C

> 

> diff --git a/gcc/cp/call.c b/gcc/cp/call.c

> index 51621b7dd87..f47f96bf1c2 100644

> --- a/gcc/cp/call.c

> +++ b/gcc/cp/call.c

> @@ -4172,8 +4172,9 @@ build_user_type_conversion_1 (tree totype, tree expr, int flags,

>         if (complain & tf_error)

>   	{

>   	  auto_diagnostic_group d;

> -	  error ("conversion from %qH to %qI is ambiguous",

> -		 fromtype, totype);

> +	  error_at (cp_expr_loc_or_input_loc (expr),

> +		    "conversion from %qH to %qI is ambiguous",

> +		    fromtype, totype);

>   	  print_z_candidates (location_of (expr), candidates);

>   	}

>   

> diff --git a/gcc/testsuite/g++.dg/diagnostic/array-init1.C b/gcc/testsuite/g++.dg/diagnostic/array-init1.C

> new file mode 100644

> index 00000000000..78580ad6b83

> --- /dev/null

> +++ b/gcc/testsuite/g++.dg/diagnostic/array-init1.C

> @@ -0,0 +1,11 @@

> +// PR c++/93710 - poor diagnostic for array initializer.

> +

> +struct A { A (int); A (char*); int i; };

> +

> +int x;

> +

> +A a1[] = {

> +  0L, // { dg-error "3:conversion from .long int. to .A. is ambiguous" }

> +  &x, // { dg-error "3:invalid conversion from .int\\*. to .int." }

> +  __builtin_offsetof (A, i) // { dg-error "23:conversion from .long unsigned int. to .A. is ambiguous" }

> +};

> 

> base-commit: 5bfc8303ffe2d86e938d45f13cd99a39469dac4f

>
Christophe Lyon Feb. 17, 2020, 1:11 p.m. | #2
On Wed, 12 Feb 2020 at 21:06, Marek Polacek <polacek@redhat.com> wrote:
>

> A small improvement for an error in build_user_type_conversion_1:

> instead of

>

> array-init1.C:11:1: error: conversion from ‘long int’ to ‘A’ is ambiguous

>    11 | };

>       | ^

>

> we will print

>

> array-init1.C:8:3: error: conversion from ‘long int’ to ‘A’ is ambiguous

>     8 |   0L,

>       |   ^~

>

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

>

> 2020-02-12  Marek Polacek  <polacek@redhat.com>

>

>         PR c++/93710 - poor diagnostic for array initializer.

>         * call.c (build_user_type_conversion_1): Use cp_expr_loc_or_input_loc

>         for an error call.

>

>         * g++.dg/diagnostic/array-init1.C: New test.

> ---

>  gcc/cp/call.c                                 |  5 +++--

>  gcc/testsuite/g++.dg/diagnostic/array-init1.C | 11 +++++++++++

>  2 files changed, 14 insertions(+), 2 deletions(-)

>  create mode 100644 gcc/testsuite/g++.dg/diagnostic/array-init1.C

>

> diff --git a/gcc/cp/call.c b/gcc/cp/call.c

> index 51621b7dd87..f47f96bf1c2 100644

> --- a/gcc/cp/call.c

> +++ b/gcc/cp/call.c

> @@ -4172,8 +4172,9 @@ build_user_type_conversion_1 (tree totype, tree expr, int flags,

>        if (complain & tf_error)

>         {

>           auto_diagnostic_group d;

> -         error ("conversion from %qH to %qI is ambiguous",

> -                fromtype, totype);

> +         error_at (cp_expr_loc_or_input_loc (expr),

> +                   "conversion from %qH to %qI is ambiguous",

> +                   fromtype, totype);

>           print_z_candidates (location_of (expr), candidates);

>         }

>

> diff --git a/gcc/testsuite/g++.dg/diagnostic/array-init1.C b/gcc/testsuite/g++.dg/diagnostic/array-init1.C

> new file mode 100644

> index 00000000000..78580ad6b83

> --- /dev/null

> +++ b/gcc/testsuite/g++.dg/diagnostic/array-init1.C

> @@ -0,0 +1,11 @@

> +// PR c++/93710 - poor diagnostic for array initializer.

> +

> +struct A { A (int); A (char*); int i; };

> +

> +int x;

> +

> +A a1[] = {

> +  0L, // { dg-error "3:conversion from .long int. to .A. is ambiguous" }

> +  &x, // { dg-error "3:invalid conversion from .int\\*. to .int." }

> +  __builtin_offsetof (A, i) // { dg-error "23:conversion from .long unsigned int. to .A. is ambiguous" }


Hi,

This test fails on arm (and i586 according to gcc-testresults:
/gcc/testsuite/g++.dg/diagnostic/array-init1.C:10:23: error:
conversion from 'unsigned int' to 'A' is ambiguous

Note sure if you want to make the dg-error conditional or match '.*
unsigned int' ?

Christophe

> +};

>

> base-commit: 5bfc8303ffe2d86e938d45f13cd99a39469dac4f

> --

> Marek Polacek • Red Hat, Inc. • 300 A St, Boston, MA

>
Marek Polacek Feb. 18, 2020, 9:20 p.m. | #3
On Mon, Feb 17, 2020 at 02:11:59PM +0100, Christophe Lyon wrote:
> On Wed, 12 Feb 2020 at 21:06, Marek Polacek <polacek@redhat.com> wrote:

> >

> > A small improvement for an error in build_user_type_conversion_1:

> > instead of

> >

> > array-init1.C:11:1: error: conversion from ‘long int’ to ‘A’ is ambiguous

> >    11 | };

> >       | ^

> >

> > we will print

> >

> > array-init1.C:8:3: error: conversion from ‘long int’ to ‘A’ is ambiguous

> >     8 |   0L,

> >       |   ^~

> >

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

> >

> > 2020-02-12  Marek Polacek  <polacek@redhat.com>

> >

> >         PR c++/93710 - poor diagnostic for array initializer.

> >         * call.c (build_user_type_conversion_1): Use cp_expr_loc_or_input_loc

> >         for an error call.

> >

> >         * g++.dg/diagnostic/array-init1.C: New test.

> > ---

> >  gcc/cp/call.c                                 |  5 +++--

> >  gcc/testsuite/g++.dg/diagnostic/array-init1.C | 11 +++++++++++

> >  2 files changed, 14 insertions(+), 2 deletions(-)

> >  create mode 100644 gcc/testsuite/g++.dg/diagnostic/array-init1.C

> >

> > diff --git a/gcc/cp/call.c b/gcc/cp/call.c

> > index 51621b7dd87..f47f96bf1c2 100644

> > --- a/gcc/cp/call.c

> > +++ b/gcc/cp/call.c

> > @@ -4172,8 +4172,9 @@ build_user_type_conversion_1 (tree totype, tree expr, int flags,

> >        if (complain & tf_error)

> >         {

> >           auto_diagnostic_group d;

> > -         error ("conversion from %qH to %qI is ambiguous",

> > -                fromtype, totype);

> > +         error_at (cp_expr_loc_or_input_loc (expr),

> > +                   "conversion from %qH to %qI is ambiguous",

> > +                   fromtype, totype);

> >           print_z_candidates (location_of (expr), candidates);

> >         }

> >

> > diff --git a/gcc/testsuite/g++.dg/diagnostic/array-init1.C b/gcc/testsuite/g++.dg/diagnostic/array-init1.C

> > new file mode 100644

> > index 00000000000..78580ad6b83

> > --- /dev/null

> > +++ b/gcc/testsuite/g++.dg/diagnostic/array-init1.C

> > @@ -0,0 +1,11 @@

> > +// PR c++/93710 - poor diagnostic for array initializer.

> > +

> > +struct A { A (int); A (char*); int i; };

> > +

> > +int x;

> > +

> > +A a1[] = {

> > +  0L, // { dg-error "3:conversion from .long int. to .A. is ambiguous" }

> > +  &x, // { dg-error "3:invalid conversion from .int\\*. to .int." }

> > +  __builtin_offsetof (A, i) // { dg-error "23:conversion from .long unsigned int. to .A. is ambiguous" }

> 

> Hi,

> 

> This test fails on arm (and i586 according to gcc-testresults:

> /gcc/testsuite/g++.dg/diagnostic/array-init1.C:10:23: error:

> conversion from 'unsigned int' to 'A' is ambiguous

> 

> Note sure if you want to make the dg-error conditional or match '.*

> unsigned int' ?


Sorry about that, fixed now.

Marek

Patch

diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index 51621b7dd87..f47f96bf1c2 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -4172,8 +4172,9 @@  build_user_type_conversion_1 (tree totype, tree expr, int flags,
       if (complain & tf_error)
 	{
 	  auto_diagnostic_group d;
-	  error ("conversion from %qH to %qI is ambiguous",
-		 fromtype, totype);
+	  error_at (cp_expr_loc_or_input_loc (expr),
+		    "conversion from %qH to %qI is ambiguous",
+		    fromtype, totype);
 	  print_z_candidates (location_of (expr), candidates);
 	}
 
diff --git a/gcc/testsuite/g++.dg/diagnostic/array-init1.C b/gcc/testsuite/g++.dg/diagnostic/array-init1.C
new file mode 100644
index 00000000000..78580ad6b83
--- /dev/null
+++ b/gcc/testsuite/g++.dg/diagnostic/array-init1.C
@@ -0,0 +1,11 @@ 
+// PR c++/93710 - poor diagnostic for array initializer.
+
+struct A { A (int); A (char*); int i; };
+
+int x;
+
+A a1[] = {
+  0L, // { dg-error "3:conversion from .long int. to .A. is ambiguous" }
+  &x, // { dg-error "3:invalid conversion from .int\\*. to .int." }
+  __builtin_offsetof (A, i) // { dg-error "23:conversion from .long unsigned int. to .A. is ambiguous" }
+};