From fdde66acee3ba4c85798ea5120fa76f86bf697fe Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sun, 30 Jun 2024 11:30:55 -0400 Subject: [PATCH] Process alias-relate obligations when proving receiver_is_valid --- .../rustc_hir_analysis/src/check/wfcheck.rs | 10 +++++++- compiler/rustc_infer/src/infer/mod.rs | 4 ++++ .../issue-100222.nn.stderr | 12 ---------- .../issue-100222.ny.stderr | 12 ---------- .../rfc-2632-const-trait-impl/issue-100222.rs | 3 ++- .../issue-100222.yn.stderr | 12 ---------- .../issue-100222.yy.stderr | 12 ---------- .../typeck/receiver-self-ty-check-eq.rs | 23 +++++++++++++++++++ 8 files changed, 38 insertions(+), 50 deletions(-) delete mode 100644 tests/ui/rfcs/rfc-2632-const-trait-impl/issue-100222.nn.stderr delete mode 100644 tests/ui/rfcs/rfc-2632-const-trait-impl/issue-100222.ny.stderr delete mode 100644 tests/ui/rfcs/rfc-2632-const-trait-impl/issue-100222.yn.stderr delete mode 100644 tests/ui/rfcs/rfc-2632-const-trait-impl/issue-100222.yy.stderr create mode 100644 tests/ui/traits/next-solver/typeck/receiver-self-ty-check-eq.rs diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index 2230528a5ae1..279e9fc5df37 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -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) { diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index ff593d7ffb7d..835c412a64d6 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -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(&self, param_env: ty::ParamEnv<'tcx>, expected: T, actual: T) -> bool where T: at::ToTrace<'tcx>, @@ -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(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> bool where T: at::ToTrace<'tcx>, diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/issue-100222.nn.stderr b/tests/ui/rfcs/rfc-2632-const-trait-impl/issue-100222.nn.stderr deleted file mode 100644 index 03536dca1e84..000000000000 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/issue-100222.nn.stderr +++ /dev/null @@ -1,12 +0,0 @@ -error[E0271]: type mismatch resolving `<() as Index>::Output == &mut <() as Index>::Output` - --> $DIR/issue-100222.rs:34:12 - | -LL | fn foo(&mut self, x: ::Output) -> ::Output - | ^^^^^^^^^ expected `()`, found `&mut <() as Index>::Output` - | - = note: expected unit type `()` - found mutable reference `&mut <() as Index>::Output` - -error: aborting due to 1 previous error - -For more information about this error, try `rustc --explain E0271`. diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/issue-100222.ny.stderr b/tests/ui/rfcs/rfc-2632-const-trait-impl/issue-100222.ny.stderr deleted file mode 100644 index 6a70a5036061..000000000000 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/issue-100222.ny.stderr +++ /dev/null @@ -1,12 +0,0 @@ -error[E0271]: type mismatch resolving `<() as Index>::Output == &mut <() as Index>::Output` - --> $DIR/issue-100222.rs:25:12 - | -LL | fn foo(&mut self, x: ::Output) -> ::Output - | ^^^^^^^^^ expected `()`, found `&mut <() as Index>::Output` - | - = note: expected unit type `()` - found mutable reference `&mut <() as Index>::Output` - -error: aborting due to 1 previous error - -For more information about this error, try `rustc --explain E0271`. diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/issue-100222.rs b/tests/ui/rfcs/rfc-2632-const-trait-impl/issue-100222.rs index 7949772a2b4e..47f9fc664cef 100644 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/issue-100222.rs +++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/issue-100222.rs @@ -1,6 +1,7 @@ //@ revisions: nn ny yn yy -//@ known-bug: #110395 //@ compile-flags: -Znext-solver +//@ check-pass + #![allow(incomplete_features)] #![feature(const_trait_impl, effects, associated_type_defaults, const_mut_refs)] diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/issue-100222.yn.stderr b/tests/ui/rfcs/rfc-2632-const-trait-impl/issue-100222.yn.stderr deleted file mode 100644 index 03536dca1e84..000000000000 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/issue-100222.yn.stderr +++ /dev/null @@ -1,12 +0,0 @@ -error[E0271]: type mismatch resolving `<() as Index>::Output == &mut <() as Index>::Output` - --> $DIR/issue-100222.rs:34:12 - | -LL | fn foo(&mut self, x: ::Output) -> ::Output - | ^^^^^^^^^ expected `()`, found `&mut <() as Index>::Output` - | - = note: expected unit type `()` - found mutable reference `&mut <() as Index>::Output` - -error: aborting due to 1 previous error - -For more information about this error, try `rustc --explain E0271`. diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/issue-100222.yy.stderr b/tests/ui/rfcs/rfc-2632-const-trait-impl/issue-100222.yy.stderr deleted file mode 100644 index 6a70a5036061..000000000000 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/issue-100222.yy.stderr +++ /dev/null @@ -1,12 +0,0 @@ -error[E0271]: type mismatch resolving `<() as Index>::Output == &mut <() as Index>::Output` - --> $DIR/issue-100222.rs:25:12 - | -LL | fn foo(&mut self, x: ::Output) -> ::Output - | ^^^^^^^^^ expected `()`, found `&mut <() as Index>::Output` - | - = note: expected unit type `()` - found mutable reference `&mut <() as Index>::Output` - -error: aborting due to 1 previous error - -For more information about this error, try `rustc --explain E0271`. diff --git a/tests/ui/traits/next-solver/typeck/receiver-self-ty-check-eq.rs b/tests/ui/traits/next-solver/typeck/receiver-self-ty-check-eq.rs new file mode 100644 index 000000000000..f8c8a17b7e50 --- /dev/null +++ b/tests/ui/traits/next-solver/typeck/receiver-self-ty-check-eq.rs @@ -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 Mirror for T { + type Mirror = T; +} + +trait Foo { + fn foo(&self) {} +} + +impl Foo for <() as Mirror>::Mirror { + fn foo(&self) {} +} + +fn main() {}