Skip to content

Commit 3077661

Browse files
committed
Auto merge of #132460 - lcnr:questionable-uwu, r=compiler-errors
Use `TypingMode` throughout the compiler instead of `ParamEnv` Hopefully the biggest single PR as part of rust-lang/types-team#128. ## `infcx.typing_env` while defining opaque types I don't know how'll be able to correctly handle opaque types when using something taking a `TypingEnv` while defining opaque types. To correctly handle the opaques we need to be able to pass in the current `opaque_type_storage` and return constraints, i.e. we need to use a proper canonical query. We should migrate all the queries used during HIR typeck and borrowck where this matters to proper canonical queries. This is ## `layout_of` and `Reveal::All` We convert the `ParamEnv` to `Reveal::All` right at the start of the `layout_of` query, so I've changed callers of `layout_of` to already use a post analysis `TypingEnv` when encountering it. https://github.com/rust-lang/rust/blob/ca87b535a05097df6abbe2a031b057de2cefac5b/compiler/rustc_ty_utils/src/layout.rs#L51 ## `Ty::is_[unpin|sized|whatever]` I haven't migrated `fn is_item_raw` to use `TypingEnv`, will do so in a followup PR, this should significantly reduce the amount of `typing_env.param_env`. At some point there will probably be zero such uses as using the type system while ignoring the `typing_mode` is incorrect. ## `MirPhase` and phase-transitions When inside of a MIR-body, we can mostly use its `MirPhase` to figure out the right `typing_mode`. This does not work during phase transitions, most notably when transitioning from `Analysis` to `Runtime`: https://github.com/rust-lang/rust/blob/dae7ac133b9eda152784c075facb31a6688c92b1/compiler/rustc_mir_transform/src/lib.rs#L606-L625 All these passes still run with `MirPhase::Analysis`, but we should only use `Reveal::All` once we're run the `RevealAll` pass. This required me to manually construct the right `TypingEnv` in all these passes. Given that it feels somewhat easy to accidentally miss this going forward, I would maybe like to change `Body::phase` to an `Option` and replace it at the start of phase transitions. This then makes it clear that the MIR is currently in a weird state. r? `@ghost`
2 parents bc2e651 + bc01bf9 commit 3077661

File tree

5 files changed

+16
-10
lines changed

5 files changed

+16
-10
lines changed

src/borrow_tracker/stacked_borrows/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::{cmp, mem};
1212
use rustc_abi::{BackendRepr, Size};
1313
use rustc_data_structures::fx::FxHashSet;
1414
use rustc_middle::mir::{Mutability, RetagKind};
15-
use rustc_middle::ty::layout::HasParamEnv;
15+
use rustc_middle::ty::layout::HasTypingEnv;
1616
use rustc_middle::ty::{self, Ty};
1717

1818
use self::diagnostics::{RetagCause, RetagInfo};

src/borrow_tracker/tree_borrows/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_abi::{BackendRepr, Size};
22
use rustc_middle::mir::{Mutability, RetagKind};
3-
use rustc_middle::ty::layout::HasParamEnv;
3+
use rustc_middle::ty::layout::HasTypingEnv;
44
use rustc_middle::ty::{self, Ty};
55
use rustc_span::def_id::DefId;
66

src/eval.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,14 @@ pub fn create_ecx<'tcx>(
268268
entry_type: EntryFnType,
269269
config: &MiriConfig,
270270
) -> InterpResult<'tcx, InterpCx<'tcx, MiriMachine<'tcx>>> {
271-
let param_env = ty::ParamEnv::reveal_all();
272-
let layout_cx = LayoutCx::new(tcx, param_env);
273-
let mut ecx =
274-
InterpCx::new(tcx, rustc_span::DUMMY_SP, param_env, MiriMachine::new(config, layout_cx));
271+
let typing_env = ty::TypingEnv::fully_monomorphized();
272+
let layout_cx = LayoutCx::new(tcx, typing_env);
273+
let mut ecx = InterpCx::new(
274+
tcx,
275+
rustc_span::DUMMY_SP,
276+
typing_env.param_env,
277+
MiriMachine::new(config, layout_cx)
278+
);
275279

276280
// Some parts of initialization require a full `InterpCx`.
277281
MiriMachine::late_init(&mut ecx, config, {
@@ -376,7 +380,7 @@ pub fn create_ecx<'tcx>(
376380
let main_ret_ty = main_ret_ty.no_bound_vars().unwrap();
377381
let start_instance = ty::Instance::try_resolve(
378382
tcx,
379-
ty::ParamEnv::reveal_all(),
383+
typing_env,
380384
start_id,
381385
tcx.mk_args(&[ty::GenericArg::from(main_ret_ty)]),
382386
)

src/helpers.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ pub fn resolve_path<'tcx>(
116116
/// Gets the layout of a type at a path.
117117
#[track_caller]
118118
pub fn path_ty_layout<'tcx>(cx: &impl LayoutOf<'tcx>, path: &[&str]) -> TyAndLayout<'tcx> {
119-
let ty =
120-
resolve_path(cx.tcx(), path, Namespace::TypeNS).ty(cx.tcx(), ty::ParamEnv::reveal_all());
119+
let ty = resolve_path(cx.tcx(), path, Namespace::TypeNS)
120+
.ty(cx.tcx(), cx.typing_env());
121121
cx.layout_of(ty).to_result().ok().unwrap()
122122
}
123123

src/machine.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,9 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
11271127
};
11281128
let info = ecx.get_alloc_info(alloc_id);
11291129
let def_ty = ecx.tcx.type_of(def_id).instantiate_identity();
1130-
let extern_decl_layout = ecx.tcx.layout_of(ty::ParamEnv::empty().and(def_ty)).unwrap();
1130+
let extern_decl_layout = ecx.tcx.layout_of(
1131+
ecx.typing_env().as_query_input(def_ty)
1132+
).unwrap();
11311133
if extern_decl_layout.size != info.size || extern_decl_layout.align.abi != info.align {
11321134
throw_unsup_format!(
11331135
"extern static `{link_name}` has been declared as `{krate}::{name}` \

0 commit comments

Comments
 (0)