From patchwork Thu Oct 1 19:55:18 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 145131 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5B8234BB3B8F for ; Thu, 1 Oct 2026 19:56:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5B8234BB3B8F Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=ihmDJxkR 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 ESMTP id 2E9114BB24CC for ; Thu, 1 Oct 2026 19:55:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2E9114BB24CC Authentication-Results: sourceware.org; dmarc=pass (p=quarantine 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 2E9114BB24CC Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790884533; cv=none; b=YKqt7I1L7vBcbTI7wtat4B/VC29JQdMRb4Ht2A+wkJMNL/lrsB0USwkBvwW8IZ1j6PAYjEnfKpB7bVvn50xXm5ifNw4aCqe7hLs+9P0exfcVOpGc58Z75s7j/EF9DfSRVU/ral4yBHKhdY5VX8cEWEuU2kV9wD9XLfSvGdpTFhg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790884533; c=relaxed/simple; bh=g7hINsAV9aCUle/v9D+8Cw3KDBbinDRbxGopfsBRJqs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=fl09Z025sRfy1iVgGJXZb6II4fdDLYN1NJYcPKhPfZDc68bKPxuMQ/lyELjdq0XcbcUcHgQdpFiutusJyUhAtlueJVyLIXTgEwroodctUZ7imeGNffQpo9QtokYhbPEjy2hltALN76k7J2KCknyOZ2ZDVDYrNc/mo60M3/igjEY= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=ihmDJxkR DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2E9114BB24CC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1790884532; 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; bh=E8j3Bd2UFY2Z6hCa+QKOyQJCoFYcjxWXBKQALXrQExU=; b=ihmDJxkReXi67XtqVLko8m6SkiI59wuWz8NHd7KJpDaCjLMcuh6NL3npA34hi5vXzZ1lTM ffhydFCDTXkkgbzTpatjbMtQhq6lZkeWbl/N0BA9kXaYe8hYSr/0bv0ZTlZdL8Jo1KotGt BgYNlfUveZ7/E1SlcymcPO9tb8Yt/Tk= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-495-RTcggqGiNsWJJ0SP2RO48g-1; Thu, 01 Oct 2026 15:55:26 -0400 X-MC-Unique: RTcggqGiNsWJJ0SP2RO48g-1 X-Mimecast-MFC-AGG-ID: RTcggqGiNsWJJ0SP2RO48g_1790884525 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 53CAB18E7303 for ; Thu, 1 Oct 2026 19:55:25 +0000 (UTC) Received: from mpolacek-thinkpadt14gen6.redhat.corp (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id D598B1956089; Thu, 1 Oct 2026 19:55:24 +0000 (UTC) From: Marek Polacek To: GCC Patches , Jason Merrill Subject: [PATCH] c++/reflection: fix extract-ref [PR127232] Date: Thu, 1 Oct 2026 15:55:18 -0400 Message-ID: <20261001195518.1251097-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: t_pH_qcW_e3hvCmvrQYaobjQxVXhtuAqKnkj4YKznp4_1790884525 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.0 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_PASS, SPF_NONE, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- A couple of problems here: [meta.reflection.extract]/5.3 says "If r represents a variable, then either that variable is usable in constant expressions or its lifetime began within the core constant expression currently under evaluation." but I was wrongly using is_constant_expression. That led to a crash because we passed a type to build_address. For "is usable..." we want decl_constant_var_p and for the "its lifetime began..." I'm adding a new function. Using cxx_eval_constant_expression for non-reference variables wouldn't work since evaluating a variable as a glvalue always succeeds, and as a prvalue fails for e.g. an uninitialized variable whose lifetime did begin. With this patch, a variable that isn't usable in constant expressions throws. Thus the extract2.C and extract3.C changes; one can use object_of to get the object instead. Also, DECL_INITIAL of a PARM_DECL is not an initializer but DECL_ARG_TYPE. So we need cxx_eval_constant_expression to handle e.g.: int g; consteval void h (const int &p) { std::meta::extract (^^p); } consteval { h (g); } where we need to get "(const int &) &g" from PARM_DECL p. This adds a new XFAIL because we don't have P2686 yet. It seems that object_of will need similar tweaks but that's for another patch. PR c++/127232 gcc/cp/ChangeLog: * constexpr.cc (constexpr_global_ctx::within_lifetime_p): New. (cxx_constexpr_in_eval_lifetime_p): New. * cp-tree.h (cxx_constexpr_in_eval_lifetime_p): Declare. * reflect.cc (extract_ref): Check decl_constant_var_p and cxx_constexpr_in_eval_lifetime_p instead of is_constant_expression. Use cxx_eval_constant_expression instead of DECL_INITIAL for references. Adjust the checking assert. gcc/testsuite/ChangeLog: * g++.dg/reflect/extract2.C: Adjust dg-error. * g++.dg/reflect/extract3.C: Add dg-error. Add tests using object_of. * g++.dg/reflect/extract13.C: New test. * g++.dg/reflect/extract14.C: New test. --- gcc/cp/constexpr.cc | 20 +++++++- gcc/cp/cp-tree.h | 1 + gcc/cp/reflect.cc | 15 ++++-- gcc/testsuite/g++.dg/reflect/extract13.C | 59 ++++++++++++++++++++++++ gcc/testsuite/g++.dg/reflect/extract14.C | 23 +++++++++ gcc/testsuite/g++.dg/reflect/extract2.C | 2 +- gcc/testsuite/g++.dg/reflect/extract3.C | 21 ++++++--- 7 files changed, 129 insertions(+), 12 deletions(-) create mode 100644 gcc/testsuite/g++.dg/reflect/extract13.C create mode 100644 gcc/testsuite/g++.dg/reflect/extract14.C base-commit: b24418a22128668b21179dfcb73fa54ee3929ecf diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 31ef5bf1111..cf31c803be6 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -1247,7 +1247,16 @@ public: return true; return false; } - tree get_value (tree t) + /* This differs from !is_outside_lifetime in that it returns false if + T is not in the map. */ + bool within_lifetime_p (tree t) + { + if (tree *p = values.get (t)) + if (*p != void_node && *p != void_list_node) + return true; + return false; + } + tree get_value (tree t) { if (tree *p = values.get (t)) if (*p != void_node && *p != void_list_node) @@ -1404,6 +1413,15 @@ cxx_constexpr_consteval_block (const constexpr_ctx *ctx) return ctx->global->consteval_block; } +/* Return true if T's lifetime began within the core constant expression + currently under evaluation. */ + +bool +cxx_constexpr_in_eval_lifetime_p (const constexpr_ctx *ctx, tree t) +{ + return ctx->global->within_lifetime_p (t); +} + /* Predicates for the meaning of *jump_target. */ static bool diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 947bf62ff98..83a287471a5 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -9486,6 +9486,7 @@ extern bool cxx_constexpr_quiet_p (const constexpr_ctx *); extern mce_value cxx_constexpr_manifestly_const_eval (const constexpr_ctx *); extern tree cxx_constexpr_caller (const constexpr_ctx *); extern tree cxx_constexpr_consteval_block (const constexpr_ctx *); +extern bool cxx_constexpr_in_eval_lifetime_p (const constexpr_ctx *, tree); /* An RAII sentinel used to restrict constexpr evaluation so that it doesn't do anything that causes extra DECL_UID generation. */ diff --git a/gcc/cp/reflect.cc b/gcc/cp/reflect.cc index 8bf518df64f..8a4c5490bbf 100644 --- a/gcc/cp/reflect.cc +++ b/gcc/cp/reflect.cc @@ -7686,18 +7686,27 @@ extract_ref (location_t loc, const constexpr_ctx *ctx, tree T, tree r, if (adju != error_mark_node && adjt != error_mark_node && is_convertible (adju, adjt) - && (!var_p || is_constant_expression (r))) + && (!var_p + || decl_constant_var_p (r) + || cxx_constexpr_in_eval_lifetime_p (ctx, r))) { if (TYPE_REF_P (TREE_TYPE (r))) { - r = DECL_INITIAL (r); + bool nc = false, ovf = false; + tree jt = NULL_TREE; + r = cxx_eval_constant_expression (ctx, r, vc_prvalue, + &nc, &ovf, &jt); + if (nc || jt) + goto throw_; r = maybe_get_reference_referent (r); - gcc_checking_assert (!TYPE_REF_P (TREE_TYPE (r))); + gcc_checking_assert (r && !TYPE_P (r) + && !TYPE_REF_P (TREE_TYPE (r))); } return build_address (r); } } +throw_: return throw_exception (loc, ctx, "value cannot be extracted", fun, non_constant_p, jump_target); } diff --git a/gcc/testsuite/g++.dg/reflect/extract13.C b/gcc/testsuite/g++.dg/reflect/extract13.C new file mode 100644 index 00000000000..8b9ecdd10bf --- /dev/null +++ b/gcc/testsuite/g++.dg/reflect/extract13.C @@ -0,0 +1,59 @@ +// PR c++/127232 +// { dg-do compile { target c++26 } } +// { dg-additional-options "-freflection" } + +#include + +void +foo (int const& x) +{ + constexpr std::meta::info y = ^^x; + constexpr auto e = extract(y); // { dg-error "value cannot be extracted" } +} + +consteval bool +throws_p (std::meta::info r) +{ + try { extract (r); } + catch (std::meta::exception &) { return true; } + return false; +} + +int g; +constexpr const int &cr = g; + +void +f (const int &x, int y) +{ + static_assert (throws_p (^^x)); + static_assert (throws_p (^^y)); + const int &lx = x; + static_assert (throws_p (^^lx)); + int &lg = g; + static_assert (!throws_p (^^lg)); + static_assert (throws_p (^^g)); + int a = 42; + int &la = a; + static_assert (!throws_p (^^la)); // { dg-bogus "" "P2686" { xfail *-*-* } } +} + +consteval bool +lifetime () +{ + int i = 0; + int &ri = i; + return &extract (^^ri) == &i; +} +static_assert (lifetime ()); + +consteval void +h (const int &p) +{ + int i = 42; + const int &ri = i; + extract (^^i); + extract (^^ri); + extract (^^p); +} + +consteval { h (g); } diff --git a/gcc/testsuite/g++.dg/reflect/extract14.C b/gcc/testsuite/g++.dg/reflect/extract14.C new file mode 100644 index 00000000000..ede1a5b3c92 --- /dev/null +++ b/gcc/testsuite/g++.dg/reflect/extract14.C @@ -0,0 +1,23 @@ +// PR c++/127232 +// { dg-do compile { target c++26 } } +// { dg-additional-options "-freflection" } + +#include + +// Check that reflections of locals/parameters refer to the current frame in +// recursive calls. +consteval bool +foo (int n, int *outer) +{ + int i = n; + if (&std::meta::extract (^^i) != &i + || &std::meta::extract (^^n) != &n) + return false; + if (outer && &std::meta::extract (^^i) == outer) + return false; + int &ri = i; + if (&std::meta::extract (^^ri) != &i) + return false; + return n == 0 || foo (n - 1, &i); +} +static_assert (foo (2, nullptr)); diff --git a/gcc/testsuite/g++.dg/reflect/extract2.C b/gcc/testsuite/g++.dg/reflect/extract2.C index 9de23d1e931..2d5f758ea96 100644 --- a/gcc/testsuite/g++.dg/reflect/extract2.C +++ b/gcc/testsuite/g++.dg/reflect/extract2.C @@ -93,7 +93,7 @@ static_assert (!can_extract(reflect_constant (i))); static_assert (!can_extract(^^i)); int j; -static_assert (extract(^^j)); // { dg-error "non-constant|not usable" } +constexpr auto e = extract(^^j); // { dg-error "cannot be extracted" } consteval info bar () diff --git a/gcc/testsuite/g++.dg/reflect/extract3.C b/gcc/testsuite/g++.dg/reflect/extract3.C index f8c173c0208..42e06fac342 100644 --- a/gcc/testsuite/g++.dg/reflect/extract3.C +++ b/gcc/testsuite/g++.dg/reflect/extract3.C @@ -33,12 +33,16 @@ static_assert (extract(^^rj) == j); static_assert (&extract(^^rj) == &j); int k = 1; -static_assert (&extract(^^k) == &k); -static_assert (&extract(^^k) == &k); +constexpr auto e1 = extract(^^k); // { dg-error "cannot be extracted" } +constexpr auto e2 = extract(^^k); // { dg-error "cannot be extracted" } +static_assert (&extract (object_of (^^k)) == &k); +static_assert (&extract (object_of (^^k)) == &k); int arr[5] {}; -static_assert (&extract(^^arr) == &arr); -static_assert (&extract(^^arr) == &arr); +constexpr auto e3 = extract(^^arr); // { dg-error "cannot be extracted" } +constexpr auto e4 = extract(^^arr); // { dg-error "cannot be extracted" } +static_assert (&extract(object_of (^^arr)) == &arr); +static_assert (&extract(object_of (^^arr)) == &arr); struct S { int m; }; constexpr S s{42}; @@ -54,9 +58,12 @@ same_type(reflect_constant (S{42}))), const S &> st7 same_type(reflect_constant (S{42}))), const S *> st8; S sr{42}; -static_assert (&extract(^^sr) == &sr); -static_assert (&extract(^^sr) == &sr); -static_assert (&extract(^^sr).m == &sr.m); +constexpr auto e5 = extract(^^sr); // { dg-error "cannot be extracted" } +constexpr auto e6 = extract(^^sr); // { dg-error "cannot be extracted" } +constexpr auto e7 = extract(^^sr).m; // { dg-error "cannot be extracted" } +static_assert (&extract(object_of (^^sr)) == &sr); +static_assert (&extract(object_of (^^sr)) == &sr); +static_assert (&extract(object_of (^^sr)).m == &sr.m); int &srm = sr.m; static_assert (&extract(^^srm) == &sr.m);