Bug 2486 – taking address of slice rvalue should not be allowed

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2008-12-02T13:03:00Z
Last change time
2015-06-09T05:11:38Z
Keywords
accepts-invalid, pull
Assigned to
nobody
Creator
kamm-removethis

Comments

Comment #0 by kamm-removethis — 2008-12-02T13:03:24Z
I was surprised to see that following compiles and generates reasonable code: int[] a = [1, 2, 3] auto addr = &a[0..2]; Since the frontend seems to explicitly allow it, it's probably intentional and we've made LDC behave the same way. Could a description and rationale be added to the spec?
Comment #1 by k.hara.pg — 2011-11-10T03:09:27Z
This is still valid in D2. import std.stdio; void main() { int[] arr = [1,2,3]; auto p = &(arr[0..2]); // same as &(arr[0..2]) writeln(typeof(p).stringof); // int[]* writeln(arr.ptr); writeln(&arr); writeln(p); assert(&arr == p); } I think this is bad behavior, because it allows nonsense code like follows. void main() { int[] arr = [1,2,3]; foo(arr[0..2]); // ref parameter can receive lvalue made by slicing assert(arr == [1,2,3]); // assertion succeeds... } void foo(ref int[] arr) { arr = [4,5,6]; // what is modified? }
Comment #2 by bugzilla — 2012-01-22T20:13:33Z
I agree, and will fix the spec. So it's a compiler bug that this code is accepted. It should only work for const references. Changing to a compiler bug.
Comment #3 by k.hara.pg — 2012-01-24T05:38:49Z
Comment #4 by k.hara.pg — 2012-02-23T02:38:57Z
I found related bug that returning slice by auto ref causes an error. ---- struct S { int[] a; auto ref opSlice(){ return a[]; } // line 4 } void main() { S s; s[]; } Output: ---- test.d(4): Error: slice expression this.a[] is not a modifiable lvalue
Comment #5 by k.hara.pg — 2012-12-02T22:22:32Z
Comment #6 by github-bugzilla — 2012-12-06T21:50:15Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/7062ce191a651ca94fc779ac646a5ef5653b4559 fix Issue 2486 - taking address of slice rvalue is valid https://github.com/D-Programming-Language/phobos/commit/af4fea78550216647dbf3e4aaa7d89ab1b79f542 Merge pull request #989 from 9rnsr/fix2486 Issue 2486 - taking address of slice rvalue is valid
Comment #7 by yebblies — 2013-01-06T18:45:08Z
*** Issue 9270 has been marked as a duplicate of this issue. ***
Comment #8 by github-bugzilla — 2013-03-05T22:52:16Z
Comment #9 by andrej.mitrovich — 2013-03-06T05:52:43Z
Changing title to make it easier to understand in changelog.
Comment #10 by andrej.mitrovich — 2013-03-07T18:50:48Z
*** Issue 9651 has been marked as a duplicate of this issue. ***
Comment #11 by github-bugzilla — 2013-03-11T22:05:59Z
Comment #12 by yebblies — 2013-07-27T23:35:48Z
Appears to be fixed