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.133.124]) by sourceware.org (Postfix) with ESMTPS id 0C56C3857C71 for ; Wed, 1 Mar 2023 21:30:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0C56C3857C71 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677706230; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=G30aSnFT/GKmp4lXmHByVAIHCWP+YjzgeayP051wxGk=; b=MJ6lo47iRp7T8kMTHd/7RPxCFIpuZ3e0MWXP8yYkKXTN6NJWQIFAqeJia3lzLULw+Knh5R t55OTwDDkgtSrQSR3d/0LWEPsItIAP4oETTAtqHu9WO/ywEcwkxqSof1XVt4cYLW6pprW5 FRdLCz9iZZZZz7qMI5B0I1feeaC7pLI= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-556-GH4st4v8PyGJ5SlCsb3Rdw-1; Wed, 01 Mar 2023 16:30:22 -0500 X-MC-Unique: GH4st4v8PyGJ5SlCsb3Rdw-1 Received: by mail-qk1-f199.google.com with SMTP id z23-20020a05620a101700b0073b328e7d17so8832822qkj.9 for ; Wed, 01 Mar 2023 13:30:20 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677706218; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=G30aSnFT/GKmp4lXmHByVAIHCWP+YjzgeayP051wxGk=; b=F0qdDxDSO5+nC7UBPCb+XXGBKoG8T0hQxXow9F/GtAfymK7jDU9miUoZnvekC9hRbV LebCOLf983Ye9zpuiNJb5xNvcYuIMUMzGNGxA4Q0E/2Y7NHdKagA3Q3fTV2j+wj5GkeL dSvg2RwDgGr/+ior19urN8eb5Job+rjFFgUiuePS8KfTOkKq6sXNKMJcTHJF4qC0vZGs tC8WQJnMaNz0KB7/PXhr27k2+soLsaFw3/9t2CA5c04ZtjIp2YnKlncFl+I65F4tNtXd tbK0nId3viz7sVES2+PDWgMsh4ZoLI04CuzOQVrAfkbXrAkbzB45ES+QJcKoYKYNRCW6 ZLQw== X-Gm-Message-State: AO0yUKUuPeRyIHbQldgadxFbJ6BN0876D0iuDD1Jm69GSCmaTibPSV6P tOTitP1Aa1kU0TjRjPi4fvDC3m9l4awCI98aSZZ2Cqjn4MhtNjWGc6uInC1ArvqPS27cv6iQB9l Ci7RqpjEIgk6xSJhNaQ== X-Received: by 2002:ac8:5b4e:0:b0:3b9:fb0e:adba with SMTP id n14-20020ac85b4e000000b003b9fb0eadbamr13783080qtw.25.1677706218422; Wed, 01 Mar 2023 13:30:18 -0800 (PST) X-Google-Smtp-Source: AK7set84VI0YfaAzJd04Rsimeh9DHLLz9QNn6TUANO9E5BGl8Wg5UzoCWlE0g+76O26HGW2dU5m+rw== X-Received: by 2002:ac8:5b4e:0:b0:3b9:fb0e:adba with SMTP id n14-20020ac85b4e000000b003b9fb0eadbamr13783025qtw.25.1677706217862; Wed, 01 Mar 2023 13:30:17 -0800 (PST) Received: from [192.168.1.108] (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 a2-20020ac80002000000b003b9bca1e093sm8958077qtg.27.2023.03.01.13.30.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 01 Mar 2023 13:30:17 -0800 (PST) Message-ID: <41623b13-6b28-45ba-5839-e46207090f5c@redhat.com> Date: Wed, 1 Mar 2023 16:30:16 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.8.0 Subject: Re: [PATCH] c++: ICE with -Wmismatched-tags and member template [PR106259] To: Marek Polacek , GCC Patches References: <20230301203308.405645-1-polacek@redhat.com> From: Jason Merrill In-Reply-To: <20230301203308.405645-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.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 3/1/23 15:33, Marek Polacek wrote: > -Wmismatched-tags warns about the (harmless) struct/class mismatch. > For, e.g., > > template struct A { }; > class A a; > > it works by adding A to the class2loc hash table while parsing the > class-head and then, while parsing the elaborate type-specifier, we > add A. At the end of c_parse_file we go through the table and > warn about the class-key mismatches. In this PR we crash though; we > have > > template struct A { > template struct W { }; > }; > struct A::W w; // #1 > > where while parsing A and #1 we've stashed > A > A::W > A::W > into class2loc. Then in class_decl_loc_t::diag_mismatched_tags TYPE > is A::W, and specialization_of gets us A::W, which > is not in class2loc, so we crash on gcc_assert (cdlguide). But it's > OK not to have found A::W, we should just look one "level" up, > that is, A::W. > > It's important to handle class specializations, so e.g. > > template<> > struct A { > template > class W { }; > }; > > where W's class-key is different than in the primary template above, > so we should warn depending on whether we're looking into A > or into a different instantiation. > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > PR c++/106259 > > gcc/cp/ChangeLog: > > * parser.cc (class_decl_loc_t::diag_mismatched_tags): If the first > lookup of SPEC didn't find anything, try to look for > most_general_template. > > gcc/testsuite/ChangeLog: > > * g++.dg/warn/Wmismatched-tags-11.C: New test. > --- > gcc/cp/parser.cc | 30 +++++++++++++++---- > .../g++.dg/warn/Wmismatched-tags-11.C | 23 ++++++++++++++ > 2 files changed, 47 insertions(+), 6 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/warn/Wmismatched-tags-11.C > > diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc > index 1a124f5395e..b528ee7b1d9 100644 > --- a/gcc/cp/parser.cc > +++ b/gcc/cp/parser.cc > @@ -34473,14 +34473,32 @@ class_decl_loc_t::diag_mismatched_tags (tree type_decl) > be (and inevitably is) at index zero. */ > tree spec = specialization_of (type); > cdlguide = class2loc.get (spec); > + /* It's possible that we didn't find SPEC. Consider: > + > + template struct A { > + template struct W { }; > + }; > + struct A::W w; // #1 > + > + where while parsing A and #1 we've stashed > + A > + A::W > + A::W > + into CLASS2LOC. If TYPE is A::W, specialization_of > + will yield A::W which may be in CLASS2LOC if we had > + an A class specialization, but otherwise won't be in it. > + So try to look up A::W. */ > + if (!cdlguide) > + { > + spec = DECL_TEMPLATE_RESULT (most_general_template (spec)); Would it make sense to only look at most_general_template, not A::W at all? > + cdlguide = class2loc.get (spec); > + } > + /* Now we really should have found something. */ > gcc_assert (cdlguide != NULL); > } > - else > - { > - /* Skip declarations that consistently use the same class-key. */ > - if (def_class_key != none_type) > - return; > - } > + /* Skip declarations that consistently use the same class-key. */ > + else if (def_class_key != none_type) > + return; > > /* Set if a definition for the class has been seen. */ > const bool def_p = cdlguide->def_p (); > diff --git a/gcc/testsuite/g++.dg/warn/Wmismatched-tags-11.C b/gcc/testsuite/g++.dg/warn/Wmismatched-tags-11.C > new file mode 100644 > index 00000000000..6c4e571726a > --- /dev/null > +++ b/gcc/testsuite/g++.dg/warn/Wmismatched-tags-11.C > @@ -0,0 +1,23 @@ > +// PR c++/106259 > +// { dg-do compile } > +// { dg-options "-Wmismatched-tags" } > + > +template struct A { > + template > + struct W { }; > +}; > + > +template<> > +struct A { > + template > + class W { }; > +}; > + > +void > +g () > +{ > + struct A::W w1; // { dg-warning "mismatched" } > + struct A::W w2; > + class A::W w3; > + class A::W w4; // { dg-warning "mismatched" } > +} > > base-commit: 096f034a8f5df41f610e62c1592fb90a3f551cd5