From patchwork Tue Feb 15 17:54:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1593186 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=vl1EMts6; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Jypdk6znZz9s07 for ; Wed, 16 Feb 2022 04:55:05 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A9BF2385842E for ; Tue, 15 Feb 2022 17:55:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A9BF2385842E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1644947703; bh=WiZXhczL+fqw9EKCCFDA+oghAFeqYflEzcI3UzCbIx8=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=vl1EMts6aJN8VYcKPjstCrcm7gErSf5jRKPjaC+KBKoJX50KyOTPcBrXk7uvA3g62 8hKwERrSpHgP9iicSZWUz+6vdNmooajoB35IEG6zofZIOQO/fJ1m1ysCRIUYSI6pb4 zOMv/SxTYMYwnTr4jp96Gd3/oV8XY1BCpR28bC4Y= 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 4B1D03858C83 for ; Tue, 15 Feb 2022 17:54:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4B1D03858C83 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-313-VhTBebNaNDSjzuzPOozIug-1; Tue, 15 Feb 2022 12:54:34 -0500 X-MC-Unique: VhTBebNaNDSjzuzPOozIug-1 Received: by mail-qt1-f200.google.com with SMTP id y1-20020ac87041000000b002c3db9c25f8so15437158qtm.5 for ; Tue, 15 Feb 2022 09:54:34 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=WiZXhczL+fqw9EKCCFDA+oghAFeqYflEzcI3UzCbIx8=; b=MfoGzFG4HC7NKjG0TmIwY1gjl73HX7AP2IuPRWx1l2TONR9+1OK/gdVjmfQhSidobh SChr4i+JqrIrvU/alvM2Rm9a/OEr4l/xwmQiFoR4FkNwgaxKY2I/Nnli5/wE+BFmdLIg nPe6RE5GbU7WlvDvud7EsZnbIMeOBZ+ZJDa3Lq15gLhYDFDqHPF/FFOF4un96OFgu199 b3u5S2V4vZD+QXDLFofjtoJvlQW/niBk4LqlKk/ddEYrIflW8Y6A6VBQSde/XzljEAaN Ww48MbVO+26nJ3LUQamGxwxF8t8oUdI7UeyD6WRdPw2/2bJFAo5CpntvBQPQC9O5tBRt l1HA== X-Gm-Message-State: AOAM530KSrliPpffaE1SXKa4AThy0SP37LbA/04eWvxplz8Bu3wgoNFW Y306BilLWDMIzuVOflkx++EAxiVB/2GPbdCEHWFGMLr+pL5QdgqRByLDanSL2OCkvFygdejwMZv CirA1vJVjpG0WaqBrzq0ALYPL3NZTrtuDyB2iJmGZE0gxIWkHjNRpy7XhPCTB5nZbKic= X-Received: by 2002:a05:620a:298e:: with SMTP id r14mr97508qkp.340.1644947673404; Tue, 15 Feb 2022 09:54:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJw6ntLTzZ/5lW/pL8Pwtk5iuniXNWvkmBT8KYKObby2vIrlXmOUm797oU4SJnpwfF2HfzXu+Q== X-Received: by 2002:a05:620a:298e:: with SMTP id r14mr97497qkp.340.1644947673135; Tue, 15 Feb 2022 09:54:33 -0800 (PST) Received: from localhost.localdomain (ool-18e40894.dyn.optonline.net. [24.228.8.148]) by smtp.gmail.com with ESMTPSA id y15sm17648953qko.133.2022.02.15.09.54.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Feb 2022 09:54:32 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: NON_DEPENDENT_EXPR is not potentially constant [PR104507] Date: Tue, 15 Feb 2022 12:54:30 -0500 Message-Id: <20220215175430.632649-1-ppalka@redhat.com> X-Mailer: git-send-email 2.35.1.129.gb80121027d MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.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_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Here we're crashing from potential_constant_expression because it tries to perform trial evaluation of the first operand '(bool)__r' of the conjunction (which is overall wrapped in a NON_DEPENDENT_EXPR), but cxx_eval_constant_expression ICEs on unhandled trees (of which CAST_EXPR is one). Since cxx_eval_constant_expression always treats NON_DEPENDENT_EXPR as non-constant, and since NON_DEPENDENT_EXPR is also opaque to instantiate_non_dependent_expr, it seems futile to have p_c_e_1 ever return true for NON_DEPENDENT_EXPR, so let's just instead return false and avoid recursing. Alternatively p_c_e_1 could continue to recurse into NON_DEPENDENT_EXPR, but with trial evaluation disabled by setting processing_template_decl, but as mentioned it seems pointless for p_c_e_1 to ever return true for NON_DEPENDENT_EXPR. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk and perhaps 10/11? PR c++/104507 gcc/cp/ChangeLog: * constexpr.cc (potential_constant_expression_1) : Return false rather than recursing. gcc/testsuite/ChangeLog: * g++.dg/template/non-dependent21.C: New test. --- gcc/cp/constexpr.cc | 4 +++- gcc/testsuite/g++.dg/template/non-dependent21.C | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/template/non-dependent21.C diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 7274c3b760e..c0523551f7b 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -9065,6 +9065,9 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, case BIND_EXPR: return RECUR (BIND_EXPR_BODY (t), want_rval); + case NON_DEPENDENT_EXPR: + return false; + case CLEANUP_POINT_EXPR: case MUST_NOT_THROW_EXPR: case TRY_CATCH_EXPR: @@ -9072,7 +9075,6 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, case EH_SPEC_BLOCK: case EXPR_STMT: case PAREN_EXPR: - case NON_DEPENDENT_EXPR: /* For convenience. */ case LOOP_EXPR: case EXIT_EXPR: diff --git a/gcc/testsuite/g++.dg/template/non-dependent21.C b/gcc/testsuite/g++.dg/template/non-dependent21.C new file mode 100644 index 00000000000..89900837b8b --- /dev/null +++ b/gcc/testsuite/g++.dg/template/non-dependent21.C @@ -0,0 +1,9 @@ +// PR c++/104507 + +extern const char *_k_errmsg[]; + +template +const char* DoFoo(int __r, int __s) { + const char* n = _k_errmsg[(bool)__r && __s ? 1 : 2]; + return n; +}