From patchwork Tue Nov 29 12:24:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 27212 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp302931wrr; Tue, 29 Nov 2022 04:24:59 -0800 (PST) X-Google-Smtp-Source: AA0mqf5sJ9arCBFyODaRu4XH9jGNYVsA/Ib4rLBOA+DttgxvD1qH5qWCbBqBwHn/UGmgqUR6XwE6 X-Received: by 2002:a17:906:3109:b0:7a0:b505:cae5 with SMTP id 9-20020a170906310900b007a0b505cae5mr48773324ejx.648.1669724699181; Tue, 29 Nov 2022 04:24:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669724699; cv=none; d=google.com; s=arc-20160816; b=WbyQyPyHCBgx5mJ1fR7wQK4uOaBCwDCYNcF9VctU9+Xu3W22iDJ5a2kd0OiTE+xbF2 yrFB5ITAZUt7Dcj8GFVye8bIwpNxnWAEBe+O+578Vy/kD9JRsv76kZUYRugj32rPDXch 1d2ZECOTisX0lXf8yNRVv6s/o6SMB/SdFSJ0J6GQOxZGVh9BcW+tClw4x+Rbxg/+DLzZ vZlOR3QNAgAfh5/I9SqimrYUg6BRldDGRB32ufrG1mpIcIx5yYm+o/4Oyj0WxGvX4TyR izTG/i5IPGNnztzwJZZBpTe/pOYHlqT87w8DqBJzto0CjtWUlW0vjg10n0QPGSMWM0+y iZdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=whof3Ow+IHJymPrFWPh9U4BRm7mdh/LgfIvLXJuIjV0=; b=puuiYsV5I9LmbjsvnRGzzPRrvoXGORoiIEJ3pLisg5U2CCkZ9Cc2QfijfjxGHRil2g JjMNKfowlL/vaVSi8+FBtqydcoUllFAKXN1GcmrKrfIGKRPLI2oOiEGy7ST3knzOX9ds QvAO89qvFB+Lr9qyMw8LI+AXv6+/O4fmu9E+MWyjh6RqEIuMYYc4XOCmKiKSJmaj7W4b iAsFiToaDt5H3JIi5O5Lr35/fDv9wP9GUHLBfbP2qZqDfjzlUFBIdo+Fq+1SIVkaA8Bk ECpef3+fK0XiXPpDerYiT8LNRcE+iidfoCqXAhECT9IADA/FdEAfqGIV6/QwTcZW3NL9 LMew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=QBSbO5xA; 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=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id y22-20020a50e616000000b00461bde34a12si35167edm.627.2022.11.29.04.24.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Nov 2022 04:24:59 -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=@gcc.gnu.org header.s=default header.b=QBSbO5xA; 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=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 013C1385B52C for ; Tue, 29 Nov 2022 12:24:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 013C1385B52C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669724698; bh=whof3Ow+IHJymPrFWPh9U4BRm7mdh/LgfIvLXJuIjV0=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=QBSbO5xAa+2M+GddY8F7e23N2vAPEy2VvmQZgddzINRpr/LfVoClBlsNDvyWeciz2 6Y37mDlQ51GZVrbAGA675/ws5imBGsgOupLv29fOFeC9MhAARSEjLTTyoyZLpWtkRt IBEWhqPa1Gd5eah0FMXNHjUZy6uM7k4G+41BOD4M= 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 ESMTPS id C171F3858407 for ; Tue, 29 Nov 2022 12:24:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C171F3858407 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-474-aJcZ5KqRO1CAd2odOdMlxQ-1; Tue, 29 Nov 2022 07:24:12 -0500 X-MC-Unique: aJcZ5KqRO1CAd2odOdMlxQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 95ADD3803907 for ; Tue, 29 Nov 2022 12:24:12 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.195.114]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 536BF2024CB7; Tue, 29 Nov 2022 12:24:12 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 2ATCO7Xs4185403 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 29 Nov 2022 13:24:08 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 2ATCO7VY4185402; Tue, 29 Nov 2022 13:24:07 +0100 Date: Tue, 29 Nov 2022 13:24:07 +0100 To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Deduce range for structured bindings if expression is not type dependent [PR84469] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_SOMETLD_ARE_BAD_TLD, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=no 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750833246405782290?= X-GMAIL-MSGID: =?utf-8?q?1750833246405782290?= Hi! As shown on the decomp56.C testcase, if the range for expression when using structured bindings is not type dependent, we deduce the finish the structured binding types only when not in template (cp_convert_range_for takes care of that), but if in templates, do_range_for_auto_deduction is called instead and it doesn't handle structured bindings. During instantiation they are handled later, but during the parsing keeping the structured bindings type dependent when they shouldn't be changes behavior. The following patch calls cp_finish_decomp even from do_range_for_auto_deduction. The patch regresses the OpenMP g++.dg/gomp/for-21.C test (3 errors are gone), I'll post an incremental patch for it momentarily. Otherwise bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2022-11-29 Jakub Jelinek PR c++/84469 * parser.cc (do_range_for_auto_deduction): Add DECOMP_FIRST_NAME and DECOMP_CNT arguments. Call cp_finish_decomp if DECL is a structured binding. (cp_parser_range_for): Adjust do_range_for_auto_deduction caller. (cp_convert_omp_range_for): Likewise. * g++.dg/cpp1z/decomp56.C: New test. * g++.dg/gomp/pr84469.C: New test. Jakub --- gcc/cp/parser.cc.jj 2022-11-19 09:21:14.000000000 +0100 +++ gcc/cp/parser.cc 2022-11-25 15:39:15.326262120 +0100 @@ -2342,7 +2342,7 @@ static tree cp_parser_c_for static tree cp_parser_range_for (cp_parser *, tree, tree, tree, bool, unsigned short, bool); static void do_range_for_auto_deduction - (tree, tree); + (tree, tree, tree, unsigned int); static tree cp_parser_perform_range_for_lookup (tree, tree *, tree *); static tree cp_parser_range_for_member_function @@ -13668,7 +13668,8 @@ cp_parser_range_for (cp_parser *parser, if (!type_dependent_expression_p (range_expr) /* do_auto_deduction doesn't mess with template init-lists. */ && !BRACE_ENCLOSED_INITIALIZER_P (range_expr)) - do_range_for_auto_deduction (range_decl, range_expr); + do_range_for_auto_deduction (range_decl, range_expr, decomp_first_name, + decomp_cnt); } else { @@ -13707,7 +13708,8 @@ build_range_temp (tree range_expr) a shortcut version of cp_convert_range_for. */ static void -do_range_for_auto_deduction (tree decl, tree range_expr) +do_range_for_auto_deduction (tree decl, tree range_expr, + tree decomp_first_name, unsigned int decomp_cnt) { tree auto_node = type_uses_auto (TREE_TYPE (decl)); if (auto_node) @@ -13727,6 +13729,8 @@ do_range_for_auto_deduction (tree decl, iter_decl, auto_node, tf_warning_or_error, adc_variable_type); + if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl)) + cp_finish_decomp (decl, decomp_first_name, decomp_cnt); } } } @@ -42981,15 +42985,21 @@ cp_convert_omp_range_for (tree &this_pre && !BRACE_ENCLOSED_INITIALIZER_P (init)) { tree d = decl; + tree decomp_first_name = NULL_TREE; + unsigned decomp_cnt = 0; if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl)) { tree v = DECL_VALUE_EXPR (decl); if (TREE_CODE (v) == ARRAY_REF && VAR_P (TREE_OPERAND (v, 0)) && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0))) - d = TREE_OPERAND (v, 0); + { + d = TREE_OPERAND (v, 0); + decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1; + decomp_first_name = decl; + } } - do_range_for_auto_deduction (d, init); + do_range_for_auto_deduction (d, init, decomp_first_name, decomp_cnt); } cond = global_namespace; incr = NULL_TREE; --- gcc/testsuite/g++.dg/cpp1z/decomp56.C.jj 2022-11-25 15:55:27.673217565 +0100 +++ gcc/testsuite/g++.dg/cpp1z/decomp56.C 2022-11-25 16:08:28.238930284 +0100 @@ -0,0 +1,29 @@ +// PR c++/84469 +// { dg-do compile { target c++11 } } +// { dg-options "" } + +struct A { + template + void bar () const {} + template + void baz () const {} +}; +struct B { A a; }; + +template +void +foo () +{ + A a[1][1]; + for (auto const& [b]: a) // { dg-warning "structured bindings only available with" "" { target c++14_down } } + b.bar (); + B c; + auto const& [d] = c; // { dg-warning "structured bindings only available with" "" { target c++14_down } } + d.baz (); +} + +int +main () +{ + foo (); +} --- gcc/testsuite/g++.dg/gomp/pr84469.C.jj 2022-11-25 15:57:25.805510359 +0100 +++ gcc/testsuite/g++.dg/gomp/pr84469.C 2022-11-25 16:08:40.123758315 +0100 @@ -0,0 +1,24 @@ +// PR c++/84469 +// { dg-do compile { target c++11 } } +// { dg-options "" } + +struct A { + template + void bar () const {} +}; + +template +void +foo () +{ + A a[1][1]; + #pragma omp for + for (auto const& [b]: a) // { dg-warning "structured bindings only available with" "" { target c++14_down } } + b.bar (); +} + +int +main () +{ + foo (); +}