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

fix issue 12504 - Wrong 'cannot cover index range' error message #11359

Merged
merged 1 commit into from Jul 2, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {}
}
}