Bug 20245 – DIP1000: Should infer scope when taking address of ref
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2019-09-26T06:37:35Z
Last change time
2021-08-22T07:42:19Z
Keywords
pull, safe
Assigned to
No Owner
Creator
Walter Bright
Comments
Comment #0 by bugzilla — 2019-09-26T06:37:35Z
For -preview=dip1000 :
@safe int* foo(ref int x) {
int* a = &x;
return a;
}
This should infer `scope` for `a`, and then issue an error on returning `a`.
The following should not compile:
@safe int** foo(ref scope int* x) {
int** a = &x;
return a;
}
because there's not way to attach `scope` to anything but the head, i.e. no scope pointers to scope pointers to int.
This should compile successfully:
@safe int* foo(return ref int x) {
int* a = &x;
return a;
}
Comment #1 by moonlightsentinel — 2021-03-15T07:35:10Z
Works fine with current master:
----------------------------------------------
@safe int** foo(ref scope int* x)
{
int** a = &x;
return a;
}
@safe int* fooRet(return ref int x)
{
int* a = &x;
return a;
}
----------------------------------------------
./generated/linux/release/64/dmd -c -o- -dip1000 dip_escape.d
dip_escape.d(3): Error: cannot take address of `scope` parameter `x` in `@safe` function `foo
Comment #2 by dkorpel — 2021-06-09T08:30:46Z
(In reply to moonlightsentinel from comment #1)
> Works fine with current master:
>
> ----------------------------------------------
> @safe int** foo(ref scope int* x)
> {
> int** a = &x;
> return a;
> }
That only works because `x` is a `scope` pointer, and the compiler can't give double `scope` to it when taking its address. The original example of a non-scope ref still compiles:
```
@safe:
int* foo(ref int x) {
int* a = &x;
return a;
}
void main() {
int* ptr;
{
int x;
ptr = foo(x);
}
// x escaped via ptr here
}
```
Comment #3 by dkorpel — 2021-06-10T18:11:54Z
*** Issue 21212 has been marked as a duplicate of this issue. ***
Comment #4 by dlang-bot — 2021-07-05T16:16:46Z
@dkorpel created dlang/dmd pull request #12812 "Fix issue 20245 - address of ref should be scope" fixing this issue:
- fix issue 20245 - address of ref should be scope
https://github.com/dlang/dmd/pull/12812
Comment #5 by dlang-bot — 2021-08-22T07:42:19Z
dlang/dmd pull request #12812 "Fix issue 20245 - address of ref should be scope" was merged into master:
- 47fa750bc76a5835fe8c547fed30133ed2a12a8a by dkorpel:
fix issue 20245 - address of ref should be scope
https://github.com/dlang/dmd/pull/12812