From patchwork Wed Sep 4 17:18:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 97094 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8C5CD385DDCF for ; Wed, 4 Sep 2024 17:19:00 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 5E5153858406 for ; Wed, 4 Sep 2024 17:18:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5E5153858406 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5E5153858406 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725470309; cv=none; b=euQVRBrCjc4jpJfhbHEBWok48nGiqHYJVYOETmsW/Bn8fCuy9nVAefnYfmLA+foVpJNtYtVxXu5Ykg2eIT5NK4C1PLwUxe+DC76DkwHZbiao1nWKwCvWJhbDYyjRniTALFqp71zTzFfcVGGUYFo5PB/0Ye18Khn5ePOQi3KoAAQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725470309; c=relaxed/simple; bh=IzO70BufrVq4FBk9D1I4ylzXPOOG/A53E2+AHIprkWc=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=sQW5+zm4i71ulnh9uCHZZQLRQJ7OrRvXsqqYk97oV88kXW2gjsXuUYeSTvAJYZd3lMkjn8iWeHGoQzMebPc4llvMSyNUbuWleGkE+KwmpcR27BXDLq6IHN2t7KUcIx0dPQYZNP9actn4O0wmWRUsS0XsiEt51Rgu+/a+N/XNMRg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725470307; 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=GD66BycHsV9ijLUvvV7mvVQQzCznrwncexSmpAg12pA=; b=bU2se58HBZCFQQfYLqUbDKEAFv+7VUsFjYylP1sUDfTgBIICaHgpsytDzyR4YPkqVwQYov k8N4eNeXIQoVyDTPZ6468kDOrKTDLHaIr7CD8j+PpubtFUh5vUU2HHZggaavaCbXk67nja DnC2v9FmVYNBJg8Zcf1uDL7phfVy3Ow= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-333-6A_4fEpROWiwuWXuJwpBtg-1; Wed, 04 Sep 2024 13:18:26 -0400 X-MC-Unique: 6A_4fEpROWiwuWXuJwpBtg-1 Received: by mail-qt1-f200.google.com with SMTP id d75a77b69052e-45684591127so100206671cf.3 for ; Wed, 04 Sep 2024 10:18:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725470305; x=1726075105; 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=GD66BycHsV9ijLUvvV7mvVQQzCznrwncexSmpAg12pA=; b=oxoxB2cAjp/f2/5VhHEapq5DdZLluZw5RZ1KNdTzz0jKftsUbwGvRUaCRj0hysPW+e 9Q54P5cnO2+sYyenS4b91xgSETx9N2g326Sb5VVMG1gNB52DeGkZRRRScYQ4vau9bn30 R5uhz49DuTPbFoTAYVStbfntE7CkLPaJ8zMvDgtiLAK4byFBX6W6N1/F3tDK2e/ogzGr FO7Cvc+nSajGfnyl/oHp8Y1RJ3NO6jq4ZqGIH9x20DFUH3ALbRbs+25lJcXAgFAbjnVl 9mUJ9jENfpi1IONSgzlREUTQzdoWZf7wmUpcVIBKnFaFudY25q3LG/xgXYrWnHZnrSUD ZEmw== X-Gm-Message-State: AOJu0YxvUnkIAQlS04QN8dMvqi89hSvLaNtQpZWC2v9pxoFxfPjFeaVa u60bWXCwwRMbGfEwSCNTtezAIBHH/oImDqVRHDi7EDkI6jkM5yC330WsGUxosFYiM0SkqpLgPxa HIRBeDxTcrRv+vIYCi4Sqi5emloF/d81kAlaqthxQUjPb5bhoS34FZmfbwtn34fc= X-Received: by 2002:a05:6214:4b09:b0:6bf:7fba:c829 with SMTP id 6a1803df08f44-6c355f70872mr165527166d6.42.1725470305157; Wed, 04 Sep 2024 10:18:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGm/QfXfDC3c4CkW1J01Avu3g2M8cAbdkNztKHztQA93+NtKApR4mWEyYF50QMA4eqqMqnmhA== X-Received: by 2002:a05:6214:4b09:b0:6bf:7fba:c829 with SMTP id 6a1803df08f44-6c355f70872mr165526746d6.42.1725470304692; Wed, 04 Sep 2024 10:18:24 -0700 (PDT) Received: from redhat.com ([2603:7000:9500:34a5::1db4]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6c5201e46e9sm74696d6.38.2024.09.04.10.18.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 10:18:24 -0700 (PDT) Date: Wed, 4 Sep 2024 13:18:22 -0400 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: [PATCH v2] c++: fn redecl in fn scope wrongly accepted [PR116239] Message-ID: References: <20240830194018.889736-1-polacek@redhat.com> MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/2.2.12 (2023-09-09) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-12.2 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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org On Wed, Sep 04, 2024 at 12:28:49PM -0400, Jason Merrill wrote: > On 8/30/24 3:40 PM, Marek Polacek wrote: > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > -- >8 -- > > Redeclaration such as > > > > void f(void); > > consteval void f(void); > > > > is invalid. In a namespace scope, we detect the collision in > > validate_constexpr_redeclaration, but not when one declaration is > > at block scope. > > > > When we have > > > > void f(void); > > void g() { consteval void f(void); } > > > > we call pushdecl on the second f and call push_local_extern_decl_alias. > > It finds the namespace-scope f: > > > > for (ovl_iterator iter (binding); iter; ++iter) > > if (decls_match (decl, *iter, /*record_versions*/false)) > > { > > alias = *iter; > > break; > > } > > > > but decls_match says they match so we just set DECL_LOCAL_DECL_ALIAS > > (and do not call another pushdecl leading to duplicate_decls which > > would detect mismatching return types, for example). I don't think > > we want to change decls_match, so a simple fix is to detect the > > problem in push_local_extern_decl_alias. > > > > PR c++/116239 > > > > gcc/cp/ChangeLog: > > > > * cp-tree.h (validate_constexpr_redeclaration): Declare. > > * decl.cc (validate_constexpr_redeclaration): No longer static. > > * name-lookup.cc (push_local_extern_decl_alias): Call > > validate_constexpr_redeclaration. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/diagnostic/redeclaration-6.C: New test. > > --- > > gcc/cp/cp-tree.h | 1 + > > gcc/cp/decl.cc | 2 +- > > gcc/cp/name-lookup.cc | 3 ++ > > .../g++.dg/diagnostic/redeclaration-6.C | 34 +++++++++++++++++++ > > 4 files changed, 39 insertions(+), 1 deletion(-) > > create mode 100644 gcc/testsuite/g++.dg/diagnostic/redeclaration-6.C > > > > diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h > > index 2eeb5e3e8b1..1a763b683de 100644 > > --- a/gcc/cp/cp-tree.h > > +++ b/gcc/cp/cp-tree.h > > @@ -6992,6 +6992,7 @@ extern bool member_like_constrained_friend_p (tree); > > extern bool fns_correspond (tree, tree); > > extern int decls_match (tree, tree, bool = true); > > extern bool maybe_version_functions (tree, tree, bool); > > +extern bool validate_constexpr_redeclaration (tree, tree); > > extern bool merge_default_template_args (tree, tree, bool); > > extern tree duplicate_decls (tree, tree, > > bool hiding = false, > > diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc > > index 6458e96bded..4ad68d609d7 100644 > > --- a/gcc/cp/decl.cc > > +++ b/gcc/cp/decl.cc > > @@ -1412,7 +1412,7 @@ check_redeclaration_exception_specification (tree new_decl, > > /* Return true if OLD_DECL and NEW_DECL agree on constexprness. > > Otherwise issue diagnostics. */ > > -static bool > > +bool > > validate_constexpr_redeclaration (tree old_decl, tree new_decl) > > { > > old_decl = STRIP_TEMPLATE (old_decl); > > diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc > > index 70ad4cbf3b5..6777d97ac2e 100644 > > --- a/gcc/cp/name-lookup.cc > > +++ b/gcc/cp/name-lookup.cc > > @@ -3708,6 +3708,9 @@ push_local_extern_decl_alias (tree decl) > > } > > } > > + if (!validate_constexpr_redeclaration (alias, decl)) > > + return; > > + > > retrofit_lang_decl (decl); > > DECL_LOCAL_DECL_ALIAS (decl) = alias; > > } > > I don't think we need this in the case that we built a new alias and pushed > it; in that case alias is built from decl, and should certainly match > already. So let's put this call before we decide to build a new alias, > either in the loop or just after it. That's right. How about this? dg.exp passed. -- >8 -- Redeclaration such as void f(void); consteval void f(void); is invalid. In a namespace scope, we detect the collision in validate_constexpr_redeclaration, but not when one declaration is at block scope. When we have void f(void); void g() { consteval void f(void); } we call pushdecl on the second f and call push_local_extern_decl_alias. It finds the namespace-scope f: for (ovl_iterator iter (binding); iter; ++iter) if (decls_match (decl, *iter, /*record_versions*/false)) { alias = *iter; break; } but decls_match says they match so we just set DECL_LOCAL_DECL_ALIAS (and do not call another pushdecl leading to duplicate_decls which would detect mismatching return types, for example). I don't think we want to change decls_match, so a simple fix is to detect the problem in push_local_extern_decl_alias. PR c++/116239 gcc/cp/ChangeLog: * cp-tree.h (validate_constexpr_redeclaration): Declare. * decl.cc (validate_constexpr_redeclaration): No longer static. * name-lookup.cc (push_local_extern_decl_alias): Call validate_constexpr_redeclaration. gcc/testsuite/ChangeLog: * g++.dg/diagnostic/redeclaration-6.C: New test. --- gcc/cp/cp-tree.h | 1 + gcc/cp/decl.cc | 2 +- gcc/cp/name-lookup.cc | 2 ++ .../g++.dg/diagnostic/redeclaration-6.C | 34 +++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/diagnostic/redeclaration-6.C base-commit: c755c7a32590e2eef5a8b062b9756c1513910246 diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 2eeb5e3e8b1..1a763b683de 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -6992,6 +6992,7 @@ extern bool member_like_constrained_friend_p (tree); extern bool fns_correspond (tree, tree); extern int decls_match (tree, tree, bool = true); extern bool maybe_version_functions (tree, tree, bool); +extern bool validate_constexpr_redeclaration (tree, tree); extern bool merge_default_template_args (tree, tree, bool); extern tree duplicate_decls (tree, tree, bool hiding = false, diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 6458e96bded..4ad68d609d7 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -1412,7 +1412,7 @@ check_redeclaration_exception_specification (tree new_decl, /* Return true if OLD_DECL and NEW_DECL agree on constexprness. Otherwise issue diagnostics. */ -static bool +bool validate_constexpr_redeclaration (tree old_decl, tree new_decl) { old_decl = STRIP_TEMPLATE (old_decl); diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc index 70ad4cbf3b5..98e15878657 100644 --- a/gcc/cp/name-lookup.cc +++ b/gcc/cp/name-lookup.cc @@ -3706,6 +3706,8 @@ push_local_extern_decl_alias (tree decl) /* Adjust visibility. */ determine_visibility (alias); } + else if (!validate_constexpr_redeclaration (alias, decl)) + return; } retrofit_lang_decl (decl); diff --git a/gcc/testsuite/g++.dg/diagnostic/redeclaration-6.C b/gcc/testsuite/g++.dg/diagnostic/redeclaration-6.C new file mode 100644 index 00000000000..ed8d4af7792 --- /dev/null +++ b/gcc/testsuite/g++.dg/diagnostic/redeclaration-6.C @@ -0,0 +1,34 @@ +// PR c++/116239 +// { dg-do compile { target c++20 } } + +consteval void f1(); +void f2(); +constexpr void f3(); +void f4(); +consteval void f5(); +constexpr void f6(); + +void +g () +{ + void f1(); // { dg-error "differs in .consteval." } + consteval void f2(); // { dg-error "differs in .consteval." } + + void f3(); // { dg-error "differs in .constexpr." } + constexpr void f4(); // { dg-error "differs in .constexpr." } + + consteval void f5(); + constexpr void f6(); + + void f7(); + consteval void f7(); // { dg-error "differs in .consteval." } + + consteval void f8(); + void f8(); // { dg-error "differs in .consteval." } + + void f9(); + constexpr void f9(); // { dg-error "differs in .constexpr." } + + constexpr void f10(); + void f10(); // { dg-error "differs in .constexpr." } +}