Description
Right now, the main loop of NLL region inference actually performs a lot of depth-first searches across the control-flow graph:
rust/src/librustc_mir/borrow_check/nll/region_infer/mod.rs
Lines 468 to 494 in 0f9c784
Basically, if we have a relation that R1: R2 @ P
, then we do a depth-first search in the control-flow graph, starting at P; so long as the nodes we visit are members of R2, we add that node into R1 (if not already present). That code is in dfs.rs
:
rust/src/librustc_mir/borrow_check/nll/region_infer/dfs.rs
Lines 46 to 89 in 0f9c784
(There is also a bit of special treatment around the exit from the CFG.)
This is pretty naive. I think what we should be doing is probably more like this:
- Compute a reachability relation as a big
BitMatrix
(there exists code to do this already inlibrustc_data_structures
).- If a point P can reach the end point, include the free regions in the reachability set.
- For a relation
R1: R2 @ P
, we can then intersectreachable(P)
withR2
to get "those members of R2 reachable from P".- Does this work? There might be some danger of R2 containing a point that is reachable from P, but not without leaving R2, and which hence should not be included?
Anyway, before we do a specific solution, though, it'd be great to have some specific benchmarks.