D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 23101 - [std.sumtype] canMatch does not account ref
Summary: [std.sumtype] canMatch does not account ref
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 enhancement
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2022-05-10 19:21 UTC by João Lourenço
Modified: 2022-05-12 08:17 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 João Lourenço 2022-05-10 19:21:38 UTC
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 João Lourenço 2022-05-10 19:24:11 UTC
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 Dlang Bot 2022-05-10 19:30:52 UTC
@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 <jlourenco5691@gmail.com>

https://github.com/dlang/phobos/pull/8457
Comment 3 João Lourenço 2022-05-11 18:39:53 UTC
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 Dlang Bot 2022-05-12 08:17:50 UTC
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 <jlourenco5691@gmail.com>

https://github.com/dlang/phobos/pull/8457