From patchwork Fri Feb 9 23:46:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 199158 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp1199145dyd; Fri, 9 Feb 2024 15:47:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IFNqLuseV3JHXcz7lX6NGYA7a5cK5iYjBst2PSaxjwc/peAJ3zANibE55c8Cy+uxdUyUcc3 X-Received: by 2002:a0c:f5ca:0:b0:68c:a7df:ea88 with SMTP id q10-20020a0cf5ca000000b0068ca7dfea88mr685539qvm.0.1707522429298; Fri, 09 Feb 2024 15:47:09 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707522429; cv=pass; d=google.com; s=arc-20160816; b=cl9142Q23eMdtvJqbqCLtwMppkjGeBg8vcBFSuj4zeF+lX84dZ309DTGMZaW1BhmCo MZXsLDZaQTwkUwuOrkBHC7LX0o+gQbve04YXcbInZ+z5twO72BJfXhOw1xSel3Rnewio UiOsBsZ6tF+iGpU6wcmHv87cK8xupF7VH0IWAkXBhqL4+syQAH6bO6TpWtIt0i2gFk6I TbKNWkSsHxrGzz1Kl9OCvaMY2OECPddol45c6mojgCZn6IsxZreT4sOklRMMc+aQopSW ibreilVGzA/mSG5/9NhEfl0kVHotMpnOep1cp8EMwO7wBtWx3N5nr7HASfp2fceoIsoz d/Gg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition:user-agent :in-reply-to:mime-version:references:message-id:subject:cc:to:from :date:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=m51wPetETlMdTUiJL4CIOQqceIN8T2R5FaGpKODid8A=; fh=68a2vg0ZWa+hqM4ASUIm9gIkvkUc25eGRTH/KyYmlbU=; b=LFBo22AtfVTLzLu/mmpk483OgxEywqj7u+blHfxsDRgXgK8SPQricfb5rwZ1fVNOez Mc042AaJBy1+C9J0TEfdtx2ERPXTJKABv4bf+pT6YzqTEzTAxpQJ6cndxFMM0alU2i2t f4aYVc+/IumSEM44ks21RayGLwfLptEcQZxX4Z7+DokVOfBAtd9AkSbo5z2aD2W9/AlE wrOYVa6SSKe5nXdviw3s/7aiXkqbLkbDO98SPycC2fYw6sjSVutrnPQ1QTmt2C9vz/QH aQWvgzUlBkVbGTGGKlxxcG9fDoX62TVhog24NkscKXPzBm6BsOMqEGGP6uR8fWiH4nwY ajgw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ejWaIiWM; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=2; AJvYcCVwyx5MroZTO0FitduEIKET+3gkrl6HvpUr0nYvAa1AH8aNQBF3XO7rcElX2oZrzWE+aYwTnSzjDZv7sl64xp38pm0zIw== Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id 10-20020a0562140d0a00b0068c711cf6cdsi3119207qvh.475.2024.02.09.15.47.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 15:47:09 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ejWaIiWM; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F0E763858CDA for ; Fri, 9 Feb 2024 23:47:08 +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 ESMTPS id C6E313858D20 for ; Fri, 9 Feb 2024 23:46:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C6E313858D20 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 C6E313858D20 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=1707522379; cv=none; b=EwiqYBJSTnqZflafp/+J3hga+24NMOHftgbhIwNGbUf0QbtgBh+Xz62HRKoXBzt5qwdv+ldS/Jr+byAB0HUjQp6Y4k3r9de638lM+Zry+wLlGVG/GTHUgM5PLXiRQN/uwIfTvI/UWEnZEzc2NWY+LGCDGahVr1/UW/4Oa7/m3Cc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707522379; c=relaxed/simple; bh=19lSXOW5sbNL0dyOBm1TrjKOPBvZ0ots+uz8hH/YUTE=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=aLqVIPzNUugk9NIK6Lf+uzvTBp62/sucGf31XeJONVVlwdbzzRZvmWWdjNKxqu+rCd4GKb5LwfZta/EN7nrmbc1/gEa3LhDGCkNYuq0cKKSpYHnEIWrFLy/F32+OatkTj5vqSrUVWi0biN9qBR14f84xQKZv8WMqYF29Ri8ifgs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707522376; 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=m51wPetETlMdTUiJL4CIOQqceIN8T2R5FaGpKODid8A=; b=ejWaIiWMxkxC4iCxjFi6QikYF4UM+7t8XmQXKsVJHAO0ZuaQQqkNFMsOvkJ0pjWoCLNMrC 21seUyJzTAyIPqyODbq+STqIDBLZuuq/ErULXJN6FTV86LGMaxVW0EsguBDyudISXUSGoC lYDZLBDNbgOVoGz/BD7wOMTnzk+vnH4= Received: from mail-yb1-f198.google.com (mail-yb1-f198.google.com [209.85.219.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-509-IBIjm7TMN5CQTaP6Js3YfQ-1; Fri, 09 Feb 2024 18:46:15 -0500 X-MC-Unique: IBIjm7TMN5CQTaP6Js3YfQ-1 Received: by mail-yb1-f198.google.com with SMTP id 3f1490d57ef6-dc6dbdcfd39so3088012276.2 for ; Fri, 09 Feb 2024 15:46:15 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707522374; x=1708127174; 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=m51wPetETlMdTUiJL4CIOQqceIN8T2R5FaGpKODid8A=; b=BBUnkSI2UiSanGoFkHqU2COTty20Qqb568oDxmdQazlwEasd4DPhuQvC9//exf2ntQ 3xOcyiu/NGzQWZiJGpp6cM9Q/UYNEWN5GoJva/NrvYtO2+G/Zi96P3d0XKZEUa5T8A4I 7r2Fw8SamToESz62QZ1aCVgftNgCjuJx/+hFhw5DwxNpWxahaVkD/6HbL5hf8hYLqZ4/ WS2cT8Vu2v9Vx+6x0LMz7aZhkrfnpOoGgGxDEjdSu00dTTu/ZN246vScgbwd4b+h5TWx eH0nnF7BpFfyy503Wjzzns2MXdpulfiIoRqlA9YTZaEDPuCT9Dwr5UZhCW+5TSiU947z 5MgQ== X-Gm-Message-State: AOJu0YyTpt65ns0/66Jog45mMk9j7MFeQtsB8ZrkEFsQqhsJBRZkTc5m jxkGosHS+5zGDpRVtSEwis4k/zTWY3hkBWf2F0L0mShsnAUxWN6NwCM1QFFtRz5gAw43S5STbSa kKD2J9GeqPVN3NNdPoR5aM4h3Qc/snMJU9eWcY1T7uKfdkuPxM87SdtG+4xTX6bM= X-Received: by 2002:a25:dbc9:0:b0:dc2:65e7:a50f with SMTP id g192-20020a25dbc9000000b00dc265e7a50fmr728068ybf.53.1707522373603; Fri, 09 Feb 2024 15:46:13 -0800 (PST) X-Received: by 2002:a25:dbc9:0:b0:dc2:65e7:a50f with SMTP id g192-20020a25dbc9000000b00dc265e7a50fmr728054ybf.53.1707522373091; Fri, 09 Feb 2024 15:46:13 -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 d9-20020a05620a166900b00783f1e600aasm183265qko.38.2024.02.09.15.46.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 15:46:12 -0800 (PST) Date: Fri, 9 Feb 2024 18:46:10 -0500 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: [PATCH v4] c++: DR2237, cdtor and template-id tweaks [PR107126] Message-ID: References: <20240203152404.1199437-1-polacek@redhat.com> <69077fcc-97fe-43be-9e34-9d91d1a7c7af@redhat.com> <3bd79ed5-57fa-4ea9-a250-07417738579a@redhat.com> <63bbdd3c-f71a-45b1-bcb7-13569fb5f8da@redhat.com> MIME-Version: 1.0 In-Reply-To: <63bbdd3c-f71a-45b1-bcb7-13569fb5f8da@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.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE, URIBL_SBL_A 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789891861675487993 X-GMAIL-MSGID: 1790467038723541884 On Fri, Feb 09, 2024 at 10:20:04AM -0500, Jason Merrill wrote: > On 2/8/24 16:26, Marek Polacek wrote: > > This patch does *not* fix > > > > where the C++20 diagnostic is missing altogether. > > What would it take to fix that as well? It's the "further DR2237 fix" below which is basically a one liner. I'd thought it would be more involved than that. > > * doc/invoke.texi: Document -Wdeprecated-template-id-cdtor. > > Was it ever deprecated? I'm not seeing that in > > https://timsong-cpp.github.io/cppwp/n4659/#depr (C++17) Aha, [diff] != [depr]... > Let's drop the word "deprecated" from the option name and documentation. Done throughout. > > @@ -32331,11 +32338,11 @@ cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags, > > if (next_token->type != CPP_NAME > > && next_token->type != CPP_SCOPE > > && next_token->type != CPP_NESTED_NAME_SPECIFIER > > - /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the > > - declarator-id of a constructor or destructor. */ > > - && (next_token->type != CPP_TEMPLATE_ID || cxx_dialect >= cxx20)) > > + && next_token->type != CPP_TEMPLATE_ID) > > return false; > > + const bool saw_template_id = (next_token->type == CPP_TEMPLATE_ID); > > Please incorporate your "further DR2237 fix" patch into this one. Patches squashed. > > @@ -32552,6 +32559,19 @@ cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags, > > /* We did not really want to consume any tokens. */ > > cp_parser_abort_tentative_parse (parser); > > + /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the > > + declarator-id of a constructor or destructor. */ > > + if (constructor_p && saw_template_id) > > + { > > + gcc_checking_assert > > + (!cp_parser_uncommitted_to_tentative_parse_p (parser)); > > Now I see the abort_ just above, so this seems unnecessary after all. Done, thanks. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- Since my r11-532 changes to implement DR2237, for this test: template struct S { S(); }; in C++20 we emit the ugly: q.C:3:8: error: expected unqualified-id before ')' token 3 | S(); which doesn't explain what the problem is. This patch improves that diagnostic, reduces the error to a pedwarn, and adds a -Wc++20-compat diagnostic. We now say: q.C:3:7: warning: template-id not allowed for constructor in C++20 [-Wtemplate-id-cdtor] 3 | S(); q.C:3:7: note: remove the '< >' This patch also fixes where the C++20 diagnostic was missing altogether: The problem was that I checked for CPP_TEMPLATE_ID too early, at a point at which cp_parser_template_id may not have been called yet. So let's check for it at the end of the function, after the tentative parse and rollback. -Wc++20-compat triggered in libitm/; I sent a patch for that. DR 2237 PR c++/107126 PR c++/97202 gcc/c-family/ChangeLog: * c-opts.cc (c_common_post_options): In C++20 or with -Wc++20-compat, turn on -Wtemplate-id-cdtor. * c.opt (Wtemplate-id-cdtor): New. gcc/cp/ChangeLog: * parser.cc (cp_parser_unqualified_id): Downgrade the DR2237 error to a pedwarn. (cp_parser_constructor_declarator_p): Likewise. gcc/ChangeLog: * doc/invoke.texi: Document -Wtemplate-id-cdtor. gcc/testsuite/ChangeLog: * g++.dg/DRs/dr2237.C: Adjust dg-error. * g++.dg/parse/constructor2.C: Likewise. * g++.dg/template/error34.C: Likewise. * g++.old-deja/g++.pt/ctor2.C: Likewise. * g++.dg/DRs/dr2237-2.C: New test. * g++.dg/DRs/dr2237-3.C: New test. * g++.dg/DRs/dr2237-4.C: New test. * g++.dg/DRs/dr2237-5.C: New test. * g++.dg/warn/Wtemplate-id-cdtor-1.C: New test. * g++.dg/warn/Wtemplate-id-cdtor-2.C: New test. * g++.dg/warn/Wtemplate-id-cdtor-3.C: New test. * g++.dg/warn/Wtemplate-id-cdtor-4.C: New test. --- gcc/c-family/c-opts.cc | 5 +++ gcc/c-family/c.opt | 4 +++ gcc/cp/parser.cc | 33 ++++++++++++++----- gcc/doc/invoke.texi | 19 ++++++++++- gcc/testsuite/g++.dg/DRs/dr2237-2.C | 9 +++++ gcc/testsuite/g++.dg/DRs/dr2237-3.C | 16 +++++++++ gcc/testsuite/g++.dg/DRs/dr2237-4.C | 11 +++++++ gcc/testsuite/g++.dg/DRs/dr2237-5.C | 7 ++++ gcc/testsuite/g++.dg/DRs/dr2237.C | 2 +- gcc/testsuite/g++.dg/parse/constructor2.C | 16 ++++----- gcc/testsuite/g++.dg/template/error34.C | 10 +++--- .../g++.dg/warn/Wtemplate-id-cdtor-1.C | 9 +++++ .../g++.dg/warn/Wtemplate-id-cdtor-2.C | 9 +++++ .../g++.dg/warn/Wtemplate-id-cdtor-3.C | 9 +++++ .../g++.dg/warn/Wtemplate-id-cdtor-4.C | 9 +++++ gcc/testsuite/g++.old-deja/g++.pt/ctor2.C | 2 +- 16 files changed, 146 insertions(+), 24 deletions(-) create mode 100644 gcc/testsuite/g++.dg/DRs/dr2237-2.C create mode 100644 gcc/testsuite/g++.dg/DRs/dr2237-3.C create mode 100644 gcc/testsuite/g++.dg/DRs/dr2237-4.C create mode 100644 gcc/testsuite/g++.dg/DRs/dr2237-5.C create mode 100644 gcc/testsuite/g++.dg/warn/Wtemplate-id-cdtor-1.C create mode 100644 gcc/testsuite/g++.dg/warn/Wtemplate-id-cdtor-2.C create mode 100644 gcc/testsuite/g++.dg/warn/Wtemplate-id-cdtor-3.C create mode 100644 gcc/testsuite/g++.dg/warn/Wtemplate-id-cdtor-4.C base-commit: f29f7f86935e29786bf9f976ec99d7639b381b14 diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc index b845aff2226..be3058dca63 100644 --- a/gcc/c-family/c-opts.cc +++ b/gcc/c-family/c-opts.cc @@ -998,6 +998,11 @@ c_common_post_options (const char **pfilename) warn_deprecated_enum_float_conv, cxx_dialect >= cxx20 && warn_deprecated); + /* -Wtemplate-id-cdtor is enabled by default in C++20. */ + SET_OPTION_IF_UNSET (&global_options, &global_options_set, + warn_template_id_cdtor, + cxx_dialect >= cxx20 || warn_cxx20_compat); + /* Declone C++ 'structors if -Os. */ if (flag_declone_ctor_dtor == -1) flag_declone_ctor_dtor = optimize_size; diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 9c0a28092fc..b7a4a1a68e3 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -1408,6 +1408,10 @@ Wtautological-compare C ObjC C++ ObjC++ Var(warn_tautological_compare) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall) Warn if a comparison always evaluates to true or false. +Wtemplate-id-cdtor +C++ ObjC++ Var(warn_template_id_cdtor) Warning +Warn about simple-template-id in a constructor or destructor. + Wterminate C++ ObjC++ Warning Var(warn_terminate) Init(1) Warn if a throw expression will always result in a call to terminate(). diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 09ecfa23b5d..8bba2fdef96 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -6719,12 +6719,19 @@ cp_parser_unqualified_id (cp_parser* parser, /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the declarator-id of a constructor or destructor. */ - if (token->type == CPP_TEMPLATE_ID && declarator_p - && cxx_dialect >= cxx20) + if (token->type == CPP_TEMPLATE_ID && declarator_p) { - if (!cp_parser_simulate_error (parser)) - error_at (tilde_loc, "template-id not allowed for destructor"); - return error_mark_node; + auto_diagnostic_group d; + bool w = false; + if (cxx_dialect >= cxx20 && !cp_parser_simulate_error (parser)) + w = pedwarn (tilde_loc, OPT_Wtemplate_id_cdtor, + "template-id not allowed for destructor in C++20"); + else if (cxx_dialect < cxx20 + && !cp_parser_uncommitted_to_tentative_parse_p (parser)) + w = warning_at (tilde_loc, OPT_Wtemplate_id_cdtor, + "template-id not allowed for destructor in C++20"); + if (w) + inform (tilde_loc, "remove the %qs", "< >"); } /* If there was an explicit qualification (S::~T), first look @@ -32331,9 +32338,7 @@ cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags, if (next_token->type != CPP_NAME && next_token->type != CPP_SCOPE && next_token->type != CPP_NESTED_NAME_SPECIFIER - /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the - declarator-id of a constructor or destructor. */ - && (next_token->type != CPP_TEMPLATE_ID || cxx_dialect >= cxx20)) + && next_token->type != CPP_TEMPLATE_ID) return false; /* Parse tentatively; we are going to roll back all of the tokens @@ -32552,6 +32557,18 @@ cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags, /* We did not really want to consume any tokens. */ cp_parser_abort_tentative_parse (parser); + /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the + declarator-id of a constructor or destructor. */ + if (constructor_p + && cp_lexer_peek_token (parser->lexer)->type == CPP_TEMPLATE_ID) + { + auto_diagnostic_group d; + if (emit_diagnostic (cxx_dialect >= cxx20 ? DK_PEDWARN : DK_WARNING, + input_location, OPT_Wtemplate_id_cdtor, + "template-id not allowed for constructor in C++20")) + inform (input_location, "remove the %qs", "< >"); + } + return constructor_p; } diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 71339b8b30f..0de184f6241 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -270,7 +270,7 @@ in the following sections. -Wno-non-template-friend -Wold-style-cast -Woverloaded-virtual -Wno-pmf-conversions -Wself-move -Wsign-promo -Wsized-deallocation -Wsuggest-final-methods --Wsuggest-final-types -Wsuggest-override +-Wsuggest-final-types -Wsuggest-override -Wno-template-id-cdtor -Wno-terminate -Wno-vexing-parse -Wvirtual-inheritance -Wno-virtual-move-assign -Wvolatile -Wzero-as-null-pointer-constant} @@ -4604,6 +4604,23 @@ namespaces, and this may be used to enforce that rule. The warning is inactive inside a system header file, such as the STL, so one can still use the STL. One may also use using directives and qualified names. +@opindex Wtemplate-id-cdtor +@opindex Wno-template-id-cdtor +@item -Wno-template-id-cdtor @r{(C++ and Objective-C++ only)} +Disable the warning about the use of simple-template-id as the declarator-id +of a constructor or destructor, which became invalid in C++20 via DR 2237. +For example: + +@smallexample +template struct S @{ + S(); // should be S(); + ~S(); // should be ~S(); +@}; +@end smallexample + +@option{-Wtemplate-id-cdtor} is enabled by default with +@option{-std=c++20}; it is also enabled by @option{-Wc++20-compat}. + @opindex Wterminate @opindex Wno-terminate @item -Wno-terminate @r{(C++ and Objective-C++ only)} diff --git a/gcc/testsuite/g++.dg/DRs/dr2237-2.C b/gcc/testsuite/g++.dg/DRs/dr2237-2.C new file mode 100644 index 00000000000..1d99347229c --- /dev/null +++ b/gcc/testsuite/g++.dg/DRs/dr2237-2.C @@ -0,0 +1,9 @@ +// DR 2237 - Can a template-id name a constructor? +// { dg-options "" } + +template +struct X { + X(); // { dg-warning "template-id not allowed for constructor" "" { target c++20 } } + X(int); // OK, injected-class-name used + ~X(); // { dg-warning "template-id not allowed for destructor" "" { target c++20 } } +}; diff --git a/gcc/testsuite/g++.dg/DRs/dr2237-3.C b/gcc/testsuite/g++.dg/DRs/dr2237-3.C new file mode 100644 index 00000000000..c8ad6852389 --- /dev/null +++ b/gcc/testsuite/g++.dg/DRs/dr2237-3.C @@ -0,0 +1,16 @@ +// PR c++/107126 +// { dg-options "" } + +template +struct C +{ + ~C(); +}; +template +C::~C() // { dg-warning "template-id not allowed for destructor" "" { target c++20 } } +{ +} +int main() +{ + C c;; +} diff --git a/gcc/testsuite/g++.dg/DRs/dr2237-4.C b/gcc/testsuite/g++.dg/DRs/dr2237-4.C new file mode 100644 index 00000000000..a358dd521b5 --- /dev/null +++ b/gcc/testsuite/g++.dg/DRs/dr2237-4.C @@ -0,0 +1,11 @@ +// PR c++/97202 +// { dg-options "" } + +template +struct F +{ + F(); // { dg-warning "template-id not allowed for constructor" "" { target c++20 } } +}; + +template +inline F::F() { } diff --git a/gcc/testsuite/g++.dg/DRs/dr2237-5.C b/gcc/testsuite/g++.dg/DRs/dr2237-5.C new file mode 100644 index 00000000000..fd51968f7e1 --- /dev/null +++ b/gcc/testsuite/g++.dg/DRs/dr2237-5.C @@ -0,0 +1,7 @@ +// PR c++/97202 +// { dg-options "" } + +template struct S : Base { + inline S() {} // { dg-warning "template-id not allowed for constructor" "" { target c++20 } } + inline ~S() {} // { dg-warning "template-id not allowed for destructor" "" { target c++20 } } +}; diff --git a/gcc/testsuite/g++.dg/DRs/dr2237.C b/gcc/testsuite/g++.dg/DRs/dr2237.C index f3d6d11e61e..830c8f5a2a6 100644 --- a/gcc/testsuite/g++.dg/DRs/dr2237.C +++ b/gcc/testsuite/g++.dg/DRs/dr2237.C @@ -2,7 +2,7 @@ template struct X { - X(); // { dg-error "expected" "" { target c++20 } } + X(); // { dg-error "template-id not allowed for constructor" "" { target c++20 } } X(int); // OK, injected-class-name used ~X(); // { dg-error "template-id not allowed for destructor" "" { target c++20 } } }; diff --git a/gcc/testsuite/g++.dg/parse/constructor2.C b/gcc/testsuite/g++.dg/parse/constructor2.C index d620f41e027..e4b2ad3f2fe 100644 --- a/gcc/testsuite/g++.dg/parse/constructor2.C +++ b/gcc/testsuite/g++.dg/parse/constructor2.C @@ -1,11 +1,11 @@ // PR c++/14260 -template -class T -{ -public: - T(short,short f=0) {} - T(int f) {} // { dg-error "expected" "" { target c++20 } } - T(int f=0,const char* b=0) {} // { dg-error "expected" "" { target c++20 } } -}; +template +class T +{ +public: + T(short,short f=0) {} + T(int f) {} // { dg-error "template-id not allowed for constructor" "" { target c++20 } } + T(int f=0,const char* b=0) {} // { dg-error "template-id not allowed for constructor" "" { target c++20 } } +}; diff --git a/gcc/testsuite/g++.dg/template/error34.C b/gcc/testsuite/g++.dg/template/error34.C index ab688d9ba8c..921cb8fb729 100644 --- a/gcc/testsuite/g++.dg/template/error34.C +++ b/gcc/testsuite/g++.dg/template/error34.C @@ -3,27 +3,27 @@ template struct A { - A<__builtin_offsetof(T, x)>(); // { dg-error "type/value mismatch|offsetof\\(T, x\\)|expected" } + A<__builtin_offsetof(T, x)>(); // { dg-error "type/value mismatch|offsetof\\(T, x\\)|template-id" } }; template struct B { - B<__builtin_offsetof(T, x.y)>(); // { dg-error "type/value mismatch|offsetof\\(T, x.y\\)|expected" } + B<__builtin_offsetof(T, x.y)>(); // { dg-error "type/value mismatch|offsetof\\(T, x.y\\)|template-id" } }; template struct C { - C<__builtin_offsetof(T, x[6])>(); // { dg-error "type/value mismatch|offsetof\\(T, x\\\[6\\\]\\)|expected" } + C<__builtin_offsetof(T, x[6])>(); // { dg-error "type/value mismatch|offsetof\\(T, x\\\[6\\\]\\)|template-id" } }; template struct D { - D<__builtin_offsetof(T, x.y[6].z)>(); // { dg-error "type/value mismatch|offsetof\\(T, x.y\\\[6\\\].z\\)|expected" } + D<__builtin_offsetof(T, x.y[6].z)>(); // { dg-error "type/value mismatch|offsetof\\(T, x.y\\\[6\\\].z\\)|template-id" } }; struct E { int x; }; template struct F { - F<__builtin_offsetof(E, x)>(); // { dg-error "type/value mismatch|offsetof\\(E, x\\)|expected" } + F<__builtin_offsetof(E, x)>(); // { dg-error "type/value mismatch|offsetof\\(E, x\\)|template-id" } }; diff --git a/gcc/testsuite/g++.dg/warn/Wtemplate-id-cdtor-1.C b/gcc/testsuite/g++.dg/warn/Wtemplate-id-cdtor-1.C new file mode 100644 index 00000000000..429490708bc --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wtemplate-id-cdtor-1.C @@ -0,0 +1,9 @@ +// PR c++/107126 +// { dg-do compile } +// { dg-options "-Wc++20-compat" } + +template +struct X { + X(); // { dg-warning "template-id not allowed for constructor" } + ~X(); // { dg-warning "template-id not allowed for destructor" } +}; diff --git a/gcc/testsuite/g++.dg/warn/Wtemplate-id-cdtor-2.C b/gcc/testsuite/g++.dg/warn/Wtemplate-id-cdtor-2.C new file mode 100644 index 00000000000..2b1c4ea8222 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wtemplate-id-cdtor-2.C @@ -0,0 +1,9 @@ +// PR c++/107126 +// { dg-do compile } +// { dg-options "-Wtemplate-id-cdtor" } + +template +struct X { + X(); // { dg-warning "template-id not allowed for constructor" } + ~X(); // { dg-warning "template-id not allowed for destructor" } +}; diff --git a/gcc/testsuite/g++.dg/warn/Wtemplate-id-cdtor-3.C b/gcc/testsuite/g++.dg/warn/Wtemplate-id-cdtor-3.C new file mode 100644 index 00000000000..bed96e84bd2 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wtemplate-id-cdtor-3.C @@ -0,0 +1,9 @@ +// PR c++/107126 +// { dg-do compile } +// { dg-options "-Wc++20-compat -Wno-template-id-cdtor" } + +template +struct X { + X(); // { dg-bogus "template-id not allowed for constructor" } + ~X(); // { dg-bogus "template-id not allowed for destructor" } +}; diff --git a/gcc/testsuite/g++.dg/warn/Wtemplate-id-cdtor-4.C b/gcc/testsuite/g++.dg/warn/Wtemplate-id-cdtor-4.C new file mode 100644 index 00000000000..706e5746e60 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wtemplate-id-cdtor-4.C @@ -0,0 +1,9 @@ +// PR c++/107126 +// { dg-do compile } +// { dg-options "-Wtemplate-id-cdtor -Wno-c++20-compat" } + +template +struct X { + X(); // { dg-warning "template-id not allowed for constructor" } + ~X(); // { dg-warning "template-id not allowed for destructor" } +}; diff --git a/gcc/testsuite/g++.old-deja/g++.pt/ctor2.C b/gcc/testsuite/g++.old-deja/g++.pt/ctor2.C index bf418ba48c7..56b4232d04b 100644 --- a/gcc/testsuite/g++.old-deja/g++.pt/ctor2.C +++ b/gcc/testsuite/g++.old-deja/g++.pt/ctor2.C @@ -4,7 +4,7 @@ template struct A { - A(); // { dg-error "expected" "" { target c++20 } } + A(); // { dg-error "template-id" "" { target c++20 } } }; template