Skip to content

Commit 13a86e9

Browse files
committed
Reveal opaques defined in the body during analysis
1 parent 503dce3 commit 13a86e9

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,19 @@ impl<'tcx> Body<'tcx> {
418418

419419
pub fn typing_env(&self, tcx: TyCtxt<'tcx>) -> TypingEnv<'tcx> {
420420
match self.phase {
421-
// FIXME(#132279): we should reveal the opaques defined in the body during analysis.
422-
MirPhase::Built | MirPhase::Analysis(_) => TypingEnv {
423-
typing_mode: ty::TypingMode::non_body_analysis(),
424-
param_env: tcx.param_env(self.source.def_id()),
425-
},
421+
MirPhase::Built | MirPhase::Analysis(_) => {
422+
let def_id = self.source.def_id();
423+
let typing_mode = match self.source.instance {
424+
InstanceKind::Item(item_def_id) => item_def_id
425+
.as_local()
426+
.map_or(ty::TypingMode::non_body_analysis(), |local_def_id| {
427+
ty::TypingMode::analysis_in_body(tcx, local_def_id)
428+
}),
429+
_ => ty::TypingMode::non_body_analysis(),
430+
};
431+
432+
TypingEnv { typing_mode, param_env: tcx.param_env(def_id) }
433+
}
426434
MirPhase::Runtime(_) => TypingEnv::post_analysis(tcx, self.source.def_id()),
427435
}
428436
}

compiler/rustc_mir_transform/src/validate.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
619619
if pred.has_opaque_types() {
620620
return true;
621621
}
622+
if self.typing_env.typing_mode.may_define_opaque_types() {
623+
return true;
624+
}
622625

623626
let (infcx, param_env) = self.tcx.infer_ctxt().build_with_typing_env(self.typing_env);
624627
let ocx = ObligationCtxt::new(&infcx);

compiler/rustc_type_ir/src/infer_ctxt.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ impl<I: Interner> TypingMode<I> {
133133
TypingMode::PostBorrowckAnalysis { defined_opaque_types }
134134
}
135135
}
136+
137+
/// Returns `true` if this typing mode is allowed to define new opaque types.
138+
pub fn may_define_opaque_types(&self) -> bool {
139+
match self {
140+
TypingMode::Analysis { defining_opaque_types_and_generators } => {
141+
!defining_opaque_types_and_generators.is_empty()
142+
}
143+
TypingMode::Borrowck { defining_opaque_types } => !defining_opaque_types.is_empty(),
144+
_ => false,
145+
}
146+
}
136147
}
137148

138149
#[cfg_attr(feature = "nightly", rustc_diagnostic_item = "type_ir_infer_ctxt_like")]

0 commit comments

Comments
 (0)