Bug 12928 – Bounds check dropped for array[length]
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Mac OS X
Creation time
2014-06-16T02:22:00Z
Last change time
2014-06-17T11:57:58Z
Keywords
pull
Assigned to
lio+bugzilla
Creator
lio+bugzilla
Comments
Comment #0 by lio+bugzilla — 2014-06-16T02:22:10Z
Index expression with [length] is not valid and should through a Range violation exception, but this is not the case.
void main(string[] args)
{
int[2] a;
foreach(const i; 0..3)
a[i] = i;
}
Comment #1 by issues.dlang — 2014-06-16T03:24:35Z
On 64-bit Linux with the latest git master, this works perfectly fine for me. It throws a RangeError like it's supposed to.
Comment #2 by lio+bugzilla — 2014-06-16T04:41:02Z
(In reply to Jonathan M Davis from comment #1)
> On 64-bit Linux with the latest git master, this works perfectly fine for
> me. It throws a RangeError like it's supposed to.
You'll need the very latest to repro the test case, 34d970836b202f71d0a07c4b2a195a405b69bc6d.
Before that commit the compiler didn't know the range of the foreach iterator and would keep the bounds check. Here's a way to repro on older versions:
void main(string[] args)
{
int[3] a;
a[args.length&3] = 2;
}
Comment #3 by issues.dlang — 2014-06-16T04:50:47Z
I'm pretty sure that I updated within the last 24 hours, but it's possible that I don't have that particular commit yet. However, your second example does indeed fail to throw a RangeError.
Comment #4 by issues.dlang — 2014-06-16T04:56:21Z
Okay. I just updated again, and the first example does indeed now fail to throw a RangeError.