Skip to content

Commit 4f4138a

Browse files
Rollup merge of rust-lang#156141 - jdonszelmann:use-right-typingmode, r=lcnr
Resolve some cases of rust-lang#132279 by using the right typing mode in the next solver r? @lcnr Convert 3 FIXMEs of rust-lang#132279 to using the right typing mode when we can (`tcx.use_typing_mode_borrowck()`) Also resolves rust-lang#155093, which I closed
2 parents ca6f18a + da70164 commit 4f4138a

7 files changed

Lines changed: 76 additions & 34 deletions

File tree

compiler/rustc_const_eval/src/check_consts/qualifs.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_errors::ErrorGuaranteed;
99
use rustc_hir::LangItem;
1010
use rustc_infer::infer::TyCtxtInferExt;
1111
use rustc_middle::mir::*;
12-
use rustc_middle::ty::{self, AdtDef, Ty};
12+
use rustc_middle::ty::{self, AdtDef, Ty, TypingMode};
1313
use rustc_middle::{bug, mir};
1414
use rustc_trait_selection::traits::{Obligation, ObligationCause, ObligationCtxt};
1515
use tracing::instrument;
@@ -100,13 +100,14 @@ impl Qualif for HasMutInterior {
100100
// Instead we invoke an obligation context manually, and provide the opaque type inference settings
101101
// that allow the trait solver to just error out instead of cycling.
102102
let freeze_def_id = cx.tcx.require_lang_item(LangItem::Freeze, cx.body.span);
103-
// FIXME(#132279): Once we've got a typing mode which reveals opaque types using the HIR
104-
// typeck results without causing query cycles, we should use this here instead of defining
105-
// opaque types.
106-
let typing_env = ty::TypingEnv::new(
107-
cx.typing_env.param_env,
108-
ty::TypingMode::analysis_in_body(cx.tcx, cx.body.source.def_id().expect_local()),
109-
);
103+
let did = cx.body.source.def_id().expect_local();
104+
105+
let typing_env = if cx.tcx.use_typing_mode_borrowck() {
106+
cx.typing_env
107+
} else {
108+
ty::TypingEnv::new(cx.typing_env.param_env, TypingMode::analysis_in_body(cx.tcx, did))
109+
};
110+
110111
let (infcx, param_env) = cx.tcx.infer_ctxt().build_with_typing_env(typing_env);
111112
let ocx = ObligationCtxt::new(&infcx);
112113
let obligation = Obligation::new(

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,33 @@ pub struct InferCtxt<'tcx> {
319319
pub obligation_inspector: Cell<Option<ObligationInspector<'tcx>>>,
320320
}
321321

322+
impl<'tcx> Drop for InferCtxt<'tcx> {
323+
fn drop(&mut self) {
324+
let mut inner = self.inner.borrow_mut();
325+
let opaque_type_storage = &mut inner.opaque_type_storage;
326+
327+
// No need for the drop bomb when we're in TypingMode::Borrowck, and the InferCtxt doesn't consider regions.
328+
// This is okay since in `Borrowck`, the only reason we care about opaques is in relation to regions.
329+
// In some places *after* typeck, like in lints we use `TypingMode::Borrowck`
330+
// to prevent defining opaque types and we simply don't care about regions.
331+
match self.typing_mode() {
332+
TypingMode::Coherence
333+
| TypingMode::Analysis { .. }
334+
| TypingMode::PostBorrowckAnalysis { .. }
335+
| TypingMode::PostAnalysis => {}
336+
TypingMode::Borrowck { .. } => {
337+
if !self.considering_regions {
338+
return;
339+
}
340+
}
341+
}
342+
343+
if !opaque_type_storage.is_empty() {
344+
ty::tls::with(|tcx| tcx.dcx().delayed_bug(format!("{opaque_type_storage:?}")));
345+
}
346+
}
347+
}
348+
322349
/// See the `error_reporting` module for more details.
323350
#[derive(Clone, Copy, Debug, PartialEq, Eq, TypeFoldable, TypeVisitable)]
324351
pub enum ValuePairs<'tcx> {

compiler/rustc_infer/src/infer/opaque_types/table.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::ops::Deref;
33
use rustc_data_structures::fx::FxIndexMap;
44
use rustc_data_structures::undo_log::UndoLogs;
55
use rustc_middle::bug;
6-
use rustc_middle::ty::{self, OpaqueTypeKey, ProvisionalHiddenType, Ty};
6+
use rustc_middle::ty::{OpaqueTypeKey, ProvisionalHiddenType, Ty};
77
use tracing::instrument;
88

99
use crate::infer::snapshot::undo_log::{InferCtxtUndoLogs, UndoLog};
@@ -121,14 +121,6 @@ impl<'tcx> OpaqueTypeStorage<'tcx> {
121121
}
122122
}
123123

