about summary refs log tree commit diff
path: root/compiler/rustc_middle/src/ty/predicate.rs
AgeCommit message (Collapse)AuthorLines
2025-09-08Make some matches non-exhaustive.Nicholas Nethercote-30/+2
Exhaustive match isn't necessary for these trivial cases, and some similar nearby methods are non-exhaustive.
2025-08-07Add a missing UpcastFrom impl in rustc_type_irFlorian Diebold-11/+1
2025-07-31Fix up size asserts.Nicholas Nethercote-0/+12
- Put them in the module that defines the type. - Add some `WithCachedTypeInfo<T>` asserts for consistency.
2025-07-18Auto merge of #143545 - compiler-errors:coroutine-obl, r=oli-obkbors-0/+1
`-Zhigher-ranked-assumptions`: Consider WF of coroutine witness when proving outlives assumptions ### TL;DR This PR introduces an unstable flag `-Zhigher-ranked-assumptions` which tests out a new algorithm for dealing with some of the higher-ranked outlives problems that come from auto trait bounds on coroutines. See: * rust-lang/rust#110338 While it doesn't fix all of the issues, it certainly fixed many of them, so I'd like to get this landed so people can test the flag on their own code. ### Background Consider, for example: ```rust use std::future::Future; trait Client { type Connecting<'a>: Future + Send where Self: 'a; fn connect(&self) -> Self::Connecting<'_>; } fn call_connect<C>(c: C) -> impl Future + Send where C: Client + Send + Sync, { async move { c.connect().await } } ``` Due to the fact that we erase the lifetimes in a coroutine, we can think of the interior type of the async block as something like: `exists<'r, 's> { C, &'r C, C::Connecting<'s> }`. The first field is the `c` we capture, the second is the auto-ref that we perform on the call to `.connect()`, and the third is the resulting future we're awaiting at the first and only await point. Note that every region is uniquified differently in the interior types. For the async block to be `Send`, we must prove that both of the interior types are `Send`. First, we have an `exists<'r, 's>` binder, which needs to be instantiated universally since we treat the regions in this binder as *unknown*[^exist]. This gives us two types: `{ &'!r C, C::Connecting<'!s> }`. Proving `&'!r C: Send` is easy due to a [`Send`](https://doc.rust-lang.org/nightly/std/marker/trait.Send.html#impl-Send-for-%26T) impl for references. Proving `C::Connecting<'!s>: Send` can only be done via the item bound, which then requires `C: '!s` to hold (due to the `where Self: 'a` on the associated type definition). Unfortunately, we don't know that `C: '!s` since we stripped away any relationship between the interior type and the param `C`. This leads to a bogus borrow checker error today! ### Approach Coroutine interiors are well-formed by virtue of them being borrow-checked, as long as their callers are invoking their parent functions in a well-formed way, then substitutions should also be well-formed. Therefore, in our example above, we should be able to deduce the assumption that `C: '!s` holds from the well-formedness of the interior type `C::Connecting<'!s>`. This PR introduces the notion of *coroutine assumptions*, which are the outlives assumptions that we can assume hold due to the well-formedness of a coroutine's interior types. These are computed alongside the coroutine types in the `CoroutineWitnessTypes` struct. When we instantiate the binder when proving an auto trait for a coroutine, we instantiate the `CoroutineWitnessTypes` and stash these newly instantiated assumptions in the region storage in the `InferCtxt`. Later on in lexical region resolution or MIR borrowck, we use these registered assumptions to discharge any placeholder outlives obligations that we would otherwise not be able to prove. ### How well does it work? I've added a ton of tests of different reported situations that users have shared on issues like rust-lang/rust#110338, and an (anecdotally) large number of those examples end up working straight out of the box! Some limitations are described below. ### How badly does it not work? The behavior today is quite rudimentary, since we currently discharge the placeholder assumptions pretty early in region resolution. This manifests itself as some limitations on the code that we accept. For example, `tests/ui/async-await/higher-ranked-auto-trait-11.rs` continues to fail. In that test, we must prove that a placeholder is equal to a universal for a param-env candidate to hold when proving an auto trait, e.g. `'!1 = 'a` is required to prove `T: Trait<'!1>` in a param-env that has `T: Trait<'a>`. Unfortunately, at that point in the MIR body, we only know that the placeholder is equal to some body-local existential NLL var `'?2`, which only gets equated to the universal `'a` when being stored into the return local later on in MIR borrowck. This could be fixed by integrating these assumptions into the type outlives machinery in a more first-class way, and delaying things to the end of MIR typeck when we know the full relationship between existential and universal NLL vars. Doing this integration today is quite difficult today. `tests/ui/async-await/higher-ranked-auto-trait-11.rs` fails because we don't compute the full transitive outlives relations between placeholders. In that test, we have in our region assumptions that some `'!1 = '!2` and `'!2 = '!3`, but we must prove `'!1 = '!3`. This can be fixed by computing the set of coroutine outlives assumptions in a more transitive way, or as I mentioned above, integrating these assumptions into the type outlives machinery in a more first-class way, since it's already responsible for the transitive outlives assumptions of universals. ### Moving forward I'm still quite happy with this implementation, and I'd like to land it for testing. I may work on overhauling both the way we compute these coroutine assumptions and also how we deal with the assumptions during (lexical/nll) region checking. But for now, I'd like to give users a chance to try out this new `-Zhigher-ranked-assumptions` flag to uncover more shortcomings. [^exist]: Instantiating this binder with infer regions would be incomplete, since we'd be asking for *some* instantiation of the interior types, not proving something for *all* instantiations of the interior types.
2025-07-15Add alias for ArgOutlivesPredicateMichael Goulet-0/+1
2025-07-15Implement other logicstiif-0/+3
2025-05-26add additional `TypeFlags` fast pathslcnr-0/+2
2025-04-24norm nested aliases before evaluating the parent goallcnr-4/+4
2025-03-15Fold visit into tyMichael Goulet-1/+1
2025-02-28reviewlcnr-14/+0
2025-02-28rework cycle handlinglcnr-4/+2
A cycle was previously coinductive if all steps were coinductive. Change this to instead considerm cycles to be coinductive if they step through at least one where-bound of an impl of a coinductive trait goal.
2025-02-25Teach structured errors to display short `Ty`Esteban Küber-6/+12
Make it so that every structured error annotated with `#[derive(Diagnostic)]` that has a field of type `Ty<'_>`, the printing of that value into a `String` will look at the thread-local storage `TyCtxt` in order to shorten to a length appropriate with the terminal width. When this happen, the resulting error will have a note with the file where the full type name was written to. ``` error[E0618]: expected function, found `((..., ..., ..., ...), ..., ..., ...)`` --> long.rs:7:5 | 6 | fn foo(x: D) { //~ `x` has type `(... | - `x` has type `((..., ..., ..., ...), ..., ..., ...)` 7 | x(); //~ ERROR expected function, found `(... | ^-- | | | call expression requires function | = note: the full name for the type has been written to 'long.long-type-14182675702747116984.txt' = note: consider using `--verbose` to print the full type name to the console ```
2025-02-22Greatly simplify lifetime captures in edition 2024Michael Goulet-8/+5
2025-01-18Get rid of `ToPolyTraitRef`Michael Goulet-10/+0
2025-01-06Add derived causes for host effect predicatesMichael Goulet-0/+22
2024-10-24Implement const effect predicate in new solverMichael Goulet-0/+4
2024-10-04Elaborate supertrait span correctly to label the error betterMichael Goulet-0/+4
2024-09-25Compiler: Rename "object safe" to "dyn compatible"León Orell Valerian Liehr-4/+4
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-1/+1
2024-07-29Reformat `use` declarations.Nicholas Nethercote-1/+2
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-07-08Rollup merge of #120248 - WaffleLapkin:bonk-ptr-object-casts, ↵Matthias Krüger-0/+8
r=compiler-errors,oli-obk,lnicola Make casts of pointers to trait objects stricter This is an attempt to `fix` https://github.com/rust-lang/rust/issues/120222 and https://github.com/rust-lang/rust/issues/120217. This is done by adding restrictions on casting pointers to trait objects. Before this PR the rules were as follows: > When casting `*const X<dyn A>` -> `*const Y<dyn B>`, principal traits in `A` and `B` must refer to the same trait definition (or no trait). With this PR the rules are changed to > When casting `*const X<dyn Src>` -> `*const Y<dyn Dst>` > - if `Dst` has a principal trait `DstP`, > - `Src` must have a principal trait `SrcP` > - `dyn SrcP` and `dyn DstP` must be the same type (modulo the trait object lifetime, `dyn T+'a` -> `dyn T+'b` is allowed) > - Auto traits in `Dst` must be a subset of auto traits in `Src` > - Not adhering to this is currently a FCW (warn-by-default + `FutureReleaseErrorReportInDeps`), instead of an error > - if `Src` has a principal trait `Dst` must as well > - this restriction will be removed in a follow up PR This ensures that 1. Principal trait's generic arguments match (no `*const dyn Tr<A>` -> `*const dyn Tr<B>` casts, which are a problem for [#120222](https://github.com/rust-lang/rust/issues/120222)) 2. Principal trait's lifetime arguments match (no `*const dyn Tr<'a>` -> `*const dyn Tr<'b>` casts, which are a problem for [#120217](https://github.com/rust-lang/rust/issues/120217)) 3. No auto traits can be _added_ (this is a problem for arbitrary self types, see [this comment](https://github.com/rust-lang/rust/pull/120248#discussion_r1463835350)) Some notes: - We only care about the metadata/last field, so you can still cast `*const dyn T` to `*const WithHeader<dyn T>`, etc - The lifetime of the trait object itself (`dyn A + 'lt`) is not checked, so you can still cast `*mut FnOnce() + '_` to `*mut FnOnce() + 'static`, etc - This feels fishy, but I couldn't come up with a reason it must be checked The diagnostics are currently not great, to say the least, but as far as I can tell this correctly fixes the issues. cc `@oli-obk` `@compiler-errors` `@lcnr`
2024-07-07Uplift elaborationMichael Goulet-1/+9
2024-07-04Align the changes to the lang decisionMaybe Lapkin-0/+8
2024-06-18Uplift the new trait solverMichael Goulet-6/+36
2024-06-13Address nitsMichael Goulet-0/+4
- Remove the ValuePairs glob import - Make DummyPairs -> ValuePairs::Dummy and make it bug more - Fix WC - Make interner return `impl IntoIterator`s
2024-06-13Finish uplifting all of structural_traitsMichael Goulet-0/+9
2024-06-12Stop passing traitref/traitpredicate by refMichael Goulet-1/+1
2024-05-28eagerly normalize when adding goalslcnr-6/+3
2024-05-23Remove `#[macro_use] extern crate tracing` from `rustc_middle`.Nicholas Nethercote-0/+1
2024-05-21Uplift OutlivesPredicate, remove a bunch of unnecessary associated types ↵Michael Goulet-21/+12
from Interner
2024-05-21Uplift binderMichael Goulet-136/+17
2024-05-19Make search graph generic over internerMichael Goulet-1/+5
2024-05-18Uplift GenericArgKind, CanonicalVarValues, QueryInputMichael Goulet-1/+0
and make NestedGoals generic
2024-05-17to_opt_poly_X_pred -> as_X_clauseMichael Goulet-2/+2
2024-05-16Uplift Goal to rustc_type_irMichael Goulet-50/+28
2024-05-16Make impls UpcastFrom, implement Upcast for UpcastFromMichael Goulet-89/+91
2024-05-16Make P parameter explicitMichael Goulet-16/+16
2024-05-16Rename ToPredicate for UpcastMichael Goulet-73/+73
2024-05-13Uplift AliasTyMichael Goulet-0/+1
2024-05-13split out AliasTy -> AliasTermMichael Goulet-3/+3
2024-05-11Apply nits, uplift ExistentialPredicate tooMichael Goulet-52/+28
2024-05-11Uplift `NormalizesTo`, `CoercePredicate`, and `SubtypePredicate`Michael Goulet-49/+11
2024-05-11Uplift `ExistentialTraitRef`, `ExistentialProjection`, `ProjectionPredicate`Michael Goulet-140/+7
2024-05-11Uplift `TraitPredicate`Michael Goulet-29/+2
2024-05-10Apply nits, make some bounds into supertraits on inherent traitsMichael Goulet-0/+2
2024-05-10Lift `TraitRef` into `rustc_type_ir`Michael Goulet-80/+4
2024-05-09Rename Generics::params to Generics::own_paramsMichael Goulet-2/+2
2024-04-29Remove `extern crate rustc_macros` from `rustc_middle`.Nicholas Nethercote-0/+1
2024-03-27Remove `Ord` from `Ty`, `Const`, and `Region`Oli Scherer-4/+4
2024-03-22Split out ImplPolarity and PredicatePolarityMichael Goulet-6/+6