D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 11208 - returned ref to scoped isn't caught by local ref escape error
Summary: returned ref to scoped isn't caught by local ref escape error
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-10-09 10:02 UTC by Brad Anderson
Modified: 2020-03-21 03:56 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Brad Anderson 2013-10-09 10:02:20 UTC
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 basile-z 2017-09-14 16:18:26 UTC
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.