124-
impl<'tcx> Drop for OpaqueTypeStorage<'tcx> {
125-
fn drop(&mut self) {
126-
if !self.is_empty() {
127-
ty::tls::with(|tcx| tcx.dcx().delayed_bug(format!("{:?}", self.opaque_types)));
128-
}
129-
}
130-
}
131-
132124
pub struct OpaqueTypeTable<'a, 'tcx> {
133125
storage: &'a mut OpaqueTypeStorage<'tcx>,
134126

compiler/rustc_lint/src/context.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,9 +633,14 @@ impl<'tcx> LateContext<'tcx> {
633633
/// The typing mode of the currently visited node. Use this when
634634
/// building a new `InferCtxt`.
635635
pub fn typing_mode(&self) -> TypingMode<'tcx> {
636-
// FIXME(#132279): In case we're in a body, we should use a typing
637-
// mode which reveals the opaque types defined by that body.
638-
TypingMode::non_body_analysis()
636+
if let Some(body_id) = self.enclosing_body
637+
&& self.tcx.use_typing_mode_borrowck()
638+
{
639+
let def_id = self.tcx.hir_enclosing_body_owner(body_id.hir_id);
640+
TypingMode::borrowck(self.tcx, def_id)
641+
} else {
642+
TypingMode::non_body_analysis()
643+
}
639644
}
640645

641646
pub fn typing_env(&self) -> TypingEnv<'tcx> {

compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound {
8484
}
8585

8686
let def_id = opaque.def_id.to_def_id();
87-
let infcx = &cx.tcx.infer_ctxt().build(cx.typing_mode());
87+
let infcx = &cx.tcx.infer_ctxt().ignoring_regions().build(cx.typing_mode());
8888
// For every projection predicate in the opaque type's explicit bounds,
8989
// check that the type that we're assigning actually satisfies the bounds
9090
// of the associated type.

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -413,13 +413,36 @@ impl<'tcx> Body<'tcx> {
413413
}
414414

415415
pub fn typing_env(&self, tcx: TyCtxt<'tcx>) -> TypingEnv<'tcx> {
416-
match self.phase {
417-
// FIXME(#132279): we should reveal the opaques defined in the body during analysis.
418-
MirPhase::Built | MirPhase::Analysis(_) => TypingEnv::new(
419-
tcx.param_env(self.source.def_id()),
420-
ty::TypingMode::non_body_analysis(),
421-
),
422-
MirPhase::Runtime(_) => TypingEnv::post_analysis(tcx, self.source.def_id()),
416+
if tcx.use_typing_mode_borrowck() {
417+
match self.phase {
418+
MirPhase::Built if let Some(def_id) = self.source.def_id().as_local() => {
419+
TypingEnv::new(
420+
tcx.param_env(self.source.def_id()),
421+
ty::TypingMode::borrowck(tcx, def_id),
422+
)
423+
}
424+
MirPhase::Analysis(_) if let Some(def_id) = self.source.def_id().as_local() => {
425+
TypingEnv::new(
426+
tcx.param_env(self.source.def_id()),
427+
ty::TypingMode::post_borrowck_analysis(tcx, def_id),
428+
)
429+
}
430+
MirPhase::Built | MirPhase::Analysis(_) => {
431+
// This branch happens for drop glue and fn ptr shims.
432+
// FIXME: why do we do any of this analysis on drop glue etc?
433+
// This should ideally all be skipped.
434+
TypingEnv::post_analysis(tcx, self.source.def_id())
435+
}
436+
MirPhase::Runtime(_) => TypingEnv::post_analysis(tcx, self.source.def_id()),
437+
}
438+
} else {
439+
match self.phase {
440+
MirPhase::Built | MirPhase::Analysis(_) => TypingEnv::new(
441+
tcx.param_env(self.source.def_id()),
442+
ty::TypingMode::non_body_analysis(),
443+
),
444+
MirPhase::Runtime(_) => TypingEnv::post_analysis(tcx, self.source.def_id()),
445+
}
423446
}
424447
}
425448

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -528,12 +528,6 @@ pub trait HasTyCtxt<'tcx>: HasDataLayout {
528528

529529
pub trait HasTypingEnv<'tcx> {
530530
fn typing_env(&self) -> ty::TypingEnv<'tcx>;
531-
532-
/// FIXME(#132279): This method should not be used as in the future
533-
/// everything should take a `TypingEnv` instead. Remove it as that point.
534-
fn param_env(&self) -> ty::ParamEnv<'tcx> {
535-
self.typing_env().param_env
536-
}
537531
}
538532

539533
impl<'tcx> HasDataLayout for TyCtxt<'tcx> {

0 commit comments

Comments
 (0)