From patchwork Wed Feb 22 19:45:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 60671 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp776229wrd; Wed, 22 Feb 2023 11:46:38 -0800 (PST) X-Google-Smtp-Source: AK7set/UIKRBtgUJmIdDkhcJJ3HfGwfwwmSXtWF/LFuuA9jsfhekCNOpMt7ZktelSxa66BiAv+/p X-Received: by 2002:a17:907:7288:b0:8bc:9bce:7eb6 with SMTP id dt8-20020a170907728800b008bc9bce7eb6mr19666993ejc.7.1677095198786; Wed, 22 Feb 2023 11:46:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677095198; cv=none; d=google.com; s=arc-20160816; b=flwgVSD5kRe9LInc099ghsDAaghQVJvf/2r3XOZFLQEA7uIVZKZU3T+KElvZE2lshG w+ByjFeqjHX/MnS8WkVwwOZYeXeUaPMJtxCJxUE5A0T2qvT8J2c+SD0/W32ueKCSQ1E7 muJwnQMv+hU26aLibHXWVr6AHVQYpQhzjmqs3hyPBE7FGo8GR/x1kUnQsGWK/ewDPXe0 b3kgL05xlML2ntHORgXYZay1198IGbe4Z5b4yTw9AGWiYqY7+wliG2NIL29bHyFLaZbX GueLEWzC6tsPwI0LXWN8oyw//ag0sC/3LpHPRl/JPKWQwvwkKiIEvWfRKymPHwbOHjjY es/w== 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-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=c49DQGM3kbA0IeEafRn2KeOzs/4NV0O8WU5rRMCMsG4=; b=U2M4To4Ly/BlEpYShV7klkTVddsr7yk/BaWBK63/xyZZmIsHmYP+pGX+pc5C4qJFFl uRZQPg8rKeMhWgRSGjFh6FyaThmcebBUHaYT78HNtTeNsmr8indKlGDHBRTe8gGIlRVk e59sLwhgKA+JRpKYMesLLdy0xSFxop87MqVNo/nyN/8Bj/mziC25lsfFPxSnh1Np9Nrp 75qWg2+CScLIkFPHmOqUnEhtJQEHOKH06t33x5YZdK5aKumdwvy5ZZml2llPI9qzVVF0 llJLM8uCPI2RTwAejlWMxrHIuM2A/BIezw3KRpCFqd0vXxZrBBd3Hqye91zigA6OSbbo SkJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=hQNCljNm; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id 9-20020a170906018900b008ddf3c18304si8122868ejb.946.2023.02.22.11.46.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Feb 2023 11:46:38 -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=hQNCljNm; 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 991E5385800A for ; Wed, 22 Feb 2023 19:46:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 991E5385800A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677095197; bh=c49DQGM3kbA0IeEafRn2KeOzs/4NV0O8WU5rRMCMsG4=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=hQNCljNmOpCzbQlLNhufscAkiIJ5OpVKzVb/TnhqxaWJ9tE8isqdfWnSQGzTYX21Y wav3nXCMubXfOZAtDueu2efFC6EEsnUjifZtpq28Q2T7wnntoBvRo/2d+yo4uJt27H I9oqopLBircDkPow/jUniO9H1y/qqcg3ftdhoVw8= 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 D6D933858D33 for ; Wed, 22 Feb 2023 19:45:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D6D933858D33 Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-398-K102PFZ9OZO_5BIg961Acw-1; Wed, 22 Feb 2023 14:45:18 -0500 X-MC-Unique: K102PFZ9OZO_5BIg961Acw-1 Received: by mail-qt1-f197.google.com with SMTP id r3-20020ac84243000000b003b9a3ab9153so4127655qtm.8 for ; Wed, 22 Feb 2023 11:45:18 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=c49DQGM3kbA0IeEafRn2KeOzs/4NV0O8WU5rRMCMsG4=; b=hmGCwgSWaRzn/aqw/OTzXoAEEy9SBKcM8NKrXCAqwKAMRVmxbZjKxGg9mLwl0ZooZz k+CFHFeePOm9goUKs6S+LPrdCp4vMJvjzWiaLwXi7rtC0n9s+yFa7ZtZJxGpq+v1GVcU CCvbaXslmSzvuGV0iVIeDDGYRmmbDU/7Mcde98JLFlf+axg5koFKp+OibatIn/7Skddj W3FCOfCG4JQw85qVF7lw+tDlBgiOO1DK1o70f75imYtepItqc+Lhc13JPEKCJvJZBaiC rcMGqBUW2Dona/4ch6Jkprfyw/T9xTFEHg4ZaYgwUqyMzKF2F/QoGq/00RUMR7KcP+pg XbMg== X-Gm-Message-State: AO0yUKWFhtz3RCHAUKPhVjVBpK9mfjjG3lI9yKFuFjLtxF58rELfvh+G PhfnvhIlsQEqqI0wF8ACmxGibLk+T5lv2fQO+r2P9j8rtquJ9FyXvHA9PqTXmSikQEdQFaHdVsP 0CxBRAmqmpoFWqA094QV3TVRIQQ1K7N0Jt+4ch6rPKqYDLeOCF5+PnYnU1AaD7r4LMfk5g1wm X-Received: by 2002:a05:622a:120d:b0:3b8:67cb:7928 with SMTP id y13-20020a05622a120d00b003b867cb7928mr16463569qtx.31.1677095116943; Wed, 22 Feb 2023 11:45:16 -0800 (PST) X-Received: by 2002:a05:622a:120d:b0:3b8:67cb:7928 with SMTP id y13-20020a05622a120d00b003b867cb7928mr16463534qtx.31.1677095116599; Wed, 22 Feb 2023 11:45:16 -0800 (PST) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id 15-20020ac856ef000000b003bb764fe4ffsm3537187qtu.3.2023.02.22.11.45.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Feb 2023 11:45:16 -0800 (PST) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: unevaluated array new-expr size constantness [PR108219] Date: Wed, 22 Feb 2023 14:45:09 -0500 Message-Id: <20230222194509.3606756-1-ppalka@redhat.com> X-Mailer: git-send-email 2.39.2.501.gd9d677b2d8 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.7 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_H2, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka 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?1758561775574373008?= X-GMAIL-MSGID: =?utf-8?q?1758561775574373008?= Here we're mishandling the unevaluated array new-expressions due to a supposed non-constant array size ever since r12-5253-g4df7f8c79835d569, made us no longer perform constant evaluation of non-manifestly-constant expressions within unevaluated contexts. This shouldn't make a difference here since the array sizes are constant literals, except they're actually NON_LVALUE_EXPR location wrappers wrapping INTEGER_CST, wrappers which used to get stripped as part of constant evaluation and now no longer do. Moreover it means build_vec_init can't constant fold the 'maxindex' passed from build_new_1 (since it uses maybe_constant_value with mce_unknown). This patch fixes the first issue by making maybe_constant_value and fold_non_dependent_expr_template shortcut handling location wrappers around constant nodes, and the second issue by using fold_build2_loc instead of cp_build_binary_op when computing the maxindex to pass to build_vec_init. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk/12? PR c++/108219 PR c++/108218 gcc/cp/ChangeLog: * constexpr.cc (maybe_constant_value): Extend the constant node shortcut to look through location wrappers too. (fold_non_dependent_expr_template): Mirror the constant node shortcut from maybe_constant_value. * init.cc (build_new_1): Use fold_build2_loc instead of cp_build_binary_op to build a MINUS_EXPR representing the maximum index. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/new6.C: New test. * g++.dg/cpp2a/concepts-new1.C: New test. --- gcc/cp/constexpr.cc | 8 ++++++-- gcc/cp/init.cc | 18 ++++++++---------- gcc/testsuite/g++.dg/cpp0x/new6.C | 9 +++++++++ gcc/testsuite/g++.dg/cpp2a/concepts-new1.C | 13 +++++++++++++ 4 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/new6.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-new1.C diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index aa2c14355f8..d38c4c80415 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -8538,9 +8538,9 @@ maybe_constant_value (tree t, tree decl /* = NULL_TREE */, t = mark_non_constant (t); return t; } - else if (CONSTANT_CLASS_P (t)) + else if (CONSTANT_CLASS_OR_WRAPPER_P (t)) /* No caching or evaluation needed. */ - return t; + return tree_strip_any_location_wrapper (t); if (manifestly_const_eval != mce_unknown) return cxx_eval_outermost_constant_expr (t, true, true, @@ -8631,6 +8631,10 @@ fold_non_dependent_expr_template (tree t, tsubst_flags_t complain, return t; } + if (CONSTANT_CLASS_OR_WRAPPER_P (t)) + /* No evaluation needed. */ + return tree_strip_any_location_wrapper (t); + if (cp_unevaluated_operand && !manifestly_const_eval) return t; diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc index 705a5b3bdb6..574d2e2586c 100644 --- a/gcc/cp/init.cc +++ b/gcc/cp/init.cc @@ -3653,16 +3653,14 @@ build_new_1 (vec **placement, tree type, tree nelts, error ("parenthesized initializer in array new"); return error_mark_node; } - init_expr - = build_vec_init (data_addr, - cp_build_binary_op (input_location, - MINUS_EXPR, outer_nelts, - integer_one_node, - complain), - vecinit, - explicit_value_init_p, - /*from_array=*/0, - complain); + tree maxindex = fold_build2_loc (input_location, MINUS_EXPR, + TREE_TYPE (outer_nelts), + outer_nelts, + build_one_cst (TREE_TYPE + (outer_nelts))); + init_expr = build_vec_init (data_addr, maxindex, vecinit, + explicit_value_init_p, /*from_array=*/0, + complain); } else { diff --git a/gcc/testsuite/g++.dg/cpp0x/new6.C b/gcc/testsuite/g++.dg/cpp0x/new6.C new file mode 100644 index 00000000000..17a669b42d0 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/new6.C @@ -0,0 +1,9 @@ +// PR c++/108218 +// { dg-do compile { target c++11 } } + +template +void f() { + decltype(new int[-1]) p; // { dg-error "negative" } +} + +decltype(new int[-1]) q; // { dg-error "negative" } diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-new1.C b/gcc/testsuite/g++.dg/cpp2a/concepts-new1.C new file mode 100644 index 00000000000..62007205108 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-new1.C @@ -0,0 +1,13 @@ +// PR c++/108219 +// { dg-do compile { target c++20 } } + +template +concept C = requires { new T[1]{{ 42 }}; }; + +template +concept D = requires { new T[2][1]{{{ 42 }}, {{ 42 }}}; }; + +struct A { A(int); }; + +static_assert(C); +static_assert(D);