Skip to content

Commit f4767c7

Browse files
C++26: Allowing exception throwing in constant evaluation [GH #368]
1 parent 4803978 commit f4767c7

26 files changed

Lines changed: 3024 additions & 215 deletions

‎doc/source/expr.rst‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,6 +2207,18 @@ in C++14, but not C++11). It also removes C++11 structural constraints for
22072207
``constexpr`` functions and constructors (e.g., it allows multiple statements
22082208
in definitions).
22092209

2210+
The global variable ``constexpr_exceptions_enabled`` is TRUE when exceptions
2211+
can be thrown and caught during constant evaluation (a C++26 feature; it can
2212+
also be controlled with ``--set_flag`` and ``--clear_flag`` options naming
2213+
``constexpr_exceptions``, and it is FALSE whenever ``exceptions_enabled`` is
2214+
FALSE). The interpreter then evaluates ``throw`` expressions and ``try``
2215+
blocks: A thrown exception unwinds the interpreted evaluation, destroying
2216+
objects along the way, until a matching handler is found. An exception that
2217+
is not caught within the evaluation, or that causes ``std::terminate`` to be
2218+
called, makes the interpretation fail. The section "Control Transfers and
2219+
Exceptions" in the comments at the start of ``interpret.c`` describes the
2220+
implementation.
2221+
22102222
To avoid burdening the IL or the rest of the front end, the interpreter does
22112223
not record state information in the IL or other front end structures. Instead,
22122224
it maintains its own data structures linked to the IL through efficient hash

‎src/cmd_line.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ STATIC_THREAD a_boolean
154154
value of FALSE. */
155155
#endif /* MICROSOFT_EXTENSIONS_ALLOWED */
156156

157+
STATIC_THREAD a_boolean
158+
constexpr_exceptions_flag_used;
159+
/* TRUE if constexpr_exceptions_enabled was set or
160+
cleared by a --set_flag or --clear_flag option, so
161+
that the language mode does not determine it. */
162+
157163

158164
static void add_config_dependent_option_description(
159165
an_option_kind kind,
@@ -2546,6 +2552,7 @@ STATIC_THREAD a_flag_name
25462552
#endif /* IL_SHOULD_BE_WRITTEN_TO_FILE */
25472553
{ "extended_float_types", &extended_float_types },
25482554
{ "use_predefined_macro_file", &use_predefined_macro_file },
2555+
{ "constexpr_exceptions", &constexpr_exceptions_enabled },
25492556
{ NULL, NULL } /* must be last */
25502557
};
25512558

@@ -2569,6 +2576,9 @@ be given to the flag.
25692576
str_command_line_error(ec_cl_invalid_flag_name, flag_name);
25702577
} /* if */
25712578
*flag_var = value;
2579+
if (flag_var == &constexpr_exceptions_enabled) {
2580+
constexpr_exceptions_flag_used = TRUE;
2581+
} /* if */
25722582
} /* set_flag_value */
25732583

25742584

@@ -4449,6 +4459,9 @@ default mode (e.g., exception handling).
44494459
embed_enabled = TRUE;
44504460
struct_binding_packs_enabled = TRUE;
44514461
pack_indexing_enabled = TRUE;
4462+
if (!constexpr_exceptions_flag_used) {
4463+
constexpr_exceptions_enabled = TRUE;
4464+
} /* if */
44524465
} /* if */
44534466
/* Disable "false" as a null pointer constant in C++11 mode (as per Core
44544467
issue 903). */
@@ -12387,6 +12400,17 @@ Process the arguments on the command line that invoked the compiler.
1238712400
} /* if */
1238812401
nonclass_prototype_instantiations = TRUE;
1238912402
} /* if */
12403+
if (!exceptions_enabled) {
12404+
/* Exceptions cannot be thrown during constant evaluation if they cannot be
12405+
thrown at all. */
12406+
constexpr_exceptions_enabled = FALSE;
12407+
} /* if */
12408+
if (constexpr_exceptions_enabled) {
12409+
/* Catching exceptions during constant evaluation requires try-blocks in
12410+
constexpr functions (e.g., with --set_flag=constexpr_exceptions in
12411+
C++17 mode). */
12412+
constexpr_try_enabled = TRUE;
12413+
} /* if */
1239012414
if (lambdas_enabled) {
1239112415
/* If lambdas are allowed, enable local types as template arguments too. */
1239212416
local_types_as_template_args_enabled = TRUE;
@@ -13862,6 +13886,8 @@ variables declared in cmd_line.h.
1386213886
embed_enabled = FALSE;
1386313887
struct_binding_packs_enabled = FALSE;
1386413888
pack_indexing_enabled = FALSE;
13889+
constexpr_exceptions_enabled = FALSE;
13890+
constexpr_exceptions_flag_used = FALSE;
1386513891
} /* cmd_line_static_var_init */
1386613892

