From patchwork Fri Oct 4 02:41:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 1992578 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=RVUJA7PA; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4XKXrX5QvMz1xt7 for ; Fri, 4 Oct 2024 12:42:16 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D3971385DDF9 for ; Fri, 4 Oct 2024 02:42:14 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by sourceware.org (Postfix) with ESMTPS id 7515A385DDDA for ; Fri, 4 Oct 2024 02:41:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7515A385DDDA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7515A385DDDA Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::102d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728009717; cv=none; b=S2tSzbPFr8mmiWyc2Q+irqBnGcq9LBuzar1jPKorAsddauAkUHyendImWO8Rs8Ivs6rm+4Zt4bblE+4LaA2kJkVFvdqgyn/Fgit1q6qtL3n4mevtS61LlsxL3jPQZqaCQC3ma12KZjC3AasvQ5Waj7BN/YT28Us7t6jyGH9ec7Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728009717; c=relaxed/simple; bh=qUDhABCipl6yoc1S4SX8cpEZTJomUUq8Hz2hNlg0ZSk=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=e1g4NvA5aXGyJwvFMDHLdntOn12dqq2vJ42Lfyzqa7QLAilP5rP74PVvRTm+Ajrjxu3eckEYtpox9zD6+bVWwRejTRQfgYeJtF1L1iDdNVeql+8Qv8/mjAtHOAcUEtGzff2LR/OeX9OIfCbL84uUUf0FCw0OZpXmo1dQMu3noqw= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pj1-x102d.google.com with SMTP id 98e67ed59e1d1-2e0f7027781so119446a91.2 for ; Thu, 03 Oct 2024 19:41:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728009714; x=1728614514; darn=gcc.gnu.org; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=9h70TfAhVQDcB99tlBR5MlsT6j+CF3yssTqQxKmEBfw=; b=RVUJA7PAlYhSn8XtCzYl5xS9yIrpktcU551ikhwaIzKTgJt7h2VSvEme4a5XzLQXw2 zvXyq+AUVrdy8RVfpK/uJONnxmvLYORuVi0zS2Kt2QKg3gjRJ7+QD596zSmDMW3dALdU sda63kniCzzD8yq76xMpbr2uhXn395s05AyRkYJCqRQHHK2lRCTSz00F9rDFGPevC9wR dQf2ZoQRKfVfh1sxQAacIYFaTUr80TeGp6rwHN8DK5AmqtZZfVE0MZPFVE3T/lL1AOP8 DHPO2wRfx/WaZ94A1yUVY+LG8TuxpJ4eCIbQLoztCnCsS55ST1XSLCQYHPC69z9BNhPL 78wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728009714; x=1728614514; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=9h70TfAhVQDcB99tlBR5MlsT6j+CF3yssTqQxKmEBfw=; b=klR3wfu63sqpREzHAr0IX4b1jR2Dizw5FjT4kKv0icUjL/uJtpXQFRtgnbd3v+ikfB lUJK37kE1ZtJw0K70yjPS083dLGTQw9ZJ+ZxXla51ododVq8OENddDM+VPKw7zsdobr7 acI54vQzVVD+Cdk1f39VRCquNX3S8ao2LzwpvbyHOQ4pQZMQdQF9fpl2dvUgukagL2fc 0p2TphifkuCjeWNhPyAguy2H0tH10ibYyjGg0cbQnysBb0P+BGU/TidjWM996MFwWmQM gylj1FxXQj2eano37xkRGRcVwpOb83Qj47PY+odk4AARwEb/nVREvmudDrrRmFjUu7Ei 1OVg== X-Gm-Message-State: AOJu0YzjuN7sWZjiu6YxtQhP0ZXx0Dtp2/xH+Pc4RIqa5Es6fhfI+oni AV5UQ5iac2qZT95RCHDhqYf28UH3Fsp0Wp+orvhgEhbhDKhxc8jwc1HTtw== X-Google-Smtp-Source: AGHT+IEiUxzBzV8U0lMYHmi43DJXfO7qTM/ZL6uG8Ft7qYbZfertAaHW75KS9wtE6F9sjAebWyslxA== X-Received: by 2002:a17:90a:6286:b0:2e0:7e80:2018 with SMTP id 98e67ed59e1d1-2e1e5dcd01amr553369a91.0.1728009714180; Thu, 03 Oct 2024 19:41:54 -0700 (PDT) Received: from Thaum. ([163.47.68.2]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e1e86221afsm297630a91.39.2024.10.03.19.41.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Oct 2024 19:41:53 -0700 (PDT) Message-ID: <66ff55f1.170a0220.dff27.11d6@mx.google.com> X-Google-Original-Message-ID: Date: Fri, 4 Oct 2024 12:41:49 +1000 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill Subject: [PATCH] c++: Allow references to internal-linkage vars in C++11 [PR113266] MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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 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 Tested on x86_64-pc-linux-gnu, so far just dg.exp with GXX_TESTSUITE_STDS=98,11,14,17,20,23,26,impcx. OK for trunk if full bootstrap + regtest passes? -- >8 -- [temp.arg.nontype] changed in C++11 to allow naming internal-linkage variables and functions. We currently already handle internal-linkage functions, but variables were missed; this patch updates this. PR c++/113266 PR c++/116911 gcc/cp/ChangeLog: * parser.cc (cp_parser_template_argument): Allow internal-linkage variables since C++11. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/nontype6.C: New test. Signed-off-by: Nathaniel Shead --- gcc/cp/parser.cc | 17 ++++++++++++----- gcc/testsuite/g++.dg/cpp0x/nontype6.C | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/nontype6.C diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 08f9c89f1f0..e758ddeb1d5 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -19864,9 +19864,11 @@ cp_parser_template_argument (cp_parser* parser) -- the name of a non-type template-parameter; or - -- the name of an object or function with external linkage... + -- the name of an object or function with external (or internal, + since C++11) linkage... - -- the address of an object or function with external linkage... + -- the address of an object or function with external (or internal, + since C++11) linkage... -- a pointer to member... */ /* Look for a non-type template parameter. */ @@ -19929,11 +19931,16 @@ cp_parser_template_argument (cp_parser* parser) probe = TREE_OPERAND (probe, 1); if (VAR_P (probe)) { - /* A variable without external linkage might still be a + /* A variable without valid linkage might still be a valid constant-expression, so no error is issued here if the external-linkage check fails. */ - if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe)) - cp_parser_simulate_error (parser); + if (!address_p && cxx_dialect < cxx17) + { + linkage_kind linkage = decl_linkage (probe); + if (linkage != lk_external + && (cxx_dialect < cxx11 || linkage != lk_internal)) + cp_parser_simulate_error (parser); + } } else if (is_overloaded_fn (argument)) /* All overloaded functions are allowed; if the external diff --git a/gcc/testsuite/g++.dg/cpp0x/nontype6.C b/gcc/testsuite/g++.dg/cpp0x/nontype6.C new file mode 100644 index 00000000000..5543d1e8b6d --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/nontype6.C @@ -0,0 +1,19 @@ +// PR c++/113266, PR c++/116911 +// { dg-do compile } + +template struct a {}; +static int guard1; +a b; // { dg-error "constant-expression|invalid" "" { target c++98_only } } + +namespace { + int guard2; +} +a c; // OK in C++98 because guard2 has external linkage + // OK since C++11 because we can refer to an internal linkage decl + +void nolinkage() { + static int guard3; + a d; // { dg-error "constant-expression|invalid" "" { target c++98_only } } + // { dg-error "constant expression|no linkage" "" { target { c++11 && c++14_down } } .-1 } + // OK since C++17 since we can now refer to no-linkage decls +}