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 Issue 23549, 22587 - Lower certain noreturn expressions to a comm… #14698

Merged
merged 1 commit into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
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
28 changes: 27 additions & 1 deletion compiler/src/dmd/dcast.d
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,14 @@ Expression implicitCastTo(Expression e, Scope* sc, Type t)
{
Expression visit(Expression e)
{
//printf("Expression.implicitCastTo(%s of type %s) => %s\n", e.toChars(), e.type.toChars(), t.toChars());
// printf("Expression.implicitCastTo(%s of type %s) => %s\n", e.toChars(), e.type.toChars(), t.toChars());

if (const match = (sc && sc.flags & SCOPE.Cfile) ? e.cimplicitConvTo(t) : e.implicitConvTo(t))
{
if (match == MATCH.convert && e.type.isTypeNoreturn())
{
return specialNoreturnCast(e, t);
}
if (match == MATCH.constant && (e.type.constConv(t) || !e.isLvalue() && e.type.equivalent(t)))
{
/* Do not emit CastExp for const conversions and
Expand Down Expand Up @@ -1526,6 +1530,10 @@ Expression castTo(Expression e, Scope* sc, Type t, Type att = null)
{
return e;
}
if (e.type.isTypeNoreturn())
{
return specialNoreturnCast(e, t);
}
if (auto ve = e.isVarExp())
{
VarDeclaration v = ve.var.isVarDeclaration();
Expand Down Expand Up @@ -3863,3 +3871,21 @@ IntRange getIntRange(Expression e)
case EXP.negate : return visitNeg(e.isNegExp());
}
}
/**
* A helper function to "cast" from expressions of type noreturn to
* any other type - noreturn is implicitly convertible to any other type.
* However, the dmd backend does not like a naive cast from a noreturn expression
* (particularly an `assert(0)`) so this function generates:
*
* `(assert(0), value)` instead of `cast(to)(assert(0))`.
*
* `value` is currently `to.init` however it cannot be read so could be made simpler.
* Params:
* toBeCasted = Expression of type noreturn to cast
* to = Type to cast the expression to.
* Returns: A CommaExp, upon any failure ErrorExp will be returned.
*/
Expression specialNoreturnCast(Expression toBeCasted, Type to)
{
return Expression.combine(toBeCasted, to.defaultInitLiteral(toBeCasted.loc));
}
2 changes: 2 additions & 0 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -12217,10 +12217,12 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
if (t1.ty == Tnoreturn)
{
exp.type = t2;
exp.e1 = specialNoreturnCast(exp.e1, exp.type);
}
else if (t2.ty == Tnoreturn)
{
exp.type = t1;
exp.e2 = specialNoreturnCast(exp.e2, exp.type);
}
// If either operand is void the result is void, we have to cast both
// the expression to void so that we explicitly discard the expression
Expand Down
11 changes: 11 additions & 0 deletions compiler/test/compilable/noreturn1.d
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,14 @@ void noreturnImplicit()
auto y = (throw new Exception("wow")) + value;
}
}

// https://issues.dlang.org/show_bug.cgi?id=23549
int foo(int g = assert(0)) {
return g;
}

// https://issues.dlang.org/show_bug.cgi?id=22587
int front(int param)
{
return param ? 1 : assert(0);
}