D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 18793 - Semantics of scope(exit/success) modifying return value
Summary: Semantics of scope(exit/success) modifying return value
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dlang.org (show other issues)
Version: D2
Hardware: All All
: P2 major
Assignee: No Owner
URL:
Keywords: industry
Depends on:
Blocks:
 
Reported: 2018-04-23 20:22 UTC by johanengelen
Modified: 2024-12-15 15:25 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 johanengelen 2018-04-23 20:22:39 UTC
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);
}
```
Comment 1 Simen Kjaeraas 2018-04-24 08:52:07 UTC
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.
Comment 2 dlangBugzillaToGithub 2024-12-15 15:25:00 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dlang.org/issues/4087

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB