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 63F833858D33 for ; Wed, 1 Mar 2023 21:40:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 63F833858D33 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=1677706834; 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: in-reply-to:in-reply-to:references:references; bh=rAFOINWU5RaWVo4gZaxqvq97Pzwr/PjGaFyRwQYtlKE=; b=F3eBzTCx2O54a/fjkbPKmc85KcUTyfz9hB3bCun7EwKJc54t+g6o6POps578hvmpKuYbV2 i4qUGjnN1dc5YDdC7ixx5K1Rnc+RBmbdDHAkM3bM/9A1Tt/pE46rMU5/Vr1/2HWiu0XiOo 5SIZIMk50mxxT1DVpYA/70QkyXin+Ss= 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-298-DtqNpy9yMReUdIa0AZvmOA-1; Wed, 01 Mar 2023 16:40:33 -0500 X-MC-Unique: DtqNpy9yMReUdIa0AZvmOA-1 Received: by mail-qk1-f199.google.com with SMTP id u28-20020a05620a085c00b0073b88cae2f5so8855339qku.8 for ; Wed, 01 Mar 2023 13:40:33 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rAFOINWU5RaWVo4gZaxqvq97Pzwr/PjGaFyRwQYtlKE=; b=MxqVcsQ2jR4I5XhkbmsNbd8pbGaZogP7D2PMj+BJnbPLIwoe6GCWm6sWNg869QchzD SvjCCoZ0vgBKJ6KBZAjOeuq2RXmRAB8Hyzpx2W9BkbLssiOo/UkqCQ6YwjCZLDYXgadr n1LwBp8fmPUqnQOkwZ+8iRxal0LPWry66iL3BWefUc2WdI9AajU5oPAR+Xl7Tlla+X5M BaGYYnwg/f7BjTtkTbdVY+4GrpirDKBg2SvJUiv+LU3WSiA1gFlTOVxBZqmtls5XQ30w a89Wvagi9TFURkzdfOcG/U4R/OQ4vioWRryJo6/4Hn5iFb3kCI1f1QvYLJQXdqV6kCtR vTwA== X-Gm-Message-State: AO0yUKXDQJcHXcPHHGGC0R61IESHgZL6fH67NNA9xCEfhf+6FzRCepYP fTnZ7occK/Nqnos8PvD4PfwATJu3NWeoqQ+rOqqXWOwcnjznXQnbkmsU/e+FK4/cJcklqO9Oq7b 8vQu6rF2LWgLUOj8RXw== X-Received: by 2002:ac8:5a52:0:b0:3bf:d1b3:2be2 with SMTP id o18-20020ac85a52000000b003bfd1b32be2mr13424805qta.63.1677706832335; Wed, 01 Mar 2023 13:40:32 -0800 (PST) X-Google-Smtp-Source: AK7set/+m/o9QGwxJ0F3OJ4FLg7lmch0r8/K3Z7SriI2KtU/Btys/QbEquinCZjuMAhNw5CSyn0juQ== X-Received: by 2002:ac8:5a52:0:b0:3bf:d1b3:2be2 with SMTP id o18-20020ac85a52000000b003bfd1b32be2mr13424787qta.63.1677706832001; Wed, 01 Mar 2023 13:40:32 -0800 (PST) Received: from redhat.com (2603-7000-9500-34a5-0000-0000-0000-1db4.res6.spectrum.com. [2603:7000:9500:34a5::1db4]) by smtp.gmail.com with ESMTPSA id 15-20020ac856ef000000b003bfeb30c24dsm3249889qtu.39.2023.03.01.13.40.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Mar 2023 13:40:31 -0800 (PST) Date: Wed, 1 Mar 2023 16:40:29 -0500 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: Re: [PATCH] c++: ICE with -Wmismatched-tags and member template [PR106259] Message-ID: References: <20230301203308.405645-1-polacek@redhat.com> <41623b13-6b28-45ba-5839-e46207090f5c@redhat.com> MIME-Version: 1.0 In-Reply-To: <41623b13-6b28-45ba-5839-e46207090f5c@redhat.com> User-Agent: Mutt/2.2.9 (2022-11-12) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-6.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 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. class2loc will be filled with A::W, added while parsing the specialization. > > + struct A::W w2; > > + class A::W w3; > > + class A::W w4; // { dg-warning "mismatched" } > > +} > > > > base-commit: 096f034a8f5df41f610e62c1592fb90a3f551cd5 > Marek