Skip to content

Commit

Permalink
fix issue 12504 - Wrong 'cannot cover index range' error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Nils Lankila authored and dlang-bot committed Jul 2, 2020
1 parent ac0735b commit 432ee00
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/dmd/statementsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,8 @@ private extern (C++) final class StatementSemanticVisitor : Visitor
{
TypeSArray ta = cast(TypeSArray)tab;
IntRange dimrange = getIntRange(ta.dim);
// https://issues.dlang.org/show_bug.cgi?id=12504
dimrange.imax = SignExtendedNumber(dimrange.imax.value-1);
if (!IntRange.fromType(var.type).contains(dimrange))
{
fs.error("index type `%s` cannot cover index range 0..%llu",
Expand Down
24 changes: 24 additions & 0 deletions test/compilable/b12504.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// https://issues.dlang.org/show_bug.cgi?id=12504
void main()
{
{
int[0xFF + 1] sta;
foreach (ubyte i; 0 .. sta.length) {}
foreach (ubyte i, x; sta) {}
}
{
int[0x7F + 1] sta;
foreach (byte i; 0 .. sta.length) {}
foreach (byte i, x; sta) {}
}
{
int[0xFFFF + 1] sta;
foreach (ushort i; 0 .. sta.length) {}
foreach (ushort i, x; sta) {}
}
{
int[0x7FFF + 1] sta;
foreach (short i; 0 .. sta.length) {}
foreach (short i, x; sta) {}
}
}
36 changes: 36 additions & 0 deletions test/fail_compilation/b12504.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
TEST_OUTPUT:
---
fail_compilation/b12504.d(18): Error: cannot implicitly convert expression `257$?:32=u|64=LU$` of type `$?:32=uint|64=ulong$` to `ubyte`
fail_compilation/b12504.d(19): Error: index type `ubyte` cannot cover index range 0..257
fail_compilation/b12504.d(23): Error: cannot implicitly convert expression `129$?:32=u|64=LU$` of type `$?:32=uint|64=ulong$` to `byte`
fail_compilation/b12504.d(24): Error: index type `byte` cannot cover index range 0..129
fail_compilation/b12504.d(28): Error: cannot implicitly convert expression `65537$?:32=u|64=LU$` of type `$?:32=uint|64=ulong$` to `ushort`
fail_compilation/b12504.d(29): Error: index type `ushort` cannot cover index range 0..65537
fail_compilation/b12504.d(33): Error: cannot implicitly convert expression `32769$?:32=u|64=LU$` of type `$?:32=uint|64=ulong$` to `short`
fail_compilation/b12504.d(34): Error: index type `short` cannot cover index range 0..32769
---
*/
void main()
{
{
int[0xFF + 2] sta;
foreach (ubyte i; 0 .. sta.length) {}
foreach (ubyte i, x; sta) {}
}
{
int[0x7F + 2] sta;
foreach (byte i; 0 .. sta.length) {}
foreach (byte i, x; sta) {}
}
{
int[0xFFFF + 2] sta;
foreach (ushort i; 0 .. sta.length) {}
foreach (ushort i, x; sta) {}
}
{
int[0x7FFF + 2] sta;
foreach (short i; 0 .. sta.length) {}
foreach (short i, x; sta) {}
}
}

0 comments on commit 432ee00

Please sign in to comment.