Skip to content

Commit

Permalink
Fix 22364 - Omit unreachable return in collectException[Msg]...
Browse files Browse the repository at this point in the history
...when instantiated with `noreturn`.

DMD is able to determine that a lazy `noreturn` expression will interrupt
the normal control flow (throw / halt / ...) s.t. it never reaches the
`return`.
  • Loading branch information
MoonlightSentinel authored and dlang-bot committed Oct 8, 2021
1 parent 12b4380 commit 1c8047b
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions std/exception.d
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,9 @@ T collectException(T = Exception, E)(lazy E expression, ref E result)
{
return e;
}
return null;
// Avoid "statement not reachable" warning
static if (!is(immutable E == immutable noreturn))
return null;
}
///
@system unittest
Expand Down Expand Up @@ -711,7 +713,9 @@ T collectException(T : Throwable = Exception, E)(lazy E expression)
{
return t;
}
return null;
// Avoid "statement not reachable" warning
static if (!is(immutable E == immutable noreturn))
return null;
}

///
Expand Down Expand Up @@ -747,7 +751,9 @@ string collectExceptionMsg(T = Exception, E)(lazy E expression)
{
expression();

return cast(string) null;
// Avoid "statement not reachable" warning
static if (!is(immutable E == immutable noreturn))
return cast(string) null;
}
catch (T e)
return e.msg.empty ? emptyExceptionMsg : e.msg;
Expand All @@ -771,6 +777,25 @@ string collectExceptionMsg(T = Exception, E)(lazy E expression)
+/
enum emptyExceptionMsg = "<Empty Exception Message>";

// https://issues.dlang.org/show_bug.cgi?id=22364
@system unittest
{
static noreturn foo() { throw new Exception(""); }

const ex = collectException!(Exception, noreturn)(foo());
assert(ex);

const msg = collectExceptionMsg!(Exception, noreturn)(foo());
assert(msg);

noreturn n;

// Triggers a backend assertion failure
// collectException!(Exception, noreturn)(foo(), n);

static assert(__traits(compiles, collectException!(Exception, noreturn)(foo(), n)));
}

/**
* Casts a mutable array to an immutable array in an idiomatic
* manner. Technically, `assumeUnique` just inserts a cast,
Expand Down

0 comments on commit 1c8047b

Please sign in to comment.