[Issue 22225] New: SumType: Some assignments should be able to execute in safe code

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Aug 19 19:57:18 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=22225

          Issue ID: 22225
           Summary: SumType: Some assignments should be able to execute in
                    safe code
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: snarwin+bugzilla at gmail.com

In general, assigning to a SumType that may contain a pointer or reference must
be @system, because it could overwrite a pointer or reference that @safe code
has access to, leading to undefined behavior:

---
int n;

int example() @safe
{
    SumType!(int*, int) x = &n;
    return x.match!(
        (int n) => n,
        (ref int* p) {
            x = 123456789; // overwrites p (currently @system)
            return *p; // kaboom
        }
    );
}
---

However, in the special case where only one member of the SumType contains
pointers or references, such an assignment could be @safe, because (a)
overwriting a non-pointer with a pointer is @safe, and (b) overwriting a
pointer with another pointer of the same type is @safe.

Example:

---
void main() @safe
{
        alias SM = SumType!(string, int);
        auto sm = SM(123);
        sm = SM("this should be @safe");
}
---

See also: https://github.com/pbackus/sumtype/issues/67

--


More information about the Digitalmars-d-bugs mailing list