diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 7ca8770bd02..982fca8983d 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -15438,6 +15438,16 @@ 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 ("class template %qD redeclared as non-template", 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. */ 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..4423e12b0b1 --- /dev/null +++ b/gcc/testsuite/g++.dg/diagnostic/redeclaration-2.C @@ -0,0 +1,19 @@ +// PR c++/103749 + +struct foo { + template + friend struct bar; +}; + +struct bar { // { dg-error "redeclared as non-template" } + int baz; +}; + +template +struct T; // { dg-message "previous" } + +struct T { // { dg-error "redeclared as non-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..c9282cde860 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 union A; // { dg-message "previous" } -struct A; // { dg-error "non-template" } +struct A; // { dg-error "redeclared as non-template" }