about summary refs log tree commit diff
path: root/compiler/rustc_trait_selection/src/traits
AgeCommit message (Collapse)AuthorLines
2025-07-20Consider param-env for fast pathMichael Goulet-4/+22
2025-07-18Auto merge of #143545 - compiler-errors:coroutine-obl, r=oli-obkbors-25/+89
`-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-17Eagerly unify coroutine witness in old solverMichael Goulet-9/+40
2025-07-17Unstall obligations by looking for coroutines in old solverMichael Goulet-7/+32
2025-07-17Auto merge of #144044 - fmease:rollup-kg413pt, r=fmeasebors-0/+7
Rollup of 15 pull requests Successful merges: - rust-lang/rust#142304 (tests: Add `RUST_BACKTRACE` and `-Cpanic` revisions to `panic-main.rs` test) - rust-lang/rust#143388 (Various refactors to the LTO handling code) - rust-lang/rust#143409 (Enable xgot feature for mips64 musl targets) - rust-lang/rust#143592 (UWP: link ntdll functions using raw-dylib) - rust-lang/rust#143595 (add `const_make_global`; err for `const_allocate` ptrs if didn't call) - rust-lang/rust#143678 (Added error for invalid char cast) - rust-lang/rust#143820 (Fixed a core crate compilation failure when enabling the `optimize_for_size` feature on some targets) - rust-lang/rust#143829 (Trim `BorrowedCursor` API) - rust-lang/rust#143851 (ci cleanup: rustdoc-gui-test now installs browser-ui-test) - rust-lang/rust#143856 (Linting public reexport of private dependencies) - rust-lang/rust#143895 (Dont collect assoc ty item bounds from trait where clause for host effect predicates) - rust-lang/rust#143922 (Improve path segment joining) - rust-lang/rust#143964 (Fix handling of SCRIPT_ARG in docker images) - rust-lang/rust#144002 (Update poison.rs) - rust-lang/rust#144016 (trait_sel: `MetaSized` always holds temporarily) r? `@ghost` `@rustbot` modify labels: rollup
2025-07-16trait_sel: `MetaSized` always holds temporarilyDavid Wood-0/+7
As a temporary measure while a proper fix for `tests/ui/sized-hierarchy/incomplete-inference-issue-143992.rs` is implemented, make `MetaSized` obligations always hold. In effect, temporarily reverting the `sized_hierarchy` feature. This is a small change that can be backported.
2025-07-15Add alias for ArgOutlivesPredicateMichael Goulet-1/+1
2025-07-15Consider outlives assumptions when proving auto traits for coroutine interiorsMichael Goulet-25/+89
2025-07-15Implement other logicstiif-0/+10
2025-07-15Add the core logic in old and new solverstiif-2/+19
2025-07-14Rollup merge of #143901 - compiler-errors:region-constraint-nits, r=lcnrSamuel Tardieu-4/+1
Region constraint nits Couple miscellaneous region constraints that have a bit to do with rust-lang/rust#143545 but stand on their own.
2025-07-13Simplify make_query_region_constraintsMichael Goulet-4/+1
2025-07-13Retire hir::*ItemRef.Camille GILLOT-2/+2
2025-07-10Rollup merge of #143640 - oli-obk:const-fn-traits, r=compiler-errorsMatthias Krüger-1/+50
Constify `Fn*` traits r? `@compiler-errors` `@fee1-dead` this should unlock a few things. A few `const_closures` tests have broken even more than before, but that feature is marked as incomplete anyway cc rust-lang/rust#67792
2025-07-08Instantiate binder for Copy/Clone/Sized eagerlyMichael Goulet-196/+222
2025-07-08Instantiate auto trait before computing higher-ranked constituent typesMichael Goulet-16/+23
2025-07-08Remove `const_eval_select` hackOli Scherer-5/+2
2025-07-08Constify `Fn*` traitsOli Scherer-1/+53
2025-07-07remove `has_nested`lcnr-59/+52
2025-07-04Rollup merge of #143308 - compiler-errors:no-pointer-like, r=oli-obkMatthias Krüger-78/+1
Remove `PointerLike` trait r? oli-obk
2025-07-04Rollup merge of #143307 - compiler-errors:fast-path-nitpicks, r=lcnrJacob Pratt-15/+6
Fast path nitpicks Miscellaneous commits that I didn't really want to fold into anything else. Fixes one theoretical bug with the fast path not considering polarity for `T: !Sized` bounds.
2025-07-03Remove PointerLike traitMichael Goulet-78/+1
2025-07-03Rollup merge of #134006 - klensy:typos, r=nnethercoteJana Dönszelmann-5/+5
setup typos check in CI This allows to check typos in CI, currently for compiler only (to reduce commit size with fixes). With current setup, exclude list is quite short, so it worth trying? Also includes commits with actual typo fixes. MCP: https://github.com/rust-lang/compiler-team/issues/817 typos check currently turned for: * ./compiler * ./library * ./src/bootstrap * ./src/librustdoc After merging, PRs which enables checks for other crates (tools) can be implemented too. Found typos will **not break** other jobs immediately: (tests, building compiler for perf run). Job will be marked as red on completion in ~ 20 secs, so you will not forget to fix it whenever you want, before merging pr. Check typos: `python x.py test tidy --extra-checks=spellcheck` Apply typo fixes: `python x.py test tidy --extra-checks=spellcheck:fix` (in case if there only 1 suggestion of each typo) Current fail in this pr is expected and shows how typo errors emitted. Commit with error will be removed after r+.
2025-07-03setup CI and tidy to use typos for spellchecking and fix few typosklensy-5/+5
2025-07-02Rollup merge of #143235 - compiler-errors:const-item-bound, r=oli-obkMatthias Krüger-1/+66
Assemble const bounds via normal item bounds in old solver too Fixes the first example in https://rust-lang.zulipchat.com/#narrow/channel/144729-t-types/topic/elaboration.20of.20const.20bounds.3F/with/526378135 The code duplication here is not that nice, but it's at least very localized. cc `@davidtwco` r? oli-obk
2025-07-02Consider polarity in sizedness fast pathMichael Goulet-3/+4
2025-07-02Use is_trivially_wf for ProvePredicate fast pathMichael Goulet-12/+2
2025-07-01Remove support for dyn*Michael Goulet-1/+0
2025-06-30Assemble const bounds via normal item bounds in old solver tooMichael Goulet-1/+66
2025-06-27Rollup merge of #139858 - oli-obk:new-const-traits-syntax, r=fee1-deadMatthias Krüger-10/+10
New const traits syntax This PR only affects the AST and doesn't actually change anything semantically. All occurrences of `~const` outside of libcore have been replaced by `[const]`. Within libcore we have to wait for rustfmt to be bumped in the bootstrap compiler. This will happen "automatically" (when rustfmt is run) during the bootstrap bump, as rustfmt converts `~const` into `[const]`. After this we can remove the `~const` support from the parser Caveat discovered during impl: there is no legacy bare trait object recovery for `[const] Trait` as that snippet in type position goes down the slice /array parsing code and will error r? ``@fee1-dead`` cc ``@nikomatsakis`` ``@traviscross`` ``@compiler-errors``
2025-06-27Auto merge of #142223 - compiler-errors:perf-wf, r=lcnrbors-2/+10
Fast path for WF goals in new solver Hopefully self-explanatory.
2025-06-26Rollup merge of #142927 - compiler-errors:note-find-const, r=BoxyUwUMichael Goulet-2/+2
Add note to `find_const_ty_from_env` Add a note to `find_const_ty_from_env` to explain why it has an `unwrap` which "often" causes ICEs. Also, uplift it into the new trait solver. This avoids needing to go through the interner to call this method which is otherwise an inherent method in the compiler. I can remove this part if desired. r? `@boxyuwu`
2025-06-26Rollup merge of #142647 - compiler-errors:less-work-in-coherence, r=lcnrMichael Goulet-17/+26
[perf] Compute hard errors without diagnostics in impl_intersection_has_impossible_obligation First compute hard errors without diagnostics, then ambiguities with diagnostics since we need to know if any of them overflowed.
2025-06-26Rollup merge of #142637 - compiler-errors:less-globs, r=lcnrMichael Goulet-45/+47
Remove some glob imports from the type system Namely, remove the glob imports for `BoundRegionConversionTime`, `RegionVariableOrigin`, `SubregionOrigin`, `TyOrConstInferVar`, `RegionResolutionError`, `SelectionError`, `ProjectionCandidate`, `ProjectionCandidateSet`, and some more specific scoped globs (like `Inserted` in the impl overlap graph construction. These glob imports are IMO very low value, since they're not used nearly as often as other globs (like `TyKind`).
2025-06-26const-eval: allow constants to refer to mutable/external memory, but reject ↵Ralf Jung-2/+4
such constants as patterns
2025-06-26Change const trait bound syntax from ~const to [const]Oli Scherer-10/+10
2025-06-25Remove some glob imports from the type systemMichael Goulet-45/+47
2025-06-25Compute hard errors without diagnostics in ↵Michael Goulet-17/+26
impl_intersection_has_impossible_obligation
2025-06-24Apply fast path to old solver tooMichael Goulet-2/+10
2025-06-23Add note to find_const_ty_from_envMichael Goulet-2/+2
2025-06-22Rollup merge of #142458 - oli-obk:dyn-incompat, r=compiler-errorsGuillaume Gomez-1/+1
Merge unboxed trait object error suggestion into regular dyn incompat error Another hir-walker removed from the well-formed queries. This error was always a duplicate of another, but it was able to provide more information because it could invoke `is_dyn_compatible` without worrying about cycle errors. That's also the reason we can't put the error directly into hir_ty_lowering when lowering a `dyn Trait` within an associated item signature. So instead I packed it into the error handling of wf obligation checking.
2025-06-18Rollup merge of #142619 - klensy:or_fun_call, r=nnethercoteJakub Beránek-1/+1
apply clippy::or_fun_call Applies https://rust-lang.github.io/rust-clippy/master/index.html?groups=nursery#or_fun_call to reduce needless allocs.
2025-06-17apply clippy::or_fun_callklensy-1/+1
2025-06-16trait_sel: skip `nominal_obligations` for `Sized`David Wood-0/+8
`nominal_obligations` calls `predicates_of` on a `Sized` obligation, effectively elaborating the trait and making the well-formedness checking machinery do a bunch of extra work checking a `MetaSized` obligation is well-formed, but given that both `Sized` and `MetaSized` are built-ins, if `Sized` is otherwise well-formed, so `MetaSized` will be.
2025-06-16trait_sel: skip elaboration of sizedness supertraitDavid Wood-6/+69
As a performance optimization, skip elaborating the supertraits of `Sized`, and if a `MetaSized` obligation is being checked, then look for a `Sized` predicate in the parameter environment. This makes the `ParamEnv` smaller which should improve compiler performance as it avoids all the iteration over the larger `ParamEnv`.
2025-06-16trait_sel: extend fast path with sized hierarchyDavid Wood-7/+11
Extend the fast path for `Sized` traits to include constness and `MetaSized`.
2025-06-16trait_sel: `MetaSized` bounds in dispatchable checkDavid Wood-8/+15
Given the necessary additions of bounds to these traits and their impls in the standard library, it is necessary to add `MetaSized` bounds to the obligation which is proven as part of checking for dyn dispatchability.
2025-06-16trait_sel: `{Meta,Pointee}Sized` on `?Sized` typesDavid Wood-19/+39
Expand the automatic implementation of `MetaSized` and `PointeeSized` so that it is also implemented on non-`Sized` types, just not `ty::Foreign` (extern type).
2025-06-16trait_sel: `{Meta,Pointee}Sized` on `Sized` typesDavid Wood-0/+8
Introduce the `MetaSized` and `PointeeSized` traits as supertraits of `Sized` and initially implement it on everything that currently implements `Sized` to isolate any changes that simply adding the traits introduces.
2025-06-14Rollup merge of #141811 - mejrs:bye_locals, r=compiler-errorsMatthias Krüger-2/+2
Unimplement unsized_locals Implements https://github.com/rust-lang/compiler-team/issues/630 Tracking issue here: https://github.com/rust-lang/rust/issues/111942 Note that this just removes the feature, not the implementation, and does not touch `unsized_fn_params`. This is because it is required to support `Box<dyn FnOnce()>: FnOnce()`. There may be more that should be removed (possibly in follow up prs) - the `forget_unsized` function and `forget` intrinsic. - the `unsized_locals` test directory; I've just fixed up the tests for now - various codegen support for unsized values and allocas cc ``@JakobDegen`` ``@oli-obk`` ``@Noratrieb`` ``@programmerjake`` ``@bjorn3`` ``@rustbot`` label F-unsized_locals Fixes rust-lang/rust#79409