Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semantics of scope(exit/success) modifying return value #4087

Open
dlangBugzillaToGithub opened this issue Apr 23, 2018 · 1 comment
Open

Comments

@dlangBugzillaToGithub
Copy link

johanengelen reported this on 2018-04-23T20:22:39Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=18793

CC List

  • RazvanN
  • Simen Kjaeraas

Description

When the return value is modified inside a scope(exit/success) block, the spec does not say what the semantics are.

A test program that demonstrates that the implementation exhibits different semantics depending on function call ABI (returned large structs are passed by ref):
```
import std.stdio;

struct Big {
    ulong a;
    ulong[4] b;
}

struct Small {
    ulong a;
    ulong b;
}

auto quirk(T)() {
    T result;

    scope(success)
    {
        result.a = 10;
    }
    
    return result;
}


void main() {
    auto small = quirk!Small();
    writeln(small);

    auto big = quirk!Big();
    writeln(big);
}
```


The issue is further complicated if the return expression also modifies the return value; i.e. we also need semantics for this testcase:
```
import std.stdio;

struct Big {
    ulong a;
    ulong[4] b;
}

struct Small {
    ulong a;
    ulong b;
}

auto ref addOne(T)(ref T t)
{
 	t.a += 1;
    return t;
}

auto quirk(T)() {
    T result;

    scope(exit)
    {
        result.a = 10;
    }
    
    return addOne(result);
}


void main() {
    auto small = quirk!Small();
    writeln(small);

    auto big = quirk!Big();
    writeln(big);
}
```
@dlangBugzillaToGithub
Copy link
Author

simen.kjaras commented on 2018-04-24T08:52:07Z

Worth noting: On win32, the Small struct used above is not small enough to be passed by value. The intended behavior shows up when Small.sizeof <= 8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant