[Issue 18282] [Scope][DIP1000]Assignment of local variable to `scope` variable not recognized by compiler
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Mar 12 11:20:25 UTC 2018
https://issues.dlang.org/show_bug.cgi?id=18282
ag0aep6g at gmail.com changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |ag0aep6g at gmail.com
--- Comment #2 from ag0aep6g at gmail.com ---
(In reply to Walter Bright from comment #1)
> (In reply to Mike Franklin from comment #0)
> > void main() @safe
> > {
> > string foo = "foo";
> > scope string*[] ls;
> > ls ~= &foo;
> > }
> >
> > Compile with `-dip1000`
> >
> > onlineapp.d(5): Error: reference to local variable foo assigned to non-scope
> > ls
>
> While ls is scope, ls[] is not scope. Scope is not transitive, hence the
> compiler error.
I assume that you don't mean `ls[]` literally, but rather that the elements of
`ls` aren't `scope`, right?
That means, it should be okay to return `ls[0]` because that's not `scope`,
right? But that doesn't work:
----
string* f() @safe
{
scope string*[] ls;
return ls[0]; /* Error: scope variable ls may not be returned */
}
----
On the other hand, this works:
----
void main() @safe
{
string foo = "foo";
scope string*[] ls;
ls = ls ~ &foo;
}
----
The only difference from the original code is `ls ~= &foo;` -> `ls = ls ~
&foo;`. Those two assignments should be equivalent [1], no? Either both should
be accepted, or both should be rejected.
[1] https://dlang.org/spec/expression.html#assignment_operator_expressions
--
More information about the Digitalmars-d-bugs
mailing list