Bug 11208 – returned ref to scoped isn't caught by local ref escape error
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-10-09T10:02:20Z
Last change time
2020-03-21T03:56:33Z
Assigned to
No Owner
Creator
Brad Anderson
Comments
Comment #0 by eco — 2013-10-09T10:02:20Z
Tested with dmd 2.63.2. Trying to return a ref int to a local int errors correctly as does a `scope A a = new A()`.
---
import std.typecons, std.stdio;
class A { }
// Using auto ref because I don't know how to get the type of scoped
// (I guess I should file this behavior as a bug too)
auto ref fun()
{
auto a = scoped!A();
writefln("%x", &a);
return a; // Compiler should output:
// Error: escaping reference to scope local a
}
void main()
{
auto a = fun();
writefln("%x", &a);
}
---
Comment #1 by b2.temp — 2017-09-14T16:18:26Z
scope documentation says:
> This facility is unsafe;
> it is the responsibility of the user to not escape a reference to the
> object outside the scope.
so it's invalid to the extent that it's specified.