From f16fddcb221369271f5cbf8dc6cca391ce008ab9 Mon Sep 17 00:00:00 2001 From: David Wood Date: Wed, 18 Jun 2025 17:03:15 +0000 Subject: [PATCH 1/2] hir_analysis: add missing sizedness bounds Default sizedness bounds were not being added to `explicit_super_predicates_of` and `explicit_implied_predicates_of` which meant that a trait bound added to a associated type projection would be missing the implied predicate of the default sizedness supertrait of that trait. An unexpected consequence of this change was that the check for multiple principals was now finding an additional `MetaSized` principal when eagerly expanding trait aliases. Instead of special-casing trait aliases as different from traits and not adding a `MetaSized` supertrait to trait aliases, filter out `MetaSized` when lowering `dyn Trait`. --- .../src/collect/predicates_of.rs | 8 ++++ .../src/hir_ty_lowering/dyn_compatibility.rs | 7 +++- .../bound-on-assoc-type-projection.rs | 15 ++++++++ .../ui/sized-hierarchy/default-supertrait.rs | 5 +-- .../sized-hierarchy/default-supertrait.stderr | 37 +------------------ .../ui/sized-hierarchy/elaboration-simple.rs | 7 ++++ .../normalize/normalize-param-env-2.stderr | 19 +--------- .../normalize-param-env-4.next.stderr | 16 +------- .../drop-impl-pred.no.stderr | 17 ++++++--- .../non_lifetime_binders/drop-impl-pred.rs | 3 +- .../drop-impl-pred.yes.stderr | 11 +++++- 11 files changed, 64 insertions(+), 81 deletions(-) create mode 100644 tests/ui/sized-hierarchy/bound-on-assoc-type-projection.rs create mode 100644 tests/ui/sized-hierarchy/elaboration-simple.rs diff --git a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs index a93e58b101fe5..f3e020323cddc 100644 --- a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs @@ -660,6 +660,14 @@ pub(super) fn implied_predicates_with_filter<'tcx>( | PredicateFilter::SelfOnly | PredicateFilter::SelfTraitThatDefines(_) | PredicateFilter::SelfAndAssociatedTypeBounds => { + icx.lowerer().add_sizedness_bounds( + &mut bounds, + self_param_ty, + superbounds, + None, + Some(trait_def_id), + item.span, + ); icx.lowerer().add_default_super_traits( trait_def_id, &mut bounds, diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs index cb106962be18e..24cb3deb09746 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs @@ -64,8 +64,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { self.add_default_traits(&mut user_written_bounds, dummy_self, &ast_bounds, None, span); - let (elaborated_trait_bounds, elaborated_projection_bounds) = + let (mut elaborated_trait_bounds, elaborated_projection_bounds) = traits::expand_trait_aliases(tcx, user_written_bounds.iter().copied()); + + // FIXME(sized-hierarchy): https://github.com/rust-lang/rust/pull/142712#issuecomment-3013231794 + let meta_sized_did = tcx.require_lang_item(LangItem::MetaSized, span); + elaborated_trait_bounds.retain(|(pred, _)| pred.def_id() != meta_sized_did); + let (regular_traits, mut auto_traits): (Vec<_>, Vec<_>) = elaborated_trait_bounds .into_iter() .partition(|(trait_ref, _)| !tcx.trait_is_auto(trait_ref.def_id())); diff --git a/tests/ui/sized-hierarchy/bound-on-assoc-type-projection.rs b/tests/ui/sized-hierarchy/bound-on-assoc-type-projection.rs new file mode 100644 index 0000000000000..ddd3964f4180b --- /dev/null +++ b/tests/ui/sized-hierarchy/bound-on-assoc-type-projection.rs @@ -0,0 +1,15 @@ +//@ check-pass +#![crate_type = "lib"] +#![feature(sized_hierarchy)] + +trait FalseDeref { + type Target: std::marker::PointeeSized; +} + +trait Bar {} + +fn foo() +where + T::Target: Bar, +{ +} diff --git a/tests/ui/sized-hierarchy/default-supertrait.rs b/tests/ui/sized-hierarchy/default-supertrait.rs index b25acf9e6ea4f..b929a4a242439 100644 --- a/tests/ui/sized-hierarchy/default-supertrait.rs +++ b/tests/ui/sized-hierarchy/default-supertrait.rs @@ -47,14 +47,11 @@ fn with_pointeesized_supertrait() { requires_pointeesized::(); } -// `T` won't inherit the `const MetaSized` implicit supertrait of `Bare`, so there is an error on -// the bound, which is expected. +// `T` inherits the `const MetaSized` implicit supertrait of `Bare`. fn with_bare_trait() { -//~^ ERROR the size for values of type `T` cannot be known requires_sized::(); //~^ ERROR the size for values of type `T` cannot be known requires_metasized::(); - //~^ ERROR the size for values of type `T` cannot be known requires_pointeesized::(); } diff --git a/tests/ui/sized-hierarchy/default-supertrait.stderr b/tests/ui/sized-hierarchy/default-supertrait.stderr index de23936b900bc..fe3f8339eec54 100644 --- a/tests/ui/sized-hierarchy/default-supertrait.stderr +++ b/tests/ui/sized-hierarchy/default-supertrait.stderr @@ -28,22 +28,6 @@ LL | trait NegPointeeSized: ?PointeeSized { } = help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date -error[E0277]: the size for values of type `T` cannot be known - --> $DIR/default-supertrait.rs:52:38 - | -LL | fn with_bare_trait() { - | ^^^^ doesn't have a known size - | -note: required by a bound in `Bare` - --> $DIR/default-supertrait.rs:22:1 - | -LL | trait Bare {} - | ^^^^^^^^^^^^^ required by this bound in `Bare` -help: consider further restricting type parameter `T` with unstable trait `MetaSized` - | -LL | fn with_bare_trait() { - | ++++++++++++++++++++++++ - error[E0277]: the size for values of type `T` cannot be known at compilation time --> $DIR/default-supertrait.rs:35:22 | @@ -89,11 +73,10 @@ LL | fn with_pointeesized_supertrait $DIR/default-supertrait.rs:54:22 + --> $DIR/default-supertrait.rs:52:22 | LL | fn with_bare_trait() { | - this type parameter needs to be `Sized` -LL | LL | requires_sized::(); | ^ doesn't have a size known at compile-time | @@ -103,23 +86,7 @@ note: required by a bound in `requires_sized` LL | fn requires_sized() {} | ^^^^^ required by this bound in `requires_sized` -error[E0277]: the size for values of type `T` cannot be known - --> $DIR/default-supertrait.rs:56:26 - | -LL | requires_metasized::(); - | ^ doesn't have a known size - | -note: required by a bound in `requires_metasized` - --> $DIR/default-supertrait.rs:25:26 - | -LL | fn requires_metasized() {} - | ^^^^^^^^^ required by this bound in `requires_metasized` -help: consider further restricting type parameter `T` with unstable trait `MetaSized` - | -LL | fn with_bare_trait() { - | ++++++++++++++++++++++++ - -error: aborting due to 9 previous errors +error: aborting due to 7 previous errors Some errors have detailed explanations: E0277, E0658. For more information about an error, try `rustc --explain E0277`. diff --git a/tests/ui/sized-hierarchy/elaboration-simple.rs b/tests/ui/sized-hierarchy/elaboration-simple.rs new file mode 100644 index 0000000000000..6d0f0165a1dbb --- /dev/null +++ b/tests/ui/sized-hierarchy/elaboration-simple.rs @@ -0,0 +1,7 @@ +//@ check-pass +//@ compile-flags: --crate-type=lib +#![feature(sized_hierarchy)] + +trait Trait {} + +fn f() {} diff --git a/tests/ui/traits/next-solver/normalize/normalize-param-env-2.stderr b/tests/ui/traits/next-solver/normalize/normalize-param-env-2.stderr index 8d8909625ffc8..d179c80596238 100644 --- a/tests/ui/traits/next-solver/normalize/normalize-param-env-2.stderr +++ b/tests/ui/traits/next-solver/normalize/normalize-param-env-2.stderr @@ -19,23 +19,6 @@ error[E0275]: overflow evaluating the requirement `<() as A>::Assoc: A` LL | Self::Assoc: A, | ^^^^ -error[E0275]: overflow evaluating the requirement `<() as A>::Assoc: MetaSized` - --> $DIR/normalize-param-env-2.rs:24:22 - | -LL | Self::Assoc: A, - | ^^^^ - | -note: required by a bound in `A` - --> $DIR/normalize-param-env-2.rs:9:1 - | -LL | / trait A { -LL | | type Assoc; -LL | | -LL | | fn f() -... | -LL | | } - | |_^ required by this bound in `A` - error[E0275]: overflow evaluating the requirement `<() as A>::Assoc well-formed` --> $DIR/normalize-param-env-2.rs:24:22 | @@ -63,6 +46,6 @@ LL | where LL | Self::Assoc: A, | ^^^^ required by this bound in `A::f` -error: aborting due to 6 previous errors +error: aborting due to 5 previous errors For more information about this error, try `rustc --explain E0275`. diff --git a/tests/ui/traits/next-solver/normalize/normalize-param-env-4.next.stderr b/tests/ui/traits/next-solver/normalize/normalize-param-env-4.next.stderr index 9f7f74f94664b..f5fd9ce9864ce 100644 --- a/tests/ui/traits/next-solver/normalize/normalize-param-env-4.next.stderr +++ b/tests/ui/traits/next-solver/normalize/normalize-param-env-4.next.stderr @@ -4,20 +4,6 @@ error[E0275]: overflow evaluating the requirement `::Assoc: Trait` LL | ::Assoc: Trait, | ^^^^^ -error[E0275]: overflow evaluating the requirement `::Assoc: MetaSized` - --> $DIR/normalize-param-env-4.rs:19:26 - | -LL | ::Assoc: Trait, - | ^^^^^ - | -note: required by a bound in `Trait` - --> $DIR/normalize-param-env-4.rs:7:1 - | -LL | / trait Trait { -LL | | type Assoc; -LL | | } - | |_^ required by this bound in `Trait` - -error: aborting due to 2 previous errors +error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0275`. diff --git a/tests/ui/traits/non_lifetime_binders/drop-impl-pred.no.stderr b/tests/ui/traits/non_lifetime_binders/drop-impl-pred.no.stderr index 1f13207e33ca3..caba33499120c 100644 --- a/tests/ui/traits/non_lifetime_binders/drop-impl-pred.no.stderr +++ b/tests/ui/traits/non_lifetime_binders/drop-impl-pred.no.stderr @@ -1,5 +1,5 @@ warning: the feature `non_lifetime_binders` is incomplete and may not be safe to use and/or cause compiler crashes - --> $DIR/drop-impl-pred.rs:6:12 + --> $DIR/drop-impl-pred.rs:5:12 | LL | #![feature(non_lifetime_binders)] | ^^^^^^^^^^^^^^^^^^^^ @@ -8,17 +8,24 @@ LL | #![feature(non_lifetime_binders)] = note: `#[warn(incomplete_features)]` on by default error[E0367]: `Drop` impl requires `H: Foo` but the struct it is implemented for does not - --> $DIR/drop-impl-pred.rs:19:15 + --> $DIR/drop-impl-pred.rs:20:15 | LL | for H: Foo, | ^^^ | note: the implementor must specify the same requirement - --> $DIR/drop-impl-pred.rs:12:1 + --> $DIR/drop-impl-pred.rs:11:1 | LL | struct Bar(T) where T: Foo; | ^^^^^^^^^^^^^ -error: aborting due to 1 previous error; 1 warning emitted +error[E0282]: type annotations needed + --> $DIR/drop-impl-pred.rs:16:18 + | +LL | impl Drop for Bar + | ^^^^^^ cannot infer type for struct `Bar` + +error: aborting due to 2 previous errors; 1 warning emitted -For more information about this error, try `rustc --explain E0367`. +Some errors have detailed explanations: E0282, E0367. +For more information about an error, try `rustc --explain E0282`. diff --git a/tests/ui/traits/non_lifetime_binders/drop-impl-pred.rs b/tests/ui/traits/non_lifetime_binders/drop-impl-pred.rs index db8f3de2149d4..579fc82f2dfcc 100644 --- a/tests/ui/traits/non_lifetime_binders/drop-impl-pred.rs +++ b/tests/ui/traits/non_lifetime_binders/drop-impl-pred.rs @@ -1,5 +1,4 @@ //@ revisions: no yes -//@[yes] check-pass // Issue 110557 @@ -15,6 +14,8 @@ struct Bar(T) where T: Foo; struct Bar(T) where for H: Foo; impl Drop for Bar +//[yes]~^ ERROR type annotations needed +//[no]~^^ ERROR type annotations needed where for H: Foo, //[no]~^ ERROR `Drop` impl requires `H: Foo` but the struct it is implemented for does not diff --git a/tests/ui/traits/non_lifetime_binders/drop-impl-pred.yes.stderr b/tests/ui/traits/non_lifetime_binders/drop-impl-pred.yes.stderr index 165cf2ee13da8..9f2476cfb212d 100644 --- a/tests/ui/traits/non_lifetime_binders/drop-impl-pred.yes.stderr +++ b/tests/ui/traits/non_lifetime_binders/drop-impl-pred.yes.stderr @@ -1,5 +1,5 @@ warning: the feature `non_lifetime_binders` is incomplete and may not be safe to use and/or cause compiler crashes - --> $DIR/drop-impl-pred.rs:6:12 + --> $DIR/drop-impl-pred.rs:5:12 | LL | #![feature(non_lifetime_binders)] | ^^^^^^^^^^^^^^^^^^^^ @@ -7,5 +7,12 @@ LL | #![feature(non_lifetime_binders)] = note: see issue #108185 for more information = note: `#[warn(incomplete_features)]` on by default -warning: 1 warning emitted +error[E0282]: type annotations needed + --> $DIR/drop-impl-pred.rs:16:18 + | +LL | impl Drop for Bar + | ^^^^^^ cannot infer type for struct `Bar` + +error: aborting due to 1 previous error; 1 warning emitted +For more information about this error, try `rustc --explain E0282`. From eac7da10743cbfa224e2e35300e6c769b5eb439d Mon Sep 17 00:00:00 2001 From: David Wood Date: Fri, 27 Jun 2025 10:11:28 +0000 Subject: [PATCH 2/2] trait_sel: sizedness goals prefer alias candidates Following the changes to lowering, sizedness supertraits are now elaborated on associated type bounds. In existing code where an item bound was used to prove a predicate, this elaboration can result in a param candidate being present which will now take precedence over the projection candidate. Evaluation of the param candidate results in types being equated and region constraints being created that would not have with the projection candidate - sometimes emitting an error as a consequence! For sizedness predicates, now prefer non-param candidates if any exist. So that the `issue-93262.rs` test continued to pass, this required removing normalisation of the predicates of projections when confirming projection candidates which permits more uses of GATs and changes the output of yet more tests. --- compiler/rustc_middle/src/ty/context.rs | 8 +++ .../src/solve/trait_goals.rs | 41 +++++++++---- .../src/traits/select/confirmation.rs | 24 -------- .../src/traits/select/mod.rs | 28 +++++++-- compiler/rustc_type_ir/src/interner.rs | 2 + .../assoc_type_bounds_sized_used.rs | 3 +- .../assoc_type_bounds_sized_used.stderr | 61 +++++++++++++++---- .../bugs/issue-100013.stderr | 35 +---------- .../generic-associated-types/issue-92096.rs | 9 +-- .../issue-92096.stderr | 8 --- ...-method-resolution-opaque-type.next.stderr | 14 +++++ ...e-method-resolution-opaque-type.old.stderr | 2 +- ...rm-before-method-resolution-opaque-type.rs | 5 +- tests/ui/nll/issue-50716.rs | 2 +- tests/ui/nll/issue-50716.stderr | 18 +----- .../bound-on-assoc-type-projection-1.rs | 21 +++++++ 16 files changed, 156 insertions(+), 125 deletions(-) delete mode 100644 tests/ui/generic-associated-types/issue-92096.stderr create mode 100644 tests/ui/higher-ranked/trait-bounds/normalize-under-binder/norm-before-method-resolution-opaque-type.next.stderr create mode 100644 tests/ui/sized-hierarchy/bound-on-assoc-type-projection-1.rs diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 72810491214aa..92e9353651197 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -493,6 +493,10 @@ impl<'tcx> Interner for TyCtxt<'tcx> { self.is_default_trait(def_id) } + fn is_sizedness_trait(self, def_id: DefId) -> bool { + self.is_sizedness_trait(def_id) + } + fn as_lang_item(self, def_id: DefId) -> Option { lang_item_to_trait_lang_item(self.lang_items().from_def_id(def_id)?) } @@ -1645,6 +1649,10 @@ impl<'tcx> TyCtxt<'tcx> { .any(|&default_trait| self.lang_items().get(default_trait) == Some(def_id)) } + pub fn is_sizedness_trait(self, def_id: DefId) -> bool { + matches!(self.as_lang_item(def_id), Some(LangItem::Sized | LangItem::MetaSized)) + } + /// Returns a range of the start/end indices specified with the /// `rustc_layout_scalar_valid_range` attribute. // FIXME(eddyb) this is an awkward spot for this method, maybe move it? diff --git a/compiler/rustc_next_trait_solver/src/solve/trait_goals.rs b/compiler/rustc_next_trait_solver/src/solve/trait_goals.rs index 6c9fb63b579ad..739606d3442c2 100644 --- a/compiler/rustc_next_trait_solver/src/solve/trait_goals.rs +++ b/compiler/rustc_next_trait_solver/src/solve/trait_goals.rs @@ -1343,6 +1343,7 @@ where #[instrument(level = "debug", skip(self), ret)] pub(super) fn merge_trait_candidates( &mut self, + prefer_alias_over_param_candidates: bool, mut candidates: Vec>, ) -> Result<(CanonicalResponse, Option), NoSolution> { if let TypingMode::Coherence = self.typing_mode() { @@ -1368,6 +1369,26 @@ where return Ok((candidate.result, Some(TraitGoalProvenVia::Misc))); } + let potential_alias_bound_response = + candidates.iter().any(|c| matches!(c.source, CandidateSource::AliasBound)).then(|| { + let alias_bounds: Vec<_> = candidates + .iter() + .filter(|c| matches!(c.source, CandidateSource::AliasBound)) + .map(|c| c.result) + .collect(); + if let Some(response) = self.try_merge_responses(&alias_bounds) { + (response, Some(TraitGoalProvenVia::AliasBound)) + } else { + (self.bail_with_ambiguity(&alias_bounds), None) + } + }); + + if prefer_alias_over_param_candidates + && let Some(alias_bound_response) = potential_alias_bound_response + { + return Ok(alias_bound_response); + } + // If there are non-global where-bounds, prefer where-bounds // (including global ones) over everything else. let has_non_global_where_bounds = candidates @@ -1386,17 +1407,8 @@ where }; } - if candidates.iter().any(|c| matches!(c.source, CandidateSource::AliasBound)) { - let alias_bounds: Vec<_> = candidates - .iter() - .filter(|c| matches!(c.source, CandidateSource::AliasBound)) - .map(|c| c.result) - .collect(); - return if let Some(response) = self.try_merge_responses(&alias_bounds) { - Ok((response, Some(TraitGoalProvenVia::AliasBound))) - } else { - Ok((self.bail_with_ambiguity(&alias_bounds), None)) - }; + if let Some(response) = potential_alias_bound_response { + return Ok(response); } self.filter_specialized_impls(AllowInferenceConstraints::No, &mut candidates); @@ -1431,7 +1443,12 @@ where goal: Goal>, ) -> Result<(CanonicalResponse, Option), NoSolution> { let candidates = self.assemble_and_evaluate_candidates(goal, AssembleCandidatesFrom::All); - self.merge_trait_candidates(candidates) + + let did = goal.predicate.def_id(); + let is_sizedness_or_auto_or_default_goal = self.cx().is_sizedness_trait(did) + || self.cx().trait_is_auto(did) + || self.cx().is_default_trait(did); + self.merge_trait_candidates(is_sizedness_or_auto_or_default_goal, candidates) } fn try_stall_coroutine_witness( diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index 3eca77b43a89a..6a1c09f3bbaf1 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -146,8 +146,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { obligation: &PolyTraitObligation<'tcx>, idx: usize, ) -> Result, SelectionError<'tcx>> { - let tcx = self.tcx(); - let placeholder_trait_predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate).trait_ref; let placeholder_self_ty = self.infcx.shallow_resolve(placeholder_trait_predicate.self_ty()); @@ -196,28 +194,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { .map_err(|_| SelectionError::Unimplemented)?, ); - // FIXME(compiler-errors): I don't think this is needed. - if let ty::Alias(ty::Projection, alias_ty) = placeholder_self_ty.kind() { - let predicates = tcx.predicates_of(alias_ty.def_id).instantiate_own(tcx, alias_ty.args); - for (predicate, _) in predicates { - let normalized = normalize_with_depth_to( - self, - obligation.param_env, - obligation.cause.clone(), - obligation.recursion_depth + 1, - predicate, - &mut obligations, - ); - obligations.push(Obligation::with_depth( - self.tcx(), - obligation.cause.clone(), - obligation.recursion_depth + 1, - obligation.param_env, - normalized, - )); - } - } - Ok(obligations) } diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index 316b4dfc15fd2..ce4f5029d1e3c 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -485,7 +485,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { } } else { let has_non_region_infer = stack.obligation.predicate.has_non_region_infer(); - if let Some(candidate) = self.winnow_candidates(has_non_region_infer, candidates) { + let did = stack.obligation.predicate.def_id(); + let is_sizedness_or_auto_or_default_predicate = self.tcx().is_sizedness_trait(did) + || self.tcx().trait_is_auto(did) + || self.tcx().is_default_trait(did); + if let Some(candidate) = self.winnow_candidates( + has_non_region_infer, + is_sizedness_or_auto_or_default_predicate, + candidates, + ) { self.filter_reservation_impls(candidate) } else { Ok(None) @@ -1826,6 +1834,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> { fn winnow_candidates( &mut self, has_non_region_infer: bool, + prefer_alias_over_param_candidates: bool, mut candidates: Vec>, ) -> Option> { if candidates.len() == 1 { @@ -1879,6 +1888,19 @@ impl<'tcx> SelectionContext<'_, 'tcx> { break; } + let alias_bound = candidates + .iter() + .filter_map(|c| if let ProjectionCandidate(i) = c.candidate { Some(i) } else { None }) + .try_reduce(|c1, c2| if has_non_region_infer { None } else { Some(c1.min(c2)) }); + + if prefer_alias_over_param_candidates { + match alias_bound { + Some(Some(index)) => return Some(ProjectionCandidate(index)), + Some(None) => {} + None => return None, + } + } + // The next highest priority is for non-global where-bounds. However, while we don't // prefer global where-clauses here, we do bail with ambiguity when encountering both // a global and a non-global where-clause. @@ -1912,10 +1934,6 @@ impl<'tcx> SelectionContext<'_, 'tcx> { // fairly arbitrary but once again necessary for backwards compatibility. // If there are multiple applicable candidates which don't affect type inference, // choose the one with the lowest index. - let alias_bound = candidates - .iter() - .filter_map(|c| if let ProjectionCandidate(i) = c.candidate { Some(i) } else { None }) - .try_reduce(|c1, c2| if has_non_region_infer { None } else { Some(c1.min(c2)) }); match alias_bound { Some(Some(index)) => return Some(ProjectionCandidate(index)), Some(None) => {} diff --git a/compiler/rustc_type_ir/src/interner.rs b/compiler/rustc_type_ir/src/interner.rs index dd3cf1fc1811f..45bf10aebb9b7 100644 --- a/compiler/rustc_type_ir/src/interner.rs +++ b/compiler/rustc_type_ir/src/interner.rs @@ -300,6 +300,8 @@ pub trait Interner: fn is_default_trait(self, def_id: Self::DefId) -> bool; + fn is_sizedness_trait(self, def_id: Self::DefId) -> bool; + fn as_lang_item(self, def_id: Self::DefId) -> Option; fn associated_type_def_ids(self, def_id: Self::DefId) -> impl IntoIterator; diff --git a/tests/ui/dyn-compatibility/assoc_type_bounds_sized_used.rs b/tests/ui/dyn-compatibility/assoc_type_bounds_sized_used.rs index d59fc1712ea7e..8243803c86d70 100644 --- a/tests/ui/dyn-compatibility/assoc_type_bounds_sized_used.rs +++ b/tests/ui/dyn-compatibility/assoc_type_bounds_sized_used.rs @@ -9,7 +9,8 @@ trait Bop { fn bop() { let _ = ::Bar::default(); - //~^ ERROR: trait bounds were not satisfied + //~^ ERROR: the size for values of type `T` cannot be known at compilation time + //~| ERROR: the size for values of type `T` cannot be known at compilation time //~| ERROR: the size for values of type `T` cannot be known at compilation time } diff --git a/tests/ui/dyn-compatibility/assoc_type_bounds_sized_used.stderr b/tests/ui/dyn-compatibility/assoc_type_bounds_sized_used.stderr index 7b152adea4925..fe048c87775a9 100644 --- a/tests/ui/dyn-compatibility/assoc_type_bounds_sized_used.stderr +++ b/tests/ui/dyn-compatibility/assoc_type_bounds_sized_used.stderr @@ -24,21 +24,58 @@ help: consider relaxing the implicit `Sized` restriction LL | type Bar: Default + ?Sized | ++++++++ -error[E0599]: the function or associated item `default` exists for associated type `::Bar`, but its trait bounds were not satisfied - --> $DIR/assoc_type_bounds_sized_used.rs:11:30 +error[E0277]: the size for values of type `T` cannot be known at compilation time + --> $DIR/assoc_type_bounds_sized_used.rs:11:13 | +LL | fn bop() { + | - this type parameter needs to be `Sized` LL | let _ = ::Bar::default(); - | ^^^^^^^ function or associated item cannot be called on `::Bar` due to unsatisfied trait bounds + | ^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | +note: required by a bound in `Bop::Bar` + --> $DIR/assoc_type_bounds_sized_used.rs:7:15 + | +LL | type Bar: Default + | --- required by a bound in this associated type +LL | where +LL | Self: Sized; + | ^^^^^ required by this bound in `Bop::Bar` +help: consider removing the `?Sized` bound to make the type parameter `Sized` + | +LL - fn bop() { +LL + fn bop() { | - = note: the following trait bounds were not satisfied: - `T: Sized` - which is required by `::Bar: Default` -help: consider restricting the type parameter to satisfy the trait bound +help: consider relaxing the implicit `Sized` restriction + | +LL | type Bar: Default + ?Sized + | ++++++++ + +error[E0277]: the size for values of type `T` cannot be known at compilation time + --> $DIR/assoc_type_bounds_sized_used.rs:11:13 | -LL | fn bop() where T: Sized { - | ++++++++++++++ +LL | fn bop() { + | - this type parameter needs to be `Sized` +LL | let _ = ::Bar::default(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | +note: required by a bound in `Bop::Bar` + --> $DIR/assoc_type_bounds_sized_used.rs:7:15 + | +LL | type Bar: Default + | --- required by a bound in this associated type +LL | where +LL | Self: Sized; + | ^^^^^ required by this bound in `Bop::Bar` +help: consider removing the `?Sized` bound to make the type parameter `Sized` + | +LL - fn bop() { +LL + fn bop() { + | +help: consider relaxing the implicit `Sized` restriction + | +LL | type Bar: Default + ?Sized + | ++++++++ -error: aborting due to 2 previous errors +error: aborting due to 3 previous errors -Some errors have detailed explanations: E0277, E0599. -For more information about an error, try `rustc --explain E0277`. +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/generic-associated-types/bugs/issue-100013.stderr b/tests/ui/generic-associated-types/bugs/issue-100013.stderr index ff82aebfef911..4da43b0511492 100644 --- a/tests/ui/generic-associated-types/bugs/issue-100013.stderr +++ b/tests/ui/generic-associated-types/bugs/issue-100013.stderr @@ -1,25 +1,3 @@ -error: lifetime bound not satisfied - --> $DIR/issue-100013.rs:15:5 - | -LL | / async { // a coroutine checked for autotrait impl `Send` -LL | | let x = None::>; // a type referencing GAT -LL | | async {}.await; // a yield point -LL | | } - | |_____^ - | - = note: this is a known limitation that will be removed in the future (see issue #100013 for more information) - -error: lifetime bound not satisfied - --> $DIR/issue-100013.rs:22:5 - | -LL | / async { // a coroutine checked for autotrait impl `Send` -LL | | let x = None::>; // a type referencing GAT -LL | | async {}.await; // a yield point -LL | | } - | |_____^ - | - = note: this is a known limitation that will be removed in the future (see issue #100013 for more information) - error: lifetime may not live long enough --> $DIR/issue-100013.rs:23:17 | @@ -33,16 +11,5 @@ LL | let x = None::>; // a type referencing GAT | = help: consider adding the following bound: `'a: 'b` -error: lifetime bound not satisfied - --> $DIR/issue-100013.rs:29:5 - | -LL | / async { // a coroutine checked for autotrait impl `Send` -LL | | let x = None::>; // a type referencing GAT -LL | | async {}.await; // a yield point -LL | | } - | |_____^ - | - = note: this is a known limitation that will be removed in the future (see issue #100013 for more information) - -error: aborting due to 4 previous errors +error: aborting due to 1 previous error diff --git a/tests/ui/generic-associated-types/issue-92096.rs b/tests/ui/generic-associated-types/issue-92096.rs index a34c41795849e..76ad7ebdce4b4 100644 --- a/tests/ui/generic-associated-types/issue-92096.rs +++ b/tests/ui/generic-associated-types/issue-92096.rs @@ -1,3 +1,4 @@ +//@ check-pass //@ edition:2018 use std::future::Future; @@ -15,14 +16,6 @@ where C: Client + Send + Sync, { async move { c.connect().await } - //~^ ERROR `C` does not live long enough - // - // FIXME(#71723). This is because we infer at some point a value of - // - // impl Future::Connection<'_>> - // - // and then we somehow fail the WF check because `where C: 'a` is not known, - // but I'm not entirely sure how that comes about. } fn main() {} diff --git a/tests/ui/generic-associated-types/issue-92096.stderr b/tests/ui/generic-associated-types/issue-92096.stderr deleted file mode 100644 index b9a16cf184e4c..0000000000000 --- a/tests/ui/generic-associated-types/issue-92096.stderr +++ /dev/null @@ -1,8 +0,0 @@ -error: `C` does not live long enough - --> $DIR/issue-92096.rs:17:5 - | -LL | async move { c.connect().await } - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: aborting due to 1 previous error - diff --git a/tests/ui/higher-ranked/trait-bounds/normalize-under-binder/norm-before-method-resolution-opaque-type.next.stderr b/tests/ui/higher-ranked/trait-bounds/normalize-under-binder/norm-before-method-resolution-opaque-type.next.stderr new file mode 100644 index 0000000000000..6b05457f07080 --- /dev/null +++ b/tests/ui/higher-ranked/trait-bounds/normalize-under-binder/norm-before-method-resolution-opaque-type.next.stderr @@ -0,0 +1,14 @@ +error[E0282]: type annotations needed for `>::Out<_>` + --> $DIR/norm-before-method-resolution-opaque-type.rs:21:9 + | +LL | let x = *x; + | ^ + | +help: consider giving `x` an explicit type, where the placeholders `_` are specified + | +LL | let x: <_ as Trait<'static>>::Out<_> = *x; + | +++++++++++++++++++++++++++++++ + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0282`. diff --git a/tests/ui/higher-ranked/trait-bounds/normalize-under-binder/norm-before-method-resolution-opaque-type.old.stderr b/tests/ui/higher-ranked/trait-bounds/normalize-under-binder/norm-before-method-resolution-opaque-type.old.stderr index 79ded34d9cd9e..7fff7f1b404a4 100644 --- a/tests/ui/higher-ranked/trait-bounds/normalize-under-binder/norm-before-method-resolution-opaque-type.old.stderr +++ b/tests/ui/higher-ranked/trait-bounds/normalize-under-binder/norm-before-method-resolution-opaque-type.old.stderr @@ -1,5 +1,5 @@ error[E0507]: cannot move out of `*x` which is behind a shared reference - --> $DIR/norm-before-method-resolution-opaque-type.rs:22:13 + --> $DIR/norm-before-method-resolution-opaque-type.rs:21:13 | LL | let x = *x; | ^^ move occurs because `*x` has type `>::Out`, which does not implement the `Copy` trait diff --git a/tests/ui/higher-ranked/trait-bounds/normalize-under-binder/norm-before-method-resolution-opaque-type.rs b/tests/ui/higher-ranked/trait-bounds/normalize-under-binder/norm-before-method-resolution-opaque-type.rs index f881fcb779fa8..501b5548df6cf 100644 --- a/tests/ui/higher-ranked/trait-bounds/normalize-under-binder/norm-before-method-resolution-opaque-type.rs +++ b/tests/ui/higher-ranked/trait-bounds/normalize-under-binder/norm-before-method-resolution-opaque-type.rs @@ -1,6 +1,5 @@ //@ revisions: old next //@[next] compile-flags: -Znext-solver -//@[next] check-pass #![feature(type_alias_impl_trait)] trait Trait<'a> { @@ -19,7 +18,9 @@ where for<'a> X: Trait<'a>, for<'a> >::Out<()>: Copy, { - let x = *x; //[old]~ ERROR: cannot move out of `*x` + let x = *x; + //[old]~^ ERROR: cannot move out of `*x` + //[next]~^^ ERROR: type annotations needed for `>::Out<_>` todo!(); } diff --git a/tests/ui/nll/issue-50716.rs b/tests/ui/nll/issue-50716.rs index 76c6fc5e7b925..96168ebeaa165 100644 --- a/tests/ui/nll/issue-50716.rs +++ b/tests/ui/nll/issue-50716.rs @@ -5,7 +5,7 @@ trait A { type X: ?Sized; } -fn foo<'a, T: 'static>(s: Box<<&'a T as A>::X>) //~ ERROR +fn foo<'a, T: 'static>(s: Box<<&'a T as A>::X>) where for<'b> &'b T: A, <&'static T as A>::X: Sized diff --git a/tests/ui/nll/issue-50716.stderr b/tests/ui/nll/issue-50716.stderr index edd7fd765dade..536f88085ded3 100644 --- a/tests/ui/nll/issue-50716.stderr +++ b/tests/ui/nll/issue-50716.stderr @@ -1,18 +1,3 @@ -error[E0308]: mismatched types - --> $DIR/issue-50716.rs:8:27 - | -LL | fn foo<'a, T: 'static>(s: Box<<&'a T as A>::X>) - | ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch - | - = note: expected trait `<<&'a T as A>::X as MetaSized>` - found trait `<<&'static T as A>::X as MetaSized>` -note: the lifetime `'a` as defined here... - --> $DIR/issue-50716.rs:8:8 - | -LL | fn foo<'a, T: 'static>(s: Box<<&'a T as A>::X>) - | ^^ - = note: ...does not necessarily outlive the static lifetime - error: lifetime may not live long enough --> $DIR/issue-50716.rs:13:14 | @@ -22,6 +7,5 @@ LL | fn foo<'a, T: 'static>(s: Box<<&'a T as A>::X>) LL | let _x = *s; | ^^ proving this value is `Sized` requires that `'a` must outlive `'static` -error: aborting due to 2 previous errors +error: aborting due to 1 previous error -For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/sized-hierarchy/bound-on-assoc-type-projection-1.rs b/tests/ui/sized-hierarchy/bound-on-assoc-type-projection-1.rs new file mode 100644 index 0000000000000..4a20ed2cafb16 --- /dev/null +++ b/tests/ui/sized-hierarchy/bound-on-assoc-type-projection-1.rs @@ -0,0 +1,21 @@ +//@ check-pass +//@ compile-flags: --crate-type=lib +//@ revisions: old next +//@[next] compile-flags: -Znext-solver + +use std::marker::PhantomData; + +pub trait ZeroMapKV<'a> { + type Container; +} + +pub trait ZeroFrom<'zf, C: ?Sized> {} + +pub struct ZeroMap<'a, K: ZeroMapKV<'a>>(PhantomData<&'a K>); + +impl<'zf, 's, K> ZeroFrom<'zf, ZeroMap<'s, K>> for ZeroMap<'zf, K> +where + K: for<'b> ZeroMapKV<'b>, + >::Container: ZeroFrom<'zf, >::Container>, +{ +}