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

Move expression.BinExp.checkOpAssignTypes to expressionsem #15755

Merged
merged 1 commit into from
Oct 30, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 0 additions & 147 deletions compiler/src/dmd/expression.d
Original file line number Diff line number Diff line change
Expand Up @@ -4133,153 +4133,6 @@ extern (C++) abstract class BinExp : Expression
return ErrorExp.get();
}

extern (D) final Expression checkOpAssignTypes(Scope* sc)
{
// At that point t1 and t2 are the merged types. type is the original type of the lhs.
Type t1 = e1.type;
Type t2 = e2.type;

// T opAssign floating yields a floating. Prevent truncating conversions (float to int).
// See https://issues.dlang.org/show_bug.cgi?id=3841.
// Should we also prevent double to float (type.isfloating() && type.size() < t2.size()) ?
if (op == EXP.addAssign || op == EXP.minAssign ||
op == EXP.mulAssign || op == EXP.divAssign || op == EXP.modAssign ||
op == EXP.powAssign)
{
if ((type.isintegral() && t2.isfloating()))
{
warning(loc, "`%s %s %s` is performing truncating conversion", type.toChars(), EXPtoString(op).ptr, t2.toChars());
}
}

// generate an error if this is a nonsensical *=,/=, or %=, eg real *= imaginary
if (op == EXP.mulAssign || op == EXP.divAssign || op == EXP.modAssign)
{
// Any multiplication by an imaginary or complex number yields a complex result.
// r *= c, i*=c, r*=i, i*=i are all forbidden operations.
const(char)* opstr = EXPtoString(op).ptr;
if (t1.isreal() && t2.iscomplex())
{
error(loc, "`%s %s %s` is undefined. Did you mean `%s %s %s.re`?", t1.toChars(), opstr, t2.toChars(), t1.toChars(), opstr, t2.toChars());
return ErrorExp.get();
}
else if (t1.isimaginary() && t2.iscomplex())
{
error(loc, "`%s %s %s` is undefined. Did you mean `%s %s %s.im`?", t1.toChars(), opstr, t2.toChars(), t1.toChars(), opstr, t2.toChars());
return ErrorExp.get();
}
else if ((t1.isreal() || t1.isimaginary()) && t2.isimaginary())
{
error(loc, "`%s %s %s` is an undefined operation", t1.toChars(), opstr, t2.toChars());
return ErrorExp.get();
}
}

// generate an error if this is a nonsensical += or -=, eg real += imaginary
if (op == EXP.addAssign || op == EXP.minAssign)
{
// Addition or subtraction of a real and an imaginary is a complex result.
// Thus, r+=i, r+=c, i+=r, i+=c are all forbidden operations.
if ((t1.isreal() && (t2.isimaginary() || t2.iscomplex())) || (t1.isimaginary() && (t2.isreal() || t2.iscomplex())))
{
error(loc, "`%s %s %s` is undefined (result is complex)", t1.toChars(), EXPtoString(op).ptr, t2.toChars());
return ErrorExp.get();
}
if (type.isreal() || type.isimaginary())
{
assert(global.errors || t2.isfloating());
e2 = e2.castTo(sc, t1);
}
}
if (op == EXP.mulAssign)
{
if (t2.isfloating())
{
if (t1.isreal())
{
if (t2.isimaginary() || t2.iscomplex())
{
e2 = e2.castTo(sc, t1);
}
}
else if (t1.isimaginary())
{
if (t2.isimaginary() || t2.iscomplex())
{
switch (t1.ty)
{
case Timaginary32:
t2 = Type.tfloat32;
break;

case Timaginary64:
t2 = Type.tfloat64;
break;

case Timaginary80:
t2 = Type.tfloat80;
break;

default:
assert(0);
}
e2 = e2.castTo(sc, t2);
}
}
}
}
else if (op == EXP.divAssign)
{
if (t2.isimaginary())
{
if (t1.isreal())
{
// x/iv = i(-x/v)
// Therefore, the result is 0
e2 = new CommaExp(loc, e2, new RealExp(loc, CTFloat.zero, t1));
e2.type = t1;
Expression e = new AssignExp(loc, e1, e2);
e.type = t1;
return e;
}
else if (t1.isimaginary())
{
Type t3;
switch (t1.ty)
{
case Timaginary32:
t3 = Type.tfloat32;
break;

case Timaginary64:
t3 = Type.tfloat64;
break;

case Timaginary80:
t3 = Type.tfloat80;
break;

default:
assert(0);
}
e2 = e2.castTo(sc, t3);
Expression e = new AssignExp(loc, e1, e2);
e.type = t1;
return e;
}
}
}
else if (op == EXP.modAssign)
{
if (t2.iscomplex())
{
error(loc, "cannot perform modulo complex arithmetic");
return ErrorExp.get();
}
}
return this;
}

extern (D) final bool checkIntegralBin()
{
bool r1 = e1.checkIntegral();
Expand Down
153 changes: 153 additions & 0 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,159 @@
return se;
}

