Bug 22225 – SumType: Some assignments should be able to execute in safe code
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-08-19T19:57:18Z
Last change time
2021-08-19T23:07:18Z
Keywords
pull
Assigned to
No Owner
Creator
Paul Backus
Comments
Comment #0 by snarwin+bugzilla — 2021-08-19T19:57:18Z
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
Comment #1 by dlang-bot — 2021-08-19T20:59:33Z
@pbackus created dlang/phobos pull request #8201 "Fix Issue 22225 - SumType: Some assignments should be able to execute…" fixing this issue:
- Fix Issue 22225 - SumType: Some assignments should be able to execute in safe code
https://github.com/dlang/phobos/pull/8201
Comment #2 by dlang-bot — 2021-08-19T23:07:18Z
dlang/phobos pull request #8201 "Fix Issue 22225 - SumType: Some assignments should be able to execute…" was merged into master:
- 895edd4260bd953fa1420cda06746287b7abb5eb by Paul Backus:
Fix Issue 22225 - SumType: Some assignments should be able to execute in safe code
https://github.com/dlang/phobos/pull/8201