about summary refs log tree commit diff
path: root/compiler/rustc_trait_selection/src
AgeCommit message (Collapse)AuthorLines
2025-07-20Consider param-env for fast pathMichael Goulet-19/+29
2025-07-18Auto merge of #143545 - compiler-errors:coroutine-obl, r=oli-obkbors-30/+104
`-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-17Auto merge of #141762 - compiler-errors:witnesser, r=lcnrbors-31/+90
Unify `CoroutineWitness` sooner in typeck, and stall coroutine obligations based off of `TypingEnv` * Stall coroutine obligations based off of `TypingMode` in the old solver. * Eagerly assign `TyKind::CoroutineWitness` to the witness arg of coroutines during typeck, rather than deferring them to the end of typeck. r? lcnr This is part of https://github.com/rust-lang/rust/issues/143017.
2025-07-17Eagerly unify coroutine witness in old solverMichael Goulet-12/+43
2025-07-17Unstall obligations by looking for coroutines in old solverMichael Goulet-15/+40
2025-07-17Check if type has coroutines before visitingMichael Goulet-4/+7
2025-07-17Auto merge of #143879 - fee1-dead-contrib:push-lrlpoouyqqry, r=fmeasebors-7/+7
parse `const trait Trait` r? oli-obk or anyone from project-const-traits cc `@rust-lang/project-const-traits`
2025-07-17parse `const trait Trait`Deadbeef-7/+7
2025-07-17Rollup merge of #143431 - xizheyin:143392, r=compiler-errorsMatthias Krüger-5/+15
Use relative visibility when noting sealed trait to reduce false positive Fixes rust-lang/rust#143392 I used relative visibility instead of just determining if it's public or not. r? compiler
2025-07-17Auto merge of #144044 - fmease:rollup-kg413pt, r=fmeasebors-5/+13
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-17Rollup merge of #144016 - davidtwco:sized-hierarchy-temp-pseudo-revert, r=lcnrLeón Orell Valerian Liehr-0/+7
trait_sel: `MetaSized` always holds temporarily 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. cc rust-lang/rust#143992 r? ```@lcnr```
2025-07-17Use `join_path_syms` in one more place.Nicholas Nethercote-5/+6
This one is a bit marginal, because the segments are a mix of symbols and strings.
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-4/+2
2025-07-15Consider outlives assumptions when proving auto traits for coroutine interiorsMichael Goulet-27/+103
2025-07-15Implement other logicstiif-0/+37
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-11/+2
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-11/+2
2025-07-13Retire hir::*ItemRef.Camille GILLOT-13/+7
2025-07-13Remove hir::AssocItemKind.Camille GILLOT-43/+21
2025-07-13Auto merge of #140717 - mejrs:diagnostic_lints, r=oli-obkbors-11/+13
Split up the `unknown_or_malformed_diagnostic_attributes` lint This splits up the lint into the following lint group: - `unknown_diagnostic_attributes` - triggers if the attribute is unknown to the current compiler - `misplaced_diagnostic_attributes` - triggers if the attribute exists but it is not placed on the item kind it's meant for - `malformed_diagnostic_attributes` - triggers if the attribute's syntax or options are invalid - `malformed_diagnostic_format_literals` - triggers if the format string literal is invalid, for example if it has unpaired curly braces or invalid parameters - this pr doesn't create it, but future lints for things like deprecations can also go here. This PR does not start emitting lints in places that previously did not. ## Motivation I want to have finer control over what `unknown_or_malformed_diagnostic_attributes` does I have a project with fairly low msrv that is/will have a lower msrv than future diagnostic attributes. So lints will be emitted when I or others compile it on a lower msrv. At this time, there are two options to silence these lints: - `#[allow(unknown_or_malformed_diagnostic_attributes)]` - this risks diagnostic regressions if I (or others) mess up using the attribute, or if the attribute's syntax ever changes. - write a build script to detect the compiler version and emit cfgs, and then conditionally enable the attribute: ```rust #[cfg_attr(rust_version_99, diagnostic::new_attr_in_rust_99(thing = ..))]` struct Foo; ``` or conditionally `allow` the lint: ```rust // lib.rs #![cfg_attr(not(current_rust), allow(unknown_or_malformed_diagnostic_attributes))] ``` I like to avoid using build scripts if I can, so the following works much better for me. That is what this PR will let me do in the future: ```rust #[allow(unknown_diagnostic_attribute, reason = "attribute came out in rust 1.99 but msrv is 1.70")] #[diagnostic::new_attr_in_rust_99(thing = ..)]` struct Foo;
2025-07-11Auto merge of #142911 - mejrs:unsized, r=compiler-errorsbors-0/+6
Remove support for dynamic allocas Followup to rust-lang/rust#141811
2025-07-10Rollup merge of #143742 - estebank:borrow-suggestion, r=compiler-errorsTrevor Gross-103/+125
Rework borrowing suggestions to use `Expr` instead of just `Span` In the suggestion machinery for borrowing expressions and types, always use the available obligation `Span` to find the appropriate `Expr` to perform appropriateness checks no the `ExprKind` instead of on the textual snippet corresponding to the `Span`. (We were already doing this, but only for a subset of cases.) This now better handles situations where parentheses and `<>` are needed for correct syntax (`&(foo + bar)`, `(&foo).bar()`, `<&Foo>::bar()`, etc.). Unify the logic for the case where `&` *and* `&mut` are appropriate with the logic for only one of those cases. (Instead of having two branches for emitting the suggestion, we now have a single one, using `Diag::multipart_suggestions` always.) Handle the case when `S::foo()` should have been `<&S>::foo()` (instead of suggesting the prior `&S::foo()`. Fix rust-lang/rust#143393. Make `Diag::multipart_suggestions` always verbose. CC rust-lang/rust#141973.
2025-07-11Split up the `unknown_or_malformed_diagnostic_attributes` lintmejrs-11/+13
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-10Rework borrowing suggestions to use `Expr` instead of just `Span`Esteban Küber-103/+125
In the suggestion machinery for borrowing expressions and types, always use the available obligation `Span` to find the appropriate `Expr` to perform appropriateness checks no the `ExprKind` instead of on the textual snippet corresponding to the `Span`. Unify the logic for the case where `&` *and* `&mut` are appropriate with the logic for only one of those cases. Handle the case when `S::foo()` should have been `<&S>::foo()` (instead of suggesting the prior `&S::foo()`.
2025-07-09Auto merge of #143538 - compiler-errors:instantiate-auto-trait, r=lcnrbors-212/+245
Instantiate auto trait/`Copy`/`Clone`/`Sized` before computing constituent types binder This makes the binder logic w.r.t. coroutines a bit simpler. r? lcnr
2025-07-08Rollup merge of #143532 - compiler-errors:mut-ref-sugg, r=davidtwcoTrevor Gross-5/+8
More carefully consider span context when suggesting remove `&mut` Use `find_ancestor_inside` to compute a relative span that is macro-aware, rather than falling back to using BytePos arithmetic which is wrong for `&mut`. Fixes https://github.com/rust-lang/rust/issues/143523
2025-07-08Rollup merge of #143499 - compiler-errors:predicates-of-crate, r=davidtwcoTrevor Gross-13/+20
Don't call `predicates_of` on a dummy obligation cause's body id See the test for a brief explanation Fixes rust-lang/rust#143481.
2025-07-08Error on moving unsized values rather than ICE'ingmejrs-0/+6
2025-07-08Rollup merge of #143620 - Muscraft:remove-newline, r=compiler-errorsMatthias Krüger-1/+1
fix: Remove newline from multiple crate versions note While working on getting `annotate-snippets` to match `rustc`, `annotate-snippets` was adding an extra new line after [this line](https://github.com/rust-lang/rust/blob/a2d45f73c70d9dec57140c9412f83586eda895f8/tests/run-make/crate-loading/multiple-dep-versions.stderr#L9) for [`run-make/crate-loading/multiple-dep-versions.rs`](https://github.com/rust-lang/rust/blob/a2d45f73c70d9dec57140c9412f83586eda895f8/tests/run-make/crate-loading/multiple-dep-versions.rs). I found out this was because there was an explicit `\n` in the message that `annotate-snippets` was respecting, while `rustc` was [skipping it](https://github.com/rust-lang/rust/blob/2f8eeb2bba86b8f457ec602c578473c711f85628/compiler/rustc_errors/src/emitter.rs#L1542). After talking with ```@estebank,``` I was told to remove the newline from the error message. r? ```@estebank```
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-08Rollup merge of #143571 - lcnr:has_nested-bb, r=compiler-errorsMatthias Krüger-59/+52
remove `has_nested` from builtin candidates it's no longer necessary r? types
2025-07-07fix: Remove newline from multiple crate versions noteScott Schafer-1/+1
2025-07-07Rollup merge of #132469 - estebank:issue-132041, r=NadrierilMatthias Krüger-7/+77
Do not suggest borrow that is already there in fully-qualified call When encountering `&str::from("value")` do not suggest `&&str::from("value")`. Fix #132041.
2025-07-07remove `has_nested`lcnr-59/+52
2025-07-06compiler: rename {ast,hir}::BareFn* to FnPtr*Jubilee Young-1/+1
Fix some comments and related types and locals where it is obvious, e.g. - bare_fn -> fn_ptr - LifetimeBinderKind::BareFnType -> LifetimeBinderKind::FnPtrType Co-authored-by: León Orell Valerian Liehr <me@fmease.dev>
2025-07-06More carefully consider span context when suggesting remove &mutMichael Goulet-5/+8
2025-07-05Don't call predicates_of on a dummy obligation cause's body idMichael Goulet-13/+20
2025-07-05Use relative visibility when noting sealed trait to reduce false positivexizheyin-5/+15
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
2025-07-04NitsMichael Goulet-12/+10
2025-07-04Fix elided lifetimes in rustdocMichael Goulet-2/+2
2025-07-04Fix pretty printing of placeholder typesMichael Goulet-6/+6
2025-07-04Remove Symbol for Named LateParam/Bound variantsMichael Goulet-49/+55
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.