mirror of
https://github.com/dlang/phobos.git
synced 2025-04-28 22:21:09 +03:00
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:
parent
12b43809a3
commit
1c8047bc6d
1 changed files with 28 additions and 3 deletions
|
@ -666,7 +666,9 @@ T collectException(T = Exception, E)(lazy E expression, ref E result)
|
||||||
{
|
{
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
return null;
|
// Avoid "statement not reachable" warning
|
||||||
|
static if (!is(immutable E == immutable noreturn))
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
///
|
///
|
||||||
@system unittest
|
@system unittest
|
||||||
|
@ -711,7 +713,9 @@ T collectException(T : Throwable = Exception, E)(lazy E expression)
|
||||||
{
|
{
|
||||||
return t;
|
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();
|
expression();
|
||||||
|
|
||||||
return cast(string) null;
|
// Avoid "statement not reachable" warning
|
||||||
|
static if (!is(immutable E == immutable noreturn))
|
||||||
|
return cast(string) null;
|
||||||
}
|
}
|
||||||
catch (T e)
|
catch (T e)
|
||||||
return e.msg.empty ? emptyExceptionMsg : e.msg;
|
return e.msg.empty ? emptyExceptionMsg : e.msg;
|
||||||
|
@ -771,6 +777,25 @@ string collectExceptionMsg(T = Exception, E)(lazy E expression)
|
||||||
+/
|
+/
|
||||||
enum emptyExceptionMsg = "<Empty Exception Message>";
|
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
|
* Casts a mutable array to an immutable array in an idiomatic
|
||||||
* manner. Technically, `assumeUnique` just inserts a cast,
|
* manner. Technically, `assumeUnique` just inserts a cast,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue