From 180e599d56c325513f1bcba038952b3cda17575c Mon Sep 17 00:00:00 2001 From: Adwin White Date: Sat, 27 Jun 2026 15:56:22 +0800 Subject: [PATCH] use correct typing mode in mir building for the next solver --- compiler/rustc_middle/src/ty/mod.rs | 17 ++++++++++++ compiler/rustc_mir_build/src/builder/mod.rs | 24 ++++++++++++----- compiler/rustc_mir_build/src/builder/scope.rs | 6 +++-- .../rustc_mir_build/src/check_tail_calls.rs | 3 +-- .../rustc_mir_build/src/check_unsafety.rs | 3 +-- compiler/rustc_mir_build/src/thir/cx/mod.rs | 4 +-- .../src/thir/pattern/check_match.rs | 3 +-- .../opaques/wrong-typing-mode-ice-1.rs | 27 +++++++++++++++++++ .../opaques/wrong-typing-mode-ice-1.stderr | 9 +++++++ .../opaques/wrong-typing-mode-ice-2.rs | 15 +++++++++++ 10 files changed, 94 insertions(+), 17 deletions(-) create mode 100644 tests/ui/traits/next-solver/opaques/wrong-typing-mode-ice-1.rs create mode 100644 tests/ui/traits/next-solver/opaques/wrong-typing-mode-ice-1.stderr create mode 100644 tests/ui/traits/next-solver/opaques/wrong-typing-mode-ice-2.rs diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 3ea798ee45fb2..b86a51b28b022 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1127,6 +1127,23 @@ impl<'tcx> TypingEnv<'tcx> { Self::new(tcx.param_env(def_id), TypingMode::non_body_analysis()) } + /// Ideally we just use `TypingMode::PostTypeckUntilBorrowck`. + /// But that's not compatible with the old solver yet. + /// + /// FIXME: this should not be needed in the long term. + pub fn post_typeck_until_borrowck_for_mir_build( + tcx: TyCtxt<'tcx>, + def_id: LocalDefId, + ) -> TypingEnv<'tcx> { + if tcx.use_typing_mode_post_typeck_until_borrowck() { + TypingEnv::new(tcx.param_env(def_id.to_def_id()), ty::TypingMode::borrowck(tcx, def_id)) + } else { + // FIXME(#132279): We're in a body, we should use a typing + // mode which reveals the opaque types defined by that body. + TypingEnv::non_body_analysis(tcx, def_id) + } + } + pub fn post_analysis(tcx: TyCtxt<'tcx>, def_id: impl IntoQueryKey) -> TypingEnv<'tcx> { TypingEnv::new(tcx.param_env_normalized_for_post_analysis(def_id), TypingMode::PostAnalysis) } diff --git a/compiler/rustc_mir_build/src/builder/mod.rs b/compiler/rustc_mir_build/src/builder/mod.rs index 854a378bb993d..34a9e573a508f 100644 --- a/compiler/rustc_mir_build/src/builder/mod.rs +++ b/compiler/rustc_mir_build/src/builder/mod.rs @@ -505,9 +505,15 @@ fn construct_fn<'tcx>( ); } - // FIXME(#132279): This should be able to reveal opaque - // types defined during HIR typeck. - let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis()); + let typing_mode = if tcx.use_typing_mode_post_typeck_until_borrowck() { + TypingMode::borrowck(tcx, fn_def) + } else { + // FIXME(#132279): This should be able to reveal opaque + // types defined during HIR typeck. + TypingMode::non_body_analysis() + }; + + let infcx = tcx.infer_ctxt().build(typing_mode); let mut builder = Builder::new( thir, infcx, @@ -586,9 +592,15 @@ fn construct_const<'a, 'tcx>( _ => span_bug!(tcx.def_span(def), "can't build MIR for {:?}", def), }; - // FIXME(#132279): We likely want to be able to use the hidden types of - // opaques used by this function here. - let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis()); + let typing_mode = if tcx.use_typing_mode_post_typeck_until_borrowck() { + TypingMode::borrowck(tcx, def) + } else { + // FIXME(#132279): This should be able to reveal opaque + // types defined during HIR typeck. + TypingMode::non_body_analysis() + }; + + let infcx = tcx.infer_ctxt().build(typing_mode); let mut builder = Builder::new(thir, infcx, def, hir_id, span, 0, const_ty, const_ty_span, None); diff --git a/compiler/rustc_mir_build/src/builder/scope.rs b/compiler/rustc_mir_build/src/builder/scope.rs index 84abb6ca70e70..4810418c4bb10 100644 --- a/compiler/rustc_mir_build/src/builder/scope.rs +++ b/compiler/rustc_mir_build/src/builder/scope.rs @@ -969,8 +969,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { tcx: self.tcx, typeck_results, module: self.tcx.parent_module(self.hir_id).to_def_id(), - // FIXME(#132279): We're in a body, should handle opaques. - typing_env: rustc_middle::ty::TypingEnv::non_body_analysis(self.tcx, self.def_id), + typing_env: ty::TypingEnv::post_typeck_until_borrowck_for_mir_build( + self.tcx, + self.def_id, + ), dropless_arena: &dropless_arena, match_lint_level: self.hir_id, whole_match_span: Some(rustc_span::Span::default()), diff --git a/compiler/rustc_mir_build/src/check_tail_calls.rs b/compiler/rustc_mir_build/src/check_tail_calls.rs index ac9f6e384cf04..26deba3f58c5a 100644 --- a/compiler/rustc_mir_build/src/check_tail_calls.rs +++ b/compiler/rustc_mir_build/src/check_tail_calls.rs @@ -27,8 +27,7 @@ pub(crate) fn check_tail_calls(tcx: TyCtxt<'_>, def: LocalDefId) -> Result<(), E tcx, thir, found_errors: Ok(()), - // FIXME(#132279): we're clearly in a body here. - typing_env: ty::TypingEnv::non_body_analysis(tcx, def), + typing_env: ty::TypingEnv::post_typeck_until_borrowck_for_mir_build(tcx, def), is_closure, caller_def_id: def, }; diff --git a/compiler/rustc_mir_build/src/check_unsafety.rs b/compiler/rustc_mir_build/src/check_unsafety.rs index a5e5b9f7699b8..70e9129ffee3f 100644 --- a/compiler/rustc_mir_build/src/check_unsafety.rs +++ b/compiler/rustc_mir_build/src/check_unsafety.rs @@ -1093,8 +1093,7 @@ pub(crate) fn check_unsafety(tcx: TyCtxt<'_>, def: LocalDefId) { body_target_features, assignment_info: None, in_union_destructure: false, - // FIXME(#132279): we're clearly in a body here. - typing_env: ty::TypingEnv::non_body_analysis(tcx, def), + typing_env: ty::TypingEnv::post_typeck_until_borrowck_for_mir_build(tcx, def), inside_adt: false, warnings: &mut warnings, suggest_unsafe_block: true, diff --git a/compiler/rustc_mir_build/src/thir/cx/mod.rs b/compiler/rustc_mir_build/src/thir/cx/mod.rs index eb8573dd5e886..8e654032e8b71 100644 --- a/compiler/rustc_mir_build/src/thir/cx/mod.rs +++ b/compiler/rustc_mir_build/src/thir/cx/mod.rs @@ -99,9 +99,7 @@ impl<'tcx> ThirBuildCx<'tcx> { Self { tcx, thir: Thir::new(body_type), - // FIXME(#132279): We're in a body, we should use a typing - // mode which reveals the opaque types defined by that body. - typing_env: ty::TypingEnv::non_body_analysis(tcx, def), + typing_env: ty::TypingEnv::post_typeck_until_borrowck_for_mir_build(tcx, def), typeck_results, body_owner: def.to_def_id(), apply_adjustments: !find_attr!(tcx, hir_id, CustomMir(..)), diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index 7a38d9293724d..a757c96bd7f3d 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -39,8 +39,7 @@ pub(crate) fn check_match(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), Err tcx, thir: &*thir, typeck_results, - // FIXME(#132279): We're in a body, should handle opaques. - typing_env: ty::TypingEnv::non_body_analysis(tcx, def_id), + typing_env: ty::TypingEnv::post_typeck_until_borrowck_for_mir_build(tcx, def_id), hir_source: tcx.local_def_id_to_hir_id(def_id), let_source: LetSource::None, pattern_arena: &pattern_arena, diff --git a/tests/ui/traits/next-solver/opaques/wrong-typing-mode-ice-1.rs b/tests/ui/traits/next-solver/opaques/wrong-typing-mode-ice-1.rs new file mode 100644 index 0000000000000..700e38cc3d8a2 --- /dev/null +++ b/tests/ui/traits/next-solver/opaques/wrong-typing-mode-ice-1.rs @@ -0,0 +1,27 @@ +//@ compile-flags: -Znext-solver +//@ edition: 2024 + +// Previously, when building MIR body, we were using typing env +// `non_body_analysis` which is wrong since we're indeed in a body. +// It caused opaque types in defining body to be not revealed. +// This caused opaque types in the defining body not to be revealed. + +#![feature(type_alias_impl_trait)] + +struct Task(F); + +impl Task { + fn spawn(&self, _: impl FnOnce() -> Foo) {} +} +type Foo = impl Sized; + +#[define_opaque(Foo)] +fn foo() { + async fn cb() {} + Task::spawn(&POOL, cb) +} + +static POOL: Task = todo!(); +//~^ ERROR: evaluation panicked + +fn main() {} diff --git a/tests/ui/traits/next-solver/opaques/wrong-typing-mode-ice-1.stderr b/tests/ui/traits/next-solver/opaques/wrong-typing-mode-ice-1.stderr new file mode 100644 index 0000000000000..95c000394a982 --- /dev/null +++ b/tests/ui/traits/next-solver/opaques/wrong-typing-mode-ice-1.stderr @@ -0,0 +1,9 @@ +error[E0080]: evaluation panicked: not yet implemented + --> $DIR/wrong-typing-mode-ice-1.rs:24:26 + | +LL | static POOL: Task = todo!(); + | ^^^^^^^ evaluation of `POOL` failed here + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0080`. diff --git a/tests/ui/traits/next-solver/opaques/wrong-typing-mode-ice-2.rs b/tests/ui/traits/next-solver/opaques/wrong-typing-mode-ice-2.rs new file mode 100644 index 0000000000000..e27858b6b6fa0 --- /dev/null +++ b/tests/ui/traits/next-solver/opaques/wrong-typing-mode-ice-2.rs @@ -0,0 +1,15 @@ +//@ compile-flags: -Znext-solver +//@ check-pass + +// Previously, when building MIR body, we were using typing env +// `non_body_analysis` which is wrong since we're indeed in a body. +// It caused opaque types in defining body to be not revealed. +// This caused opaque types in the defining body not to be revealed. + +#![feature(type_alias_impl_trait)] +fn main() { + struct Foo(U); + type U = impl Copy; + let foo: _ = Foo(()); + let Foo(()) = foo; +}