Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check that alias-relate terms are WF if reporting an error in alias-relate #126404

Merged
merged 2 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions compiler/rustc_trait_selection/src/solve/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,32 @@ impl<'tcx> ProofTreeVisitor<'tcx> for BestObligation<'tcx> {
self.with_derived_obligation(obligation, |this| nested_goal.visit_with(this))?;
}

// alias-relate may fail because the lhs or rhs can't be normalized,
// and therefore is treated as rigid.
if let Some(ty::PredicateKind::AliasRelate(lhs, rhs, _)) = pred_kind.no_bound_vars() {
if let Some(obligation) = goal
.infcx()
.visit_proof_tree_at_depth(
goal.goal().with(goal.infcx().tcx, ty::ClauseKind::WellFormed(lhs.into())),
goal.depth() + 1,
self,
)
.break_value()
{
return ControlFlow::Break(obligation);
} else if let Some(obligation) = goal
.infcx()
.visit_proof_tree_at_depth(
goal.goal().with(goal.infcx().tcx, ty::ClauseKind::WellFormed(rhs.into())),
goal.depth() + 1,
self,
)
.break_value()
{
return ControlFlow::Break(obligation);
}
}

ControlFlow::Break(self.obligation.clone())
}
}
Expand Down
15 changes: 14 additions & 1 deletion compiler/rustc_trait_selection/src/solve/inspect/analyse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
self.source
}

pub fn depth(&self) -> usize {
self.depth
}

fn candidates_recur(
&'a self,
candidates: &mut Vec<InspectCandidate<'a, 'tcx>>,
Expand Down Expand Up @@ -435,9 +439,18 @@ impl<'tcx> InferCtxt<'tcx> {
&self,
goal: Goal<'tcx, ty::Predicate<'tcx>>,
visitor: &mut V,
) -> V::Result {
self.visit_proof_tree_at_depth(goal, 0, visitor)
}

fn visit_proof_tree_at_depth<V: ProofTreeVisitor<'tcx>>(
&self,
goal: Goal<'tcx, ty::Predicate<'tcx>>,
depth: usize,
visitor: &mut V,
) -> V::Result {
let (_, proof_tree) = self.evaluate_root_goal(goal, GenerateProofTree::Yes);
let proof_tree = proof_tree.unwrap();
visitor.visit_goal(&InspectGoal::new(self, 0, proof_tree, None, GoalSource::Misc))
visitor.visit_goal(&InspectGoal::new(self, depth, proof_tree, None, GoalSource::Misc))
}
}
21 changes: 4 additions & 17 deletions tests/ui/associated-types/defaults-unsound-62211-1.next.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,17 @@ help: consider further restricting `Self`
LL | trait UncheckedCopy: Sized + AddAssign<&'static str> {
| +++++++++++++++++++++++++

error[E0271]: type mismatch resolving `<Self as Deref>::Target == str`
--> $DIR/defaults-unsound-62211-1.rs:24:96
|
LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + From<Self> + Display = Self;
| ^^^^ types differ
|
note: required by a bound in `UncheckedCopy::Output`
--> $DIR/defaults-unsound-62211-1.rs:24:31
|
LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + From<Self> + Display = Self;
| ^^^^^^^^^^^^ required by this bound in `UncheckedCopy::Output`

