From patchwork Mon Sep 30 17:45:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1991112 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=B6Ok9W8Q; 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 4XHT5Q5b5Wz1xtY for ; Tue, 1 Oct 2024 03:46:18 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7DDF73865451 for ; Mon, 30 Sep 2024 17:46:16 +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.133.124]) by sourceware.org (Postfix) with ESMTP id 20AE8384DD00 for ; Mon, 30 Sep 2024 17:45:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 20AE8384DD00 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 20AE8384DD00 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727718354; cv=none; b=oumyEeuxv0lPN1DPPBqNisDp/Kh/DKCcKf9GTSTPdhLIM5HFJCoSHbxt6+8hHtj6wZbYUzDKHVO0sF8+FO19gGlVVymJ9lp3WQV9yap9leMkkrdsNB2/0NTlUV+9cOnIrwKAMZ4pQ/ucHFcEEO5v8Q/xdCZwOgO4y2jXGMDOkU4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727718354; c=relaxed/simple; bh=f6pHHFsJ60EwX7ypWqKXk2I2KxT0K7eoRIoSiQEjKEA=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=Wmf6FYWR7qpRD//t92EhD1qJvwPs30fnvA9jp5YCmzAACuvahGcagUs+WF+bxdTsH4cg1LtURF9oz2idOZCB64CHhyjF64sJ12Cw7jq/EPQTR6kuaruv0vX3JxFvikosjSOB0l8bqb+B5ARwr43Hclh0PH6my1W/+UE1mQ+84dg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1727718351; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Q871bQQZDcE+M1B3ibLhUF18Ri5Vn2d5r7NLSSMgNw4=; b=B6Ok9W8Q2JYyryiAiEy24RWgjx3i7WaETPfp1LnAbKTBOu1/EdvoPK/POfvE5iydAx2t82 8j9v5KwcZuCX62nBcVEFGvA8cYXwDllfTU5s0srU+zJSBLu9uIVXsyPUxWW6oVcQ/hY4F7 pATEA/F0vzIxF+cc8eckeNO+Z+2GrU8= Received: from mail-oo1-f71.google.com (mail-oo1-f71.google.com [209.85.161.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-466-y4C46fSGNTeBahE0OoCXRg-1; Mon, 30 Sep 2024 13:45:47 -0400 X-MC-Unique: y4C46fSGNTeBahE0OoCXRg-1 Received: by mail-oo1-f71.google.com with SMTP id 006d021491bc7-5e1cd853298so3538500eaf.1 for ; Mon, 30 Sep 2024 10:45:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727718346; x=1728323146; h=user-agent:in-reply-to:content-transfer-encoding :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=Q871bQQZDcE+M1B3ibLhUF18Ri5Vn2d5r7NLSSMgNw4=; b=ihitlDWr/eK4D8U3IgceYYbqJyLWT3E2aRsc0NKEGKdCQ35LMr12aB6nC29E5/QCNc lkonHuS0VVbTfU+G9A7QM8XooUyDeaFjkm0MnKiXyLeNVN9BmJOMG2H5uv4jSYAY0Heh PN4N15d6kOzsyKeU8q/cYxmENi/jrLUgV7jotnH+OvYa2o0C7mH9DxljgCIq6E5Y3Ll7 59/BAzADYBcEWQQZfAX4xdW33e7RKooVq9PiysswERVUHAeeuhUpPGihOL5bN5JhGpvT D4hqbR/aVbffsZCa2ypj7Bjoq9Dj0EkGiSya2ptpiHnDArcp4f7tcaJ5un7UlcUrK8yy vKZQ== X-Gm-Message-State: AOJu0YxFShhhqwf1bQjoodFA/EjUECgX+41XCNMgyYj7z3PLh0I6kq3f blohTAuYqLKP0peJB3ebOZDuhWMkfeG/DmwweOWU7zrXGTgWtJexsP4D+BIDVnlCVF34G/sd54A NOyiato/dg2WU4/hySv6tw969+QOrLV0GfMj1JUAs4eKCft6LxC6plfbzsD7m8r4= X-Received: by 2002:a05:6870:3289:b0:24f:c31a:5c29 with SMTP id 586e51a60fabf-28710c25d83mr9849678fac.43.1727718345857; Mon, 30 Sep 2024 10:45:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHAaeB/H3pLrh2Ym5pRT2KhbwouE2vfYrlGmAmffu+4jSHfa7RfdSRRWhHH504bkL6d0VFUbw== X-Received: by 2002:a05:6870:3289:b0:24f:c31a:5c29 with SMTP id 586e51a60fabf-28710c25d83mr9849657fac.43.1727718345460; Mon, 30 Sep 2024 10:45:45 -0700 (PDT) Received: from redhat.com ([2603:7000:9500:34a5::1db4]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7ae377d7606sm428297285a.51.2024.09.30.10.45.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Sep 2024 10:45:44 -0700 (PDT) Date: Mon, 30 Sep 2024 13:45:42 -0400 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: [PATCH v3] c++: concept in default argument [PR109859] Message-ID: References: <20240918210645.950332-1-polacek@redhat.com> <5af45dc7-226d-4430-a50e-f33141335498@redhat.com> <704edcfc-eb42-45ee-bd4f-ea1f771e321b@redhat.com> MIME-Version: 1.0 In-Reply-To: <704edcfc-eb42-45ee-bd4f-ea1f771e321b@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 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 Mon, Sep 30, 2024 at 10:53:04AM -0400, Jason Merrill wrote: > On 9/27/24 5:30 PM, Marek Polacek wrote: > > On Fri, Sep 27, 2024 at 04:57:58PM -0400, Jason Merrill wrote: > > > On 9/18/24 5:06 PM, Marek Polacek wrote: > > > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > > > > > -- >8 -- > > > > 1) We're hitting the assert in cp_parser_placeholder_type_specifier. > > > > It says that if it turns out to be false, we should do error() instead. > > > > Do so, then. > > > > > > > > 2) lambda-targ8.C should compile fine, though. The problem was that > > > > local_variables_forbidden_p wasn't cleared when we're about to parse > > > > the optional template-parameter-list for a lambda in a default argument. > > > > > > > > PR c++/109859 > > > > > > > > gcc/cp/ChangeLog: > > > > > > > > * parser.cc (cp_parser_lambda_declarator_opt): Temporarily clear > > > > local_variables_forbidden_p. > > > > (cp_parser_placeholder_type_specifier): Turn an assert into an error. > > > > > > > > gcc/testsuite/ChangeLog: > > > > > > > > * g++.dg/cpp2a/concepts-defarg3.C: New test. > > > > * g++.dg/cpp2a/lambda-targ8.C: New test. > > > > --- > > > > gcc/cp/parser.cc | 9 +++++++-- > > > > gcc/testsuite/g++.dg/cpp2a/concepts-defarg3.C | 8 ++++++++ > > > > gcc/testsuite/g++.dg/cpp2a/lambda-targ8.C | 10 ++++++++++ > > > > 3 files changed, 25 insertions(+), 2 deletions(-) > > > > create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-defarg3.C > > > > create mode 100644 gcc/testsuite/g++.dg/cpp2a/lambda-targ8.C > > > > > > > > diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc > > > > index 4dd9474cf60..bdc4fef243a 100644 > > > > --- a/gcc/cp/parser.cc > > > > +++ b/gcc/cp/parser.cc > > > > @@ -11891,6 +11891,11 @@ cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr) > > > > "lambda templates are only available with " > > > > "%<-std=c++20%> or %<-std=gnu++20%>"); > > > > + /* Even though the whole lambda may be a default argument, its > > > > + template-parameter-list is a context where it's OK to create > > > > + new parameters. */ > > > > + auto lvf = make_temp_override (parser->local_variables_forbidden_p, 0u); > > > > + > > > > cp_lexer_consume_token (parser->lexer); > > > > template_param_list = cp_parser_template_parameter_list (parser); > > > > @@ -20978,8 +20983,8 @@ cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc, > > > > /* In a default argument we may not be creating new parameters. */ > > > > if (parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN) > > > > { > > > > - /* If this assert turns out to be false, do error() instead. */ > > > > - gcc_assert (tentative); > > > > + if (!tentative) > > > > + error_at (loc, "local variables may not appear in this context"); > > > > > > There's no local variable in the new testcase, the error should talk about a > > > concept-name. > > > > Ah sure. So like this? > > > > Tested dg.exp. > > > > -- >8 -- > > 1) We're hitting the assert in cp_parser_placeholder_type_specifier. > > It says that if it turns out to be false, we should do error() instead. > > Do so, then. > > > > 2) lambda-targ8.C should compile fine, though. The problem was that > > local_variables_forbidden_p wasn't cleared when we're about to parse > > the optional template-parameter-list for a lambda in a default argument. > > > > PR c++/109859 > > > > gcc/cp/ChangeLog: > > > > * parser.cc (cp_parser_lambda_declarator_opt): Temporarily clear > > local_variables_forbidden_p. > > (cp_parser_placeholder_type_specifier): Turn an assert into an error. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/cpp2a/concepts-defarg3.C: New test. > > * g++.dg/cpp2a/lambda-targ8.C: New test. > > --- > > gcc/cp/parser.cc | 9 +++++++-- > > gcc/testsuite/g++.dg/cpp2a/concepts-defarg3.C | 8 ++++++++ > > gcc/testsuite/g++.dg/cpp2a/lambda-targ8.C | 10 ++++++++++ > > 3 files changed, 25 insertions(+), 2 deletions(-) > > create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-defarg3.C > > create mode 100644 gcc/testsuite/g++.dg/cpp2a/lambda-targ8.C > > > > diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc > > index f50534f5f39..a92e6a29ba6 100644 > > --- a/gcc/cp/parser.cc > > +++ b/gcc/cp/parser.cc > > @@ -11891,6 +11891,11 @@ cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr) > > "lambda templates are only available with " > > "%<-std=c++20%> or %<-std=gnu++20%>"); > > + /* Even though the whole lambda may be a default argument, its > > + template-parameter-list is a context where it's OK to create > > + new parameters. */ > > + auto lvf = make_temp_override (parser->local_variables_forbidden_p, 0u); > > + > > cp_lexer_consume_token (parser->lexer); > > template_param_list = cp_parser_template_parameter_list (parser); > > @@ -20989,8 +20994,8 @@ cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc, > > /* In a default argument we may not be creating new parameters. */ > > if (parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN) > > { > > - /* If this assert turns out to be false, do error() instead. */ > > - gcc_assert (tentative); > > + if (!tentative) > > + error_at (loc, "concept-name may not appear in this context"); > > Hmm, actually I expect it can appear in a concept-id. Aha. I just followed the commentary. > What if we fall > through to the next case instead of erroring/returning at this point? Then for concepts-defarg3.C we emit: concepts-defarg3.C:7:19: error: expected 'auto' or 'decltype(auto)' after 'C' 7 | template // { dg-error "may not appear in this context" } | ^ concepts-defarg3.C:7:19: error: invalid use of ‘auto’ in default template argument The first error is fine, the second...not so much. We created the auto at the end of cp_parser_placeholder_type_specifier in make_constrained_auto. I could fix it by adding a flag and returning early but maybe it can stay as-is. The fall through after "expected auto" is deliberate... Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- 1) We're hitting the assert in cp_parser_placeholder_type_specifier. Rather than turning that assert into an error, we can fall through to the next block to get an error. 2) lambda-targ8.C should compile fine, though. The problem was that local_variables_forbidden_p wasn't cleared when we're about to parse the optional template-parameter-list for a lambda in a default argument. PR c++/109859 gcc/cp/ChangeLog: * parser.cc (cp_parser_lambda_declarator_opt): Temporarily clear local_variables_forbidden_p. (cp_parser_placeholder_type_specifier): Remove an assert; fall through to the next condition instead. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-defarg3.C: New test. * g++.dg/cpp2a/lambda-targ8.C: New test. Reviewed-by: Jason Merrill --- gcc/cp/parser.cc | 20 +++++++++---------- gcc/testsuite/g++.dg/cpp2a/concepts-defarg3.C | 8 ++++++++ gcc/testsuite/g++.dg/cpp2a/lambda-targ8.C | 10 ++++++++++ 3 files changed, 27 insertions(+), 11 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-defarg3.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/lambda-targ8.C base-commit: 9c14f9a9c19957d9a45a7df97701bad475c80eea diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index f50534f5f39..54c59363da1 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -11891,6 +11891,11 @@ cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr) "lambda templates are only available with " "%<-std=c++20%> or %<-std=gnu++20%>"); + /* Even though the whole lambda may be a default argument, its + template-parameter-list is a context where it's OK to create + new parameters. */ + auto lvf = make_temp_override (parser->local_variables_forbidden_p, 0u); + cp_lexer_consume_token (parser->lexer); template_param_list = cp_parser_template_parameter_list (parser); @@ -20984,17 +20989,10 @@ cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc, /* In a template parameter list, a type-parameter can be introduced by type-constraints alone. */ - if (processing_template_parmlist && !placeholder) - { - /* In a default argument we may not be creating new parameters. */ - if (parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN) - { - /* If this assert turns out to be false, do error() instead. */ - gcc_assert (tentative); - return error_mark_node; - } - return build_constrained_parameter (con, proto, args); - } + if (processing_template_parmlist + && !placeholder + && (parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN) == 0) + return build_constrained_parameter (con, proto, args); /* Diagnose issues placeholder issues. */ if (!parser->in_result_type_constraint_p && !placeholder) diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-defarg3.C b/gcc/testsuite/g++.dg/cpp2a/concepts-defarg3.C new file mode 100644 index 00000000000..aaf3edf77e6 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-defarg3.C @@ -0,0 +1,8 @@ +// PR c++/109859 +// { dg-do compile { target c++20 } } + +template +concept C = true; + +template // { dg-error "expected|invalid" } +int f(); diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-targ8.C b/gcc/testsuite/g++.dg/cpp2a/lambda-targ8.C new file mode 100644 index 00000000000..3685b0ef880 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/lambda-targ8.C @@ -0,0 +1,10 @@ +// PR c++/109859 +// { dg-do compile { target c++20 } } + +template +concept A = true; + +template {}> +int x; + +void g() { (void) x<>; }