From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 24B223858D35 for ; Fri, 17 Dec 2021 01:13:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 24B223858D35 Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-401-mtMMF0TlNseuJUjrwF_HUw-1; Thu, 16 Dec 2021 20:13:03 -0500 X-MC-Unique: mtMMF0TlNseuJUjrwF_HUw-1 Received: by mail-qv1-f72.google.com with SMTP id a7-20020a056214062700b00410c76282c3so1107795qvx.4 for ; Thu, 16 Dec 2021 17:13:03 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=Hh8XKDeWZez9BddQqb2RshIhf9F/zq0At30Tbd8z1JA=; b=yaa4c/SQZ0fq3lJZ7PGa+Yc/YI9ZwvrgpyOP2dJrUm5Q/62rIfqQw4j3ruCKWdDvtp Tsy/Kc1rDOjrds0+l+xAvRhVcmfdcvtLUvDgcV1LT5qN+FpakgaglHxFVigSpDYDzXv4 93wm2tF84lI/M4sIFRFFsPzXq6BMzD1eFhIUmGt9h07z6IRgwKurId599G+t6xF5ofMN mlZG+9oRf0dOaiVzVi036a89Kjh6xDyIrPNmg9W0p3/nKnQRMMsHKQu4ce/8UP7GwY+1 Ngj3NheQxXo5K7SwDUcXcvIZOCBFGqrofuHO1KFhQyayCgcp7qIDMFoNODIG8WBJZbUt zpfw== X-Gm-Message-State: AOAM5330H47WTZOHjpMakmzuo5b/KuBL10izDmeomZ3JiAHmd9D9tOKs E/mee2GL71bt559HNVYaXFDozW1Wc4P/hCu5JaeXBEf81pDJ93utjwwKZclLFTyBbDSAce+gxxe 1ylp0rg0UFjFRw/FhDA== X-Received: by 2002:a37:654f:: with SMTP id z76mr473769qkb.224.1639703582839; Thu, 16 Dec 2021 17:13:02 -0800 (PST) X-Google-Smtp-Source: ABdhPJxTaVcr7phHAABU4C3k9s1m0k05qzvu8qJsKpgI5+jY8vIu7I9TwO3Xej3BGkubSIbsQNm9AQ== X-Received: by 2002:a37:654f:: with SMTP id z76mr473751qkb.224.1639703582458; Thu, 16 Dec 2021 17:13:02 -0800 (PST) Received: from [192.168.1.149] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id d5sm5759256qte.27.2021.12.16.17.13.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 16 Dec 2021 17:13:01 -0800 (PST) Message-ID: <86ae73a7-0df1-1082-7d67-f8aa89a3cd1e@redhat.com> Date: Thu, 16 Dec 2021 20:13:01 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 Subject: Re: [PATCH] c++: Improve diagnostic for class tmpl/class redecl [PR103749] To: Marek Polacek , GCC Patches References: <20211216223659.820883-1-polacek@redhat.com> From: Jason Merrill In-Reply-To: <20211216223659.820883-1-polacek@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.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, NICE_REPLY_A, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TRACKER_ID, 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-List-Received-Date: Fri, 17 Dec 2021 01:13:18 -0000 On 12/16/21 17:36, Marek Polacek wrote: > 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. 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 > + 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" } > > base-commit: f91814c22595e1db642140efe030caf2c092ab6f