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 AA0C43858C83 for ; Thu, 2 Mar 2023 15:43:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AA0C43858C83 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=1677771836; 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=p32YddN58/LpgnGcnVIPhSrtPXymMc81RODourIj9rI=; b=fVMPBZwj60NE2Rb1t0ym5KdHEje9jqId3sgJ/WC7pr3wLzYur6wL8owDLmVQRQzUo3fqro nETf/1kwrIszlmUb5uwEjNSobSk/5WMfozDfKNX9H4yhq7xzci+8gl/5z1YCCdWDfXRFHI qukGTPW3Eh/kvW/2rMg6NQ2YY6o7NYk= 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_256_GCM_SHA384) id us-mta-164-vib2niVqObeaPMKm1YaqDg-1; Thu, 02 Mar 2023 10:43:54 -0500 X-MC-Unique: vib2niVqObeaPMKm1YaqDg-1 Received: by mail-qv1-f69.google.com with SMTP id pz4-20020ad45504000000b0056f060452adso8905568qvb.6 for ; Thu, 02 Mar 2023 07:43:54 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677771834; 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=p32YddN58/LpgnGcnVIPhSrtPXymMc81RODourIj9rI=; b=nqtWPdaO41Em/YKZkZJB/+SJ4Rxj4hC2WGetbdCfOt5+HcsU85ie8N9szx/djjjm0Q 5Ws7EriBnqH2oxP6MT7Gnsbn7RcV08G00RluaUdP3qtWKcpL6lkyHyZb4JJvsZsM0hEm WXjhcbLiUBEKsZ61mh/QUfgu+MmZ3f76nqnS1hujPDwSYcEkdg+V+rsTAul6r+Q0YhPo Bgk1BKxXiVZno9Gq/3v7ZU7yWRvkjzhHO3lHkBQ37YLWQ5z4J3B22syBZ9lE7ZFYD4+Y x3+vcXS5NEWkrnsO03sjET4aTXSwDTsTqh1rkl5UqFVUmWQjfueqxbLco9IDa5rGXApa Ae7w== X-Gm-Message-State: AO0yUKW8aSvNoPqvfgYki10YnI0qO7qNKqnnaJiI7+zFt1/Lr8OLMmVA ie76kJfUmqHTPO9G8MxrHGN7xB27Fgkov+9sZ8mXP+Rsss8g7D1eIo0d0u8LhyKWCgNaGF2cs64 S3Zw6k27BLV9P18C4yQ== X-Received: by 2002:ac8:4e86:0:b0:3bf:c423:c384 with SMTP id 6-20020ac84e86000000b003bfc423c384mr4772454qtp.15.1677771834319; Thu, 02 Mar 2023 07:43:54 -0800 (PST) X-Google-Smtp-Source: AK7set/v1MiU+iXgPMg/677XmhuNgP9JIgy9gsE0Snl6DN0IYBJADg5C97Gi5d/9OR6MZ8/xTbBK6A== X-Received: by 2002:ac8:4e86:0:b0:3bf:c423:c384 with SMTP id 6-20020ac84e86000000b003bfc423c384mr4772414qtp.15.1677771833920; Thu, 02 Mar 2023 07:43:53 -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 c12-20020ac85a8c000000b003b7e8c04d2esm10537684qtc.64.2023.03.02.07.43.52 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 02 Mar 2023 07:43:53 -0800 (PST) Message-ID: <249c79f4-23e2-aaad-e29e-09955e316ad5@redhat.com> Date: Thu, 2 Mar 2023 10:43:52 -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 17:33, Marek Polacek wrote: > On Wed, Mar 01, 2023 at 04:44:12PM -0500, Jason Merrill wrote: >> 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. > > Ah, I had no idea it does that. The unconditional most_general_template > works fine for the new test, but some of the existing tests then fail. > Reduced: > > template struct S2; // #1 > template class S2; // #2 > > extern class S2 s2ci; // #3 > extern struct S2 s2ci; // { dg-warning "\\\[-Wmismatched-tags" } > > where the unconditional most_general_template changes spec from > "class S2" to "struct S2" (both of which are in class2loc). > So it regresses the diagnostic, complaining that #3 should have "struct" > since #1 has "struct". I think we want to keep the current diagnostic, > saying that the last line should have "class" since the specialization > in line #2 has "class". Makes sense, the patch is OK. Jason