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.129.124]) by sourceware.org (Postfix) with ESMTPS id 369693858417 for ; Tue, 23 Apr 2024 15:53:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 369693858417 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 369693858417 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713887589; cv=none; b=IiVsdp/DX9y8x+4A+yI/7BNvx7vSazsuH+PNwUANOvBxAarCA4gxudRKSGtfMQjtrjUUZ/5RuL54MxVRGDzaQGqbQF4qvAwKjXjNg9PL5/yreXnorjgz1hPoV36xmEnVff8DjiT/lnKDLX7FfjesB7Mk787EyuB1H/Hxk/DVxLA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713887589; c=relaxed/simple; bh=Low5ZFJXInEOE1jq7VJY/ddLR7kLn82gSUYXup57IEw=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=Mh66OqOucO+aZWMfOou7GC1B3rkXGM7rDq4m+lXD58jBTX1YgTNzjz0qjvGA+98f4fFL4mL/W4s5fApxBipZd5UQtReVB82SNq+4RgaR9h51BpBDI46GDJh7yDbSF6+2tQzWSrsnb24rYqRTO8anUgBVvDyyc54J1SCPMV2kx+Q= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713887582; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references; bh=URmplgAJNnYTuEzJpmMJCKlNed3VI3Y8U+6CdjmJwpM=; b=JFv4ZYysLIpguUu4ScZf50Xwaf1Rwwt4jAcyJuhZ5XTxiShvzY1x+Jnn71oNtPQcCASQp6 825qPXcBKhSJXvtPBqIaZ4wO50TJLoiRVVbFwMYdfuY/Trg7+MDHlNeP38DKM+/0Hk7WgA v0gX8BORr34W/IXoVVeMm7uPA/aMt+4= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-47-Ul4TUkGnOe6Gzi64Zi7nug-1; Tue, 23 Apr 2024 11:52:59 -0400 X-MC-Unique: Ul4TUkGnOe6Gzi64Zi7nug-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A6C79385A182 for ; Tue, 23 Apr 2024 15:52:59 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.5]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 688221C060D0; Tue, 23 Apr 2024 15:52:59 +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 43NFqvOh932856 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 23 Apr 2024 17:52:57 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 43NFqviZ932855; Tue, 23 Apr 2024 17:52:57 +0200 Date: Tue, 23 Apr 2024 17:52:57 +0200 From: Jakub Jelinek To: Jason Merrill , gcc-patches@gcc.gnu.org Subject: [PATCH] c++, v2: Fix constexpr evaluation of parameters passed by invisible reference [PR111284] Message-ID: Reply-To: Jakub Jelinek References: MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-4.0 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,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 List-Id: 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. 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(); // { 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