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 D53C43858D33 for ; Wed, 1 Mar 2023 20:33:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D53C43858D33 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=1677702795; 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; bh=HVlvayVoAuEtbqczfCNYkOHXY4EwpBmvdYmqCmaNfqU=; b=Zw54RkW8CM/epSdtQVzrAbRSTFFPUMTJfjuB4cSLmFtTBxJ7IUoyDyM17+gvrtCEy00eH8 Mnz38S+mL2L0b283tMGWeL44VAdyR9bySDY4BxbhUcgAXSgIhcICXfkGFRsHMKyYq0rKT6 OyqBKf5xsbIym6dsiL4YNkLNTBQbEzU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-284-p2wDYc0IPdGuMF7JgcWV8A-1; Wed, 01 Mar 2023 15:33:13 -0500 X-MC-Unique: p2wDYc0IPdGuMF7JgcWV8A-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 98A7685CCE3 for ; Wed, 1 Mar 2023 20:33:12 +0000 (UTC) Received: from pdp-11.lan (unknown [10.22.17.246]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6BAF3140EBF4; Wed, 1 Mar 2023 20:33:12 +0000 (UTC) From: Marek Polacek To: Jason Merrill , GCC Patches Subject: [PATCH] c++: ICE with -Wmismatched-tags and member template [PR106259] Date: Wed, 1 Mar 2023 15:33:08 -0500 Message-Id: <20230301203308.405645-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,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: -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)); + 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 -- 2.39.2