From 9145cbde8919210e7d03c8b11923847e9f54d352 Mon Sep 17 00:00:00 2001 From: Gareth Williamson Date: Mon, 30 Nov 2026 17:13:41 +0100 Subject: [PATCH 1/2] Fix deferred instantiations during constexpr template instantiation [GH #623] --- ...h-623-constexpr-deferred-instantiation.txt | 22 +++++ src/templates.c | 90 +++++++++++++++---- .../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, 124 insertions(+), 18 deletions(-) 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..ea7cb1ddc3 100644 --- a/src/templates.c +++ b/src/templates.c @@ -7629,12 +7629,59 @@ done:; } /* instantiate_template_function_full */ +static void set_aside_deferred_instantiations( + a_symbol_list_entry_ptr *p_saved_head, + a_symbol_list_entry_ptr *p_saved_tail) +/* +Empty the deferred_instantiations list, returning its head and tail in +*p_saved_head and *p_saved_tail, so that further requests can be processed +separately. +*/ +{ + *p_saved_head = deferred_instantiations; + *p_saved_tail = deferred_instantiations_tail; + deferred_instantiations = NULL; + deferred_instantiations_tail = NULL; +} /* set_aside_deferred_instantiations */ + + +static void restore_deferred_instantiations( + a_symbol_list_entry_ptr saved_head, + a_symbol_list_entry_ptr saved_tail) +/* +Put the requests set aside by set_aside_deferred_instantiations back at the +front of the deferred_instantiations list. +*/ +{ + if (saved_head != NULL) { + saved_tail->next = deferred_instantiations; + deferred_instantiations = saved_head; + if (deferred_instantiations_tail == NULL) { + deferred_instantiations_tail = saved_tail; + } /* if */ + } /* if */ +} /* restore_deferred_instantiations */ + + static void instantiate_template_function(a_template_instance_ptr tip) /* Instantiate the body of the template function associated with tip. */ { - a_routine_ptr routine = tip->instance_sym->variant.routine.ptr; + a_routine_ptr routine = tip->instance_sym->variant.routine.ptr; + a_boolean is_constexpr = routine->is_constexpr; + a_symbol_list_entry_ptr saved_deferred_head = NULL; + a_symbol_list_entry_ptr saved_deferred_tail = NULL; + + if (is_constexpr) { + /* Deferred instantiation requests may constant-evaluate this function, + so the ones already made must wait until its body is complete. New + requests are handled as usual: immediate ones so that consteval + escalation (P2564R3) can see them, and deferred ones while this + function is still in the instantiation context of diagnostics. */ + set_aside_deferred_instantiations(&saved_deferred_head, + &saved_deferred_tail); + } /* if */ #if GNU_FUNCTION_MULTIVERSIONING /* For a GNU multiversion target-versioned function, instantiate all of the @@ -7658,6 +7705,12 @@ Instantiate the body of the template function associated with tip. { instantiate_template_function_full(tip, routine); } /* if */ + restore_deferred_instantiations(saved_deferred_head, saved_deferred_tail); + if (saved_deferred_head != NULL && defer_instantiations == 0) { + /* The body is complete, so the requests that were set aside can now be + processed. */ + process_deferred_instantiation_requests(); + } /* if */ } /* instantiate_template_function */ #if ONE_INSTANTIATION_PER_OBJECT @@ -7729,7 +7782,14 @@ expression context) rather than a declaration. a_template_cache_ptr body_cache, decl_cache; int idx = 0; a_host_large_integer intrinsic_val; - + a_symbol_list_entry_ptr saved_deferred_head; + a_symbol_list_entry_ptr saved_deferred_tail; + + /* Deferred instantiation requests may use the value of this variable in a + constant expression, so the ones already made must wait until its + initializer is complete. */ + set_aside_deferred_instantiations(&saved_deferred_head, + &saved_deferred_tail); 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 +8194,12 @@ expression context) rather than a declaration. is now present. */ establish_variable_instantiation_corresp(var_ptr); done: + restore_deferred_instantiations(saved_deferred_head, saved_deferred_tail); + if (saved_deferred_head != NULL && defer_instantiations == 0) { + /* The initializer is complete, so the requests that were set aside can + now be processed. */ + process_deferred_instantiation_requests(); + } /* if */ return; } /* instantiate_template_variable */ @@ -21450,10 +21516,8 @@ mode in-class specialization. instance. This is done to make sure a second reference to this routine is not attempted before the routine is completed. */ defer_instantiations++; - saved_deferred_instantiations = deferred_instantiations; - saved_deferred_instantiations_tail = deferred_instantiations_tail; - deferred_instantiations = NULL; - deferred_instantiations_tail = NULL; + set_aside_deferred_instantiations(&saved_deferred_instantiations, + &saved_deferred_instantiations_tail); parent_class = templ_sym->is_class_member ? sym_parent_class(templ_sym) : (a_type_ptr)NULL; rp = alloc_routine(); @@ -21986,18 +22050,8 @@ mode in-class specialization. /* Do any instantiations that were deferred while this routine was being created. */ process_deferred_class_fixups_and_instantiations(/*for_instantiation=*/TRUE); - if (saved_deferred_instantiations == NULL) { - /* The current values should be used as the list was previously empty. */ - } else { - /* Add the new list to the end of the previous list. */ - saved_deferred_instantiations_tail->next = deferred_instantiations; - deferred_instantiations = saved_deferred_instantiations; - if (deferred_instantiations_tail == NULL) { - /* If the new list is non-NULL, it now already points to the list to be - used. */ - deferred_instantiations_tail = saved_deferred_instantiations_tail; - } /* if */ - } /* if */ + restore_deferred_instantiations(saved_deferred_instantiations, + saved_deferred_instantiations_tail); if (special_kind_is(rp, sfk_deduction_guide)) { /* Don't do exception specification processing for deduction guides. */ } else if (ms_version_is(<1920) || !rp->source_corresp.is_class_member) { 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; } +}; From 4ddb66bad291f2448f9570f65192427a935070ac Mon Sep 17 00:00:00 2001 From: Gareth Williamson Date: Tue, 1 Dec 2026 18:00:18 +0100 Subject: [PATCH 2/2] Add a_symbol_list_segment for symbol list head/tail pairs [GH #623] --- src/symbol_tbl.c | 14 +++++++ src/symbol_tbl.h | 16 ++++++++ src/templates.c | 96 +++++++++++++++++++----------------------------- 3 files changed, 67 insertions(+), 59 deletions(-) diff --git a/src/symbol_tbl.c b/src/symbol_tbl.c index 875ebeab46..8c7f789298 100644 --- a/src/symbol_tbl.c +++ b/src/symbol_tbl.c @@ -2162,6 +2162,20 @@ be NULL, in which case nothing is done. } /* free_list_of_symbol_list_entries */ +void a_symbol_list_segment::append(a_symbol_list_entry_ptr slep) +/* +Append the given symbol list entry to *this. +*/ +{ + if (this->head == NULL) { + this->head = slep; + } else { + this->tail->next = slep; + } /* if */ + this->tail = slep; +} /* a_symbol_list_segment::append */ + + static void set_identifier_for_symbol_header( a_symbol_header_ptr hdr_ptr, a_const_char *string, diff --git a/src/symbol_tbl.h b/src/symbol_tbl.h index 902545cbe6..0225932f6a 100644 --- a/src/symbol_tbl.h +++ b/src/symbol_tbl.h @@ -858,6 +858,22 @@ typedef struct a_symbol_list_entry { } a_symbol_list_entry; +typedef struct a_symbol_list_segment { + /* The first and last entries of a list, or part of a list, of symbol list + entries. Both are NULL if the segment is empty. Ownership of the + entries is not implied. */ + a_symbol_list_entry_ptr + head; + /* The first entry of the segment. */ + a_symbol_list_entry_ptr + tail; + /* The last entry of the segment. */ + void append(a_symbol_list_entry_ptr slep); + void clear() + { this->head = NULL; this->tail = NULL; } +} a_symbol_list_segment; + + typedef struct a_type_list_entry { /* Entry created to produce a list of types for some special purpose. For example, such a list is used when building a list of associated diff --git a/src/templates.c b/src/templates.c index ea7cb1ddc3..fb4d13e934 100644 --- a/src/templates.c +++ b/src/templates.c @@ -404,17 +404,13 @@ STATIC_THREAD a_symbol_list_entry_ptr exported_templates_tail; /* The end of the exported templates list. */ -STATIC_THREAD a_symbol_list_entry_ptr +STATIC_THREAD a_symbol_list_segment deferred_instantiations; /* A list of symbol entries for instantiations that were deferred while a class definition was pending. These entities are instantiated when a class definition is no longer pending. */ -STATIC_THREAD a_symbol_list_entry_ptr - deferred_instantiations_tail; - /* The end of the deferred_instantiations list. */ - STATIC_THREAD a_partial_order_candidate_ptr avail_partial_order_candidates; /* Previously allocated entries available for reuse. */ @@ -7629,35 +7625,28 @@ done:; } /* instantiate_template_function_full */ -static void set_aside_deferred_instantiations( - a_symbol_list_entry_ptr *p_saved_head, - a_symbol_list_entry_ptr *p_saved_tail) +static void save_deferred_instantiations(a_symbol_list_segment *p_saved) /* -Empty the deferred_instantiations list, returning its head and tail in -*p_saved_head and *p_saved_tail, so that further requests can be processed -separately. +Empty the deferred_instantiations list, returning its entries in *p_saved, so +that further requests can be processed separately. */ { - *p_saved_head = deferred_instantiations; - *p_saved_tail = deferred_instantiations_tail; - deferred_instantiations = NULL; - deferred_instantiations_tail = NULL; -} /* set_aside_deferred_instantiations */ + *p_saved = deferred_instantiations; + deferred_instantiations.clear(); +} /* save_deferred_instantiations */ -static void restore_deferred_instantiations( - a_symbol_list_entry_ptr saved_head, - a_symbol_list_entry_ptr saved_tail) +static void restore_deferred_instantiations(a_symbol_list_segment saved) /* -Put the requests set aside by set_aside_deferred_instantiations back at the -front of the deferred_instantiations list. +Put the requests saved by save_deferred_instantiations back at the front of +the deferred_instantiations list. */ { - if (saved_head != NULL) { - saved_tail->next = deferred_instantiations; - deferred_instantiations = saved_head; - if (deferred_instantiations_tail == NULL) { - deferred_instantiations_tail = saved_tail; + if (saved.head != NULL) { + saved.tail->next = deferred_instantiations.head; + deferred_instantiations.head = saved.head; + if (deferred_instantiations.tail == NULL) { + deferred_instantiations.tail = saved.tail; } /* if */ } /* if */ } /* restore_deferred_instantiations */ @@ -7670,8 +7659,7 @@ 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; - a_symbol_list_entry_ptr saved_deferred_head = NULL; - a_symbol_list_entry_ptr saved_deferred_tail = NULL; + a_symbol_list_segment saved_deferred_instantiations = { NULL, NULL }; if (is_constexpr) { /* Deferred instantiation requests may constant-evaluate this function, @@ -7679,8 +7667,7 @@ Instantiate the body of the template function associated with tip. requests are handled as usual: immediate ones so that consteval escalation (P2564R3) can see them, and deferred ones while this function is still in the instantiation context of diagnostics. */ - set_aside_deferred_instantiations(&saved_deferred_head, - &saved_deferred_tail); + save_deferred_instantiations(&saved_deferred_instantiations); } /* if */ #if GNU_FUNCTION_MULTIVERSIONING @@ -7705,10 +7692,10 @@ Instantiate the body of the template function associated with tip. { instantiate_template_function_full(tip, routine); } /* if */ - restore_deferred_instantiations(saved_deferred_head, saved_deferred_tail); - if (saved_deferred_head != NULL && defer_instantiations == 0) { - /* The body is complete, so the requests that were set aside can now be - processed. */ + restore_deferred_instantiations(saved_deferred_instantiations); + if (saved_deferred_instantiations.head != NULL && + defer_instantiations == 0) { + /* The body is complete, so the saved requests can now be processed. */ process_deferred_instantiation_requests(); } /* if */ } /* instantiate_template_function */ @@ -7782,14 +7769,12 @@ expression context) rather than a declaration. a_template_cache_ptr body_cache, decl_cache; int idx = 0; a_host_large_integer intrinsic_val; - a_symbol_list_entry_ptr saved_deferred_head; - a_symbol_list_entry_ptr saved_deferred_tail; + a_symbol_list_segment saved_deferred_instantiations; /* Deferred instantiation requests may use the value of this variable in a constant expression, so the ones already made must wait until its initializer is complete. */ - set_aside_deferred_instantiations(&saved_deferred_head, - &saved_deferred_tail); + save_deferred_instantiations(&saved_deferred_instantiations); is_var_templ_instance = symbol_is(var_sym, sk_variable); tssp = template_supplement_for_symbol(template_sym); if (template_sym->header->has_intrinsic_name) { @@ -8194,10 +8179,11 @@ expression context) rather than a declaration. is now present. */ establish_variable_instantiation_corresp(var_ptr); done: - restore_deferred_instantiations(saved_deferred_head, saved_deferred_tail); - if (saved_deferred_head != NULL && defer_instantiations == 0) { - /* The initializer is complete, so the requests that were set aside can - now be processed. */ + restore_deferred_instantiations(saved_deferred_instantiations); + if (saved_deferred_instantiations.head != NULL && + defer_instantiations == 0) { + /* The initializer is complete, so the saved requests can now be + processed. */ process_deferred_instantiation_requests(); } /* if */ return; @@ -21491,8 +21477,7 @@ mode in-class specialization. a_type_ptr parent_class; a_boolean trans_unit_pushed; a_boolean is_deduction_guide; - a_symbol_list_entry_ptr saved_deferred_instantiations; - a_symbol_list_entry_ptr saved_deferred_instantiations_tail; + a_symbol_list_segment saved_deferred_instantiations; db_enter(4, "make_template_function"); #if CHECKING @@ -21516,8 +21501,7 @@ mode in-class specialization. instance. This is done to make sure a second reference to this routine is not attempted before the routine is completed. */ defer_instantiations++; - set_aside_deferred_instantiations(&saved_deferred_instantiations, - &saved_deferred_instantiations_tail); + save_deferred_instantiations(&saved_deferred_instantiations); parent_class = templ_sym->is_class_member ? sym_parent_class(templ_sym) : (a_type_ptr)NULL; rp = alloc_routine(); @@ -22050,8 +22034,7 @@ mode in-class specialization. /* Do any instantiations that were deferred while this routine was being created. */ process_deferred_class_fixups_and_instantiations(/*for_instantiation=*/TRUE); - restore_deferred_instantiations(saved_deferred_instantiations, - saved_deferred_instantiations_tail); + restore_deferred_instantiations(saved_deferred_instantiations); if (special_kind_is(rp, sfk_deduction_guide)) { /* Don't do exception specification processing for deduction guides. */ } else if (ms_version_is(<1920) || !rp->source_corresp.is_class_member) { @@ -40114,11 +40097,7 @@ unless the SIR_CLEAR_VALUE flag is set in "options". slep = alloc_symbol_list_entry(); slep->symbol = sym; /* Add this entry to the end of the deferred instantiations list. */ - if (deferred_instantiations == NULL) deferred_instantiations = slep; - if (deferred_instantiations_tail != NULL) { - deferred_instantiations_tail->next = slep; - } /* if */ - deferred_instantiations_tail = slep; + deferred_instantiations.append(slep); check_assertion(!defer_instantiation); /* Suppress set-but-never-used. */ defer_instantiation = TRUE; } else { @@ -40357,16 +40336,16 @@ update_instantiation_required_flag to do the appropriate processing. curr_class_fixup_header(/*for_instantiation=*/TRUE)-> pending_class_definitions == 0) { deferred_instantiations_in_process = TRUE; - for (slep = deferred_instantiations; slep != NULL; slep = slep->next) { + for (slep = deferred_instantiations.head; slep != NULL; + slep = slep->next) { a_template_instance_ptr tip; a_symbol_ptr sym = slep->symbol; tip = template_instance_for_symbol(sym); update_instantiation_required_flag(tip, /*value=*/TRUE, SIR_NONE); } /* for */ /* Free any list entries that were used. */ - free_list_of_symbol_list_entries(deferred_instantiations); - deferred_instantiations = NULL; - deferred_instantiations_tail = NULL; + free_list_of_symbol_list_entries(deferred_instantiations.head); + deferred_instantiations.clear(); deferred_instantiations_in_process = FALSE; } /* if */ } /* process_deferred_instantiation_requests */ @@ -46763,8 +46742,7 @@ Initializations for template. { curr_default_args = NULL; defer_instantiations = 0; - deferred_instantiations = NULL; - deferred_instantiations_tail = NULL; + deferred_instantiations.clear(); avail_partial_order_candidates = NULL; avail_variadic_param_infos = NULL; #if MICROSOFT_EXTENSIONS_ALLOWED