From 54e795213e01fd30da7e6b0f3645713ccfbf9359 Mon Sep 17 00:00:00 2001 From: Amirhossein Akhlaghpour Date: Mon, 5 Oct 2026 19:04:31 +0330 Subject: [PATCH 1/2] use pre-borrowck typing env during initial MIR analysis Signed-off-by: Amirhossein Akhlaghpour --- compiler/rustc_middle/src/mir/mod.rs | 8 ++++++-- compiler/rustc_mir_transform/src/validate.rs | 5 +++-- compiler/rustc_ty_utils/src/common_traits.rs | 3 ++- .../typing-env-before-borrowck.rs | 10 ++++++++++ 4 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 tests/ui/type-alias-impl-trait/typing-env-before-borrowck.rs diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 5414863f4a459..29ca694b6431b 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -415,13 +415,17 @@ impl<'tcx> Body<'tcx> { pub fn typing_env(&self, tcx: TyCtxt<'tcx>) -> TypingEnv<'tcx> { if tcx.use_typing_mode_post_typeck_until_borrowck() { match self.phase { - MirPhase::Built if let Some(def_id) = self.source.def_id().as_local() => { + MirPhase::Built | MirPhase::Analysis(AnalysisPhase::Initial) + if let Some(def_id) = self.source.def_id().as_local() => + { TypingEnv::new( tcx.param_env(self.source.def_id()), ty::TypingMode::borrowck(tcx, def_id), ) } - MirPhase::Analysis(_) if let Some(def_id) = self.source.def_id().as_local() => { + MirPhase::Analysis(AnalysisPhase::PostCleanup) + if let Some(def_id) = self.source.def_id().as_local() => + { TypingEnv::new( tcx.param_env(self.source.def_id()), ty::TypingMode::post_borrowck_analysis(tcx, def_id), diff --git a/compiler/rustc_mir_transform/src/validate.rs b/compiler/rustc_mir_transform/src/validate.rs index 34bc276e790fb..c8564b8288cd2 100644 --- a/compiler/rustc_mir_transform/src/validate.rs +++ b/compiler/rustc_mir_transform/src/validate.rs @@ -629,11 +629,12 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { // is annoying. It is harmless enough to just not validate anything // in that case. We still check this after analysis as all opaque // types have been revealed at this point. - if pred.has_opaque_types() { + if !self.tcx.next_trait_solver_globally() && pred.has_opaque_types() { return true; } - let (infcx, param_env) = self.tcx.infer_ctxt().build_with_typing_env(self.typing_env); + let (infcx, param_env) = + self.tcx.infer_ctxt().ignoring_regions().build_with_typing_env(self.typing_env); let ocx = ObligationCtxt::new(&infcx); ocx.register_obligation(Obligation::new( self.tcx, diff --git a/compiler/rustc_ty_utils/src/common_traits.rs b/compiler/rustc_ty_utils/src/common_traits.rs index 48e2a68bec7f6..be93465b3b358 100644 --- a/compiler/rustc_ty_utils/src/common_traits.rs +++ b/compiler/rustc_ty_utils/src/common_traits.rs @@ -49,7 +49,8 @@ fn is_trait_raw<'tcx>( query: ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>, item: LangItem, ) -> bool { - let (infcx, param_env) = tcx.infer_ctxt().build_with_typing_env(query.typing_env); + let (infcx, param_env) = + tcx.infer_ctxt().ignoring_regions().build_with_typing_env(query.typing_env); let trait_def_id = tcx.require_lang_item(item, DUMMY_SP); traits::type_known_to_meet_bound_modulo_regions(&infcx, param_env, query.value, trait_def_id) } diff --git a/tests/ui/type-alias-impl-trait/typing-env-before-borrowck.rs b/tests/ui/type-alias-impl-trait/typing-env-before-borrowck.rs new file mode 100644 index 0000000000000..5219ee821b189 --- /dev/null +++ b/tests/ui/type-alias-impl-trait/typing-env-before-borrowck.rs @@ -0,0 +1,10 @@ +//@ compile-flags: -Zvalidate-mir -Znext-solver=globally +//@ check-pass + +struct Opaque(F, F::Output); + +fn f() -> impl Sized { + Opaque(f, ()); +} + +fn main() {} From c3054b8c6448f5b4771e71b96b815bd7f67d97e9 Mon Sep 17 00:00:00 2001 From: Amirhossein Akhlaghpour Date: Thu, 8 Oct 2026 19:26:43 +0330 Subject: [PATCH 2/2] update UI tests for pre-borrowck typing environment Signed-off-by: Amirhossein Akhlaghpour --- .../switch-int-terminator-126680.next.stderr | 21 ----------------- .../switch-int-terminator-126680.rs | 2 +- .../trait-upcasting-131886.next.stderr | 20 ---------------- .../trait-upcasting-131886.rs | 5 ++-- .../struct-assignment-validity.next.stderr | 23 ------------------- .../struct-assignment-validity.rs | 2 +- 6 files changed, 4 insertions(+), 69 deletions(-) delete mode 100644 tests/ui/impl-trait/normalize-hir-typeck/switch-int-terminator-126680.next.stderr delete mode 100644 tests/ui/impl-trait/normalize-hir-typeck/trait-upcasting-131886.next.stderr delete mode 100644 tests/ui/type-alias-impl-trait/struct-assignment-validity.next.stderr diff --git a/tests/ui/impl-trait/normalize-hir-typeck/switch-int-terminator-126680.next.stderr b/tests/ui/impl-trait/normalize-hir-typeck/switch-int-terminator-126680.next.stderr deleted file mode 100644 index 47550ef9db818..0000000000000 --- a/tests/ui/impl-trait/normalize-hir-typeck/switch-int-terminator-126680.next.stderr +++ /dev/null @@ -1,21 +0,0 @@ -error[E0391]: cycle detected when computing type of `Bar::{opaque#0}` - --> $DIR/switch-int-terminator-126680.rs:17:12 - | -LL | type Bar = impl Sized; - | ^^^^^^^^^^ - | - = note: ...which requires computing type of opaque `Bar::{opaque#0}`... -note: ...which requires borrow-checking `foo`... - --> $DIR/switch-int-terminator-126680.rs:26:1 - | -LL | fn foo() -> Struct { - | ^^^^^^^^^^^^^^^^^^ - = note: ...which requires promoting constants in MIR for `foo`... - = note: ...which requires normalizing `fn(Bar)`... - = note: ...which again requires computing type of `Bar::{opaque#0}`, completing the cycle - = note: cycle used when checking that `Bar::{opaque#0}` is well-formed - = note: for more information, see and - -error: aborting due to 1 previous error - -For more information about this error, try `rustc --explain E0391`. diff --git a/tests/ui/impl-trait/normalize-hir-typeck/switch-int-terminator-126680.rs b/tests/ui/impl-trait/normalize-hir-typeck/switch-int-terminator-126680.rs index bb8e34ff35140..b11ae025100bc 100644 --- a/tests/ui/impl-trait/normalize-hir-typeck/switch-int-terminator-126680.rs +++ b/tests/ui/impl-trait/normalize-hir-typeck/switch-int-terminator-126680.rs @@ -1,7 +1,7 @@ //@ revisions: old next //@ ignore-compare-mode-next-solver (explicit revisions) //@[next] compile-flags: -Znext-solver -//@[next] known-bug: trait-system-refactor-initiative#303 +//@[next] check-pass //@[old] failure-status: 101 //@[old] dont-check-compiler-stderr //@[old] known-bug: #126680 diff --git a/tests/ui/impl-trait/normalize-hir-typeck/trait-upcasting-131886.next.stderr b/tests/ui/impl-trait/normalize-hir-typeck/trait-upcasting-131886.next.stderr deleted file mode 100644 index e46f78337a6f3..0000000000000 --- a/tests/ui/impl-trait/normalize-hir-typeck/trait-upcasting-131886.next.stderr +++ /dev/null @@ -1,20 +0,0 @@ -error[E0391]: cycle detected when computing type of `Tait::{opaque#0}` - --> $DIR/trait-upcasting-131886.rs:15:13 - | -LL | type Tait = impl Sized; - | ^^^^^^^^^^ - | - = note: ...which requires computing type of opaque `Tait::{opaque#0}`... -note: ...which requires borrow-checking `test_correct3`... - --> $DIR/trait-upcasting-131886.rs:21:1 - | -LL | fn test_correct3(x: &dyn Foo) { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - = note: ...which requires promoting constants in MIR for `test_correct3`... - = note: ...which again requires computing type of `Tait::{opaque#0}`, completing the cycle - = note: cycle used when checking that `Tait::{opaque#0}` is well-formed - = note: for more information, see and - -error: aborting due to 1 previous error - -For more information about this error, try `rustc --explain E0391`. diff --git a/tests/ui/impl-trait/normalize-hir-typeck/trait-upcasting-131886.rs b/tests/ui/impl-trait/normalize-hir-typeck/trait-upcasting-131886.rs index 3f506a7f5f65e..986c1efa5e385 100644 --- a/tests/ui/impl-trait/normalize-hir-typeck/trait-upcasting-131886.rs +++ b/tests/ui/impl-trait/normalize-hir-typeck/trait-upcasting-131886.rs @@ -1,7 +1,7 @@ //@ revisions: old next //@ ignore-compare-mode-next-solver (explicit revisions) //@[next] compile-flags: -Znext-solver -//@[next] known-bug: trait-system-refactor-initiative#303 +//@[next] check-pass //@[old] failure-status: 101 //@[old] dont-check-compiler-stderr //@[old] known-bug: #131886 @@ -9,8 +9,7 @@ #![feature(type_alias_impl_trait)] // This ICEd in MIR validation with the old solver. It's fixed by normalizing -// opaque types with the new solver. This should compile, but currently fails -// with a query cycle error. +// opaque types with the new solver. This should compile without a query cycle. type Tait = impl Sized; diff --git a/tests/ui/type-alias-impl-trait/struct-assignment-validity.next.stderr b/tests/ui/type-alias-impl-trait/struct-assignment-validity.next.stderr deleted file mode 100644 index 78724a5ccec1c..0000000000000 --- a/tests/ui/type-alias-impl-trait/struct-assignment-validity.next.stderr +++ /dev/null @@ -1,23 +0,0 @@ -error[E0391]: cycle detected when computing type of `::Assoc::{opaque#0}` - --> $DIR/struct-assignment-validity.rs:21:18 - | -LL | type Assoc = impl std::fmt::Debug; - | ^^^^^^^^^^^^^^^^^^^^ - | - = note: ...which requires computing type of opaque `::Assoc::{opaque#0}`... -note: ...which requires borrow-checking `::foo`... - --> $DIR/struct-assignment-validity.rs:22:5 - | -LL | / fn foo() -> Foo -LL | | where -LL | | Self::Assoc:, - | |_____________________^ - = note: ...which requires promoting constants in MIR for `::foo`... - = note: ...which requires normalizing `::Assoc`... - = note: ...which again requires computing type of `::Assoc::{opaque#0}`, completing the cycle - = note: cycle used when checking that `::Assoc::{opaque#0}` is well-formed - = note: for more information, see and - -error: aborting due to 1 previous error - -For more information about this error, try `rustc --explain E0391`. diff --git a/tests/ui/type-alias-impl-trait/struct-assignment-validity.rs b/tests/ui/type-alias-impl-trait/struct-assignment-validity.rs index 608c804be9c94..8005edbf1636e 100644 --- a/tests/ui/type-alias-impl-trait/struct-assignment-validity.rs +++ b/tests/ui/type-alias-impl-trait/struct-assignment-validity.rs @@ -1,7 +1,7 @@ //@ revisions: old next //@ ignore-compare-mode-next-solver (explicit revisions) //@[next] compile-flags: -Znext-solver -//@[next] known-bug: trait-system-refactor-initiative#303 +//@[next] check-pass //@ compile-flags: -Zvalidate-mir //@[old] check-pass