D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 17388 - [scope] e[] should be treated like &e as far as scope goes
Summary: [scope] e[] should be treated like &e as far as scope goes
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 major
Assignee: No Owner
URL:
Keywords: safe
Depends on:
Blocks:
 
Reported: 2017-05-09 18:10 UTC by Per Nordlöw
Modified: 2017-08-16 13:24 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Per Nordlöw 2017-05-09 18:10:13 UTC
A file named `test_scope.d` containing

@safe pure nothrow @nogc:

struct S
{
    @safe pure nothrow @nogc
    inout(int)[] opSlice() inout return scope
    {
        return x[];
    }
    int[4] x;
}

int[] f()
{
    S s;
    return s[];
}

compiled with -dip1000 correctly errors as

test_scope.d(16,13): Error: escaping reference to local variable s

but if return type of `opSlice` is changed to `auto` the compiler acccepts it which is wrong.
Comment 1 Walter Bright 2017-08-10 00:39:25 UTC
This shrinks down a bit to:

struct S {
    int[]         // works
    //auto        // doesn't work
	foo() return @safe {
        return x[];
    }
    int[4] x;
}

@safe int[] f() {
    S s;
    return s.foo();
}
Comment 2 Walter Bright 2017-08-10 04:01:19 UTC
Note that it works if int[] is replaced by int*.

https://github.com/dlang/dmd/pull/7076
Comment 3 github-bugzilla 2017-08-10 12:16:55 UTC
Commits pushed to master at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/fc9c84ba1722c02f0c1877ffa39134c84a5e13e0
fix Issue 17388 - [scope] e[] should be treated like &e as far as scope goes

https://github.com/dlang/dmd/commit/09cd19d51b1e000dc81a61d167dc559480958f3e
Merge pull request #7076 from WalterBright/fix17388

fix Issue 17388 - [scope] e[] should be treated like &e as far as sco…
merged-on-behalf-of: Martin Nowak <code@dawg.eu>
Comment 4 github-bugzilla 2017-08-16 13:24:23 UTC
Commits pushed to stable at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/fc9c84ba1722c02f0c1877ffa39134c84a5e13e0
fix Issue 17388 - [scope] e[] should be treated like &e as far as scope goes

https://github.com/dlang/dmd/commit/09cd19d51b1e000dc81a61d167dc559480958f3e
Merge pull request #7076 from WalterBright/fix17388