Bug 23101 – [std.sumtype] canMatch does not account ref
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2022-05-10T19:21:38Z
Last change time
2022-05-12T08:17:50Z
Keywords
pull
Assigned to
No Owner
Creator
João Lourenço
Comments
Comment #0 by jlourenco5691 — 2022-05-10T19:21:38Z
SumType allows this bit of code:
```
SumType!(int, string) st;
st.match!(
function ref int(string _) => assert(0),
function ref int(ref int i) => i,
);
```
However, it does not allow returning a pointer to i:
```
SumType!(int, string) st;
st.match!(
function ref int*(string _) => assert(0),
function ref int*(ref int i) => &i,
);
```
This is because `canMatch` does not account for `ref`. The template `valueTypes` stores all types of the member values and uses SumTypes's `get` function, which returns a `ref`. However, ref does not persist and the type is not sent to `canMatch` as a `ref`.
Comment #1 by jlourenco5691 — 2022-05-10T19:24:11Z
Because of this, when matching, `canMatch` will fail as it will test for a copy, and returning a reference of that value results in escaping it.
Comment #2 by dlang-bot — 2022-05-10T19:30:52Z
@iK4tsu created dlang/phobos pull request #8457 "Issue 23101 - [std.sumtype] canMatch does not account ref " fixing this issue:
- fix(sumtype): template canMatch does not account for ref when matching
The template `canMatch` does not account for `ref`.
The template `valueTypes` stores all types of the member values and uses SumTypes's `get` function, which returns a `ref`.
However, ref does not persist and the type is not sent to `canMatch` as a `ref`.
Because of this, when matching, `canMatch` will fail as it will test for a copy, and returning a reference of that value results in escaping it.
Fix Issue 23101
Signed-off-by: João Lourenço <[email protected]>
https://github.com/dlang/phobos/pull/8457
Comment #3 by jlourenco5691 — 2022-05-11T18:39:53Z
Sorry, correction of the bug report example:
```
SumType!(int, string) st;
st.match!(
function int*(string _) => assert(0),
function int*(ref int i) => &i,
);
```
Comment #4 by dlang-bot — 2022-05-12T08:17:50Z
dlang/phobos pull request #8457 "Issue 23101 - [std.sumtype] canMatch does not account ref " was merged into master:
- 6069c449303e8571b7b73c4fe50fc3088832d5fe by João Lourenço:
fix(sumtype): template canMatch does not account for ref when matching
The template `canMatch` does not account for `ref`.
The template `valueTypes` stores all types of the member values and uses SumTypes's `get` function, which returns a `ref`.
However, ref does not persist and the type is not sent to `canMatch` as a `ref`.
Because of this, when matching, `canMatch` will fail as it will test for a copy, and returning a reference of that value results in escaping it.
Fix Issue 23101
Signed-off-by: João Lourenço <[email protected]>
https://github.com/dlang/phobos/pull/8457