Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/pending_changes/gh-623-constexpr-deferred-instantiation.txt
Original file line number Diff line number Diff line change
@@ -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 <class T> struct A {};
template <class T> constexpr int f() { return sizeof(A<T>); }
template <class T> struct B {
constexpr ~B() { static_assert(f<T>()); } // Previously an error.
};
struct S {
void g(B<int> = {}) { f<int>(); }
};

The same error was issued if f was replaced by a variable template.
40 changes: 39 additions & 1 deletion src/templates.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 */

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fe_only -DTEST_NUMBER=1 --g++ --gnu_version 150200 --c++20 Test_name.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fe_only -DTEST_NUMBER=2 --clang_version 220100 --c++20 Test_name.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fe_only -DTEST_NUMBER=3 --ms_c++20 --microsoft_version 1950 Test_name.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fe_only -DTEST_NUMBER=1 --g++ --gnu_version 150200 --c++20 Test_name.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fe_only -DTEST_NUMBER=2 --clang_version 220100 --c++20 Test_name.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fe_only -DTEST_NUMBER=3 --ms_c++20 --microsoft_version 1950 Test_name.c
Original file line number Diff line number Diff line change
@@ -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 <class T> struct A {};
template <class T> constexpr int f() { return sizeof(A<T>); }
template <class T> struct B {
constexpr ~B() { static_assert(f<T>()); }
};
struct S {
void g(B<int> = {}) { f<int>(); }
};
Original file line number Diff line number Diff line change
@@ -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 <class T> struct A {};
template <class T> constexpr int v = sizeof(A<T>);
template <class T> struct B {
constexpr ~B() { static_assert(v<T>); }
};
struct S {
void g(B<int> = {}) { (void)v<int>; }
};
Loading