Bug 4451 – Escaping reference to local array error for slices

Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-07-13T01:38:00Z
Last change time
2015-01-11T05:55:14Z
Keywords
accepts-invalid, diagnostic
Assigned to
nobody
Creator
bearophile_hugs
See also
http://d.puremagic.com/issues/show_bug.cgi?id=9651

Comments

Comment #0 by bearophile_hugs — 2010-07-13T01:38:09Z
A D2 program that currently (dmd v2.047) compiles: int[] foo() { int[10] stackArray; int[] dynArray = stackArray[1 .. 5]; return dynArray; } void main() {} In this case the compiler can give an error like: Error: escaping reference to local arr Like the one given with the code: int[] foo() { int[10] arr; return arr; } void main() {} Currently (dmd v2.047) even this compiles: int[] foo() { int[10] arr; return arr[]; } void main() {}
Comment #1 by 4burgos — 2014-08-27T10:16:39Z
This can lead to the subtle bugs like: import std.stdio; int[] foo(int val) { int[10] array; array[0] = val; return array[]; } void main() { auto val = foo(1); auto val2 = foo(2); writeln(val[0]); // Prints 2 }
Comment #2 by k.hara.pg — 2015-01-11T05:55:14Z
*** This issue has been marked as a duplicate of issue 13902 ***