Skip to content

Commit d03f144

Browse files
authored
fix Issue 22144 - ICE(dcast.d): Floating point exception in castTo::CastTo::visit(Expression*) at dmd/dcast.d:1702 (#12924)
1 parent 9337377 commit d03f144

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

src/dmd/dcast.d

+11-7
Original file line numberDiff line numberDiff line change
@@ -1665,14 +1665,18 @@ Expression castTo(Expression e, Scope* sc, Type t, Type att = null)
16651665
{
16661666
// T[n] sa;
16671667
// cast(U[])sa; // ==> cast(U[])sa[];
1668-
d_uns64 fsize = t1b.nextOf().size();
1669-
d_uns64 tsize = tob.nextOf().size();
1670-
if (((cast(TypeSArray)t1b).dim.toInteger() * fsize) % tsize != 0)
1668+
const fsize = t1b.nextOf().size();
1669+
const tsize = tob.nextOf().size();
1670+
if (fsize != tsize)
16711671
{
1672-
// copied from sarray_toDarray() in e2ir.c
1673-
e.error("cannot cast expression `%s` of type `%s` to `%s` since sizes don't line up", e.toChars(), e.type.toChars(), t.toChars());
1674-
result = ErrorExp.get();
1675-
return;
1672+
const dim = t1b.isTypeSArray().dim.toInteger();
1673+
if (tsize == 0 || (dim * fsize) % tsize != 0)
1674+
{
1675+
e.error("cannot cast expression `%s` of type `%s` to `%s` since sizes don't line up",
1676+
e.toChars(), e.type.toChars(), t.toChars());
1677+
result = ErrorExp.get();
1678+
return;
1679+
}
16761680
}
16771681
goto Lok;
16781682
}

src/dmd/e2ir.d

+2-5
Original file line numberDiff line numberDiff line change
@@ -918,11 +918,8 @@ elem *sarray_toDarray(const ref Loc loc, Type tfrom, Type tto, elem *e)
918918
uint fsize = cast(uint)tfrom.nextOf().size();
919919
uint tsize = cast(uint)tto.nextOf().size();
920920

921-
if ((dim * fsize) % tsize != 0)
922-
{
923-
// have to change to Internal Compiler Error?
924-
error(loc, "cannot cast %s to %s since sizes don't line up", tfrom.toChars(), tto.toChars());
925-
}
921+
// Should have been caught by Expression::castTo
922+
assert(tsize != 0 && (dim * fsize) % tsize == 0);
926923
dim = (dim * fsize) / tsize;
927924
}
928925
elem *elen = el_long(TYsize_t, dim);

test/fail_compilation/fail22144.d

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// https://issues.dlang.org/show_bug.cgi?id=22144
2+
/* TEST_OUTPUT
3+
---
4+
fail_compilation/fail22144.d(12): Error: cannot cast expression `zarray1` of type `int[0]` to `int[0][]` since sizes don't line up
5+
---
6+
*/
7+
void main()
8+
{
9+
int[0] zarray1;
10+
int[0][0] zarray2;
11+
12+
auto zslice1 = cast(int[0][])zarray1; // ICE -> Error
13+
auto zslice2 = cast(int[0][])zarray2; // ICE -> OK
14+
}

0 commit comments

Comments
 (0)