1386713893

‎src/cmd_line.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,6 +3016,11 @@ EXTERN_THREAD a_boolean
30163016
pack_indexing_enabled;
30173017
/* TRUE if C++26 pack indexing is supported. */
30183018

3019+
EXTERN_THREAD a_boolean
3020+
constexpr_exceptions_enabled;
3021+
/* TRUE if exceptions can be thrown and caught during
3022+
constant evaluation (a C++26 feature). */
3023+
30193024
/*
30203025
Macro that determines whether CTAD for alias templates should be accepted.
30213026
Alias-template CTAD is a standard feature in C++20 mode (and the global

‎src/error.c‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7740,6 +7740,29 @@ list is freed and the list passed in is reset.
77407740
} /* discard_more_info_list */
77417741

77427742

7743+
void truncate_more_info_list(a_diag_list_ptr dlp,
7744+
a_diagnostic_ptr last_kept)
7745+
/*
7746+
Discard the "more information" diagnostics that follow the entry last_kept on
7747+
the list specified by dlp (all of them, if last_kept is NULL). last_kept must
7748+
be an entry of the list (or NULL).
7749+
*/
7750+
{
7751+
a_diag_list discarded;
7752+
7753+
if (last_kept == NULL) {
7754+
discarded = *dlp;
7755+
clear_diag_list(dlp);
7756+
} else {
7757+
discarded.head = last_kept->next;
7758+
discarded.tail = dlp->tail;
7759+
last_kept->next = NULL;
7760+
dlp->tail = last_kept;
7761+
} /* if */
7762+
if (discarded.head != NULL) free_diag_list_elements(&discarded);
7763+
} /* truncate_more_info_list */
7764+
7765+
77437766
void more_info_diagnostic(an_error_code error_code,
77447767
a_source_position *error_pos,
77457768
a_diag_list_ptr diag_list)

‎src/error.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,9 @@ extern void splice_diag_list(a_diag_list_ptr src,
946946

947947
extern void discard_more_info_list(a_diag_list_ptr dlp);
948948

949+
extern void truncate_more_info_list(a_diag_list_ptr dlp,
950+
a_diagnostic_ptr last_kept);
951+
949952
extern void pch_message(an_error_code error_code,
950953
a_const_char *fill_in_str);
951954

‎src/error_msg.txt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3683,5 +3683,11 @@ ec_bad_gcc_unroll_pragma_value;;"the argument of \"#pragma GCC unroll\" must be
36833683
ec_array_comparison_deprecated;;"comparison between two arrays is deprecated"
36843684
ec_array_comparison_not_allowed;;"comparison between two arrays is not allowed"
36853685
ec_array_comparison_compares_addresses;;"comparison between two arrays compares their addresses"
3686+
ec_constexpr_uncaught_exception;;"an exception of type %t is thrown and not caught during constant evaluation"
3687+
ec_constexpr_rethrow_without_exception;;"a rethrow (\"throw;\") is evaluated when no exception is being handled"
3688+
ec_constexpr_exception_escapes_nonthrowing;;"std::terminate is called because an exception escapes from non-throwing %nd"
3689+
ec_constexpr_exception_during_unwinding;;"std::terminate is called because an exception escapes from a destructor during stack unwinding"
3690+
ec_constexpr_exception_in_handler_init;;"std::terminate is called because an exception escapes from the initialization of a handler parameter"
3691+
ec_constexpr_exception_from_exception_dtor;;"std::terminate is called because the destructor of an exception object exits by throwing an exception"
36863692
###############################################################################
36873693

0 commit comments

Comments
 (0)