Fix 22364 - Omit unreachable return in collectException[Msg]...

...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`.
This commit is contained in:
MoonlightSentinel 2021-10-07 21:36:45 +02:00 committed by The Dlang Bot
parent 12b43809a3
commit 1c8047bc6d

View file

@ -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
@ -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;
}
///
@ -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;
@ -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,