Skip to content

Process alias-relate obligations when proving receiver_is_valid #127171

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

Closed
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
10 changes: 9 additions & 1 deletion compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,15 @@ fn receiver_is_valid<'tcx>(
let cause =
ObligationCause::new(span, wfcx.body_def_id, traits::ObligationCauseCode::MethodReceiver);

let can_eq_self = |ty| infcx.can_eq(wfcx.param_env, self_ty, ty);
let can_eq_self = |ty| {
wfcx.infcx.probe(|_| {
let ocx = ObligationCtxt::new(wfcx.infcx);
let Ok(()) = ocx.eq(&ObligationCause::dummy(), wfcx.param_env, self_ty, ty) else {
return false;
};
ocx.select_where_possible().is_empty()
})
};

// `self: Self` is always valid.
if can_eq_self(receiver_ty) {
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,8 @@ impl<'tcx> InferCtxt<'tcx> {
.collect()
}

// FIXME(-Znext-solver): Get rid of this method, it's never correct. Either that,
// or we need to process the obligations.
pub fn can_sub<T>(&self, param_env: ty::ParamEnv<'tcx>, expected: T, actual: T) -> bool
where
T: at::ToTrace<'tcx>,
Expand All @@ -779,6 +781,8 @@ impl<'tcx> InferCtxt<'tcx> {
})
}

// FIXME(-Znext-solver): Get rid of this method, it's never correct. Either that,
// or we need to process the obligations.
pub fn can_eq<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> bool
where
T: at::ToTrace<'tcx>,
Expand Down
23 changes: 23 additions & 0 deletions tests/ui/traits/next-solver/typeck/receiver-self-ty-check-eq.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//@ compile-flags: -Znext-solver
//@ check-pass

// Fixes a regression in `receiver_is_valid` in wfcheck where we were using
// `InferCtxt::can_eq` instead of processing alias-relate goals, leading to false
// positives, not deref'ing enough steps to check the receiver is valid.

trait Mirror {
type Mirror: ?Sized;
}
impl<T: ?Sized> Mirror for T {
type Mirror = T;
}

trait Foo {
fn foo(&self) {}
}

impl Foo for <() as Mirror>::Mirror {
fn foo(&self) {}
}

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | fn bar(self: Bar<u32>) {
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)

error[E0307]: invalid `self` parameter type: `&Bar<u32>`
--> $DIR/method_resolution3.rs:21:18
--> $DIR/method_resolution3.rs:20:18
|
LL | fn baz(self: &Bar<u32>) {
| ^^^^^^^^^
Expand Down
18 changes: 12 additions & 6 deletions tests/ui/type-alias-impl-trait/method_resolution3.next.stderr
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
error[E0271]: type mismatch resolving `Foo == u32`
error[E0307]: invalid `self` parameter type: `Bar<u32>`
--> $DIR/method_resolution3.rs:16:18
|
LL | fn bar(self: Bar<u32>) {
| ^^^^^^^^ types differ
| ^^^^^^^^
|
= note: type of `self` must be `Self` or a type that dereferences to it
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)

error[E0271]: type mismatch resolving `Foo == u32`
--> $DIR/method_resolution3.rs:21:18
error[E0307]: invalid `self` parameter type: `&Bar<u32>`
--> $DIR/method_resolution3.rs:20:18
|
LL | fn baz(self: &Bar<u32>) {
| ^^^^^^^^^ types differ
| ^^^^^^^^^
|
= note: type of `self` must be `Self` or a type that dereferences to it
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0271`.
For more information about this error, try `rustc --explain E0307`.
6 changes: 2 additions & 4 deletions tests/ui/type-alias-impl-trait/method_resolution3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ struct Bar<T>(T);

impl Bar<Foo> {
fn bar(self: Bar<u32>) {
//[current]~^ ERROR: invalid `self` parameter
//[next]~^^ ERROR: type mismatch resolving `Foo == u32`
//~^ ERROR: invalid `self` parameter
self.foo()
}
fn baz(self: &Bar<u32>) {
//[current]~^ ERROR: invalid `self` parameter
//[next]~^^ ERROR: type mismatch resolving `Foo == u32`
//~^ ERROR: invalid `self` parameter
self.foo()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | fn foo(self: Bar<Foo>) {
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)

error[E0307]: invalid `self` parameter type: `&Bar<Foo>`
--> $DIR/method_resolution4.rs:32:20
--> $DIR/method_resolution4.rs:31:20
|
LL | fn foomp(self: &Bar<Foo>) {
| ^^^^^^^^^
Expand Down
18 changes: 12 additions & 6 deletions tests/ui/type-alias-impl-trait/method_resolution4.next.stderr
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
error[E0271]: type mismatch resolving `u32 == Foo`
error[E0307]: invalid `self` parameter type: `Bar<Foo>`
--> $DIR/method_resolution4.rs:27:18
|
LL | fn foo(self: Bar<Foo>) {
| ^^^^^^^^ types differ
| ^^^^^^^^
|
= note: type of `self` must be `Self` or a type that dereferences to it
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)

error[E0271]: type mismatch resolving `u32 == Foo`
--> $DIR/method_resolution4.rs:32:20
error[E0307]: invalid `self` parameter type: `&Bar<Foo>`
--> $DIR/method_resolution4.rs:31:20
|
LL | fn foomp(self: &Bar<Foo>) {
| ^^^^^^^^^ types differ
| ^^^^^^^^^
|
= note: type of `self` must be `Self` or a type that dereferences to it
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0271`.
For more information about this error, try `rustc --explain E0307`.
6 changes: 2 additions & 4 deletions tests/ui/type-alias-impl-trait/method_resolution4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ impl Bar<Foo> {

impl Bar<u32> {
fn foo(self: Bar<Foo>) {
//[current]~^ ERROR: invalid `self` parameter
//[next]~^^ ERROR: type mismatch resolving `u32 == Foo`
//~^ ERROR: invalid `self` parameter
self.bar()
}
fn foomp(self: &Bar<Foo>) {
//[current]~^ ERROR: invalid `self` parameter
//[next]~^^ ERROR: type mismatch resolving `u32 == Foo`
//~^ ERROR: invalid `self` parameter
self.bar()
}
}
Expand Down
Loading