Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Oct 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 28 additions & 3 deletions std/exception.d
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