Skip to content

Commit 1922206

Browse files
dkorpelclaude
andauthored
Fix dlang#19075 - Operator ^^= fails to compile for many numeric type combinations (dlang#23023)
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 68d582f commit 1922206

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

compiler/src/dmd/expressionsem.d

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13409,17 +13409,19 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
1340913409

1341013410
if (exp.e1.op == EXP.variable)
1341113411
{
13412-
// Rewrite: e1 = e1 ^^ e2
13412+
// Rewrite: e1 = cast(T)(e1 ^^ e2)
1341313413
e = new PowExp(exp.loc, exp.e1.syntaxCopy(), exp.e2);
13414+
e = new CastExp(exp.loc, e, exp.e1.type);
1341413415
e = new AssignExp(exp.loc, exp.e1, e);
1341513416
}
1341613417
else
1341713418
{
13418-
// Rewrite: ref tmp = e1; tmp = tmp ^^ e2
13419+
// Rewrite: ref tmp = e1; tmp = cast(T)(tmp ^^ e2)
1341913420
auto v = copyToTemp(STC.ref_, "__powtmp", exp.e1);
1342013421
auto de = new DeclarationExp(exp.e1.loc, v);
1342113422
auto ve = new VarExp(exp.e1.loc, v);
1342213423
e = new PowExp(exp.loc, ve, exp.e2);
13424+
e = new CastExp(exp.loc, e, exp.e1.type);
1342313425
e = new AssignExp(exp.loc, new VarExp(exp.e1.loc, v), e);
1342413426
e = new CommaExp(exp.loc, de, e);
1342513427
}

compiler/test/runnable/ctorpowtests.d

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,34 @@ int anotherPowTest()
266266

267267
/************************************/
268268

269+
// https://github.com/dlang/dmd/issues/19075
270+
// ^^= must compile for small integer types
271+
272+
void test19075()
273+
{
274+
byte bw = 10;
275+
bw ^^= 3;
276+
assert(bw == cast(byte) 1000);
277+
278+
ubyte ubw = 10;
279+
ubw ^^= 3;
280+
assert(ubw == cast(ubyte) 1000);
281+
282+
short sw = 100;
283+
sw ^^= 3;
284+
assert(sw == cast(short) 1_000_000);
285+
286+
ushort usw = 100;
287+
usw ^^= 3;
288+
assert(usw == cast(ushort) 1_000_000);
289+
}
290+
291+
/************************************/
292+
269293
void main()
270294
{
271295
assert(!__ctfe);
272296
assert(magicVariable()==2);
273297
test11159();
298+
test19075();
274299
}

0 commit comments

Comments
 (0)