[Issue 19743] New: Foreach loops desugar into code that does not work with
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Mar 16 06:49:54 UTC 2019
https://issues.dlang.org/show_bug.cgi?id=19743
Issue ID: 19743
Summary: Foreach loops desugar into code that does not work
with
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: normal
Priority: P1
Component: dmd
Assignee: nobody at puremagic.com
Reporter: monkeyworks12 at hotmail.com
I have the following code:
Needle* find(Haystack, Needle)(return scope Haystack haystack, scope Needle
needle) @safe
{
foreach (ref val; haystack)
if (val == needle)
return &val; //Error: scope variable __r2 may not be returned
return null;
}
void main() @safe
{
int[9] haystack = [0, 10, 5, 6, 2, 3, 9, 4, 5];
int* foundVal = haystack.find(3);
}
>From my understanding of `return scope` parameters, this should compile.
However, the foreach loop desugars into the following:
{
int[] __r2 = haystack[];
ulong __key3 = 0LU;
for (; __key3 < __r2.length; __key3 += 1LU)
{
ref int val = __r2[__key3];
if (val == needle)
return &val;
}
}
The slicing of the static array obscures the knowledge that `haystack` is
`return scope` from the compiler. Furthermore, as `__r2` is inferred as
`scope`, the compiler thinks that I am escaping a reference to a local `scope`
variable, and does not allow this code to compile.
Unfortunately, due to https://issues.dlang.org/show_bug.cgi?id=19742, writing a
manually-desugared version does not quite work:
for (auto i = 0; i < haystack.length; i++)
if (haystack[i] == needle)
return &haystack[i]; //Error: returning &haystack[cast(ulong)i] escapes
a reference to parameter haystack, perhaps annotate with return
--
More information about the Digitalmars-d-bugs
mailing list