aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorNathaniel Shead <nathanieloshead@gmail.com>2025-04-25 22:45:41 +1000
committerNathaniel Shead <nathanieloshead@gmail.com>2025-05-01 13:38:01 +1000
commitaa49bb9deb7e040679f184009f38c5027f1a8dc4 (patch)
tree36eddfb2b9ca90e01e9e4a5868e7a26861cd3665 /gcc
parent3042862fbdba809473e3ea4ddc1697692b233d5f (diff)
downloadgcc-aa49bb9deb7e040679f184009f38c5027f1a8dc4.zip
gcc-aa49bb9deb7e040679f184009f38c5027f1a8dc4.tar.gz
gcc-aa49bb9deb7e040679f184009f38c5027f1a8dc4.tar.bz2
c++/modules: Fix imported CNTTPs being considered non-constant [PR119938]
When importing a CNTTP object, since r15-3031-g0b7904e274fbd6 we shortcut the processing of the generated NTTP so that we don't attempt to recursively load pendings. However, due to an oversight we do not properly set TREE_CONSTANT or DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P on the decl, which confuses later processing. This patch ensures that this happens correctly. PR c++/119938 gcc/cp/ChangeLog: * pt.cc (get_template_parm_object): When !check_init, add assert that expr really is constant and mark decl as such. gcc/testsuite/ChangeLog: * g++.dg/modules/tpl-nttp-2_a.H: New test. * g++.dg/modules/tpl-nttp-2_b.C: New test. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com> Reviewed-by: Jason Merrill <jason@redhat.com> (cherry picked from commit d613678c94f06809656e56b37f314501b37a5ddd)
Diffstat (limited to 'gcc')
-rw-r--r--gcc/cp/pt.cc7
-rw-r--r--gcc/testsuite/g++.dg/modules/tpl-nttp-2_a.H14
-rw-r--r--gcc/testsuite/g++.dg/modules/tpl-nttp-2_b.C10
3 files changed, 30 insertions, 1 deletions
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 1f972c7..96230b4 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -7492,8 +7492,13 @@ get_template_parm_object (tree expr, tree name, bool check_init/*=true*/)
{
/* The EXPR is the already processed initializer, set it on the NTTP
object now so that cp_finish_decl doesn't do it again later. */
+ gcc_checking_assert (reduced_constant_expression_p (expr));
DECL_INITIAL (decl) = expr;
- DECL_INITIALIZED_P (decl) = 1;
+ DECL_INITIALIZED_P (decl) = true;
+ DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
+ /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
+ if (!TYPE_REF_P (type))
+ TREE_CONSTANT (decl) = true;
}
pushdecl_top_level_and_finish (decl, expr);
diff --git a/gcc/testsuite/g++.dg/modules/tpl-nttp-2_a.H b/gcc/testsuite/g++.dg/modules/tpl-nttp-2_a.H
new file mode 100644
index 0000000..bfae11c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/tpl-nttp-2_a.H
@@ -0,0 +1,14 @@
+// PR c++/119938
+// { dg-additional-options "-fmodules -std=c++20" }
+// { dg-module-cmi {} }
+
+struct A { int x; };
+
+template <A a> struct B { static_assert(a.x == 1); };
+using C = B<A{1}>;
+
+template <A a> void D() { static_assert(a.x == 2); };
+inline void E() { D<A{2}>(); }
+
+template <A a> struct F { static constexpr int result = a.x; };
+template <int=0> constexpr int G() { return F<A{3}>::result; };
diff --git a/gcc/testsuite/g++.dg/modules/tpl-nttp-2_b.C b/gcc/testsuite/g++.dg/modules/tpl-nttp-2_b.C
new file mode 100644
index 0000000..7e661cb
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/tpl-nttp-2_b.C
@@ -0,0 +1,10 @@
+// PR c++/119938
+// { dg-additional-options "-fmodules -std=c++20" }
+
+import "tpl-nttp-2_a.H";
+
+int main() {
+ C c;
+ E();
+ static_assert(G() == 3);
+}