From patchwork Thu Sep 5 15:10:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1981306 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=NjX9Id4X; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4X02r551zCz1yfv for ; Fri, 6 Sep 2024 01:11:17 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3491E384B063 for ; Thu, 5 Sep 2024 15:11:15 +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 B6EC33858294 for ; Thu, 5 Sep 2024 15:10:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B6EC33858294 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 B6EC33858294 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=1725549056; cv=none; b=P1/nrrEnMxIwFJ184LDxA8qAVvkcxDpXaMZYB1P7ouJhiXh1wltAagI1sVXDGjrNl+bBrN6LXAdbaCIvaPs9OJ1HmXatLqbXh7y+UJODCWJAjQjXFv2Po4sdR3sOtWN8jOrA82iOWcheCx42L++PvG9kkiArLrfF4otC3YQYEMU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725549056; c=relaxed/simple; bh=ccXfBlN4E/VNWQMkOMsbwz7+SHwLRq6C3X4QPhzFbb8=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=Hef2j5KMuyUmm6mJiDus0AdVbBRyfCp1dJYDJgm+paMh98rgCIlWUW85e/m3oNI096Ywy297eYa4UajHd1xil5WOve4F/aWuLiFH6aBgso1+Zafyf8/1ImpdjnGw2S7iGXuQDE/VvD1XG4+3TLn9yhW98J39X2cctoXzPCCt7EM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725549054; 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=pmyT7Oay1bddF4CEgNBPCeKGS+uPhiR16R9KAAtsE8s=; b=NjX9Id4XrXRaFepx0aF+TilL3AUsGq3GIH4wpThp7PqdsnSjVXXlwhyuPg1xzbhWIvNFng YhguCcez2dH3LZyzsR2frRmaprdkhnpHuZyrlC1B5dlYYN/NDqTyWZ3z1Uk+EH/IfhXm2e 3yiwYL43EDURPYGZIs6NZfVrehGdCf0= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-681-L6N9UYB7Nm6EYFtlwfADsQ-1; Thu, 05 Sep 2024 11:10:52 -0400 X-MC-Unique: L6N9UYB7Nm6EYFtlwfADsQ-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-7a7ff78ce40so257032485a.1 for ; Thu, 05 Sep 2024 08:10:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725549052; x=1726153852; 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=pmyT7Oay1bddF4CEgNBPCeKGS+uPhiR16R9KAAtsE8s=; b=eusOZlHQrt2bMTUxGBuufRWx28tKa8dDxY6N2pT/yW13BXxUb+o2us4wwTQfQelbxM BrF5HZFA+RQzX8+Vz4FTLi0WEEzRHXaExXLt+ovUq/tmI4QzZy6K2DYw6EUXx6H3F084 30kCOL2SPo4CunBWsSkUODkRrAqDiE832R7TqH+QCxTCgkCzauvCHzpdkvfh43fLWuTt /oT6UAtOrwutnMKvhMkkKu9QybjJUxJe1JrqiLwCHYD0ZF83baOS3tfKWIc/YAQpSdnj j0/BkvXA/xbAStY8HQ5qmQ3ZE7qPgBimq6ZpoZKK9MA5gFuR1Z8J4jkjMTTtXgHIBcJk Nd9g== X-Gm-Message-State: AOJu0YyKhwWHevQ0FKGOf4nVZzV1EvS77gOyCBIaqyVJL2syvWgvOjAp +ZsmR5WExQANOwve7d+27JzaROmt4E0UuC4cFLMw9YKqHtPWJEtVh1r/1DVAeonEB4xS6riq6H5 51EWU8v3xY2pdV/ah+1MvVQ0NTkpWJkUFGY6nqeiesGGpeOuXi32qwQXQSPvOANA= X-Received: by 2002:a05:620a:44c9:b0:7a2:16c:b002 with SMTP id af79cd13be357-7a81d72128amr2402760085a.48.1725549051677; Thu, 05 Sep 2024 08:10:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEqi7eVq4pCo81TBTcfwVQbo7VafZxDyaHs/ZKa8gDCKeZwPIeDNKS4PNE02n5wQsK9cXYzCw== X-Received: by 2002:a05:620a:44c9:b0:7a2:16c:b002 with SMTP id af79cd13be357-7a81d72128amr2402756585a.48.1725549051104; Thu, 05 Sep 2024 08:10:51 -0700 (PDT) Received: from redhat.com ([2603:7000:9500:34a5::1db4]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a98ef49121sm81430385a.57.2024.09.05.08.10.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Sep 2024 08:10:50 -0700 (PDT) Date: Thu, 5 Sep 2024 11:10:49 -0400 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: [PATCH v3] c++: fn redecl in fn scope wrongly accepted [PR116239] Message-ID: References: <20240830194018.889736-1-polacek@redhat.com> <73d91c14-97bd-47d4-8f30-a6c568187fef@redhat.com> MIME-Version: 1.0 In-Reply-To: <73d91c14-97bd-47d4-8f30-a6c568187fef@redhat.com> 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~incoming=patchwork.ozlabs.org@gcc.gnu.org On Thu, Sep 05, 2024 at 10:42:22AM -0400, Jason Merrill wrote: > On 9/4/24 1:18 PM, Marek Polacek wrote: > > On Wed, Sep 04, 2024 at 12:28:49PM -0400, Jason Merrill wrote: > > > > > + 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? > > Hmm, I'd still prefer to have it *before* the !alias case for locality; > putting it as an else to a 66-line if seems obscure. > > But OK however. Ah, let me change it. Still OK? 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: ae88e91938af364ef5613e5461b12b484b578bc5 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 7bad3047ad9..f4128dbccdf 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 7a6cc244c15..cd3947cbe4f 100644 --- a/gcc/cp/name-lookup.cc +++ b/gcc/cp/name-lookup.cc @@ -3637,6 +3637,8 @@ push_local_extern_decl_alias (tree decl) if (decls_match (decl, *iter, /*record_versions*/false)) { alias = *iter; + if (!validate_constexpr_redeclaration (alias, decl)) + return; break; } 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." } +}