private Expression checkOpAssignTypes(BinExp binExp, Scope* sc)
{
auto e1 = binExp.e1;
auto e2 = binExp.e2;
auto op = binExp.op;
auto type = binExp.type;
auto loc = binExp.loc;

// At that point t1 and t2 are the merged types. type is the original type of the lhs.
Type t1 = e1.type;
Type t2 = e2.type;

// T opAssign floating yields a floating. Prevent truncating conversions (float to int).
// See https://issues.dlang.org/show_bug.cgi?id=3841.
// Should we also prevent double to float (type.isfloating() && type.size() < t2.size()) ?
if (op == EXP.addAssign || op == EXP.minAssign ||
op == EXP.mulAssign || op == EXP.divAssign || op == EXP.modAssign ||
op == EXP.powAssign)
{
if ((type.isintegral() && t2.isfloating()))
{
warning(loc, "`%s %s %s` is performing truncating conversion", type.toChars(), EXPtoString(op).ptr, t2.toChars());
}
}

// generate an error if this is a nonsensical *=,/=, or %=, eg real *= imaginary
if (op == EXP.mulAssign || op == EXP.divAssign || op == EXP.modAssign)
{
// Any multiplication by an imaginary or complex number yields a complex result.
// r *= c, i*=c, r*=i, i*=i are all forbidden operations.
const(char)* opstr = EXPtoString(op).ptr;
if (t1.isreal() && t2.iscomplex())
{
error(loc, "`%s %s %s` is undefined. Did you mean `%s %s %s.re`?", t1.toChars(), opstr, t2.toChars(), t1.toChars(), opstr, t2.toChars());
return ErrorExp.get();
}
else if (t1.isimaginary() && t2.iscomplex())
{
error(loc, "`%s %s %s` is undefined. Did you mean `%s %s %s.im`?", t1.toChars(), opstr, t2.toChars(), t1.toChars(), opstr, t2.toChars());
return ErrorExp.get();
}
else if ((t1.isreal() || t1.isimaginary()) && t2.isimaginary())
{
error(loc, "`%s %s %s` is an undefined operation", t1.toChars(), opstr, t2.toChars());
return ErrorExp.get();
}
}

// generate an error if this is a nonsensical += or -=, eg real += imaginary
if (op == EXP.addAssign || op == EXP.minAssign)
{
// Addition or subtraction of a real and an imaginary is a complex result.
// Thus, r+=i, r+=c, i+=r, i+=c are all forbidden operations.
if ((t1.isreal() && (t2.isimaginary() || t2.iscomplex())) || (t1.isimaginary() && (t2.isreal() || t2.iscomplex())))
{
error(loc, "`%s %s %s` is undefined (result is complex)", t1.toChars(), EXPtoString(op).ptr, t2.toChars());
return ErrorExp.get();
}
if (type.isreal() || type.isimaginary())
{
assert(global.errors || t2.isfloating());
e2 = e2.castTo(sc, t1);
}
}
if (op == EXP.mulAssign)
{
if (t2.isfloating())
{
if (t1.isreal())
{
if (t2.isimaginary() || t2.iscomplex())
{
e2 = e2.castTo(sc, t1);

Check warning on line 306 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L306

Added line #L306 was not covered by tests
}
}
else if (t1.isimaginary())
{
if (t2.isimaginary() || t2.iscomplex())

Check warning on line 311 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L311

Added line #L311 was not covered by tests
{
switch (t1.ty)

Check warning on line 313 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L313

Added line #L313 was not covered by tests
{
case Timaginary32:
t2 = Type.tfloat32;
break;

Check warning on line 317 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L315-L317

Added lines #L315 - L317 were not covered by tests

case Timaginary64:
t2 = Type.tfloat64;
break;

Check warning on line 321 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L319-L321

Added lines #L319 - L321 were not covered by tests

case Timaginary80:
t2 = Type.tfloat80;
break;

Check warning on line 325 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L323-L325

Added lines #L323 - L325 were not covered by tests

default:
assert(0);
}
e2 = e2.castTo(sc, t2);

Check warning on line 330 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L330

Added line #L330 was not covered by tests
}
}
}
}
else if (op == EXP.divAssign)
{
if (t2.isimaginary())
{
if (t1.isreal())

Check warning on line 339 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L339

Added line #L339 was not covered by tests
{
// x/iv = i(-x/v)
// Therefore, the result is 0
e2 = new CommaExp(loc, e2, new RealExp(loc, CTFloat.zero, t1));
e2.type = t1;
Expression e = new AssignExp(loc, e1, e2);
e.type = t1;
return e;

Check warning on line 347 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L343-L347

Added lines #L343 - L347 were not covered by tests
}
else if (t1.isimaginary())

Check warning on line 349 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L349

Added line #L349 was not covered by tests
{
Type t3;
switch (t1.ty)

Check warning on line 352 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L351-L352

Added lines #L351 - L352 were not covered by tests
{
case Timaginary32:
t3 = Type.tfloat32;
break;

Check warning on line 356 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L354-L356

Added lines #L354 - L356 were not covered by tests

case Timaginary64:
t3 = Type.tfloat64;
break;

Check warning on line 360 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L358-L360

Added lines #L358 - L360 were not covered by tests

case Timaginary80:
t3 = Type.tfloat80;
break;

Check warning on line 364 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L362-L364

Added lines #L362 - L364 were not covered by tests

default:
assert(0);
}
e2 = e2.castTo(sc, t3);
Expression e = new AssignExp(loc, e1, e2);
e.type = t1;
return e;

Check warning on line 372 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L369-L372

Added lines #L369 - L372 were not covered by tests
}
}
}
else if (op == EXP.modAssign)
{
if (t2.iscomplex())
{
error(loc, "cannot perform modulo complex arithmetic");
return ErrorExp.get();

Check warning on line 381 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L380-L381

Added lines #L380 - L381 were not covered by tests
}
}
return binExp;
}

private Expression extractOpDollarSideEffect(Scope* sc, UnaExp ue)
{
Expression e0;
Expand Down
Loading