mirror of
https://github.com/allankoechke/rust.git
synced 2026-11-30 11:05:36 +00:00
Treat vars created for opaque subtyping as live everywhere Fixes rust-lang/rust#160669 r? lqd Let's look at a minimal variant of rust-lang/rust#160669 ```rust fn minimal(short: (), out: &'static ()) -> impl Sized { if true { return &short; //~ ERROR `short` does not live long enough } out } ``` That has the following (simplified) MIR: ```rust fn foo(_1: (), _2: &'?4 ()) -> Opaque(DefId(foo::{opaque#0}), []) { bb0: { switchInt(move _4) -> [0: bb2, otherwise: bb1]; } bb1: { _0 = &'?2 _1; // bb1[0] goto -> bb3; } bb2: { _0 = &'?3 (*_2); // bb2[3] goto -> bb3; } bb3: { return; } } ``` And the following region constraints: <img width="236" height="583" alt="image" src="https://github.com/user-attachments/assets/4c450ccd-1c5c-4a9c-a4f1-0875cf0e5553" /> Note `'?5` and `'?6`: these don't show up in the MIR graph because they arise from the `enable_subtyping` closure in [`relate_opaques`](c9b7f17889/compiler/rustc_borrowck/src/type_check/relate_tys.rs (L116)). Importantly (today), these regions are related to each other (and to other regions in hidden types) *at a single point*, but **are not live at any point**. For NLL this is not a problem (since liveness does not matter for outlives constraint propagation. For Polonius, this is an issue. The fix here is fairly simple: consider these lifetimes live at all points. This may be an *overestimate* of liveness, but this is all code that doesn't get called with the next solver. This is also the same reason that this is not an issue with the next solver. Opaque types are normalized in HIR typeck, so these hacky regions are never created. There are a couple alternative fixes that I think are ultimately not as good: 1) Equate the previous hidden type with the new hidden type using `Locations::All` - when [registering the goals](c9b7f17889/compiler/rustc_borrowck/src/type_check/relate_tys.rs (L161)) from equating these two, force `Locations::All` instead of `Locations::Single`. 2) [**Equate all uses, not just the final defining use, in borrowck**](59a4c62a0e) - this has us store any duplicates in `insert_hidden_type`, which then get equated with the "expected" hidden type in `apply_definition_site_hidden_types` with `Locations::All`. I think this is fine, but a lot less "local" logic. I'll note that for the next solver, we equate the hidden types with the return type in the signature with`Locations::Single`: <img width="289" height="586" alt="image" src="https://github.com/user-attachments/assets/34d15820-06e3-4205-9278-e82465dca139" /> (note, this MIR is slightly different: `'5` is `short`'s lifetime; `'6` is the lifetime from the normalized opaque (`&'?6 ()`); `'4` is the renumbered lifetime for the MIR body) Given that, perhaps (1) is the most faithful to the spirit of the next solver (that there is one hidden type that things get related to), but this seems like a *slightly* smaller-in-scope fix. In any case, I think any solution here is mostly just a waiting game for the next solver.