Bug 23933 – auto return type disables DIP1000 scope check
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2023-05-24T09:18:10Z
Last change time
2023-05-24T19:13:24Z
Keywords
accepts-invalid, safe
Assigned to
No Owner
Creator
Atila Neves
Comments
Comment #0 by atila.neves — 2023-05-24T09:18:10Z
This fails to compile with -preview=dip1000:
---------------
struct Struct {
int* p;
void hmm(int i) @safe { }
}
void safeUsage() @safe {
int i;
scope s = Struct();
s.hmm(42);
}
---------------
bug.d(9): Error: scope variable `s` assigned to non-scope parameter `this` calling `hmm`
But changing `hmm`'s return type to `auto` makes the error go away.
Comment #1 by razvan.nitu1305 — 2023-05-24T13:35:04Z
Under the current specification this is not a bug, even though the behavior is surprising. Attributes are inferred for auto functions because you a function body needs to be present. Attributes are not inferred for non-auto functions, even if the body is present.
Comment #2 by razvan.nitu1305 — 2023-05-24T13:36:17Z
[...] for auto functions because a function body needs to be present*
Comment #3 by razvan.nitu1305 — 2023-05-24T13:38:32Z
So, if we enable attribute inference for all functions that have a body, this will be fixed as a consequence. Or, the workaround would be to manually annotate `hmm` as scope.
Comment #4 by alphaglosined — 2023-05-24T19:13:24Z
The inference can be seen when you use auto instead of scope inside of safeUsage when you output the AST.
```d
struct Struct
{
int* p;
@safe void hmm(int i)
{
}
}
```
vs with scope & auto:
```d
struct Struct
{
int* p;
auto pure nothrow @nogc @safe void hmm(int i)
{
}
}
```
I don't see a bug here. This looks to be working correctly. A method that was not annotated with scope, used with scope, works when inference is turned on. So I'm closing.