Consider captured regions for opaque type region liveness.#156027
Consider captured regions for opaque type region liveness.#156027jackh726 wants to merge 8 commits into
Conversation
|
Until I can look at this next week, @bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Consider captured regions for opaque type region liveness.
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (97f0332): comparison URL. Overall result: no relevant changes - no action neededBenchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up. @rustbot label: -S-waiting-on-perf -perf-regression Instruction countThis perf run didn't have relevant results for this metric. Max RSS (memory usage)Results (primary -2.3%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (secondary 2.0%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeResults (primary 0.2%, secondary 0.2%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Bootstrap: 481.713s -> 483.106s (0.29%) |
There was a problem hiding this comment.
I also started down this direction so some of this makes sense to me. We stopped because it felt fragile and test coverage was, and still is, weak. So with that caveat this generally feels fine to me, but again, I don't know enough about this area to say what problems there could be.
This direction felt less nice than what lcnr described in the t-types meeting where we described the issue, so I'll defer to them.
| // Thinking about it, I was originally a bit concerned about something like `'a: 'static`, and | ||
| // whether or not we need to mark `'a` as live. I don't think *today* we do, since I think regions | ||
| // that outlive `'static` are special enough, but I *could* imagine some world where we need to be | ||
| // more careful about this. Given I can't find a test that goes wrong, I'm going to leave in this | ||
| // optimization. |
There was a problem hiding this comment.
I was also a bit worried, and we should expand test coverage here. I'm not confident enough in any of this to trust the two tests we have.
| // Unfortunately, we have to use a new `InferCtxt` each call, because | ||
| // region constraints get added and solved there and we need to test each | ||
| // call individually. |
There was a problem hiding this comment.
This is also unfortunate, combined with the fact that it's being called a quadratic number of times (thankfully the N should be small in most cases).
This comment has been minimized.
This comment has been minimized.
0718cad to
ca97579
Compare
|
@bors try |
This comment has been minimized.
This comment has been minimized.
Consider captured regions for opaque type region liveness.
|
@craterbot check |
|
👌 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
@craterbot p=2 We want to get this in so that we can do a nightly preview of Polonius |
|
📝 Configuration of the ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
🚧 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
🎉 Experiment
Footnotes
|
There was a problem hiding this comment.
Mainly a bunch of nits. From reading https://hackmd.io/rvBf2GtiRGKqUVIdDd5g2g I am still left feeling unclear why polonius cares about liveness the way it does, what additional constraints this puts on liveness (e.g. cases where something has to outlive 'static are fine for liveness apparently).
The other question I have is whether you see this having benefits outside of this specific need of polonius? I think yes?
| /// (and so is `T`, since `T: 'static` implies `T: 'a`) | ||
| /// - `bar` outlives `'static`, so we know that no args are potentially live and we can return an empty set | ||
| /// - `baz` has no outlives bound, so return `None` and let the caller decide what to do | ||
| query live_args_for_alias_from_outlives_bounds(kind: ty::AliasTyKind<'tcx>) -> &'tcx Option<ty::EarlyBinder<'tcx, Vec<ty::GenericArg<'tcx>>>> { |
There was a problem hiding this comment.
feel like as a query we want to just take a DefId and have a wrapper function that calls this query if necessary?
There was a problem hiding this comment.
The issue is that we search through the bounds on the item and need to match against the self type. Probably what this indicates is that item_bounds should potentially return something akin to ExistentialPredicate where there is no self type. But that's definitely out of scope for the current PR.
There was a problem hiding this comment.
There should no difference between AliasTyKind and DefId wrt what information they give you. Both are just a DefId, where AliasTyKind also has some information about DefKind in there.
Reconstructing an AliasTy from a DefId is intentionally vaguely annoying 🤔 even though I'd expect that the query system is significantly better at dealing with raw DefIds I guess it's whatever whether to use AliasTy or DefId here
There was a problem hiding this comment.
I'm going to leave this as AliasTyKind for now. It's really not clear to me how to make this use DefId correctly: both AliasTyKind::Projection and AliasTyKind::Inherent use DefKind::AssocTy, so I can't disambiguate on that; and, I can't immediately think of what else I would use here. And, passing AliasTy could be okay because it would be item-self generics, but that feels like a recipe for disaster: potentially mitigated with something like EarlyBinder<AliasTy>, but that's really ugly and still prone to failure.
| // If any of the outlives bounds are `'static`, then we know the alias | ||
| // doesn't capture *any* regions, so we can skip visiting any regions at all. | ||
| // | ||
| // I was originally a bit concerned about something like `'a: 'static`, and | ||
| // whether or not we need to mark `'a` as live. I don't think *today* we do, | ||
| // since I think regions that outlive `'static` are special enough, but I | ||
| // *could* imagine some world where we need to be more careful about this. | ||
| // Given I can't find a test that goes wrong, I'm going to leave in this optimization. |
There was a problem hiding this comment.
This does really hinge on "how do liveness and outlives requirements interact", which still feels unclear to me.
There was a problem hiding this comment.
I adjusted this comment a bit. I feel pretty strongly positive that we're good here.
| // A free region (e.g. `for<'a> T::Assoc<'a, 'b>: 'b`): no higher-ranked | ||
| // arg of the alias can be proven (by a caller) to outlive a free region | ||
| // today, and pinned args only ever match universal regions (which are | ||
| // always live), so we conservatively treat this as giving no | ||
| // restriction. | ||
| // | ||
| // NB: if we ever get implied bounds inside binders, a bound var *could* | ||
| // be assumed to outlive a free region (e.g. `for<'a> T::Assoc<'x, 'a>: 'x` | ||
| // with a declared `'b: 'a` on the assoc type implies `'a: 'x` under the | ||
| // binder), so this would need revisiting -- though the result would | ||
| // still only need to include bound-var positions. |
There was a problem hiding this comment.
vaguely confused by this. I feel like we would want to already consider the implied bounds from free -> bound regions here? Or well, this feels very easy to miss with -Zassumptions-on-binders
There was a problem hiding this comment.
This comment just wasn't correct. The tl;dr is that we could consider fewer lifetimes as live with the free region as the outlived region. That would allow more code to compile that does today, so sticking with the conservative choice for now (i.e. don't restrict any arg liveness).
c5d5540 to
39ae191
Compare
Polonius cares about liveness because for an outlives relationship to be relevant at a given point, both regions must be live.
So, I think the key think about
Yes, it does. This logic lets us consider few alias args as live outside borrowck too (which is needed to fix #42940). I have an (outdated) branch that does this: https://github.com/jackh726/rust/tree/opaque-liveness-42490 |
39ae191 to
b8c8868
Compare
| /// (and so is `T`, since `T: 'static` implies `T: 'a`) | ||
| /// - `bar` outlives `'static`, so we know that no args are potentially live and we can return an empty set | ||
| /// - `baz` has no outlives bound, so return `None` and let the caller decide what to do | ||
| query live_args_for_alias_from_outlives_bounds(kind: ty::AliasTyKind<'tcx>) -> &'tcx Option<ty::EarlyBinder<'tcx, Vec<ty::GenericArg<'tcx>>>> { |
There was a problem hiding this comment.
There should no difference between AliasTyKind and DefId wrt what information they give you. Both are just a DefId, where AliasTyKind also has some information about DefKind in there.
Reconstructing an AliasTy from a DefId is intentionally vaguely annoying 🤔 even though I'd expect that the query system is significantly better at dealing with raw DefIds I guess it's whatever whether to use AliasTy or DefId here
| let parent_param_env = tcx.param_env(parent_def_id); | ||
| tracing::debug!(?parent_param_env); | ||
|
|
||
| // Map the outlives regions to the parent regions |
There was a problem hiding this comment.
I mean concretely for this loop 🤔 this is a lot of nothing which feels unrelated to the actual semantics of this function?
Like, we take self_identity_args and give back a parent_outlives_regions. I do not understand why this is necessary or what exactly it even does. What does parent_outlives_regions represent, why do we need to do this etc?
bf2a68f to
203af9a
Compare
This comment has been minimized.
This comment has been minimized.
203af9a to
1912e4f
Compare
This comment was marked as outdated.
This comment was marked as outdated.
…arams, use tcx.assumed_wf_types, and test polonius_alpha in gat-outlives-implied test
NLLs introduced liveness to allow invalidations on dead loans, and its regions are sets of CFG points. The points where the regions are live, the regions they outlive (in a flow-insensitive manner) and vice-versa, and so on are used to compute the CFG points that represent regions. A loan is considered live at a point if the region where it was introduced contains this point (modulo kills, unimportant here). To support flow-sensitivity, polonius regions are sets of loans, and loan liveness needs to track the outlives relationships between regions more precisely: a loan is considered live if it is contained in a live region. Polonius thus needs the accurate liveness of regions to check if an invalidation is indeed an error. As an NLL superset, if a loan is contained in a dead region and is invalidated, polonius will accept that as well. If the region is unexpectedly dead like in the #153215 examples, this is of course a soundness issue, and this PR fixes the current inaccuracy in liveness for some regions related to aliases. The approach makes sense to me, and we've talked about it with jack and niko, and despite the complexity I think this is an improvement we need. There may be ways to simplify in the near future, a part of it is caused by the mapping and our current encoding of opaque types, etc. lcnr has told jack that we can land this if I'm also happy with it, and I am. It does get us closer to enabling polonius alpha on nightly. If there are more improvements to do after this handful of review rounds, I think it's fine to do them in follow-ups. Let's do this 🚀, @bors r+ |
Consider captured regions for opaque type region liveness. Fixes rust-lang#153215 For opaques, when we're calculating liveness for opaques, we want to consider any captured lifetimes that can outlive the opaque type, which is more than just the outlives bounds. r? lqd
…uwer Rollup of 24 pull requests Successful merges: - #150946 (intrinsics: Add a fallback for non-const libm float functions) - #158510 (Enable `static_position_independent_executables` on all gnu and musl targets) - #158541 (Move `std::io::Write` to `core::io`) - #158899 (Fix `unaligned_volatile_store` by removing `MemFlags::UNALIGNED`) - #156027 (Consider captured regions for opaque type region liveness.) - #156370 (Reject linked dylib EII default overrides) - #156508 (Infer all anonymous lifetimes in assoc consts as `'static`) - #157561 (rustdoc: do not include extra stuff in span) - #158617 (allow mGCA const arguments to fall back to anon consts) - #158645 (Fix splat ICEs and ban it in closures) - #158859 (Improve `-Zls` diagnostic message on `.rs` files) - #158988 (Redo `TokenStreamIter`) - #158347 (Improve generic parameters handling for #[diagnostic::on_const]) - #158384 (Allow BackwardIncompatibleDropHint in polonius legacy) - #158722 (delegation: do not always inherit `ConstArgHasType` predicates) - #158739 (view-types: HIR lowering) - #158877 (borrowck: Keep returned `path` from `best_blame_constraint()` consistent) - #158883 (tests: fix enum-match.rs to handle LLVM 23) - #158886 (Add documentation for the `no_std` attribute) - #158940 (Implement feature `char_to_u32`) - #158951 (Merge three `MaxUniverse`s into one) - #158961 (Reapply "LLVM 23: Run AssignGUIDPass in some places") - #158995 (Use REST API in linkchecker script) - #158996 ([compiler] Implement `PartialOrd` via `Ord` for `Span` and newtype_indexes)
Consider captured regions for opaque type region liveness. Fixes rust-lang#153215 For opaques, when we're calculating liveness for opaques, we want to consider any captured lifetimes that can outlive the opaque type, which is more than just the outlives bounds. r? lqd
View all comments
Fixes #153215
For opaques, when we're calculating liveness for opaques, we want to consider any captured lifetimes that can outlive the opaque type, which is more than just the outlives bounds.
r? lqd