Skip to content
Open
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
17 changes: 9 additions & 8 deletions compiler/rustc_const_eval/src/check_consts/qualifs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc_errors::ErrorGuaranteed;
use rustc_hir::LangItem;
use rustc_infer::infer::TyCtxtInferExt;
use rustc_middle::mir::*;
use rustc_middle::ty::{self, AdtDef, Ty};
use rustc_middle::ty::{self, AdtDef, Ty, TypingMode};
use rustc_middle::{bug, mir};
use rustc_trait_selection::traits::{Obligation, ObligationCause, ObligationCtxt};
use tracing::instrument;
Expand Down Expand Up @@ -100,13 +100,14 @@ impl Qualif for HasMutInterior {
// Instead we invoke an obligation context manually, and provide the opaque type inference settings
// that allow the trait solver to just error out instead of cycling.
let freeze_def_id = cx.tcx.require_lang_item(LangItem::Freeze, cx.body.span);
// FIXME(#132279): Once we've got a typing mode which reveals opaque types using the HIR
// typeck results without causing query cycles, we should use this here instead of defining
// opaque types.
let typing_env = ty::TypingEnv::new(
cx.typing_env.param_env,
ty::TypingMode::analysis_in_body(cx.tcx, cx.body.source.def_id().expect_local()),
);
let did = cx.body.source.def_id().expect_local();

let typing_env = if cx.tcx.use_typing_mode_borrowck() {
cx.typing_env
} else {
ty::TypingEnv::new(cx.typing_env.param_env, TypingMode::analysis_in_body(cx.tcx, did))
};

let (infcx, param_env) = cx.tcx.infer_ctxt().build_with_typing_env(typing_env);
let ocx = ObligationCtxt::new(&infcx);
let obligation = Obligation::new(
Expand Down
25 changes: 25 additions & 0 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,31 @@ pub struct InferCtxt<'tcx> {
pub obligation_inspector: Cell<Option<ObligationInspector<'tcx>>>,
}

impl<'tcx> Drop for InferCtxt<'tcx> {
fn drop(&mut self) {
// Defuse the drop bomb in the OpaqueTypeStorage when we're in TypingMode::Borrowck,
// and the InferCtxt doesn't consider regions. This is okay since in `Borrowck`,
// the only reason we care about opaques is in relation to regions.
// In some places *after* typeck, like in lints we use `TypingMode::Borrowck`
// to prevent defining opaque types and we simply don't care about regions.
Comment thread
jdonszelmann marked this conversation as resolved.
match self.typing_mode() {
TypingMode::Coherence
| TypingMode::Analysis { .. }
| TypingMode::PostBorrowckAnalysis { .. }
| TypingMode::PostAnalysis => {}
TypingMode::Borrowck { .. } => {
if !self.considering_regions {
let _ = self.inner.borrow_mut().opaque_type_storage.take_opaque_types();
}
}
}

if !self.is_empty() {
ty::tls::with(|tcx| tcx.dcx().delayed_bug(format!("{:?}", self.opaque_types)));
}
}
Copy link
Copy Markdown
Contributor

@lcnr lcnr May 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please update comment the merge the controlflow

View changes since the review

}

/// See the `error_reporting` module for more details.
#[derive(Clone, Copy, Debug, PartialEq, Eq, TypeFoldable, TypeVisitable)]
pub enum ValuePairs<'tcx> {
Expand Down
8 changes: 0 additions & 8 deletions compiler/rustc_infer/src/infer/opaque_types/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,6 @@ impl<'tcx> OpaqueTypeStorage<'tcx> {
}
}

impl<'tcx> Drop for OpaqueTypeStorage<'tcx> {
fn drop(&mut self) {
if !self.is_empty() {
ty::tls::with(|tcx| tcx.dcx().delayed_bug(format!("{:?}", self.opaque_types)));
}
}
}

pub struct OpaqueTypeTable<'a, 'tcx> {
storage: &'a mut OpaqueTypeStorage<'tcx>,

Expand Down
11 changes: 8 additions & 3 deletions compiler/rustc_lint/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,14 @@ impl<'tcx> LateContext<'tcx> {
/// The typing mode of the currently visited node. Use this when
/// building a new `InferCtxt`.
pub fn typing_mode(&self) -> TypingMode<'tcx> {
// FIXME(#132279): In case we're in a body, we should use a typing
// mode which reveals the opaque types defined by that body.
TypingMode::non_body_analysis()
if let Some(body_id) = self.enclosing_body
&& self.tcx.use_typing_mode_borrowck()
{
let def_id = self.tcx.hir_enclosing_body_owner(body_id.hir_id);
TypingMode::borrowck(self.tcx, def_id)
} else {
TypingMode::non_body_analysis()
}
}

pub fn typing_env(&self) -> TypingEnv<'tcx> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound {
}

let def_id = opaque.def_id.to_def_id();
let infcx = &cx.tcx.infer_ctxt().build(cx.typing_mode());
let infcx = &cx.tcx.infer_ctxt().ignoring_regions().build(cx.typing_mode());
// For every projection predicate in the opaque type's explicit bounds,
// check that the type that we're assigning actually satisfies the bounds
// of the associated type.
Expand Down
37 changes: 30 additions & 7 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,13 +413,36 @@ impl<'tcx> Body<'tcx> {
}

pub fn typing_env(&self, tcx: TyCtxt<'tcx>) -> TypingEnv<'tcx> {
match self.phase {
// FIXME(#132279): we should reveal the opaques defined in the body during analysis.
MirPhase::Built | MirPhase::Analysis(_) => TypingEnv::new(
tcx.param_env(self.source.def_id()),
ty::TypingMode::non_body_analysis(),
),
MirPhase::Runtime(_) => TypingEnv::post_analysis(tcx, self.source.def_id()),
if tcx.use_typing_mode_borrowck() {
match self.phase {
MirPhase::Built 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() => {
TypingEnv::new(
tcx.param_env(self.source.def_id()),
ty::TypingMode::post_borrowck_analysis(tcx, def_id),
)
}
MirPhase::Built | MirPhase::Analysis(_) => {
// This branch happens for drop glue and fn ptr shims.
// FIXME: why do we do any of this analysis on drop glue etc?
// This should ideally all be skipped.
TypingEnv::post_analysis(tcx, self.source.def_id())
}
MirPhase::Runtime(_) => TypingEnv::post_analysis(tcx, self.source.def_id()),
}
} else {
match self.phase {
MirPhase::Built | MirPhase::Analysis(_) => TypingEnv::new(
tcx.param_env(self.source.def_id()),
ty::TypingMode::non_body_analysis(),
),
MirPhase::Runtime(_) => TypingEnv::post_analysis(tcx, self.source.def_id()),
}
}
}

Expand Down
6 changes: 0 additions & 6 deletions compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,6 @@ pub trait HasTyCtxt<'tcx>: HasDataLayout {

pub trait HasTypingEnv<'tcx> {
fn typing_env(&self) -> ty::TypingEnv<'tcx>;

/// FIXME(#132279): This method should not be used as in the future
/// everything should take a `TypingEnv` instead. Remove it as that point.
fn param_env(&self) -> ty::ParamEnv<'tcx> {
self.typing_env().param_env
}
}

impl<'tcx> HasDataLayout for TyCtxt<'tcx> {
Expand Down
Loading