Thread overview
[Issue 20674] [DIP1000] inference of `scope` is easily confused
Mar 16, 2020
Walter Bright
Feb 19, 2021
ag0aep6g
May 09, 2021
Paul Backus
Aug 10, 2021
Dennis
Aug 10, 2021
Paul Backus
Sep 01, 2022
Walter Bright
Sep 17, 2022
Ate Eskola
Nov 03, 2022
timon.gehr@gmx.ch
Dec 17, 2022
Iain Buclaw
Jan 27, 2023
Dlang Bot
March 16, 2020
https://issues.dlang.org/show_bug.cgi?id=20674

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com
           Severity|normal                      |enhancement

--- Comment #1 from Walter Bright <bugzilla@digitalmars.com> ---
To do what this example asks for, data flow analysis would be required. This can be done, but is a large increase in implementation complexity and time to compile. It is not a bug that DFA is not currently done.

Marked as an enhancement.

--
February 19, 2021
https://issues.dlang.org/show_bug.cgi?id=20674

ag0aep6g <ag0aep6g@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=21209

--
May 09, 2021
https://issues.dlang.org/show_bug.cgi?id=20674

Paul Backus <snarwin+bugzilla@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |snarwin+bugzilla@gmail.com

--- Comment #2 from Paul Backus <snarwin+bugzilla@gmail.com> ---
Note that this can be worked around by explicitly declaring the local variable in the second example `scope`:

---
int* f()(int* p)
{
    static int g;
    g = 0;
    scope p2 = p; /* the only difference */
    return new int;
}

int* g() @safe
{
    int x;
    return f(&x); /* ok */
}
---

--
August 10, 2021
https://issues.dlang.org/show_bug.cgi?id=20674

Dennis <dkorpel@live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dkorpel@live.nl

--- Comment #3 from Dennis <dkorpel@live.nl> ---
(In reply to Walter Bright from comment #1)
> To do what this example asks for, data flow analysis would be required. This can be done, but is a large increase in implementation complexity and time to compile.

This puzzles me. The examples don't contain a single branch or loop. Surely you can infer scope here without creating a control-flow graph, right? Also, while I don't know exactly how the current inference works (it's not documented anywhere), it looks like it has a pretty complex loop already:

https://github.com/dlang/dmd/blob/8884ad0d232d300b424c025cf65ecd2623ec8df5/src/dmd/escape.d#L2176

--
August 10, 2021
https://issues.dlang.org/show_bug.cgi?id=20674

--- Comment #4 from Paul Backus <snarwin+bugzilla@gmail.com> ---
(In reply to Dennis from comment #3)
> (In reply to Walter Bright from comment #1)
> > To do what this example asks for, data flow analysis would be required. This can be done, but is a large increase in implementation complexity and time to compile.
> 
> This puzzles me. The examples don't contain a single branch or loop. Surely you can infer scope here without creating a control-flow graph, right?

Currently, in order to infer scope for `p`, you only need to check each usage of `p` for compliance with scope.

To make the example (and others like it) work, you would need to recursively check each usage of any variable that `p` might be assigned to (like `p2`), and each usage of any variable that any of *those* variables might be assigned to, and so on, until you cannot find any new variables to check.

What I've just described is a graph search problem. Whether the compiler actually builds an explicit control-flow graph or not, it amounts to the same thing.

--
September 01, 2022
https://issues.dlang.org/show_bug.cgi?id=20674

--- Comment #5 from Walter Bright <bugzilla@digitalmars.com> ---
(In reply to Dennis from comment #3)
> Surely you can infer scope here without creating a control-flow graph, right?

The trouble is, once you start doing a lame DFA, then people want a real DFA. Doing DFA in the front end will significantly slow it down.

--
September 17, 2022
https://issues.dlang.org/show_bug.cgi?id=20674

Ate Eskola <Ajieskola@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |Ajieskola@gmail.com

--
November 03, 2022
https://issues.dlang.org/show_bug.cgi?id=20674

timon.gehr@gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |timon.gehr@gmx.ch

--- Comment #6 from timon.gehr@gmx.ch ---
AFAIU DIP1000 just copies the lifetime attribute from the initializer to the variable, so I don't think you need anything more fancy than computing connected components.

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=20674

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P4

--
January 27, 2023
https://issues.dlang.org/show_bug.cgi?id=20674

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #7 from Dlang Bot <dlang-bot@dlang.rocks> ---
@dkorpel updated dlang/dmd pull request #14492 "Fix 20674, 23208, 23300 - improve `scope` inference" fixing this issue:

- Fix 20674, 23208, 23300, 23294 - improve `scope` inference

https://github.com/dlang/dmd/pull/14492

--