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 992BD3858D33 for ; Wed, 1 Mar 2023 21:44:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 992BD3858D33 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=1677707057; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DN3OfO+EUiOS7ORfvSAsOIr6YO05A58HE9/tJpfo67I=; b=S7kH52IYPc76PbO/EcMgXzaSYC3jeVaIW0uYrOvqFjrumV1pHehOSpctG4OO1N68oOvP75 o6FMlAE1C5wWhfgZTUK3YQ3xqoqI3ITRP5spHv8BOuO3J0mrfGbOu7d9Wt2X9EwsoAzStc K2YO8ixFQtqwiR/gz6d0qoIVmCd0XSM= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-317-gPELiS7gOiW55i1kCvPR7g-1; Wed, 01 Mar 2023 16:44:16 -0500 X-MC-Unique: gPELiS7gOiW55i1kCvPR7g-1 Received: by mail-qv1-f69.google.com with SMTP id pz4-20020ad45504000000b0056f060452adso7678652qvb.6 for ; Wed, 01 Mar 2023 13:44:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677707055; h=content-transfer-encoding:in-reply-to:from:references:cc: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=DN3OfO+EUiOS7ORfvSAsOIr6YO05A58HE9/tJpfo67I=; b=Z/f9CFmNERFn+3fuJRCazPUpjmuN36btw/dCG7CRkjroPJA2OAkX7kMYNYk3RK/v6I Eyjr5J0NoHzam4/zC1wFvFjWDW+XdHiOckBCdM/4iy1qsdCemG/nDrmJnUFMHhtq3eSc aTkmRhwHFFzvVt/Q9xn5d9vOHsPbprRjpqBvdI9zKJ7UWIpR2vmLAMs70cIyJ+FM4bSl kw/hJY+vWF3MWHeSTrzd+HTrKS3XlVjW0oMbkN9L2EqGNQLHdNDMFYz52s3cPKYm4pNS nltp5ngdF+AfMXpMjMSGNBhOL6yvD56tbGG6f2EvqS+UQkdP2VY315XW7UWqwcw3vhRH 435A== X-Gm-Message-State: AO0yUKWiFTGOgQZlSBx3oHx1zz4Oqml3YUWyX97x74seQjAm0xUAs5dM xRpKAWKMQiabH06xzovd3K45NQRdvA6Qpfi1+9lC2UjSfGM8/6iv2ucvWBOK22J6AhHuPx1gFpB mj81Yv+piMhY/FgPMfw7GNUw= X-Received: by 2002:ac8:7f46:0:b0:3bf:dfe2:6312 with SMTP id g6-20020ac87f46000000b003bfdfe26312mr12459379qtk.5.1677707055353; Wed, 01 Mar 2023 13:44:15 -0800 (PST) X-Google-Smtp-Source: AK7set/ywYaa4QbA8/l8wAJFd/4xoA3c115dwClK6hFwx1nKD14mVLBTRb9YLyf/HYy7hgNaiDLe3w== X-Received: by 2002:ac8:7f46:0:b0:3bf:dfe2:6312 with SMTP id g6-20020ac87f46000000b003bfdfe26312mr12459348qtk.5.1677707054943; Wed, 01 Mar 2023 13:44:14 -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 g3-20020ac84803000000b003be56bdd3b1sm8919116qtq.92.2023.03.01.13.44.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 01 Mar 2023 13:44:14 -0800 (PST) Message-ID: Date: Wed, 1 Mar 2023 16:44:12 -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 Cc: GCC Patches References: <20230301203308.405645-1-polacek@redhat.com> <41623b13-6b28-45ba-5839-e46207090f5c@redhat.com> From: Jason Merrill In-Reply-To: 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 16:40, Marek Polacek wrote: > On Wed, Mar 01, 2023 at 04:30:16PM -0500, Jason Merrill wrote: >> 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? > > I think that would break with class specialization, as in... > >>> +template struct A { >>> + template >>> + struct W { }; >>> +}; >>> + >>> +template<> >>> +struct A { >>> + template >>> + class W { }; >>> +}; >>> + >>> +void >>> +g () >>> +{ >>> + struct A::W w1; // { dg-warning "mismatched" } > > ...this, where we should first look into A, and only if not > found, go to A. I'd expect the > /* Stop if we run into an explicitly specialized class template. */ code in most_general_template to avoid that problem. Jason