diff mbox series

[05/10] c++/modules: Allow imported references in constant expressions

Message ID 66f1fd8e.170a0220.2e1c55.07f2@mx.google.com
State New
Headers show
Series c++/modules: Implement P1815 "Translation-unit-local entities" | expand

Commit Message

Nathaniel Shead Sept. 23, 2024, 11:45 p.m. UTC
Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk?

-- >8 --

Currently the streaming code uses TREE_CONSTANT to determine whether an
entity will have a definition that is interesting to stream out.  This
is not sufficient, however; we also need to write the definition of
references, since although not TREE_CONSTANT they can still be usable in
constant expressions.

gcc/cp/ChangeLog:

	* module.cc (has_definition): Also write definition of
	references initialized with a constant expression.

gcc/testsuite/ChangeLog:

	* g++.dg/modules/cexpr-5_a.C: New test.
	* g++.dg/modules/cexpr-5_b.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
---
 gcc/cp/module.cc                         |  6 +++++-
 gcc/testsuite/g++.dg/modules/cexpr-5_a.C | 13 +++++++++++++
 gcc/testsuite/g++.dg/modules/cexpr-5_b.C |  9 +++++++++
 3 files changed, 27 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/modules/cexpr-5_a.C
 create mode 100644 gcc/testsuite/g++.dg/modules/cexpr-5_b.C

Comments

Jason Merrill Sept. 26, 2024, 9:35 p.m. UTC | #1
On 9/23/24 7:45 PM, Nathaniel Shead wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk?
> 
> -- >8 --
> 
> Currently the streaming code uses TREE_CONSTANT to determine whether an
> entity will have a definition that is interesting to stream out.  This
> is not sufficient, however; we also need to write the definition of
> references, since although not TREE_CONSTANT they can still be usable in
> constant expressions.
> 
> gcc/cp/ChangeLog:
> 
> 	* module.cc (has_definition): Also write definition of
> 	references initialized with a constant expression.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/modules/cexpr-5_a.C: New test.
> 	* g++.dg/modules/cexpr-5_b.C: New test.
> 
> Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
> ---
>   gcc/cp/module.cc                         |  6 +++++-
>   gcc/testsuite/g++.dg/modules/cexpr-5_a.C | 13 +++++++++++++
>   gcc/testsuite/g++.dg/modules/cexpr-5_b.C |  9 +++++++++
>   3 files changed, 27 insertions(+), 1 deletion(-)
>   create mode 100644 gcc/testsuite/g++.dg/modules/cexpr-5_a.C
>   create mode 100644 gcc/testsuite/g++.dg/modules/cexpr-5_b.C
> 
> diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
> index f5df9e875d3..7589de2348d 100644
> --- a/gcc/cp/module.cc
> +++ b/gcc/cp/module.cc
> @@ -11829,7 +11829,11 @@ has_definition (tree decl)
>   	       since there's no TU to emit them in otherwise.  */
>   	    return true;
>   
> -	  if (!TREE_CONSTANT (decl))
> +	  if (!TREE_CONSTANT (decl)
> +	      /* A reference is never TREE_CONSTANT, but still stream its
> +		 definition if it's usable in constant expressions.  */
> +	      && !(TYPE_REF_P (TREE_TYPE (decl))
> +		   && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)))

Maybe this wants decl_{maybe_,}constant_var_p?  OK either way.

Jason
diff mbox series

Patch

diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index f5df9e875d3..7589de2348d 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -11829,7 +11829,11 @@  has_definition (tree decl)
 	       since there's no TU to emit them in otherwise.  */
 	    return true;
 
-	  if (!TREE_CONSTANT (decl))
+	  if (!TREE_CONSTANT (decl)
+	      /* A reference is never TREE_CONSTANT, but still stream its
+		 definition if it's usable in constant expressions.  */
+	      && !(TYPE_REF_P (TREE_TYPE (decl))
+		   && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)))
 	    return false;
 
 	  return true;
diff --git a/gcc/testsuite/g++.dg/modules/cexpr-5_a.C b/gcc/testsuite/g++.dg/modules/cexpr-5_a.C
new file mode 100644
index 00000000000..3a9f00523f6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/cexpr-5_a.C
@@ -0,0 +1,13 @@ 
+// { dg-additional-options "-fmodules-ts" }
+// { dg-module-cmi M }
+
+export module M;
+
+int x = 123;
+void f() {}
+
+int& xr = x;
+auto& fr = f;
+
+constexpr int& cxr = xr;
+constexpr auto& cfr = fr;
diff --git a/gcc/testsuite/g++.dg/modules/cexpr-5_b.C b/gcc/testsuite/g++.dg/modules/cexpr-5_b.C
new file mode 100644
index 00000000000..4b1b901104b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/cexpr-5_b.C
@@ -0,0 +1,9 @@ 
+// { dg-additional-options "-fmodules-ts" }
+
+module M;
+
+constexpr auto& use_xr = xr;
+constexpr auto& use_fr = fr;
+
+static_assert(&cxr == &use_xr);
+static_assert(&cfr == &use_fr);