Skip to content

Commit

Permalink
fix Issue 24084 - Add -nothrow Switch to Compiler (#15536)
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright authored Aug 18, 2023
1 parent 705782a commit 622a432
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
21 changes: 21 additions & 0 deletions changelog/dmd.fix24084.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Add -nothrow Switch to Compiler

dmd itself (and presumably others) do not throw Exceptions, preferring other methods
for dealing with errors. There is a cost, however, in supporting Exceptions even when
they are never thrown. The cost is in adding stack unwinders for things like RAII
objects, and preventing numerous optimizations across try-catch boundaries.

Adding `nothrow` to all the code in a project turns out to be an inordinate amount of
work if the program is large. Putting `nothrow:` at the top of the module doesn't
influence the status for member functions in a class or struct, the `nothrow:`
will have to be repeated for each class/struct.

Adding the -nothrow switch to the compiler causes the stack unwinders to not be added
and enables the optimizations. This capability is already there for -betterC code,
this would just enable it for regular D code.

The switch does not affect semantic analysis, just the code generation. Name mangling
is not affected.

The switch is useful for determining what effect exception handling has on an executable's
size and performance in non-throwing code.
5 changes: 5 additions & 0 deletions compiler/src/dmd/cli.d
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,11 @@ dmd -cov -unittest myprog.d
`Turns off all array bounds checking, even for safe functions. $(RED Deprecated
(use $(TT $(SWLINK -boundscheck)=off) instead).)`,
),
Option("nothrow",
"assume no Exceptions will be thrown",
`Turns off generation of exception stack unwinding code, enables
more efficient code for RAII objects.`,
),
Option("O",
"optimize",
`Optimize generated code. For fastest executables, compile
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/dmd/mars.d
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,10 @@ bool parseCommandLine(const ref Strings arguments, const size_t argc, ref Param
else
goto Lerror;
}
else if (arg == "-nothrow") // https://dlang.org/dmd.html#switch-nothrow
{
params.useExceptions = false;
}
else if (arg == "-unittest")
params.useUnitTests = true;
else if (p[1] == 'I') // https://dlang.org/dmd.html#switch-I
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dmd/statementsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -3281,7 +3281,7 @@ Statement statementSemanticVisit(Statement s, Scope* sc)

if (!global.params.useExceptions)
{
tcs.error("cannot use try-catch statements with -betterC");
tcs.error("cannot use try-catch statements with %s", global.params.betterC ? "-betterC".ptr : "-nothrow".ptr);
return setError();
}

Expand Down Expand Up @@ -3696,7 +3696,7 @@ public bool throwSemantic(const ref Loc loc, ref Expression exp, Scope* sc)
{
if (!global.params.useExceptions)
{
loc.error("cannot use `throw` statements with -betterC");
loc.error("cannot use `throw` statements with %s", global.params.betterC ? "-betterC".ptr : "-nothrow".ptr);
return false;
}

Expand Down
28 changes: 28 additions & 0 deletions compiler/test/fail_compilation/test24084.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* REQUIRED_ARGS: -nothrow
* TEST_OUTPUT:
---
fail_compilation/test24084.d(110): Error: cannot use `throw` statements with -nothrow
fail_compilation/test24084.d(112): Error: cannot use try-catch statements with -nothrow
---
*/

// https://issues.dlang.org/show_bug.cgi?id=24084

#line 100

struct S
{
int x;
~this() { }
}

void xyzzy()
{
S s;
throw new Exception("xx");

try
{
int y;
} catch (Exception) { }
}

0 comments on commit 622a432

Please sign in to comment.