From patchwork Mon Jul 29 15:38:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1966112 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=L8FK4FDv; 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 4WXjFM0kLjz1yf4 for ; Tue, 30 Jul 2024 01:38:47 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 06A083858432 for ; Mon, 29 Jul 2024 15:38:45 +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 712013858D37 for ; Mon, 29 Jul 2024 15:38:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 712013858D37 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 712013858D37 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=1722267492; cv=none; b=v9y7guDwzyQ/MBW9Gb3n1CmmdQM5V+Yw4H8fb+sI9i/UV7QbDSbjYnsvo5ewsQzTiigxMbuhmad2R9O/xKbdq82pUXI82w54ghSm98/pcH7doIQlZIPaYkqW+TxR7eT5ya0GQfhx6RvZJ65d5pHxOLgWBGORE4gUFUARNOnPeRc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722267492; c=relaxed/simple; bh=5rnNRFY9Nq1rsJ+9HDPG3Xd/5RgQOAquZCLAOfk52z0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=SNqIEBCbPKYVx7EfkgPToeN3bekNqzgvJrvhqWDlPwnI98ljB+1PSn1fWMel/GJNqTNj+mtXCs7Rw2qTnOHWEKp48J5L/oKbU6ZvIfGtZ+tlUjji2FbWAlsgaUXEsmebUMXoCIje7/jCjt3U2aDUvVu3Gwh2jBAzzLt04xT/GDk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722267490; 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; bh=0IqOu8Vm/tHtMQV8h+ZvmQGFYYRfa4RcxOYJ9FSGw4I=; b=L8FK4FDvCm76PfoSSUehS4TcTdxq6fK/z83hNiyNGlZEUKrMtCZCihn4Zs3be6Ul8ICbzD OQRUFyMRjCMA0a8GxCmuBDHTlRhzc3SMuxk3aOKzc1cqUfQDdSof49HRSSLx1h4nU9H5EK mQqCZYhQRQifUR4J8gcIUbzuLZmMG/8= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-257-H-A5w1KGNpmA_kwYPChPww-1; Mon, 29 Jul 2024 11:38:08 -0400 X-MC-Unique: H-A5w1KGNpmA_kwYPChPww-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-6b7a1c45abbso66095406d6.0 for ; Mon, 29 Jul 2024 08:38:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722267487; x=1722872287; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=0IqOu8Vm/tHtMQV8h+ZvmQGFYYRfa4RcxOYJ9FSGw4I=; b=T3WXTgNygKVTPS67Bhdf2RLqBwTk52a/bCupOAnG5SfSV0uvMyd6RuK+sY2HxdPrU2 oo51XGwz20LQU0YZy+fxuoMJjOLd54MlpoCX2Pec5BBa7RBwRiCmZGJf2GEvEEk3/orV dvQdPkUUPcfNt3qQnzAZ1xBs+SN9oG/Mq/BHZyLq3MiEUHsO2YJEcxIWGbNOSX4x8adh pyFDPVdKyRz8ygvZGywlXoKbudSD8jBd/dgVoObb90KMxcc1khkjGG3Mnxyq/eCzDlh5 bT1X+gzkd83Z/x9jG/BYwBY1+j2yckCH6vsG8igcmQRtV/3OBHEYAA34aheAvFMPTF7d d6pw== X-Gm-Message-State: AOJu0YxtSfooJoHIogjoqLHnnceDnKqd+KiY3jkmFBJMORm7fdM/1Ver BDoBFxWjHFTnBL3JA5TfPCfLYmPBscrwNA/HlmdEvDPgdB5bXXofp6LVNdy0EsFdhTxtw+BbgmH 2+eQ8AOIa6yFrwnihq93mkt4uGNcWsWGGvePqJmKxMKhOqhfZS0EtlyiMb8npvFalP8+6gs8vLl Pi7Ha+jXh3e4HKGJRdhQvc7KWWPxS73AQcCFIb X-Received: by 2002:a05:6214:20ea:b0:6b5:1584:62bf with SMTP id 6a1803df08f44-6bb562e1ca6mr157115376d6.6.1722267487366; Mon, 29 Jul 2024 08:38:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEZahNo1SyRYWAtKc7IaGSlWYAhrqoIEOkJpf5MgblR9+4xtVa03KzA6DE8FSvdRs9ZI/qJ/g== X-Received: by 2002:a05:6214:20ea:b0:6b5:1584:62bf with SMTP id 6a1803df08f44-6bb562e1ca6mr157114906d6.6.1722267486858; Mon, 29 Jul 2024 08:38:06 -0700 (PDT) Received: from localhost.localdomain (ool-18bb2a2e.dyn.optonline.net. [24.187.42.46]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6bb5a7fb286sm32449586d6.54.2024.07.29.08.38.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jul 2024 08:38:06 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: generic lambda as default template argument [PR88313] Date: Mon, 29 Jul 2024 11:38:02 -0400 Message-ID: <20240729153802.1045105-1-ppalka@redhat.com> X-Mailer: git-send-email 2.46.0.rc2 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.4 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 Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk and perhaps 14.3? It should only make a differenc for C++20 code where lambdas are permitted as template arguments. -- >8 -- Here we're rejecting the generic lambda inside the default template argument ultimately because auto_is_implicit_function_template_parm_p doesn't get set during parsing of the lambda's parameter list, due to the !processing_template_parmlist restriction. But when parsing a lambda parameter list we should always set that flag regardless of where the lambda appears. This patch makes sure this happens by way of a local lambda_p flag. PR c++/88313 gcc/cp/ChangeLog: * parser.cc (cp_parser_lambda_declarator_opt): Pass lambda_p=true to cp_parser_parameter_declaration_clause. (cp_parser_direct_declarator): Pass lambda_p=false to to cp_parser_parameter_declaration_clause. (cp_parser_parameter_declaration_clause): Add bool lambda_p parameter. Consider lambda_p instead of current_class_type when setting parser->auto_is_implicit_function_template_parm_p. Don't consider processing_template_parmlist. (cp_parser_requirement_parameter_list): Pass lambda_p=false to cp_parser_parameter_declaration_clause. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/lambda-targ6.C: New test. --- gcc/cp/parser.cc | 34 +++++++++++++---------- gcc/testsuite/g++.dg/cpp2a/lambda-targ6.C | 11 ++++++++ 2 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/lambda-targ6.C diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index f79736c17ac..f5336eae74a 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -2600,7 +2600,7 @@ static tree cp_parser_type_id_1 static void cp_parser_type_specifier_seq (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *); static tree cp_parser_parameter_declaration_clause - (cp_parser *, cp_parser_flags); + (cp_parser *, cp_parser_flags, bool); static tree cp_parser_parameter_declaration_list (cp_parser *, cp_parser_flags, auto_vec *); static cp_parameter_declarator *cp_parser_parameter_declaration @@ -11889,7 +11889,7 @@ cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr) /* Parse parameters. */ param_list = cp_parser_parameter_declaration_clause - (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL); + (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL, /*lambda_p=*/true); /* Default arguments shall not be specified in the parameter-declaration-clause of a lambda-declarator. */ @@ -24097,7 +24097,8 @@ cp_parser_direct_declarator (cp_parser* parser, /* Parse the parameter-declaration-clause. */ params - = cp_parser_parameter_declaration_clause (parser, flags); + = cp_parser_parameter_declaration_clause (parser, flags, + /*lambda=*/false); const location_t parens_end = cp_lexer_peek_token (parser->lexer)->location; @@ -25444,13 +25445,17 @@ function_being_declared_is_template_p (cp_parser* parser) The parser flags FLAGS is used to control type-specifier parsing. + LAMBDA_P is true if this is the parameter-declaration-clause of + a lambda-declarator. + Returns a representation for the parameter declarations. A return value of NULL indicates a parameter-declaration-clause consisting only of an ellipsis. */ static tree cp_parser_parameter_declaration_clause (cp_parser* parser, - cp_parser_flags flags) + cp_parser_flags flags, + bool lambda_p) { tree parameters; cp_token *token; @@ -25459,15 +25464,15 @@ cp_parser_parameter_declaration_clause (cp_parser* parser, auto cleanup = make_temp_override (parser->auto_is_implicit_function_template_parm_p); - if (!processing_specialization - && !processing_template_parmlist - && !processing_explicit_instantiation - /* default_arg_ok_p tracks whether this is a parameter-clause for an - actual function or a random abstract declarator. */ - && parser->default_arg_ok_p) - if (!current_function_decl - || (current_class_type && LAMBDA_TYPE_P (current_class_type))) - parser->auto_is_implicit_function_template_parm_p = true; + if (lambda_p + || (!processing_specialization + && !processing_template_parmlist + && !processing_explicit_instantiation + /* default_arg_ok_p tracks whether this is a parameter-clause for an + actual function or a random abstract declarator. */ + && parser->default_arg_ok_p + && !current_function_decl)) + parser->auto_is_implicit_function_template_parm_p = true; /* Peek at the next token. */ token = cp_lexer_peek_token (parser->lexer); @@ -31743,7 +31748,8 @@ cp_parser_requirement_parameter_list (cp_parser *parser) return error_mark_node; tree parms = (cp_parser_parameter_declaration_clause - (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL)); + (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL, + /*lambda_p=*/false)); if (!parens.require_close (parser)) return error_mark_node; diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-targ6.C b/gcc/testsuite/g++.dg/cpp2a/lambda-targ6.C new file mode 100644 index 00000000000..6dbcbf40970 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/lambda-targ6.C @@ -0,0 +1,11 @@ +// PR c++/88313 +// { dg-do compile { target c++20 } } + +template +constexpr int f() { return N; } + +template +constexpr int g(T t = {}) { return t(42); } + +static_assert(f() == 42); +static_assert(g() == 42);