From 119c7bbef7a32b2858b1ab82f0f177ab3f68b0b6 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 16 May 2024 20:29:54 -0400 Subject: [PATCH] Report better WF obligation leaf obligations in new solver --- .../src/solve/fulfill.rs | 77 ++++++++++++++----- .../bugs/wf-check-skipped.next.stderr | 9 ++- .../bugs/wf-check-skipped.rs | 2 +- tests/ui/for/issue-20605.next.stderr | 20 +---- tests/ui/for/issue-20605.rs | 3 - .../in-trait/alias-bounds-when-not-wf.rs | 2 +- .../in-trait/alias-bounds-when-not-wf.stderr | 7 +- ...st-region-infer-to-static-in-binder.stderr | 6 +- .../next-solver/issue-118950-root-region.rs | 2 +- .../issue-118950-root-region.stderr | 12 ++- .../member-constraints-in-root-universe.rs | 17 ---- ...member-constraints-in-root-universe.stderr | 8 -- .../ui/traits/next-solver/object-unsafety.rs | 3 +- .../traits/next-solver/object-unsafety.stderr | 18 ++--- .../ui/wf/wf-normalization-sized.next.stderr | 31 +++++--- tests/ui/wf/wf-normalization-sized.rs | 8 +- 16 files changed, 117 insertions(+), 108 deletions(-) delete mode 100644 tests/ui/traits/next-solver/member-constraints-in-root-universe.rs delete mode 100644 tests/ui/traits/next-solver/member-constraints-in-root-universe.stderr diff --git a/compiler/rustc_trait_selection/src/solve/fulfill.rs b/compiler/rustc_trait_selection/src/solve/fulfill.rs index 3379c1d51a8a1..625bcf33a16b0 100644 --- a/compiler/rustc_trait_selection/src/solve/fulfill.rs +++ b/compiler/rustc_trait_selection/src/solve/fulfill.rs @@ -384,39 +384,64 @@ impl<'tcx> ProofTreeVisitor<'tcx> for BestObligation<'tcx> { return ControlFlow::Break(self.obligation.clone()); } - // FIXME: Could we extract a trait ref from a projection here too? + let tcx = goal.infcx().tcx; // FIXME: Also, what about considering >1 layer up the stack? May be necessary // for normalizes-to. - let Some(parent_trait_pred) = goal.goal().predicate.to_opt_poly_trait_pred() else { - return ControlFlow::Break(self.obligation.clone()); + let pred_kind = goal.goal().predicate.kind(); + let child_mode = match pred_kind.skip_binder() { + ty::PredicateKind::Clause(ty::ClauseKind::Trait(parent_trait_pred)) => { + ChildMode::Trait(pred_kind.rebind(parent_trait_pred)) + } + ty::PredicateKind::NormalizesTo(normalizes_to) + if matches!( + normalizes_to.alias.kind(tcx), + ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst + ) => + { + ChildMode::Trait(pred_kind.rebind(ty::TraitPredicate { + trait_ref: normalizes_to.alias.trait_ref(tcx), + polarity: ty::PredicatePolarity::Positive, + })) + } + ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_)) => { + ChildMode::WellFormedObligation + } + _ => { + return ControlFlow::Break(self.obligation.clone()); + } }; - let tcx = goal.infcx().tcx; let mut impl_where_bound_count = 0; for nested_goal in candidate.instantiate_nested_goals(self.span()) { + let make_obligation = |cause| Obligation { + cause, + param_env: nested_goal.goal().param_env, + predicate: nested_goal.goal().predicate, + recursion_depth: self.obligation.recursion_depth + 1, + }; + let obligation; - match nested_goal.source() { - GoalSource::Misc => { + match (child_mode, nested_goal.source()) { + (ChildMode::Trait(_), GoalSource::Misc) => { continue; } - GoalSource::ImplWhereBound => { - obligation = Obligation { - cause: derive_cause( - tcx, - candidate.kind(), - self.obligation.cause.clone(), - impl_where_bound_count, - parent_trait_pred, - ), - param_env: nested_goal.goal().param_env, - predicate: nested_goal.goal().predicate, - recursion_depth: self.obligation.recursion_depth + 1, - }; + (ChildMode::Trait(parent_trait_pred), GoalSource::ImplWhereBound) => { + obligation = make_obligation(derive_cause( + tcx, + candidate.kind(), + self.obligation.cause.clone(), + impl_where_bound_count, + parent_trait_pred, + )); impl_where_bound_count += 1; } - GoalSource::InstantiateHigherRanked => { + // Skip over a higher-ranked predicate. + (_, GoalSource::InstantiateHigherRanked) => { obligation = self.obligation.clone(); } + (ChildMode::WellFormedObligation, _) => { + obligation = make_obligation(self.obligation.cause.clone()); + } } // Skip nested goals that aren't the *reason* for our goal's failure. @@ -436,6 +461,18 @@ impl<'tcx> ProofTreeVisitor<'tcx> for BestObligation<'tcx> { } } +#[derive(Copy, Clone)] +enum ChildMode<'tcx> { + // Try to derive an `ObligationCause::{ImplDerived,BuiltinDerived}`, + // and skip all `GoalSource::Misc`, which represent useless obligations + // such as alias-eq which may not hold. + Trait(ty::PolyTraitPredicate<'tcx>), + // Skip trying to derive an `ObligationCause` from this obligation, and + // report *all* sub-obligations as if they came directly from the parent + // obligation. + WellFormedObligation, +} + fn derive_cause<'tcx>( tcx: TyCtxt<'tcx>, candidate_kind: ProbeKind<'tcx>, diff --git a/tests/ui/associated-inherent-types/bugs/wf-check-skipped.next.stderr b/tests/ui/associated-inherent-types/bugs/wf-check-skipped.next.stderr index 77fbaf529348c..bf53089675d5b 100644 --- a/tests/ui/associated-inherent-types/bugs/wf-check-skipped.next.stderr +++ b/tests/ui/associated-inherent-types/bugs/wf-check-skipped.next.stderr @@ -1,8 +1,11 @@ -error: the type `Foo::Bar>` is not well-formed - --> $DIR/wf-check-skipped.rs:17:14 +error[E0277]: the size for values of type `[u32]` cannot be known at compilation time + --> $DIR/wf-check-skipped.rs:17:25 | LL | fn main() -> Foo::Bar::> {} - | ^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u32]` error: aborting due to 1 previous error +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/associated-inherent-types/bugs/wf-check-skipped.rs b/tests/ui/associated-inherent-types/bugs/wf-check-skipped.rs index 5b812a2295ee6..52df4efd13e91 100644 --- a/tests/ui/associated-inherent-types/bugs/wf-check-skipped.rs +++ b/tests/ui/associated-inherent-types/bugs/wf-check-skipped.rs @@ -15,4 +15,4 @@ impl Foo { } fn main() -> Foo::Bar::> {} -//[next]~^ ERROR the type `Foo::Bar>` is not well-formed +//[next]~^ ERROR the size for values of type `[u32]` cannot be known at compilation time diff --git a/tests/ui/for/issue-20605.next.stderr b/tests/ui/for/issue-20605.next.stderr index 6855e17df9aac..98609211865c4 100644 --- a/tests/ui/for/issue-20605.next.stderr +++ b/tests/ui/for/issue-20605.next.stderr @@ -11,31 +11,13 @@ help: consider mutably borrowing here LL | for item in &mut *things { *item = 0 } | ++++ -error: the type ` as IntoIterator>::IntoIter` is not well-formed - --> $DIR/issue-20605.rs:6:17 - | -LL | for item in *things { *item = 0 } - | ^^^^^^^ - -error: the type `&mut as IntoIterator>::IntoIter` is not well-formed - --> $DIR/issue-20605.rs:6:17 - | -LL | for item in *things { *item = 0 } - | ^^^^^^^ - -error: the type `Option<< as IntoIterator>::IntoIter as Iterator>::Item>` is not well-formed - --> $DIR/issue-20605.rs:6:17 - | -LL | for item in *things { *item = 0 } - | ^^^^^^^ - error[E0614]: type ` as IntoIterator>::Item` cannot be dereferenced --> $DIR/issue-20605.rs:6:27 | LL | for item in *things { *item = 0 } | ^^^^^ -error: aborting due to 5 previous errors +error: aborting due to 2 previous errors Some errors have detailed explanations: E0277, E0614. For more information about an error, try `rustc --explain E0277`. diff --git a/tests/ui/for/issue-20605.rs b/tests/ui/for/issue-20605.rs index b923a7088fe01..647dc84028c33 100644 --- a/tests/ui/for/issue-20605.rs +++ b/tests/ui/for/issue-20605.rs @@ -6,9 +6,6 @@ fn changer<'a>(mut things: Box>) { for item in *things { *item = 0 } //[current]~^ ERROR `dyn Iterator` is not an iterator //[next]~^^ ERROR `dyn Iterator` is not an iterator - //[next]~| ERROR the type ` as IntoIterator>::IntoIter` is not well-formed - //[next]~| ERROR the type `&mut as IntoIterator>::IntoIter` is not well-formed - //[next]~| ERROR the type `Option<< as IntoIterator>::IntoIter as Iterator>::Item>` is not well-formed //[next]~| ERROR type ` as IntoIterator>::Item` cannot be dereferenced // FIXME(-Znext-solver): these error messages are horrible and have to be diff --git a/tests/ui/impl-trait/in-trait/alias-bounds-when-not-wf.rs b/tests/ui/impl-trait/in-trait/alias-bounds-when-not-wf.rs index 365955166e641..5a6bf9bfaef24 100644 --- a/tests/ui/impl-trait/in-trait/alias-bounds-when-not-wf.rs +++ b/tests/ui/impl-trait/in-trait/alias-bounds-when-not-wf.rs @@ -14,6 +14,6 @@ struct W(T); // `usize: Foo` doesn't hold. Therefore we ICE, because we don't expect to still // encounter weak types in `assemble_alias_bound_candidates_recur`. fn hello(_: W>) {} -//~^ ERROR the type `W>` is not well-formed +//~^ ERROR the size for values of type `A` cannot be known at compilation time fn main() {} diff --git a/tests/ui/impl-trait/in-trait/alias-bounds-when-not-wf.stderr b/tests/ui/impl-trait/in-trait/alias-bounds-when-not-wf.stderr index 5df27ac3bc6a3..9663fab3d8c39 100644 --- a/tests/ui/impl-trait/in-trait/alias-bounds-when-not-wf.stderr +++ b/tests/ui/impl-trait/in-trait/alias-bounds-when-not-wf.stderr @@ -7,11 +7,14 @@ LL | #![feature(lazy_type_alias)] = note: see issue #112792 for more information = note: `#[warn(incomplete_features)]` on by default -error: the type `W>` is not well-formed +error[E0277]: the size for values of type `A` cannot be known at compilation time --> $DIR/alias-bounds-when-not-wf.rs:16:13 | LL | fn hello(_: W>) {} - | ^^^^^^^^^^^ + | ^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `A` error: aborting due to 1 previous error; 1 warning emitted +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/traits/next-solver/canonical/const-region-infer-to-static-in-binder.stderr b/tests/ui/traits/next-solver/canonical/const-region-infer-to-static-in-binder.stderr index 044c24fd2b211..170f2c7d34c45 100644 --- a/tests/ui/traits/next-solver/canonical/const-region-infer-to-static-in-binder.stderr +++ b/tests/ui/traits/next-solver/canonical/const-region-infer-to-static-in-binder.stderr @@ -1,8 +1,8 @@ -error[E0282]: type annotations needed +error[E0284]: type annotations needed: cannot satisfy `the constant `{ || {} }` can be evaluated` --> $DIR/const-region-infer-to-static-in-binder.rs:4:10 | LL | struct X; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer the value of the constant `{ || {} }` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot satisfy `the constant `{ || {} }` can be evaluated` error: using function pointers as const generic parameters is forbidden --> $DIR/const-region-infer-to-static-in-binder.rs:4:20 @@ -23,4 +23,4 @@ LL | struct X; error: aborting due to 3 previous errors -For more information about this error, try `rustc --explain E0282`. +For more information about this error, try `rustc --explain E0284`. diff --git a/tests/ui/traits/next-solver/issue-118950-root-region.rs b/tests/ui/traits/next-solver/issue-118950-root-region.rs index 8667b3fe466c8..9f6dea5d5bf86 100644 --- a/tests/ui/traits/next-solver/issue-118950-root-region.rs +++ b/tests/ui/traits/next-solver/issue-118950-root-region.rs @@ -12,7 +12,7 @@ trait ToUnit<'a> { trait Overlap {} type Assoc<'a, T> = <*const T as ToUnit<'a>>::Unit; -//~^ ERROR: not well-formed +//~^ ERROR the trait bound `*const T: ToUnit<'a>` is not satisfied impl Overlap for T {} diff --git a/tests/ui/traits/next-solver/issue-118950-root-region.stderr b/tests/ui/traits/next-solver/issue-118950-root-region.stderr index 5cb24fa19aae2..2e0566cad08e2 100644 --- a/tests/ui/traits/next-solver/issue-118950-root-region.stderr +++ b/tests/ui/traits/next-solver/issue-118950-root-region.stderr @@ -13,11 +13,17 @@ LL | #![feature(lazy_type_alias)] = note: see issue #112792 for more information = note: `#[warn(incomplete_features)]` on by default -error: the type `<*const T as ToUnit<'a>>::Unit` is not well-formed +error[E0277]: the trait bound `*const T: ToUnit<'a>` is not satisfied --> $DIR/issue-118950-root-region.rs:14:21 | LL | type Assoc<'a, T> = <*const T as ToUnit<'a>>::Unit; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `ToUnit<'a>` is not implemented for `*const T` + | +help: this trait has no implementations, consider adding one + --> $DIR/issue-118950-root-region.rs:8:1 + | +LL | trait ToUnit<'a> { + | ^^^^^^^^^^^^^^^^ WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTy { args: ['^0.Named(DefId(0:15 ~ issue_118950_root_region[d54f]::{impl#1}::'a), "'a"), ?1t], def_id: DefId(0:8 ~ issue_118950_root_region[d54f]::Assoc) } WARN rustc_infer::infer::relate::generalize may incompletely handle alias type: AliasTy { args: ['^0.Named(DefId(0:15 ~ issue_118950_root_region[d54f]::{impl#1}::'a), "'a"), ?1t], def_id: DefId(0:8 ~ issue_118950_root_region[d54f]::Assoc) } @@ -34,5 +40,5 @@ LL | impl Overlap fn(Assoc<'a, T>)> for T where Missing: Overlap { error: aborting due to 3 previous errors; 1 warning emitted -Some errors have detailed explanations: E0119, E0412. +Some errors have detailed explanations: E0119, E0277, E0412. For more information about an error, try `rustc --explain E0119`. diff --git a/tests/ui/traits/next-solver/member-constraints-in-root-universe.rs b/tests/ui/traits/next-solver/member-constraints-in-root-universe.rs deleted file mode 100644 index a5696fc77961f..0000000000000 --- a/tests/ui/traits/next-solver/member-constraints-in-root-universe.rs +++ /dev/null @@ -1,17 +0,0 @@ -//@ compile-flags: -Znext-solver - -trait Trait { - type Ty; -} - -impl Trait for for<'a> fn(&'a u8, &'a u8) { - type Ty = (); -} - -// argument is necessary to create universes before registering the hidden type. -fn test<'a>(_: ::Ty) -> impl Sized { - //~^ ERROR the type ` fn(&'a u8, &'b u8) as Trait>::Ty` is not well-formed - "hidden type is `&'?0 str` with '?0 member of ['static,]" -} - -fn main() {} diff --git a/tests/ui/traits/next-solver/member-constraints-in-root-universe.stderr b/tests/ui/traits/next-solver/member-constraints-in-root-universe.stderr deleted file mode 100644 index 7a9982f07f6f3..0000000000000 --- a/tests/ui/traits/next-solver/member-constraints-in-root-universe.stderr +++ /dev/null @@ -1,8 +0,0 @@ -error: the type ` fn(&'a u8, &'b u8) as Trait>::Ty` is not well-formed - --> $DIR/member-constraints-in-root-universe.rs:12:16 - | -LL | fn test<'a>(_: ::Ty) -> impl Sized { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: aborting due to 1 previous error - diff --git a/tests/ui/traits/next-solver/object-unsafety.rs b/tests/ui/traits/next-solver/object-unsafety.rs index 3aa1af4956e22..a347984daf6a6 100644 --- a/tests/ui/traits/next-solver/object-unsafety.rs +++ b/tests/ui/traits/next-solver/object-unsafety.rs @@ -10,9 +10,8 @@ fn copy(from: &U::From) -> U::From { pub fn copy_any(t: &T) -> T { copy::>(t) - //~^ ERROR the type `& as Setup>::From` is not well-formed + //~^ ERROR the trait bound `T: Copy` is not satisfied in `dyn Setup` //~| ERROR mismatched types - //~| ERROR the type ` as Setup>::From` is not well-formed //~| ERROR the trait bound `T: Copy` is not satisfied // FIXME(-Znext-solver): These error messages are horrible and some of them diff --git a/tests/ui/traits/next-solver/object-unsafety.stderr b/tests/ui/traits/next-solver/object-unsafety.stderr index 7c9a6077fe7fd..75d0ce244132e 100644 --- a/tests/ui/traits/next-solver/object-unsafety.stderr +++ b/tests/ui/traits/next-solver/object-unsafety.stderr @@ -15,12 +15,6 @@ help: consider restricting type parameter `T` LL | pub fn copy_any(t: &T) -> T { | +++++++++++++++++++ -error: the type `& as Setup>::From` is not well-formed - --> $DIR/object-unsafety.rs:12:31 - | -LL | copy::>(t) - | ^ - error[E0308]: mismatched types --> $DIR/object-unsafety.rs:12:31 | @@ -37,13 +31,19 @@ note: function defined here LL | fn copy(from: &U::From) -> U::From { | ^^^^ -------------- -error: the type ` as Setup>::From` is not well-formed +error[E0277]: the trait bound `T: Copy` is not satisfied in `dyn Setup` --> $DIR/object-unsafety.rs:12:5 | LL | copy::>(t) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ within `dyn Setup`, the trait `Copy` is not implemented for `T`, which is required by `dyn Setup: Setup` + | + = note: required because it appears within the type `dyn Setup` +help: consider restricting type parameter `T` + | +LL | pub fn copy_any(t: &T) -> T { + | +++++++++++++++++++ -error: aborting due to 4 previous errors +error: aborting due to 3 previous errors Some errors have detailed explanations: E0277, E0308. For more information about an error, try `rustc --explain E0277`. diff --git a/tests/ui/wf/wf-normalization-sized.next.stderr b/tests/ui/wf/wf-normalization-sized.next.stderr index 599b1f3d45e66..1e898fb7b78ab 100644 --- a/tests/ui/wf/wf-normalization-sized.next.stderr +++ b/tests/ui/wf/wf-normalization-sized.next.stderr @@ -1,30 +1,37 @@ -error: the type `<[[[[[[u8]]]]]] as WellUnformed>::RequestNormalize` is not well-formed - --> $DIR/wf-normalization-sized.rs:19:10 +error[E0277]: the size for values of type `[[[[[u8]]]]]` cannot be known at compilation time + --> $DIR/wf-normalization-sized.rs:19:11 | LL | const _: <[[[[[[u8]]]]]] as WellUnformed>::RequestNormalize = (); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[[[[[u8]]]]]` -error: the type `<[[[[[[u8]]]]]] as WellUnformed>::RequestNormalize` is not well-formed - --> $DIR/wf-normalization-sized.rs:19:10 +error[E0277]: the size for values of type `[[[[[u8]]]]]` cannot be known at compilation time + --> $DIR/wf-normalization-sized.rs:19:11 | LL | const _: <[[[[[[u8]]]]]] as WellUnformed>::RequestNormalize = (); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^ doesn't have a size known at compile-time | + = help: the trait `Sized` is not implemented for `[[[[[u8]]]]]` = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` -error: the type ` as WellUnformed>::RequestNormalize` is not well-formed - --> $DIR/wf-normalization-sized.rs:22:10 +error[E0277]: the size for values of type `str` cannot be known at compilation time + --> $DIR/wf-normalization-sized.rs:22:11 | LL | const _: as WellUnformed>::RequestNormalize = (); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `str` -error: the type ` as WellUnformed>::RequestNormalize` is not well-formed - --> $DIR/wf-normalization-sized.rs:22:10 +error[E0277]: the size for values of type `str` cannot be known at compilation time + --> $DIR/wf-normalization-sized.rs:22:11 | LL | const _: as WellUnformed>::RequestNormalize = (); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^ doesn't have a size known at compile-time | + = help: the trait `Sized` is not implemented for `str` = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` error: aborting due to 4 previous errors +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/wf/wf-normalization-sized.rs b/tests/ui/wf/wf-normalization-sized.rs index e6e24ff9e8588..80b2c8803ff17 100644 --- a/tests/ui/wf/wf-normalization-sized.rs +++ b/tests/ui/wf/wf-normalization-sized.rs @@ -17,10 +17,10 @@ impl WellUnformed for T { } const _: <[[[[[[u8]]]]]] as WellUnformed>::RequestNormalize = (); -//[next]~^ the type -//[next]~| the type +//[next]~^ the size for values of type `[[[[[u8]]]]]` cannot be known at compilation time +//[next]~| the size for values of type `[[[[[u8]]]]]` cannot be known at compilation time const _: as WellUnformed>::RequestNormalize = (); -//[next]~^ the type -//[next]~| the type +//[next]~^ the size for values of type `str` cannot be known at compilation time +//[next]~| the size for values of type `str` cannot be known at compilation time fn main() {}