Bug 22041 – SumType assignments should work with pointers in @safe code
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2021-06-18T19:04:22Z
Last change time
2021-06-19T00:43:00Z
Keywords
safe
Assigned to
No Owner
Creator
Luís Ferreira
Comments
Comment #0 by contact — 2021-06-18T19:04:22Z
Pretty much self-explainatory.
This should work in @safe code:
```d
alias FooBar = SumType!(int*);
auto foobar = FooBar(new int);
foobar = FooBar(new int);
```
Comment #1 by dlang-bot — 2021-06-18T19:05:29Z
@ljmf00 created dlang/phobos pull request #8146 "sumtype: remove forced @system for opAssign preventing @safe code" fixing this issue:
- sumtype: remove forced @system for opAssign preventing @safe code
Fixes #22041 .
D programming language is moving towards @safe code and introducing new
@system code makes std.sumtype fundamentally useless when using it under @safe
code. Forcing @system when the users are using pointers is just wrong.
The simple example to prove it is the following:
```d
void main() @safe
{
int* thisIsNotUnsafe = new int;
}
```
Using pointers in D is not necessarily @system.
Moreover, this code is preventing the following code to compile in @safe:
```d
alias FooBar = SumType!string;
auto foobar = FooBar("foo");
foobar = FooBar("bar");
```
Signed-off-by: Luís Ferreira <[email protected]>
https://github.com/dlang/phobos/pull/8146
Comment #2 by snarwin+bugzilla — 2021-06-19T00:43:00Z
This would allow undefined behavior in @safe code:
---
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
}
);
}
---