c++: Improve diagnostic for class tmpl/class redecl [PR103749]
Commit Message
For code like
template<typename>
struct bar;
struct bar {
int baz;
};
bar var;
we emit a fairly misleading and unwieldy diagnostic:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$ g++ -c u.cc
u.cc:6:8: error: template argument required for 'struct bar'
6 | struct bar {
| ^~~
u.cc:10:5: error: class template argument deduction failed:
10 | bar var;
| ^~~
u.cc:10:5: error: no matching function for call to 'bar()'
u.cc:3:17: note: candidate: 'template<class> bar()-> bar< <template-parameter-1-1> >'
3 | friend struct bar;
| ^~~
u.cc:3:17: note: template argument deduction/substitution failed:
u.cc:10:5: note: couldn't deduce template parameter '<template-parameter-1-1>'
10 | bar var;
| ^~~
u.cc:3:17: note: candidate: 'template<class> bar(bar< <template-parameter-1-1> >)-> bar< <template-parameter-1-1> >'
3 | friend struct bar;
| ^~~
u.cc:3:17: note: template argument deduction/substitution failed:
u.cc:10:5: note: candidate expects 1 argument, 0 provided
10 | bar var;
| ^~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
but with this patch we get:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
z.C:6:8: error: 'bar' was declared as template but no template header provided
6 | struct bar {
| ^~~
z.C:3:17: note: previous declaration here
3 | friend struct bar;
| ^~~
z.C:10:5: error: 'bar<...auto...> var' has incomplete type
10 | bar var;
| ^~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
which is clearer about what the problem is.
I thought it'd be nice to avoid printing the messages about failed CTAD,
too. To that end, I'm using CLASSTYPE_ERRONEOUS to suppress CTAD. Not
sure if that's entirely kosher.
The other direction (first a non-template class declaration followed by
a class template definition) we handle quite well:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
z.C:11:8: error: 'bar' is not a template
11 | struct bar {};
| ^~~
z.C:8:8: note: previous declaration here
8 | struct bar;
| ^~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
PR c++/103749
gcc/cp/ChangeLog:
* decl.c (lookup_and_check_tag): Give an error when a class was
declared as template but no template header has been provided.
* pt.c (do_class_deduction): Don't deduce CLASSTYPE_ERRONEOUS
types.
gcc/testsuite/ChangeLog:
* g++.dg/template/redecl4.C: Adjust dg-error.
* g++.dg/diagnostic/redeclaration-2.C: New test.
---
gcc/cp/decl.c | 11 +++++++++++
gcc/cp/pt.c | 7 +++++++
.../g++.dg/diagnostic/redeclaration-2.C | 19 +++++++++++++++++++
gcc/testsuite/g++.dg/template/redecl4.C | 2 +-
4 files changed, 38 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/g++.dg/diagnostic/redeclaration-2.C
base-commit: f91814c22595e1db642140efe030caf2c092ab6f
Comments
On 12/16/21 17:36, Marek Polacek wrote:
> For code like
>
> template<typename>
> struct bar;
>
> struct bar {
> int baz;
> };
>
> bar var;
>
> we emit a fairly misleading and unwieldy diagnostic:
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> $ g++ -c u.cc
> u.cc:6:8: error: template argument required for 'struct bar'
> 6 | struct bar {
> | ^~~
> u.cc:10:5: error: class template argument deduction failed:
> 10 | bar var;
> | ^~~
> u.cc:10:5: error: no matching function for call to 'bar()'
> u.cc:3:17: note: candidate: 'template<class> bar()-> bar< <template-parameter-1-1> >'
> 3 | friend struct bar;
> | ^~~
> u.cc:3:17: note: template argument deduction/substitution failed:
> u.cc:10:5: note: couldn't deduce template parameter '<template-parameter-1-1>'
> 10 | bar var;
> | ^~~
> u.cc:3:17: note: candidate: 'template<class> bar(bar< <template-parameter-1-1> >)-> bar< <template-parameter-1-1> >'
> 3 | friend struct bar;
> | ^~~
> u.cc:3:17: note: template argument deduction/substitution failed:
> u.cc:10:5: note: candidate expects 1 argument, 0 provided
> 10 | bar var;
> | ^~~
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> but with this patch we get:
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> z.C:6:8: error: 'bar' was declared as template but no template header provided
> 6 | struct bar {
> | ^~~
> z.C:3:17: note: previous declaration here
> 3 | friend struct bar;
> | ^~~
> z.C:10:5: error: 'bar<...auto...> var' has incomplete type
> 10 | bar var;
> | ^~~
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> which is clearer about what the problem is.
>
> I thought it'd be nice to avoid printing the messages about failed CTAD,
> too. To that end, I'm using CLASSTYPE_ERRONEOUS to suppress CTAD. Not
> sure if that's entirely kosher.
A bit dubious, but error-recovery often is. Let's go with it.
> The other direction (first a non-template class declaration followed by
> a class template definition) we handle quite well:
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> z.C:11:8: error: 'bar' is not a template
> 11 | struct bar {};
> | ^~~
> z.C:8:8: note: previous declaration here
> 8 | struct bar;
> | ^~~
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
>
> PR c++/103749
>
> gcc/cp/ChangeLog:
>
> * decl.c (lookup_and_check_tag): Give an error when a class was
> declared as template but no template header has been provided.
> * pt.c (do_class_deduction): Don't deduce CLASSTYPE_ERRONEOUS
> types.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/template/redecl4.C: Adjust dg-error.
> * g++.dg/diagnostic/redeclaration-2.C: New test.
> ---
> gcc/cp/decl.c | 11 +++++++++++
> gcc/cp/pt.c | 7 +++++++
> .../g++.dg/diagnostic/redeclaration-2.C | 19 +++++++++++++++++++
> gcc/testsuite/g++.dg/template/redecl4.C | 2 +-
> 4 files changed, 38 insertions(+), 1 deletion(-)
> create mode 100644 gcc/testsuite/g++.dg/diagnostic/redeclaration-2.C
>
> diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
> index 7ca8770bd02..56fa5b3ffeb 100644
> --- a/gcc/cp/decl.c
> +++ b/gcc/cp/decl.c
> @@ -15438,6 +15438,17 @@ lookup_and_check_tag (enum tag_types tag_code, tree name,
> return error_mark_node;
> }
>
> + if (DECL_CLASS_TEMPLATE_P (decl)
> + && !template_header_p
> + && how == TAG_how::CURRENT_ONLY)
> + {
> + error ("%qD was declared as template but no template header provided",
> + name);
Maybe "class template %qD redeclared as non-template" ?
OK with that change.
> + inform (location_of (decl), "previous declaration here");
> + CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true;
> + return error_mark_node;
> + }
> +
> if (DECL_CLASS_TEMPLATE_P (decl)
> /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
> so ignore a template template parameter. */
> diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
> index 18c6f118ae6..4f0ae6d5851 100644
> --- a/gcc/cp/pt.c
> +++ b/gcc/cp/pt.c
> @@ -29601,6 +29601,13 @@ do_class_deduction (tree ptype, tree tmpl, tree init,
> if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
> return ptype;
>
> + /* If the class was erroneous, don't try to deduce, because that
> + can generate a lot of diagnostic. */
> + if (TREE_TYPE (tmpl)
> + && TYPE_LANG_SPECIFIC (TREE_TYPE (tmpl))
> + && CLASSTYPE_ERRONEOUS (TREE_TYPE (tmpl)))
> + return ptype;
> +
> /* Wait until the enclosing scope is non-dependent. */
> if (DECL_CLASS_SCOPE_P (tmpl)
> && dependent_type_p (DECL_CONTEXT (tmpl)))
> diff --git a/gcc/testsuite/g++.dg/diagnostic/redeclaration-2.C b/gcc/testsuite/g++.dg/diagnostic/redeclaration-2.C
> new file mode 100644
> index 00000000000..ac14c2c135f
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/diagnostic/redeclaration-2.C
> @@ -0,0 +1,19 @@
> +// PR c++/103749
> +
> +struct foo {
> + template<typename>
> + friend struct bar;
> +};
> +
> +struct bar { // { dg-error "declared as template" }
> + int baz;
> +};
> +
> +template<typename>
> +struct T; // { dg-message "previous" }
> +
> +struct T { // { dg-error "declared as template" }
> +};
> +
> +bar var; // { dg-error "" }
> +T t; // { dg-error "" }
> diff --git a/gcc/testsuite/g++.dg/template/redecl4.C b/gcc/testsuite/g++.dg/template/redecl4.C
> index 5638bde413d..498ca83a0f4 100644
> --- a/gcc/testsuite/g++.dg/template/redecl4.C
> +++ b/gcc/testsuite/g++.dg/template/redecl4.C
> @@ -2,4 +2,4 @@
> // { dg-do compile }
>
> template<int> union A; // { dg-message "previous" }
> -struct A; // { dg-error "non-template" }
> +struct A; // { dg-error "declared as template" }
>
> base-commit: f91814c22595e1db642140efe030caf2c092ab6f
@@ -15438,6 +15438,17 @@ lookup_and_check_tag (enum tag_types tag_code, tree name,
return error_mark_node;
}
+ if (DECL_CLASS_TEMPLATE_P (decl)
+ && !template_header_p
+ && how == TAG_how::CURRENT_ONLY)
+ {
+ error ("%qD was declared as template but no template header provided",
+ name);
+ inform (location_of (decl), "previous declaration here");
+ CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true;
+ return error_mark_node;
+ }
+
if (DECL_CLASS_TEMPLATE_P (decl)
/* If scope is TAG_how::CURRENT_ONLY we're defining a class,
so ignore a template template parameter. */
@@ -29601,6 +29601,13 @@ do_class_deduction (tree ptype, tree tmpl, tree init,
if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
return ptype;
+ /* If the class was erroneous, don't try to deduce, because that
+ can generate a lot of diagnostic. */
+ if (TREE_TYPE (tmpl)
+ && TYPE_LANG_SPECIFIC (TREE_TYPE (tmpl))
+ && CLASSTYPE_ERRONEOUS (TREE_TYPE (tmpl)))
+ return ptype;
+
/* Wait until the enclosing scope is non-dependent. */
if (DECL_CLASS_SCOPE_P (tmpl)
&& dependent_type_p (DECL_CONTEXT (tmpl)))
new file mode 100644
@@ -0,0 +1,19 @@
+// PR c++/103749
+
+struct foo {
+ template<typename>
+ friend struct bar;
+};
+
+struct bar { // { dg-error "declared as template" }
+ int baz;
+};
+
+template<typename>
+struct T; // { dg-message "previous" }
+
+struct T { // { dg-error "declared as template" }
+};
+
+bar var; // { dg-error "" }
+T t; // { dg-error "" }
@@ -2,4 +2,4 @@
// { dg-do compile }
template<int> union A; // { dg-message "previous" }
-struct A; // { dg-error "non-template" }
+struct A; // { dg-error "declared as template" }