]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++/modules: Allow imported references in constant expressions
authorNathaniel Shead <nathanieloshead@gmail.com>
Thu, 12 Sep 2024 10:06:39 +0000 (20:06 +1000)
committerNathaniel Shead <nathanieloshead@gmail.com>
Fri, 27 Sep 2024 03:49:23 +0000 (13:49 +1000)
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.

As such this patch uses the existing decl_maybe_constant_var function
which correctly handles this case.

gcc/cp/ChangeLog:

* module.cc (has_definition): Use decl_maybe_constant_var
instead of TREE_CONSTANT.

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>
Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/module.cc
gcc/testsuite/g++.dg/modules/cexpr-5_a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/cexpr-5_b.C [new file with mode: 0644]

index f5df9e875d3af6383ec93c12cf28db02f790f30c..65b37b4b5544e60ab33ee6082f48c5776120a8e7 100644 (file)
@@ -11829,7 +11829,7 @@ has_definition (tree decl)
               since there's no TU to emit them in otherwise.  */
            return true;
 
-         if (!TREE_CONSTANT (decl))
+         if (!decl_maybe_constant_var_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 (file)
index 0000000..3a9f005
--- /dev/null
@@ -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 (file)
index 0000000..4b1b901
--- /dev/null
@@ -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);