From f7f6c15c54c312cc2e08171e7881c554995d742b Mon Sep 17 00:00:00 2001 From: Gareth Williamson Date: Mon, 30 Nov 2026 17:13:41 +0100 Subject: [PATCH] Fix deferred instantiations during constexpr template instantiation [GH #621] --- ...h-623-constexpr-deferred-instantiation.txt | 22 ++++++++++ src/templates.c | 40 ++++++++++++++++++- .../default.1.1.txt | 1 + .../default.2.1.txt | 1 + .../default.3.1.txt | 1 + .../default.1.1.txt | 1 + .../default.2.1.txt | 1 + .../default.3.1.txt | 1 + ...pr-deferred-instantiation-function.sft.cpp | 12 ++++++ ...pr-deferred-instantiation-variable.sft.cpp | 12 ++++++ 10 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/pending_changes/gh-623-constexpr-deferred-instantiation.txt create mode 100644 tests/tests/regressions/.constexpr-deferred-instantiation-function.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.constexpr-deferred-instantiation-function.rto/default.2.1.txt create mode 100644 tests/tests/regressions/.constexpr-deferred-instantiation-function.rto/default.3.1.txt create mode 100644 tests/tests/regressions/.constexpr-deferred-instantiation-variable.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.constexpr-deferred-instantiation-variable.rto/default.2.1.txt create mode 100644 tests/tests/regressions/.constexpr-deferred-instantiation-variable.rto/default.3.1.txt create mode 100644 tests/tests/regressions/constexpr-deferred-instantiation-function.sft.cpp create mode 100644 tests/tests/regressions/constexpr-deferred-instantiation-variable.sft.cpp diff --git a/src/pending_changes/gh-623-constexpr-deferred-instantiation.txt b/src/pending_changes/gh-623-constexpr-deferred-instantiation.txt new file mode 100644 index 0000000000..3ce9d775c0 --- /dev/null +++ b/src/pending_changes/gh-623-constexpr-deferred-instantiation.txt @@ -0,0 +1,22 @@ +[GH #623] +Deferred instantiations during constexpr template instantiation + +Previously, instantiations that had been deferred (for example, that of a +constexpr destructor needed by a default argument) could be performed in the +middle of instantiating a constexpr function template instance or a variable +template instance, such as when that instantiation caused a class template to +be instantiated. If a deferred instantiation constant-evaluated the instance +whose definition was not yet complete, a spurious error was issued. Such +instantiations are now deferred until the instance is complete. For example, +with --c++20: + + template struct A {}; + template constexpr int f() { return sizeof(A); } + template struct B { + constexpr ~B() { static_assert(f()); } // Previously an error. + }; + struct S { + void g(B = {}) { f(); } + }; + +The same error was issued if f was replaced by a variable template. diff --git a/src/templates.c b/src/templates.c index d10ff1057d..ca52b25575 100644 --- a/src/templates.c +++ b/src/templates.c @@ -444,6 +444,16 @@ STATIC_THREAD a_boolean not a function static so that it can be reset if a compilation is terminated abnormally. */ +STATIC_THREAD int + constexpr_instantiations_in_progress; + /* The number of instantiations of constexpr + function templates and of variable templates (or + static data members of class templates) that are + in progress. While it is nonzero, deferred + instantiation requests are not processed, because + they may constant-evaluate an entity whose + definition is not yet complete. */ + STATIC_THREAD unsigned long num_total_pending_instantiations; /* The number of function instantiations that are @@ -7635,6 +7645,15 @@ Instantiate the body of the template function associated with tip. */ { a_routine_ptr routine = tip->instance_sym->variant.routine.ptr; + a_boolean is_constexpr = routine->is_constexpr; + + if (is_constexpr) { + /* Deferred instantiation requests may constant-evaluate this function, + so they must wait until its body is complete. New requests are not + affected: a function called from this one is still instantiated + immediately, so that consteval escalation (P2564R3) can see it. */ + constexpr_instantiations_in_progress++; + } /* if */ #if GNU_FUNCTION_MULTIVERSIONING /* For a GNU multiversion target-versioned function, instantiate all of the @@ -7658,6 +7677,13 @@ Instantiate the body of the template function associated with tip. { instantiate_template_function_full(tip, routine); } /* if */ + if (is_constexpr) { + constexpr_instantiations_in_progress--; + if (constexpr_instantiations_in_progress == 0 && + defer_instantiations == 0) { + process_deferred_instantiation_requests(); + } /* if */ + } /* if */ } /* instantiate_template_function */ #if ONE_INSTANTIATION_PER_OBJECT @@ -7730,6 +7756,10 @@ expression context) rather than a declaration. int idx = 0; a_host_large_integer intrinsic_val; + /* Deferred instantiation requests may use the value of this variable in a + constant expression, so they must wait until its initializer is + complete. */ + constexpr_instantiations_in_progress++; is_var_templ_instance = symbol_is(var_sym, sk_variable); tssp = template_supplement_for_symbol(template_sym); if (template_sym->header->has_intrinsic_name) { @@ -8134,6 +8164,11 @@ expression context) rather than a declaration. is now present. */ establish_variable_instantiation_corresp(var_ptr); done: + constexpr_instantiations_in_progress--; + if (constexpr_instantiations_in_progress == 0 && + defer_instantiations == 0) { + process_deferred_instantiation_requests(); + } /* if */ return; } /* instantiate_template_variable */ @@ -40298,8 +40333,10 @@ update_instantiation_required_flag to do the appropriate processing. flag is used to make sure that the list is not processed during potential recursive calls of this routine. Also, don't do the instantiations if there are pending class instantiations or namespace - scope class definitions. */ + scope class definitions, or while a constexpr function or variable is + being instantiated (it is processed again when that is complete). */ if (!deferred_instantiations_in_process && + constexpr_instantiations_in_progress == 0 && curr_class_fixup_header(/*for_instantiation=*/TRUE)-> pending_class_definitions == 0) { deferred_instantiations_in_process = TRUE; @@ -46718,6 +46755,7 @@ Initializations for template. do_strict_constraint_checking = FALSE; #endif /* MICROSOFT_EXTENSIONS_ALLOWED */ deferred_instantiations_in_process = FALSE; + constexpr_instantiations_in_progress = 0; num_total_pending_instantiations = 0; num_pending_default_arg_instantiations = 0; master_instantiations_list = NULL; diff --git a/tests/tests/regressions/.constexpr-deferred-instantiation-function.rto/default.1.1.txt b/tests/tests/regressions/.constexpr-deferred-instantiation-function.rto/default.1.1.txt new file mode 100644 index 0000000000..bb2ad24455 --- /dev/null +++ b/tests/tests/regressions/.constexpr-deferred-instantiation-function.rto/default.1.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=1 --g++ --gnu_version 150200 --c++20 Test_name.c diff --git a/tests/tests/regressions/.constexpr-deferred-instantiation-function.rto/default.2.1.txt b/tests/tests/regressions/.constexpr-deferred-instantiation-function.rto/default.2.1.txt new file mode 100644 index 0000000000..7b89464752 --- /dev/null +++ b/tests/tests/regressions/.constexpr-deferred-instantiation-function.rto/default.2.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=2 --clang_version 220100 --c++20 Test_name.c diff --git a/tests/tests/regressions/.constexpr-deferred-instantiation-function.rto/default.3.1.txt b/tests/tests/regressions/.constexpr-deferred-instantiation-function.rto/default.3.1.txt new file mode 100644 index 0000000000..f2c4c0a72a --- /dev/null +++ b/tests/tests/regressions/.constexpr-deferred-instantiation-function.rto/default.3.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=3 --ms_c++20 --microsoft_version 1950 Test_name.c diff --git a/tests/tests/regressions/.constexpr-deferred-instantiation-variable.rto/default.1.1.txt b/tests/tests/regressions/.constexpr-deferred-instantiation-variable.rto/default.1.1.txt new file mode 100644 index 0000000000..bb2ad24455 --- /dev/null +++ b/tests/tests/regressions/.constexpr-deferred-instantiation-variable.rto/default.1.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=1 --g++ --gnu_version 150200 --c++20 Test_name.c diff --git a/tests/tests/regressions/.constexpr-deferred-instantiation-variable.rto/default.2.1.txt b/tests/tests/regressions/.constexpr-deferred-instantiation-variable.rto/default.2.1.txt new file mode 100644 index 0000000000..7b89464752 --- /dev/null +++ b/tests/tests/regressions/.constexpr-deferred-instantiation-variable.rto/default.2.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=2 --clang_version 220100 --c++20 Test_name.c diff --git a/tests/tests/regressions/.constexpr-deferred-instantiation-variable.rto/default.3.1.txt b/tests/tests/regressions/.constexpr-deferred-instantiation-variable.rto/default.3.1.txt new file mode 100644 index 0000000000..f2c4c0a72a --- /dev/null +++ b/tests/tests/regressions/.constexpr-deferred-instantiation-variable.rto/default.3.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=3 --ms_c++20 --microsoft_version 1950 Test_name.c diff --git a/tests/tests/regressions/constexpr-deferred-instantiation-function.sft.cpp b/tests/tests/regressions/constexpr-deferred-instantiation-function.sft.cpp new file mode 100644 index 0000000000..d85d45a3d9 --- /dev/null +++ b/tests/tests/regressions/constexpr-deferred-instantiation-function.sft.cpp @@ -0,0 +1,12 @@ +//type:fp +//options:--g++ --gnu_version 150200 --c++20:--clang_version 220100 --c++20:--ms_c++20 --microsoft_version 1950 +//remark:Defer instantiations while a constexpr function template instance is instantiated. + +template struct A {}; +template constexpr int f() { return sizeof(A); } +template struct B { + constexpr ~B() { static_assert(f()); } +}; +struct S { + void g(B = {}) { f(); } +}; diff --git a/tests/tests/regressions/constexpr-deferred-instantiation-variable.sft.cpp b/tests/tests/regressions/constexpr-deferred-instantiation-variable.sft.cpp new file mode 100644 index 0000000000..1027a02e06 --- /dev/null +++ b/tests/tests/regressions/constexpr-deferred-instantiation-variable.sft.cpp @@ -0,0 +1,12 @@ +//type:fp +//options:--g++ --gnu_version 150200 --c++20:--clang_version 220100 --c++20:--ms_c++20 --microsoft_version 1950 +//remark:Defer instantiations while a variable template instance is instantiated. + +template struct A {}; +template constexpr int v = sizeof(A); +template struct B { + constexpr ~B() { static_assert(v); } +}; +struct S { + void g(B = {}) { (void)v; } +};