In this issue, the terms "result location semantics", "result location", and "result pointer" are not intended to include the concept of "result types", which is a core part of Zig's type system whereby type information propagates "down" expression trees to indicate the type an expression "should" have. No change to result types is proposed here.
Zig has a feature known as Result Location Semantics (RLS), which in a nutshell guarantees that stores to aggregates do not construct temporaries by forwarding "result pointers" through assignment expressions and providing them to "primitive" expressions to store their result directly to. This feature originally had a few motivations, including:
- Guaranteed copy elision, even through multiple return values, for performance reasons
- Initializing pinned types, which were under consideration as a language feature
- Initializing async frames, which were also effectively pinned types
- Allowing functions to return self-referential values by directly referencing their result locations
However, RLS comes with several serious issues:
- Unexpected behavior when swapping struct fields: https://github.com/ziglang/zig/issues/12064
- Unexpected
undefined values when aggregate initialization is short-circuited with a control flow keyword
- Significant language specification and implementation complexity
- (i feel like more things which i forgot?)
Additionally, some of the original use cases don't really hold up:
- Pinned types have been rejected to maintain language simplicity in favour of userland solutions
- After https://github.com/ziglang/zig/issues/23446, async frames are unlikely to be distinct types, so cannot have "pinned" semantics; and even if they are distinct types, they will typically only be used by
std.Io implementations, so do not need such strict safety
- Returning self-referential values requires the ability to explicitly refer to a function's result location, which is a problematic language feature so has been rejected
At this point, the Zig core team broadly considers RLS to be a failed experiment. This issue is to remove the feature from the Zig language, thereby solving the associated footguns and simplifying the compiler implementation.
In this issue, the terms "result location semantics", "result location", and "result pointer" are **not** intended to include the concept of "result types", which is a core part of Zig's type system whereby type information propagates "down" expression trees to indicate the type an expression "should" have. No change to result types is proposed here.
---
Zig has a feature known as Result Location Semantics (RLS), which in a nutshell guarantees that stores to aggregates do not construct temporaries by forwarding "result pointers" through assignment expressions and providing them to "primitive" expressions to store their result directly to. This feature originally had a few motivations, including:
* Guaranteed copy elision, even through multiple return values, for performance reasons
* Initializing *pinned types*, which were under consideration as a language feature
* Initializing *async frames*, which were also effectively pinned types
* Allowing functions to return self-referential values by directly referencing their result locations
However, RLS comes with several serious issues:
* Unexpected behavior when swapping struct fields: https://github.com/ziglang/zig/issues/12064
* Unexpected `undefined` values when aggregate initialization is short-circuited with a control flow keyword
* Significant language specification and implementation complexity
* (i feel like more things which i forgot?)
Additionally, some of the original use cases don't really hold up:
* Pinned types [have been rejected](https://github.com/ziglang/zig/issues/7769) to maintain language simplicity in favour of userland solutions
* After https://github.com/ziglang/zig/issues/23446, async frames are unlikely to be distinct types, so cannot have "pinned" semantics; and even if they are distinct types, they will typically only be used by `std.Io` implementations, so do not need such strict safety
* Returning self-referential values requires the ability to explicitly refer to a function's result location, which is a problematic language feature so [has been rejected](https://github.com/ziglang/zig/issues/2765)
At this point, the Zig core team broadly considers RLS to be a failed experiment. This issue is to remove the feature from the Zig language, thereby solving the associated footguns and simplifying the compiler implementation.