Issue 12944 - std.variant does not observe value semantics for large value types.
Summary: std.variant does not observe value semantics for large value types.
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-06-19 01:30 UTC by Kapps
Modified: 2024-12-01 16:21 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 Kapps 2014-06-19 01:30:17 UTC
Assigning a Variant to another Variant which contains a large value type causes both Variants to have a reference to the same value. Currently this is not a huge issue because the only way to modify the Variant's reference to a large value type (that is, larger than Variant.size which is 32 bytes on x64), is through the use of peek on a struct. This will become a larger issue when std.variant supports static arrays however.

Sample:
import std.variant;

struct Foo {
    int foo;
    ubyte[32] padding;
}

void main() {
    Foo f;
    f.foo = 3;
    Variant v = f;
    Variant v2 = v;
    auto fp = v.peek!Foo;
    fp.foo = 6;
    assert(v2.get!Foo.foo == 3); // fails
}

Commenting out the padding makes this work as expected.
Comment 1 Kapps 2014-06-19 01:38:47 UTC
Also worth noting that fixing this would require all peek and operations that alter the value (i.e., opIndexAssign) to allocate if storing a reference to another Variant. https://github.com/D-Programming-Language/phobos/pull/2188#issuecomment-44763385 has some discussion on this. It could get somewhat expensive, but would only be in the rare situations where this bug could be observed.
Comment 2 dlangBugzillaToGithub 2024-12-01 16:21:29 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/10062

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