From patchwork Thu Dec 16 22:36:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 49029 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C3E753858431 for ; Thu, 16 Dec 2021 22:39:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C3E753858431 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1639694373; bh=oYwSNbclgenIdzgIlz19bYSB0thGvCZG9bhlG9vp7DQ=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=beNo1d+ek3v6IO3CeQiJ0moUROxobrqLMi8a8zHfpXT3ULRrKbfBCVNBLE9+7cRq5 NtiFhIo1AI2HirGWy0X2S5ktVSUlbFT8Dc27SaGqwUp1cAdutkWx+LGh/C7jlMNi/y OMBfHa7P/dBbkLPPl2a2IqRy/mpnZMRN9b6/nE7c= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id BA1E23858415 for ; Thu, 16 Dec 2021 22:37:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BA1E23858415 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-356-q99HPlGMO7u7WD_iwpEN3A-1; Thu, 16 Dec 2021 17:37:06 -0500 X-MC-Unique: q99HPlGMO7u7WD_iwpEN3A-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1876781EE60 for ; Thu, 16 Dec 2021 22:37:06 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.18.74]) by smtp.corp.redhat.com (Postfix) with ESMTP id ED0C66E98C; Thu, 16 Dec 2021 22:37:00 +0000 (UTC) To: GCC Patches , Jason Merrill Subject: [PATCH] c++: Improve diagnostic for class tmpl/class redecl [PR103749] Date: Thu, 16 Dec 2021 17:36:59 -0500 Message-Id: <20211216223659.820883-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" For code like template 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 bar()-> bar< >' 3 | friend struct bar; | ^~~ u.cc:3:17: note: template argument deduction/substitution failed: u.cc:10:5: note: couldn't deduce template parameter '' 10 | bar var; | ^~~ u.cc:3:17: note: candidate: 'template bar(bar< >)-> bar< >' 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 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); + 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 + friend struct bar; +}; + +struct bar { // { dg-error "declared as template" } + int baz; +}; + +template +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 union A; // { dg-message "previous" } -struct A; // { dg-error "non-template" } +struct A; // { dg-error "declared as template" }