From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 5B2CF3858D20 for ; Thu, 25 Apr 2024 18:40:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5B2CF3858D20 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 5B2CF3858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714070425; cv=none; b=S4QkGu1U0df5VWtYYS8ZpnXsWh7HCP1Clv0yocnq0D8KHLGalvcBrCqrdEiWu2eiw9NyHYVdbXpv87JE3Cl79DvYS3Q9ST/McsqzaJuzVDLrgEuQGNnSYbaKRBAAKXL3xUsfvaX94KyKpYncvfqhWiQRYClkEsTK5K8uOpkqekA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714070425; c=relaxed/simple; bh=tpkR/hH73bPT1HhDo4STF/watrR3oertPeS2iqezp0Q=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=F5zBvlpoGhfLr1JQd7sK/o+d+Di8JsTQ/ORjH/3orZxP0msuvHipsKNsmnmXx927bf9d2EW/KYVjQdL9EMD9j1Ye7BPzdlB/LnrTtGIYxhLzjucXSNTpwNcXbakFvUx/KrcD7Jyx1qaFo2fgqDXF2uVbRo/mo751izmp6u122AE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1714070422; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZBYTcQl1Staw+memj5ivlZexNzqhMdqWeLQ//ymoxgI=; b=Qx9WCY4FpJCcFK8ebxoyQ0urRpjwdZYoDImd/ykguhAbnBpW+XlHjsTEtrMYNs2k8n30iu FAzcgQQ3wBcybNh8bpk/K5ga2cAMhIrWsYain3TILwpXEIC4MrJoGaAajL5no4ZIcKjaoh OLtxQxeharJvzgBy3/h+UR5bgRB8djg= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-387-qpOmRKS9OTyHw39zvic9QQ-1; Thu, 25 Apr 2024 14:38:52 -0400 X-MC-Unique: qpOmRKS9OTyHw39zvic9QQ-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-439d6a512e9so18087391cf.1 for ; Thu, 25 Apr 2024 11:38:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714070332; x=1714675132; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ZBYTcQl1Staw+memj5ivlZexNzqhMdqWeLQ//ymoxgI=; b=FTfop6/0AA/nFYFRr9rTZ05RaFFrGQ0AGwjErlRD5PU8dgPo5rGxV+WrvwwsEOG0uu Q+tndt+8ntlD3aNgKYKHmz6r/dO67zKxE1pzClDQEGewSt5Ub5WUXhdnzH8fakFItb12 pe3gTqOZIed5TvLDe2zk5yVFhnnLKD59912YqqECFW5yn7WNNweaS3Ac2d2zYtMP18Fb x/N/Wl5HYq1Lx0FtroYlfLJDAPCVOJZH7jyteLca5tQk8nqfnhkfXxaD2mOMdActFLJZ AmhQXgp/QVAmWO71d3CPTwR+g45BypjwCKu2w5sWJlB8W6MuIJoVQbQ/DtQjcNpyZirg OkiA== X-Forwarded-Encrypted: i=1; AJvYcCXaKmp8IQiLDEk0ROy4/JWpkCPImCyVgX+SK8d9wQ2algrZL5mZWHJ5P6Vp6OKvN+iXkIYc898vGHYryiUIOWKQQZBBJ6+cYQ== X-Gm-Message-State: AOJu0YxPpTQ4Y3dOVfm1pkNJmaMHLmXkhyOxmhkMK6c0aIPjebLmk8WB t0WHOxBlY6Sy6HYMVv+Uk9NoaJPCnjjFgBbRqheWsi5jPUK9uOawRi7dp5eeLfpHrDrlVaKln0U fQymPDfVCYc4tDtsmLykcr7qN0t/orWoSZq8dKD8rN7H/iLOTLy/bsdM= X-Received: by 2002:ac8:7fcf:0:b0:439:eb3f:6c37 with SMTP id b15-20020ac87fcf000000b00439eb3f6c37mr506645qtk.55.1714070331687; Thu, 25 Apr 2024 11:38:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF0YXC7J/kU1qBQUKkHz9kh3DF8I/A0ZFy+a8n/Xe4j67tzNik7jSRM6Esh9HY0vqFg3LKxEw== X-Received: by 2002:ac8:7fcf:0:b0:439:eb3f:6c37 with SMTP id b15-20020ac87fcf000000b00439eb3f6c37mr506619qtk.55.1714070331265; Thu, 25 Apr 2024 11:38:51 -0700 (PDT) Received: from [192.168.1.130] (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id l23-20020ac848d7000000b004372459413fsm7150960qtr.93.2024.04.25.11.38.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 25 Apr 2024 11:38:50 -0700 (PDT) Message-ID: <45e52f2a-113c-4e7f-a491-5fff76041d17@redhat.com> Date: Thu, 25 Apr 2024 14:38:49 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] c++, v2: Fix constexpr evaluation of parameters passed by invisible reference [PR111284] To: Jakub Jelinek , gcc-patches@gcc.gnu.org References: From: Jason Merrill In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-5.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,RCVD_IN_SORBS_WEB,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 List-Id: On 4/23/24 08:52, Jakub Jelinek wrote: > On Mon, Apr 15, 2024 at 02:19:36PM +0200, Jakub Jelinek wrote: >> They weren't the same, one was trying to evaluate the convert_from_reference >> with vc_glvalue, the other evaluates it without that and with vc_prvalue. >> Now, I guess the >> + /* Undo convert_for_arg_passing work here. */ >> + if (TYPE_REF_P (TREE_TYPE (x)) >> + && !same_type_p (type, TREE_TYPE (TREE_TYPE (x)))) >> + x = cp_fold_convert (build_reference_type (type), x); >> part could be thrown away, given the other !same_type_p check (that one is >> needed because adjust_temp_type can't deal with that), at least >> when I remove that >> GXX_TESTSUITE_STDS=98,11,14,17,20,23,26 make check-g++ RUNTESTFLAGS="dg.exp='constexpr-dtor* pr114426.C constexpr-111284.C constexpr-lifetime7.C'" >> still passes. > > I've now tested that version and it passed bootstrap/regtest on x86_64-linux > and i686-linux. But as I said earlier, trying to tweak the patch further > doesn't work on the constexpr-dtor{5,6}.C tests. OK. > 2024-04-23 Jakub Jelinek > > PR c++/111284 > * constexpr.cc (cxx_bind_parameters_in_call): For PARM_DECLs with > TREE_ADDRESSABLE types use vc_glvalue rather than vc_prvalue for > cxx_eval_constant_expression and if it doesn't have the same > type as it should, cast the reference type to reference to type > before convert_from_reference and instead of adjust_temp_type > take address of the arg, cast to reference to type and then > convert_from_reference. > (cxx_eval_constant_expression) : For lval case > on parameters with TREE_ADDRESSABLE types lookup result in > ctx->globals if possible. Otherwise if lookup in ctx->globals > was successful for parameter with TREE_ADDRESSABLE type, > recurse with vc_prvalue on the returned value. > > * g++.dg/cpp1z/constexpr-111284.C: New test. > * g++.dg/cpp1y/constexpr-lifetime7.C: Expect one error on a different > line. > > --- gcc/cp/constexpr.cc.jj 2024-02-13 10:29:57.979155641 +0100 > +++ gcc/cp/constexpr.cc 2024-03-07 19:35:01.032412221 +0100 > @@ -1877,13 +1877,18 @@ cxx_bind_parameters_in_call (const const > x = build_address (x); > } > if (TREE_ADDRESSABLE (type)) > - /* Undo convert_for_arg_passing work here. */ > - x = convert_from_reference (x); > - /* Normally we would strip a TARGET_EXPR in an initialization context > - such as this, but here we do the elision differently: we keep the > - TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */ > - arg = cxx_eval_constant_expression (ctx, x, vc_prvalue, > - non_constant_p, overflow_p); > + { > + /* Undo convert_for_arg_passing work here. */ > + x = convert_from_reference (x); > + arg = cxx_eval_constant_expression (ctx, x, vc_glvalue, > + non_constant_p, overflow_p); > + } > + else > + /* Normally we would strip a TARGET_EXPR in an initialization context > + such as this, but here we do the elision differently: we keep the > + TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */ > + arg = cxx_eval_constant_expression (ctx, x, vc_prvalue, > + non_constant_p, overflow_p); > /* Check we aren't dereferencing a null pointer when calling a non-static > member function, which is undefined behaviour. */ > if (i == 0 && DECL_OBJECT_MEMBER_FUNCTION_P (fun) > @@ -1909,7 +1914,16 @@ cxx_bind_parameters_in_call (const const > { > /* Make sure the binding has the same type as the parm. But > only for constant args. */ > - if (!TYPE_REF_P (type)) > + if (TREE_ADDRESSABLE (type)) > + { > + if (!same_type_p (type, TREE_TYPE (arg))) > + { > + arg = build_fold_addr_expr (arg); > + arg = cp_fold_convert (build_reference_type (type), arg); > + arg = convert_from_reference (arg); > + } > + } > + else if (!TYPE_REF_P (type)) > arg = adjust_temp_type (type, arg); > if (!TREE_CONSTANT (arg)) > *non_constant_args = true; > @@ -7499,9 +7513,19 @@ cxx_eval_constant_expression (const cons > > case PARM_DECL: > if (lval && !TYPE_REF_P (TREE_TYPE (t))) > - /* glvalue use. */; > + { > + /* glvalue use. */ > + if (TREE_ADDRESSABLE (TREE_TYPE (t))) > + if (tree v = ctx->global->get_value (t)) > + r = v; > + } > else if (tree v = ctx->global->get_value (t)) > - r = v; > + { > + r = v; > + if (TREE_ADDRESSABLE (TREE_TYPE (t))) > + r = cxx_eval_constant_expression (ctx, r, vc_prvalue, > + non_constant_p, overflow_p); > + } > else if (lval) > /* Defer in case this is only used for its type. */; > else if (ctx->global->is_outside_lifetime (t)) > --- gcc/testsuite/g++.dg/cpp1z/constexpr-111284.C.jj 2024-03-07 16:27:48.113651999 +0100 > +++ gcc/testsuite/g++.dg/cpp1z/constexpr-111284.C 2024-03-07 16:26:49.565466606 +0100 > @@ -0,0 +1,19 @@ > +// PR c++/111284 > +// { dg-do compile { target c++17 } } > + > +struct S { > + S () = default; > + constexpr S (const S &) noexcept : s{this} {} > + constexpr S & operator= (const S &) noexcept { return *this; } > + constexpr bool foo () const noexcept { return s == this; } > + S *s = this; > +}; > + > +constexpr bool > +bar (S x) noexcept > +{ > + return x.foo (); > +} > + > +static_assert (bar (S {}), ""); > +static_assert ([] (S x) { return x.foo (); } (S {}), ""); > --- gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime7.C.jj 2023-12-13 19:09:33.252657826 +0100 > +++ gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime7.C 2024-03-07 19:49:49.342334090 +0100 > @@ -87,7 +87,7 @@ constexpr bool n1 = test_access constexpr bool n2 = test_modification(); // { dg-message "in .constexpr." "" { target c++20 } } > constexpr bool n3 = test_scope(); // { dg-message "in .constexpr." "" { target c++20 } } > constexpr bool n4 = test_destroy_temp(); // { dg-message "in .constexpr." "" { target c++20 } } > -constexpr bool n5 = test_parameter(NonTrivial{}); // { dg-error "destroying" "" { target c++20 } } > +constexpr bool n5 = test_parameter(NonTrivial{}); // { dg-message "in .constexpr." "" { target c++20 } } > constexpr bool n6 = test_bindings(); > #endif > > > > Jakub >