Skip to content

Commit 10f612d

Browse files
BorisCarvajaldlang-bot
authored andcommitted
Fix Issue 23172 - [REG2.100] Wrong cast inserted for ternary operator and non-int enums
1 parent cdd3e2a commit 10f612d

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/dmd/dcast.d

+8-3
Original file line numberDiff line numberDiff line change
@@ -1107,9 +1107,14 @@ MATCH implicitConvTo(Expression e, Type t)
11071107

11081108
MATCH visitCond(CondExp e)
11091109
{
1110-
auto result = visit(e);
1111-
if (result != MATCH.nomatch)
1112-
return result;
1110+
e.econd = e.econd.optimize(WANTvalue);
1111+
const opt = e.econd.toBool();
1112+
if (opt.isPresent())
1113+
{
1114+
auto result = visit(e);
1115+
if (result != MATCH.nomatch)
1116+
return result;
1117+
}
11131118

11141119
MATCH m1 = e.e1.implicitConvTo(t);
11151120
MATCH m2 = e.e2.implicitConvTo(t);

test/compilable/test23172.d

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// https://issues.dlang.org/show_bug.cgi?id=23172
2+
3+
enum E : ubyte { // `ubyte` is needed to trigger the bug
4+
A,
5+
B,
6+
}
7+
8+
struct S {
9+
E e;
10+
}
11+
12+
void compiles(bool b, S s) {
13+
E e = b ? E.A : s.e;
14+
}
15+
16+
void errors(bool b, const ref S s) {
17+
E e = b ? E.A : s.e;
18+
}
19+
20+
// from https://issues.dlang.org/show_bug.cgi?id=23188
21+
22+
enum Status : byte
23+
{
24+
A, B, C
25+
}
26+
27+
Status foo()
28+
{
29+
Status t = Status.A;
30+
const Status s = t;
31+
32+
return (s == Status.A) ? Status.B : s; // <-- here
33+
}

0 commit comments

Comments
 (0)