Change how scope of & is computed #2979
Labels
A-lifetimes
Area: Lifetimes / regions
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
Right now we compute the scope of an expression like
&expr
in a pre-determined fashion. What we should do is to create a region variable and use that instead, with a lower bound of the&expr
itself but no upper bound (we should also use this technique for implicit borrows). Inference will then select the smallest scope it can for the resulting region. Finally, in borrowck, make sure thatexpr
can be guaranteed for the result. This will require some modifications to borrowck, I think, to be sure it takes loops and so forth into account for rooting—but that code makes sense to have anyway.The reason to make this change is that it allows us to select the lifetime of
&expr
in a variety of ways. Ifexpr
is a shared box, or owned by a shared box, we can use the rooting rules which can sometimes be the best. Otherwise, we can use the lifetime ofexpr
itself, which is sometimes the right choice. Basically we can't know until after inference what would be the true upper bound and so we have to check later in a second pass. This isn't a very clear bug report but I'll draw up some examples in the code of what I mean.This enhancement helps with sendable hashtables and would allow us to do things like return pointers to the interior of data structures in more cases.
The text was updated successfully, but these errors were encountered: