From patchwork Tue May 24 20:03:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 54353 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CDFB83857BB2 for ; Tue, 24 May 2022 20:04:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CDFB83857BB2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1653422648; bh=IU7+ULGosktyEx/trnj/HC/Jrjme0J+jWXfEcmWr1HQ=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=OJXzMrHD6//nzRFBiT8qT6FY1xGs8AH71ZORpAT8vmd7LYv6rhPHPfgse/tAQNU2c f7zvqWxnfm6d50rgLl8Vi1axRqKC9Q+9/5QxSIW31iKZoBcUzlpqcnEkQyRRzlW9ze fevH+naq+vZd3TNWQTOTaGrudQmK+OYMMgRFoxSE= 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 42CD23857B90 for ; Tue, 24 May 2022 20:03:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 42CD23857B90 Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-327-osUdBrxFPI2-Tpo19DX_9w-1; Tue, 24 May 2022 16:03:36 -0400 X-MC-Unique: osUdBrxFPI2-Tpo19DX_9w-1 Received: by mail-qt1-f200.google.com with SMTP id v14-20020a05622a130e00b002f940c8704dso2555311qtk.21 for ; Tue, 24 May 2022 13:03:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=IU7+ULGosktyEx/trnj/HC/Jrjme0J+jWXfEcmWr1HQ=; b=NcVIIMNmKEXCRYxMcNQAX9q6bgTyiWw21lVxn+A6g1fm1SlLFINEEa8RNM0+xRNSDo fgM8fs0gAfsCHOqN15O9+OQyp4qIkCxvGPdGlwsXyZanLR6/d1rEDnEeJSdYV5L19r4q Q6CVwhe4ARG9Oi4Tgr5hqlJjKE6McwlBVY7QRyWTBdSAzufBeiTIXGj2TvtSym8WAxGJ /ZTCmrRwTGPvymu4+wsNScFT7QZHmavT4oPNoxbZPioY5iVzmDwWl3Cwopx+0hFAPka8 0/oYZJwp1F6LUsITUGYUsSqzLxAC6kiJk8Qjgi4ZLCN0Mxu9eSxphJlUFAZLNIAndHUo 72+g== X-Gm-Message-State: AOAM53037jlLPEDRlpDbVOzIAGABxIhnT5pGT7L1dmmlQgZRvl1rm7CW 7+G4l8sEN3CRz2psR6/Q006YBJ9SVb4c/iss88OpYFQAbSDqYgRTNz8LHDRVcbXpR98s1TxhXoX Q3Vwrin0nK7bAtQXvJ/cMcJl/8czZeORNk+GTCzmHluclKsq7BRXXPBcx4eSA2hHpbQ== X-Received: by 2002:a37:5346:0:b0:69c:679d:9d51 with SMTP id h67-20020a375346000000b0069c679d9d51mr18925348qkb.189.1653422615779; Tue, 24 May 2022 13:03:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzgt0L5Tt9F651olJ7p3KdbYDODLLyOuj5Xz0dhwHPtxyG7qYo21nwYM3o47x57FLjlm/c2/Q== X-Received: by 2002:a37:5346:0:b0:69c:679d:9d51 with SMTP id h67-20020a375346000000b0069c679d9d51mr18925321qkb.189.1653422615334; Tue, 24 May 2022 13:03:35 -0700 (PDT) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id p17-20020ac87411000000b002f93c5444e3sm165755qtq.52.2022.05.24.13.03.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 May 2022 13:03:34 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: strict constexpr and local vars Date: Tue, 24 May 2022 16:03:32 -0400 Message-Id: <20220524200332.1011424-1-jason@redhat.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.5 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_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" A change I was working on made constexpr_searcher.cc start to fail, and when I looked at it I wondered why it had been accepted before. This turned out to be because we try to be more flexible about constant-evaluation of static initializers, as allowed, but we were wrongly doing the same for non-static initializers as well. Tested x86_64-pc-linux-gnu, applying to trunk. gcc/cp/ChangeLog: * constexpr.cc (maybe_constant_init_1): Only pass false for strict when initializing a variable of static duration. libstdc++-v3/ChangeLog: * testsuite/20_util/function_objects/constexpr_searcher.cc: Add constexpr. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/constexpr-local4.C: New test. --- gcc/cp/constexpr.cc | 12 +++++++++--- gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C | 17 +++++++++++++++++ .../function_objects/constexpr_searcher.cc | 4 ++-- 3 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C base-commit: 1189c03859cefef4fc4fd44d57eb3d4d3348b562 diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 388239ea8a8..1a70fda1dc5 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -8301,9 +8301,15 @@ maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant, else if (CONSTANT_CLASS_P (t) && allow_non_constant) /* No evaluation needed. */; else - t = cxx_eval_outermost_constant_expr (t, allow_non_constant, - /*strict*/false, - manifestly_const_eval, false, decl); + { + /* [basic.start.static] allows constant-initialization of variables with + static or thread storage duration even if it isn't required, but we + shouldn't bend the rules the same way for automatic variables. */ + bool is_static = (decl && DECL_P (decl) + && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))); + t = cxx_eval_outermost_constant_expr (t, allow_non_constant, !is_static, + manifestly_const_eval, false, decl); + } if (TREE_CODE (t) == TARGET_EXPR) { tree init = TARGET_EXPR_INITIAL (t); diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C new file mode 100644 index 00000000000..bef62488579 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C @@ -0,0 +1,17 @@ +// { dg-do compile { target c++14 } } + +struct A +{ + int i; + constexpr A(int i): i(i) {}; +}; + +const A a = 42; + +constexpr int f() +{ + const int j = a.i; // { dg-message "'a'" } + return j; +} + +static_assert (f() == 42,""); // { dg-error "non-constant" } diff --git a/libstdc++-v3/testsuite/20_util/function_objects/constexpr_searcher.cc b/libstdc++-v3/testsuite/20_util/function_objects/constexpr_searcher.cc index 33012200cb0..17069694c1b 100644 --- a/libstdc++-v3/testsuite/20_util/function_objects/constexpr_searcher.cc +++ b/libstdc++-v3/testsuite/20_util/function_objects/constexpr_searcher.cc @@ -28,13 +28,13 @@ #include -const std::string_view +constexpr std::string_view patt = "World"; constexpr std::string_view greet = "Hello, Humongous World of Wonder!!!"; -const std::wstring_view +constexpr std::wstring_view wpatt = L"World"; constexpr std::wstring_view