error[E0277]: the trait bound `Self: Deref` is not satisfied
--> $DIR/defaults-unsound-62211-1.rs:24:96
|
LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + From<Self> + Display = Self;
| ^^^^ the trait `Deref` is not implemented for `Self`
|
note: required by a bound in `UncheckedCopy::Output`
--> $DIR/defaults-unsound-62211-1.rs:24:25
--> $DIR/defaults-unsound-62211-1.rs:24:31
|
LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + From<Self> + Display = Self;
| ^^^^^^^^^^^^^^^^^^^ required by this bound in `UncheckedCopy::Output`
| ^^^^^^^^^^^^ required by this bound in `UncheckedCopy::Output`
help: consider further restricting `Self`
|
LL | trait UncheckedCopy: Sized + Deref {
Expand All @@ -75,7 +63,6 @@ help: consider further restricting `Self`
LL | trait UncheckedCopy: Sized + Copy {
| ++++++

error: aborting due to 5 previous errors
error: aborting due to 4 previous errors

Some errors have detailed explanations: E0271, E0277.
For more information about an error, try `rustc --explain E0271`.
For more information about this error, try `rustc --explain E0277`.
1 change: 0 additions & 1 deletion tests/ui/associated-types/defaults-unsound-62211-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ trait UncheckedCopy: Sized {
//~| ERROR the trait bound `Self: Deref` is not satisfied
//~| ERROR cannot add-assign `&'static str` to `Self`
//~| ERROR `Self` doesn't implement `std::fmt::Display`
//[next]~| ERROR type mismatch resolving `<Self as Deref>::Target == str`

// We said the Output type was Copy, so we can Copy it freely!
fn unchecked_copy(other: &Self::Output) -> Self::Output {
Expand Down
21 changes: 4 additions & 17 deletions tests/ui/associated-types/defaults-unsound-62211-2.next.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,17 @@ help: consider further restricting `Self`
LL | trait UncheckedCopy: Sized + AddAssign<&'static str> {
| +++++++++++++++++++++++++

error[E0271]: type mismatch resolving `<Self as Deref>::Target == str`
--> $DIR/defaults-unsound-62211-2.rs:24:96
|
LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + From<Self> + Display = Self;
| ^^^^ types differ
|
note: required by a bound in `UncheckedCopy::Output`
--> $DIR/defaults-unsound-62211-2.rs:24:31
|
LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + From<Self> + Display = Self;
| ^^^^^^^^^^^^ required by this bound in `UncheckedCopy::Output`

error[E0277]: the trait bound `Self: Deref` is not satisfied
--> $DIR/defaults-unsound-62211-2.rs:24:96
|
LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + From<Self> + Display = Self;
| ^^^^ the trait `Deref` is not implemented for `Self`
|
note: required by a bound in `UncheckedCopy::Output`
--> $DIR/defaults-unsound-62211-2.rs:24:25
--> $DIR/defaults-unsound-62211-2.rs:24:31
|
LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + From<Self> + Display = Self;
| ^^^^^^^^^^^^^^^^^^^ required by this bound in `UncheckedCopy::Output`
| ^^^^^^^^^^^^ required by this bound in `UncheckedCopy::Output`
help: consider further restricting `Self`
|
LL | trait UncheckedCopy: Sized + Deref {
Expand All @@ -75,7 +63,6 @@ help: consider further restricting `Self`
LL | trait UncheckedCopy: Sized + Copy {
| ++++++

error: aborting due to 5 previous errors
error: aborting due to 4 previous errors

Some errors have detailed explanations: E0271, E0277.
For more information about an error, try `rustc --explain E0271`.
For more information about this error, try `rustc --explain E0277`.
1 change: 0 additions & 1 deletion tests/ui/associated-types/defaults-unsound-62211-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ trait UncheckedCopy: Sized {
//~| ERROR the trait bound `Self: Deref` is not satisfied
//~| ERROR cannot add-assign `&'static str` to `Self`
//~| ERROR `Self` doesn't implement `std::fmt::Display`
//[next]~| ERROR type mismatch resolving `<Self as Deref>::Target == str`

// We said the Output type was Copy, so we can Copy it freely!
fn unchecked_copy(other: &Self::Output) -> Self::Output {
Expand Down
21 changes: 4 additions & 17 deletions tests/ui/associated-types/issue-54108.next.stderr
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
error[E0271]: type mismatch resolving `<<T as SubEncoder>::ActualSize as Add>::Output == <T as SubEncoder>::ActualSize`
--> $DIR/issue-54108.rs:23:17
|
LL | type Size = <Self as SubEncoder>::ActualSize;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ types differ
|
note: required by a bound in `Encoder::Size`
--> $DIR/issue-54108.rs:8:20
|
LL | type Size: Add<Output = Self::Size>;
| ^^^^^^^^^^^^^^^^^^^ required by this bound in `Encoder::Size`

error[E0277]: cannot add `<T as SubEncoder>::ActualSize` to `<T as SubEncoder>::ActualSize`
--> $DIR/issue-54108.rs:23:17
|
Expand All @@ -18,16 +6,15 @@ LL | type Size = <Self as SubEncoder>::ActualSize;
|
= help: the trait `Add` is not implemented for `<T as SubEncoder>::ActualSize`
note: required by a bound in `Encoder::Size`
--> $DIR/issue-54108.rs:8:16
--> $DIR/issue-54108.rs:8:20
|
LL | type Size: Add<Output = Self::Size>;
| ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Encoder::Size`
| ^^^^^^^^^^^^^^^^^^^ required by this bound in `Encoder::Size`
help: consider further restricting the associated type
|
LL | T: SubEncoder, <T as SubEncoder>::ActualSize: Add
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error: aborting due to 2 previous errors
error: aborting due to 1 previous error

Some errors have detailed explanations: E0271, E0277.
For more information about an error, try `rustc --explain E0271`.
For more information about this error, try `rustc --explain E0277`.
1 change: 0 additions & 1 deletion tests/ui/associated-types/issue-54108.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ where
{
type Size = <Self as SubEncoder>::ActualSize;
//~^ ERROR: cannot add `<T as SubEncoder>::ActualSize` to `<T as SubEncoder>::ActualSize`
//[next]~| ERROR type mismatch resolving `<<T as SubEncoder>::ActualSize as Add>::Output == <T as SubEncoder>::ActualSize`

fn foo(&self) -> Self::Size {
self.bar() + self.bar()
Expand Down
47 changes: 2 additions & 45 deletions tests/ui/traits/next-solver/coroutine.fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ LL | needs_coroutine(
LL | #[coroutine]
LL | / || {
LL | |
LL | |
LL | |
LL | | yield ();
LL | | },
| |_________^ the trait `Coroutine<A>` is not implemented for `{coroutine@$DIR/coroutine.rs:20:9: 20:11}`
Expand All @@ -18,47 +16,6 @@ note: required by a bound in `needs_coroutine`
LL | fn needs_coroutine(_: impl Coroutine<A, Yield = B, Return = C>) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `needs_coroutine`

error[E0271]: type mismatch resolving `<{coroutine@$DIR/coroutine.rs:20:9: 20:11} as Coroutine<A>>::Yield == B`
--> $DIR/coroutine.rs:20:9
|
LL | needs_coroutine(
| --------------- required by a bound introduced by this call
LL | #[coroutine]
LL | / || {
LL | |
LL | |
LL | |
LL | | yield ();
LL | | },
| |_________^ types differ
|
note: required by a bound in `needs_coroutine`
--> $DIR/coroutine.rs:14:41
|
LL | fn needs_coroutine(_: impl Coroutine<A, Yield = B, Return = C>) {}
| ^^^^^^^^^ required by this bound in `needs_coroutine`

error[E0271]: type mismatch resolving `<{coroutine@$DIR/coroutine.rs:20:9: 20:11} as Coroutine<A>>::Return == C`
--> $DIR/coroutine.rs:20:9
|
LL | needs_coroutine(
| --------------- required by a bound introduced by this call
LL | #[coroutine]
LL | / || {
LL | |
LL | |
LL | |
LL | | yield ();
LL | | },
| |_________^ types differ
|
note: required by a bound in `needs_coroutine`
--> $DIR/coroutine.rs:14:52
|
LL | fn needs_coroutine(_: impl Coroutine<A, Yield = B, Return = C>) {}
| ^^^^^^^^^^ required by this bound in `needs_coroutine`

error: aborting due to 3 previous errors
error: aborting due to 1 previous error

Some errors have detailed explanations: E0271, E0277.
For more information about an error, try `rustc --explain E0271`.
For more information about this error, try `rustc --explain E0277`.
2 changes: 0 additions & 2 deletions tests/ui/traits/next-solver/coroutine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ fn main() {
#[coroutine]
|| {
//[fail]~^ ERROR Coroutine<A>` is not satisfied
//[fail]~| ERROR as Coroutine<A>>::Yield == B`
//[fail]~| ERROR as Coroutine<A>>::Return == C`
yield ();
},
);
Expand Down
4 changes: 0 additions & 4 deletions tests/ui/traits/next-solver/fn-trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@ fn main() {
require_fn(f as fn() -> i32);
require_fn(f as unsafe fn() -> i32);
//~^ ERROR: expected a `Fn()` closure, found `unsafe fn() -> i32`
//~| ERROR: type mismatch resolving `<unsafe fn() -> i32 as FnOnce<()>>::Output == i32`
require_fn(g);
//~^ ERROR: expected a `Fn()` closure, found `extern "C" fn() -> i32 {g}`
//~| ERROR: type mismatch resolving `<extern "C" fn() -> i32 {g} as FnOnce<()>>::Output == i32`
require_fn(g as extern "C" fn() -> i32);
//~^ ERROR: expected a `Fn()` closure, found `extern "C" fn() -> i32`
//~| ERROR: type mismatch resolving `<extern "C" fn() -> i32 as FnOnce<()>>::Output == i32`
require_fn(h);
//~^ ERROR: expected a `Fn()` closure, found `unsafe fn() -> i32 {h}`
//~| ERROR: type mismatch resolving `<unsafe fn() -> i32 {h} as FnOnce<()>>::Output == i32`
}
67 changes: 5 additions & 62 deletions tests/ui/traits/next-solver/fn-trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,8 @@ note: required by a bound in `require_fn`
LL | fn require_fn(_: impl Fn() -> i32) {}
| ^^^^^^^^^^^ required by this bound in `require_fn`

error[E0271]: type mismatch resolving `<unsafe fn() -> i32 as FnOnce<()>>::Output == i32`
--> $DIR/fn-trait.rs:20:16
|
LL | require_fn(f as unsafe fn() -> i32);
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^ types differ
| |
| required by a bound introduced by this call
|
note: required by a bound in `require_fn`
--> $DIR/fn-trait.rs:3:31
|
LL | fn require_fn(_: impl Fn() -> i32) {}
| ^^^ required by this bound in `require_fn`

error[E0277]: expected a `Fn()` closure, found `extern "C" fn() -> i32 {g}`
--> $DIR/fn-trait.rs:23:16
--> $DIR/fn-trait.rs:22:16
|
LL | require_fn(g);
| ---------- ^ expected an `Fn()` closure, found `extern "C" fn() -> i32 {g}`
Expand All @@ -45,22 +31,8 @@ note: required by a bound in `require_fn`
LL | fn require_fn(_: impl Fn() -> i32) {}
| ^^^^^^^^^^^ required by this bound in `require_fn`

error[E0271]: type mismatch resolving `<extern "C" fn() -> i32 {g} as FnOnce<()>>::Output == i32`
--> $DIR/fn-trait.rs:23:16
|
LL | require_fn(g);
| ---------- ^ types differ
| |
| required by a bound introduced by this call
|
note: required by a bound in `require_fn`
--> $DIR/fn-trait.rs:3:31
|
LL | fn require_fn(_: impl Fn() -> i32) {}
| ^^^ required by this bound in `require_fn`

error[E0277]: expected a `Fn()` closure, found `extern "C" fn() -> i32`
--> $DIR/fn-trait.rs:26:16
--> $DIR/fn-trait.rs:24:16
|
LL | require_fn(g as extern "C" fn() -> i32);
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an `Fn()` closure, found `extern "C" fn() -> i32`
Expand All @@ -75,22 +47,8 @@ note: required by a bound in `require_fn`
LL | fn require_fn(_: impl Fn() -> i32) {}
| ^^^^^^^^^^^ required by this bound in `require_fn`

error[E0271]: type mismatch resolving `<extern "C" fn() -> i32 as FnOnce<()>>::Output == i32`
--> $DIR/fn-trait.rs:26:16
|
LL | require_fn(g as extern "C" fn() -> i32);
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ types differ
| |
| required by a bound introduced by this call
|
note: required by a bound in `require_fn`
--> $DIR/fn-trait.rs:3:31
|
LL | fn require_fn(_: impl Fn() -> i32) {}
| ^^^ required by this bound in `require_fn`

error[E0277]: expected a `Fn()` closure, found `unsafe fn() -> i32 {h}`
--> $DIR/fn-trait.rs:29:16
--> $DIR/fn-trait.rs:26:16
|
LL | require_fn(h);
| ---------- ^ call the function in a closure: `|| unsafe { /* code */ }`
Expand All @@ -106,21 +64,6 @@ note: required by a bound in `require_fn`
LL | fn require_fn(_: impl Fn() -> i32) {}
| ^^^^^^^^^^^ required by this bound in `require_fn`

error[E0271]: type mismatch resolving `<unsafe fn() -> i32 {h} as FnOnce<()>>::Output == i32`
--> $DIR/fn-trait.rs:29:16
|
LL | require_fn(h);
| ---------- ^ types differ
| |
| required by a bound introduced by this call
|
note: required by a bound in `require_fn`
--> $DIR/fn-trait.rs:3:31
|
LL | fn require_fn(_: impl Fn() -> i32) {}
| ^^^ required by this bound in `require_fn`

error: aborting due to 8 previous errors
error: aborting due to 4 previous errors

Some errors have detailed explanations: E0271, E0277.
For more information about an error, try `rustc --explain E0271`.
For more information about this error, try `rustc --explain E0277`.
Loading