On Tue, Mar 10, 2026 at 03:42:51PM -0400, Jason Merrill wrote:
> On 3/10/26 8:38 AM, Marek Polacek wrote:
> > > For -fno-exceptions, we reject throw statements in the source, and a lot
> > > of code in the header has #ifdef __cpp_exceptions guarded stuff and the
> > > FE for !flag_exceptions doesn't emit some parts of the IL needed for
> > > exceptions. For the errors in metafns, we had just a todo to handle it
> > > in the source but no actual implementation, so we allowed throwing
> > > an exception and sometimes it worked to some extent and sometimes
> > > it didn't.
> > >
> > > The following patch fixes it by not throwing an exception if user
> > > asked for -fno-exceptions - instead we just emit an error including
> > > the planned what () (unless ctx->quiet) and make the evaluation
> > > non-constant.
> > >
> > > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
> >
> > Ok by me, thanks.
> > > 2026-03-10 Jakub Jelinek <[email protected]>
> > >
> > > PR c++/124417
> > > * reflect.cc (get_meta_exception_object): Add CTX argument. For
> > > !flag_exceptions emit error unless ctx->quiet, set *non_constant_p
> > > to true and return NULL_TREE instead of throwing an exception.
> > > (throw_exception): Adjust get_meta_exception_object caller.
> > >
> > > * g++.dg/reflect/no-exceptions1.C: New test.
> > >
> > > --- gcc/cp/reflect.cc.jj 2026-03-09 11:41:58.060768044 +0100
> > > +++ gcc/cp/reflect.cc 2026-03-09 18:09:08.967171309 +0100
> > > @@ -929,17 +929,31 @@ get_info_vec (location_t loc, const cons
> > > and FROM is the info for from(). */
> > > static tree
> > > -get_meta_exception_object (location_t loc, const char *what, tree from,
> > > - bool *non_constant_p)
> > > +get_meta_exception_object (location_t loc, const constexpr_ctx *ctx,
> > > + const char *what, tree from, bool *non_constant_p)
> > > {
> > > /* Don't throw in a template. */
> > > - // TODO For -fno-exceptions, report an error.
> > > if (processing_template_decl)
> > > {
> > > *non_constant_p = true;
> > > return NULL_TREE;
> > > }
> > > + /* Don't try to throw exceptions with -fno-exceptions. */
> > > + if (!flag_exceptions)
> > > + {
> > > + if (!cxx_constexpr_quiet_p (ctx))
> > > + {
> > > + auto_diagnostic_group d;
> > > + error_at (loc, "%qD should throw %qs; %<what()%>: %qs",
> > > + from, "std::meta::exception", _(what));
> > > + inform (loc, "exceptions are disabled, treating as non-constant; "
> > > + "use %qs to enable", "-fexceptions");
>
> Hmm, I don't think suggesting -fexceptions is useful here; presumably they
> are disabled by -fno-exceptions, and removing that (perhaps after a project
> policy discussion) would be better than having -fno-exceptions -fexceptions.
That was just following the error we emit on throw stmt when using
-fno-exceptions:
a.C: In function ‘void foo()’:
a.C:4:9: error: exception handling disabled, use ‘-fexceptions’ to enable
4 | throw 1;
| ^
But if you think the ; and following part is useless, I can certainly drop
that.
Jakub