Skip to content

Commit b71fb5e

Browse files
committed
Auto merge of rust-lang#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 03ee484 + 2e087d2 commit b71fb5e

File tree

240 files changed

+1745
-1341
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

240 files changed

+1745
-1341
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
682682
// Normalize before comparing to see through type aliases and projections.
683683
let old_ty = ty::EarlyBinder::bind(ty).instantiate(tcx, generic_args);
684684
let new_ty = ty::EarlyBinder::bind(ty).instantiate(tcx, new_args);
685-
if let Ok(old_ty) = tcx.try_normalize_erasing_regions(self.param_env, old_ty)
686-
&& let Ok(new_ty) = tcx.try_normalize_erasing_regions(self.param_env, new_ty)
685+
if let Ok(old_ty) =
686+
tcx.try_normalize_erasing_regions(self.infcx.typing_env(self.param_env), old_ty)
687+
&& let Ok(new_ty) = tcx.try_normalize_erasing_regions(
688+
self.infcx.typing_env(self.param_env),
689+
new_ty,
690+
)
687691
{
688692
old_ty == new_ty
689693
} else {
@@ -703,7 +707,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
703707
// Test the callee's predicates, substituting in `ref_ty` for the moved argument type.
704708
clauses.instantiate(tcx, new_args).predicates.iter().all(|&(mut clause)| {
705709
// Normalize before testing to see through type aliases and projections.
706-
if let Ok(normalized) = tcx.try_normalize_erasing_regions(self.param_env, clause) {
710+
if let Ok(normalized) =
711+
tcx.try_normalize_erasing_regions(self.infcx.typing_env(self.param_env), clause)
712+
{
707713
clause = normalized;
708714
}
709715
self.infcx.predicate_must_hold_modulo_regions(&Obligation::new(
@@ -3831,11 +3837,16 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
38313837
if tcx.is_diagnostic_item(sym::deref_method, method_did) {
38323838
let deref_target =
38333839
tcx.get_diagnostic_item(sym::deref_target).and_then(|deref_target| {
3834-
Instance::try_resolve(tcx, self.param_env, deref_target, method_args)
3835-
.transpose()
3840+
Instance::try_resolve(
3841+
tcx,
3842+
self.infcx.typing_env(self.param_env),
3843+
deref_target,
3844+
method_args,
3845+
)
3846+
.transpose()
38363847
});
38373848
if let Some(Ok(instance)) = deref_target {
3838-
let deref_target_ty = instance.ty(tcx, self.param_env);
3849+
let deref_target_ty = instance.ty(tcx, self.infcx.typing_env(self.param_env));
38393850
err.note(format!("borrow occurs due to deref coercion to `{deref_target_ty}`"));
38403851
err.span_note(tcx.def_span(instance.def_id()), "deref defined here");
38413852
}

compiler/rustc_borrowck/src/diagnostics/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
864864

865865
let kind = call_kind(
866866
self.infcx.tcx,
867-
self.param_env,
867+
self.infcx.typing_env(self.param_env),
868868
method_did,
869869
method_args,
870870
*fn_span,

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
952952

953953
if let Ok(Some(instance)) = ty::Instance::try_resolve(
954954
tcx,
955-
self.param_env,
955+
self.infcx.typing_env(self.param_env),
956956
*fn_did,
957957
self.infcx.resolve_vars_if_possible(args),
958958
) {

compiler/rustc_borrowck/src/type_check/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1527,7 +1527,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
15271527
// The signature in this call can reference region variables,
15281528
// so erase them before calling a query.
15291529
let output_ty = self.tcx().erase_regions(sig.output());
1530-
if !output_ty.is_privately_uninhabited(self.tcx(), self.param_env) {
1530+
if !output_ty
1531+
.is_privately_uninhabited(self.tcx(), self.infcx.typing_env(self.param_env))
1532+
{
15311533
span_mirbug!(self, term, "call to converging function {:?} w/o dest", sig);
15321534
}
15331535
}

compiler/rustc_codegen_cranelift/src/abi/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
376376
let instance = if let ty::FnDef(def_id, fn_args) = *func.layout().ty.kind() {
377377
let instance = ty::Instance::expect_resolve(
378378
fx.tcx,
379-
ty::ParamEnv::reveal_all(),
379+
ty::TypingEnv::fully_monomorphized(),
380380
def_id,
381381
fn_args,
382382
source_info.span,

compiler/rustc_codegen_cranelift/src/base.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ fn codegen_stmt<'tcx>(
666666
let func_ref = fx.get_function_ref(
667667
Instance::resolve_for_fn_ptr(
668668
fx.tcx,
669-
ParamEnv::reveal_all(),
669+
ty::TypingEnv::fully_monomorphized(),
670670
def_id,
671671
args,
672672
)
@@ -841,14 +841,18 @@ fn codegen_stmt<'tcx>(
841841
lval.write_cvalue(fx, CValue::by_val(operand, box_layout));
842842
}
843843
Rvalue::NullaryOp(ref null_op, ty) => {
844-
assert!(lval.layout().ty.is_sized(fx.tcx, ParamEnv::reveal_all()));
844+
assert!(lval.layout().ty.is_sized(fx.tcx, ty::ParamEnv::reveal_all()));
845845
let layout = fx.layout_of(fx.monomorphize(ty));
846846
let val = match null_op {
847847
NullOp::SizeOf => layout.size.bytes(),
848848
NullOp::AlignOf => layout.align.abi.bytes(),
849849
NullOp::OffsetOf(fields) => fx
850850
.tcx
851-
.offset_of_subfield(ParamEnv::reveal_all(), layout, fields.iter())
851+
.offset_of_subfield(
852+
ty::TypingEnv::fully_monomorphized(),
853+
layout,
854+
fields.iter(),
855+
)
852856
.bytes(),
853857
NullOp::UbChecks => {
854858
let val = fx.tcx.sess.ub_checks();

compiler/rustc_codegen_cranelift/src/common.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ fn clif_pair_type_from_ty<'tcx>(
103103

104104
/// Is a pointer to this type a wide ptr?
105105
pub(crate) fn has_ptr_meta<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
106-
if ty.is_sized(tcx, ParamEnv::reveal_all()) {
106+
if ty.is_sized(tcx, ty::ParamEnv::reveal_all()) {
107107
return false;
108108
}
109109

110-
let tail = tcx.struct_tail_for_codegen(ty, ParamEnv::reveal_all());
110+
let tail = tcx.struct_tail_for_codegen(ty, ty::TypingEnv::fully_monomorphized());
111111
match tail.kind() {
112112
ty::Foreign(..) => false,
113113
ty::Str | ty::Slice(..) | ty::Dynamic(..) => true,
@@ -339,9 +339,9 @@ impl<'tcx> rustc_abi::HasDataLayout for FunctionCx<'_, '_, 'tcx> {
339339
}
340340
}
341341

342-
impl<'tcx> layout::HasParamEnv<'tcx> for FunctionCx<'_, '_, 'tcx> {
343-
fn param_env(&self) -> ParamEnv<'tcx> {
344-
ParamEnv::reveal_all()
342+
impl<'tcx> layout::HasTypingEnv<'tcx> for FunctionCx<'_, '_, 'tcx> {
343+
fn typing_env(&self) -> ty::TypingEnv<'tcx> {
344+
ty::TypingEnv::fully_monomorphized()
345345
}
346346
}
347347

@@ -358,7 +358,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
358358
{
359359
self.instance.instantiate_mir_and_normalize_erasing_regions(
360360
self.tcx,
361-
ty::ParamEnv::reveal_all(),
361+
ty::TypingEnv::fully_monomorphized(),
362362
ty::EarlyBinder::bind(value),
363363
)
364364
}
@@ -497,9 +497,9 @@ impl<'tcx> rustc_abi::HasDataLayout for RevealAllLayoutCx<'tcx> {
497497
}
498498
}
499499

500-
impl<'tcx> layout::HasParamEnv<'tcx> for RevealAllLayoutCx<'tcx> {
501-
fn param_env(&self) -> ParamEnv<'tcx> {
502-
ParamEnv::reveal_all()
500+
impl<'tcx> layout::HasTypingEnv<'tcx> for RevealAllLayoutCx<'tcx> {
501+
fn typing_env(&self) -> ty::TypingEnv<'tcx> {
502+
ty::TypingEnv::fully_monomorphized()
503503
}
504504
}
505505

compiler/rustc_codegen_cranelift/src/constant.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub(crate) fn eval_mir_constant<'tcx>(
7878
let cv = fx.monomorphize(constant.const_);
7979
// This cannot fail because we checked all required_consts in advance.
8080
let val = cv
81-
.eval(fx.tcx, ty::ParamEnv::reveal_all(), constant.span)
81+
.eval(fx.tcx, ty::TypingEnv::fully_monomorphized(), constant.span)
8282
.expect("erroneous constant missed by mono item collection");
8383
(val, cv.ty())
8484
}
@@ -265,8 +265,13 @@ fn data_id_for_static(
265265
assert!(!definition);
266266
assert!(!tcx.is_mutable_static(def_id));
267267

268-
let ty = instance.ty(tcx, ParamEnv::reveal_all());
269-
let align = tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap().align.pref.bytes();
268+
let ty = instance.ty(tcx, ty::TypingEnv::fully_monomorphized());
269+
let align = tcx
270+
.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(ty))
271+
.unwrap()
272+
.align
273+
.pref
274+
.bytes();
270275

271276
let linkage = if import_linkage == rustc_middle::mir::mono::Linkage::ExternalWeak
272277
|| import_linkage == rustc_middle::mir::mono::Linkage::WeakAny

compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ impl DebugContext {
210210

211211
type_names::push_generic_params(
212212
tcx,
213-
tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), args),
213+
tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), args),
214214
&mut name,
215215
);
216216

@@ -275,8 +275,10 @@ impl DebugContext {
275275
let span = tcx.def_span(def_id);
276276
let (file_id, line, _column) = self.get_span_loc(tcx, span, span);
277277

278-
let static_type = Instance::mono(tcx, def_id).ty(tcx, ty::ParamEnv::reveal_all());
279-
let static_layout = tcx.layout_of(ty::ParamEnv::reveal_all().and(static_type)).unwrap();
278+
let static_type = Instance::mono(tcx, def_id).ty(tcx, ty::TypingEnv::fully_monomorphized());
279+
let static_layout = tcx
280+
.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(static_type))
281+
.unwrap();
280282
// FIXME use the actual type layout
281283
let type_id = self.debug_type(tcx, type_dbg, static_type);
282284

compiler/rustc_codegen_cranelift/src/inline_asm.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub(crate) fn codegen_inline_asm_terminator<'tcx>(
9292
if let ty::FnDef(def_id, args) = *const_.ty().kind() {
9393
let instance = ty::Instance::resolve_for_fn_ptr(
9494
fx.tcx,
95-
ty::ParamEnv::reveal_all(),
95+
ty::TypingEnv::fully_monomorphized(),
9696
def_id,
9797
args,
9898
)
@@ -227,11 +227,11 @@ pub(crate) fn codegen_naked_asm<'tcx>(
227227
InlineAsmOperand::Const { ref value } => {
228228
let cv = instance.instantiate_mir_and_normalize_erasing_regions(
229229
tcx,
230-
ty::ParamEnv::reveal_all(),
230+
ty::TypingEnv::fully_monomorphized(),
231231
ty::EarlyBinder::bind(value.const_),
232232
);
233233
let const_value = cv
234-
.eval(tcx, ty::ParamEnv::reveal_all(), value.span)
234+
.eval(tcx, ty::TypingEnv::fully_monomorphized(), value.span)
235235
.expect("erroneous constant missed by mono item collection");
236236

237237
let value = rustc_codegen_ssa::common::asm_const_to_str(
@@ -250,13 +250,13 @@ pub(crate) fn codegen_naked_asm<'tcx>(
250250

251251
let const_ = instance.instantiate_mir_and_normalize_erasing_regions(
252252
tcx,
253-
ty::ParamEnv::reveal_all(),
253+
ty::TypingEnv::fully_monomorphized(),
254254
ty::EarlyBinder::bind(value.const_),
255255
);
256256
if let ty::FnDef(def_id, args) = *const_.ty().kind() {
257257
let instance = ty::Instance::resolve_for_fn_ptr(
258258
tcx,
259-
ty::ParamEnv::reveal_all(),
259+
ty::TypingEnv::fully_monomorphized(),
260260
def_id,
261261
args,
262262
)

compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mod simd;
2020
use cranelift_codegen::ir::AtomicRmwOp;
2121
use rustc_middle::ty;
2222
use rustc_middle::ty::GenericArgsRef;
23-
use rustc_middle::ty::layout::{HasParamEnv, ValidityRequirement};
23+
use rustc_middle::ty::layout::ValidityRequirement;
2424
use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths};
2525
use rustc_span::source_map::Spanned;
2626
use rustc_span::symbol::{Symbol, sym};
@@ -682,7 +682,10 @@ fn codegen_regular_intrinsic_call<'tcx>(
682682
if let Some(requirement) = requirement {
683683
let do_panic = !fx
684684
.tcx
685-
.check_validity_requirement((requirement, fx.param_env().and(ty)))
685+
.check_validity_requirement((
686+
requirement,
687+
ty::TypingEnv::fully_monomorphized().as_query_input(ty),
688+
))
686689
.expect("expect to have layout during codegen");
687690

688691
if do_panic {
@@ -741,7 +744,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
741744

742745
let const_val = fx
743746
.tcx
744-
.const_eval_instance(ParamEnv::reveal_all(), instance, source_info.span)
747+
.const_eval_instance(ty::ParamEnv::reveal_all(), instance, source_info.span)
745748
.unwrap();
746749
let val = crate::constant::codegen_const_value(fx, const_val, ret.layout().ty);
747750
ret.write_cvalue(fx, val);

compiler/rustc_codegen_cranelift/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ mod prelude {
9898
pub(crate) use rustc_middle::mir::{self, *};
9999
pub(crate) use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
100100
pub(crate) use rustc_middle::ty::{
101-
self, FloatTy, Instance, InstanceKind, IntTy, ParamEnv, Ty, TyCtxt, UintTy,
101+
self, FloatTy, Instance, InstanceKind, IntTy, Ty, TyCtxt, UintTy,
102102
};
103103
pub(crate) use rustc_span::Span;
104104

compiler/rustc_codegen_cranelift/src/main_shim.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub(crate) fn maybe_create_entry_wrapper(
4949
// regions must appear in the argument
5050
// listing.
5151
let main_ret_ty = tcx.normalize_erasing_regions(
52-
ty::ParamEnv::reveal_all(),
52+
ty::TypingEnv::fully_monomorphized(),
5353
main_ret_ty.no_bound_vars().unwrap(),
5454
);
5555

@@ -113,7 +113,7 @@ pub(crate) fn maybe_create_entry_wrapper(
113113
.unwrap();
114114
let report = Instance::expect_resolve(
115115
tcx,
116-
ParamEnv::reveal_all(),
116+
ty::TypingEnv::fully_monomorphized(),
117117
report.def_id,
118118
tcx.mk_args(&[GenericArg::from(main_ret_ty)]),
119119
DUMMY_SP,
@@ -139,7 +139,7 @@ pub(crate) fn maybe_create_entry_wrapper(
139139
let start_def_id = tcx.require_lang_item(LangItem::Start, None);
140140
let start_instance = Instance::expect_resolve(
141141
tcx,
142-
ParamEnv::reveal_all(),
142+
ty::TypingEnv::fully_monomorphized(),
143143
start_def_id,
144144
tcx.mk_args(&[main_ret_ty.into()]),
145145
DUMMY_SP,

compiler/rustc_codegen_cranelift/src/unsize.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! [`PointerCoercion::Unsize`]: `rustc_middle::ty::adjustment::PointerCoercion::Unsize`
44
55
use rustc_codegen_ssa::base::validate_trivial_unsize;
6+
use rustc_middle::ty::layout::HasTypingEnv;
67
use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths};
78

89
use crate::base::codegen_panic_nounwind;
@@ -23,7 +24,7 @@ pub(crate) fn unsized_info<'tcx>(
2324
old_info: Option<Value>,
2425
) -> Value {
2526
let (source, target) =
26-
fx.tcx.struct_lockstep_tails_for_codegen(source, target, ParamEnv::reveal_all());
27+
fx.tcx.struct_lockstep_tails_for_codegen(source, target, fx.typing_env());
2728
match (&source.kind(), &target.kind()) {
2829
(&ty::Array(_, len), &ty::Slice(_)) => fx.bcx.ins().iconst(
2930
fx.pointer_type,

compiler/rustc_codegen_cranelift/src/value_and_place.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use cranelift_codegen::entity::EntityRef;
44
use cranelift_codegen::ir::immediates::Offset32;
55
use cranelift_frontend::Variable;
66
use rustc_middle::ty::FnSig;
7+
use rustc_middle::ty::layout::HasTypingEnv;
78

89
use crate::prelude::*;
910

@@ -884,19 +885,17 @@ pub(crate) fn assert_assignable<'tcx>(
884885
assert_assignable(fx, *a, *b, limit - 1);
885886
}
886887
(ty::FnPtr(..), ty::FnPtr(..)) => {
887-
let from_sig = fx.tcx.normalize_erasing_late_bound_regions(
888-
ParamEnv::reveal_all(),
889-
from_ty.fn_sig(fx.tcx),
890-
);
888+
let from_sig = fx
889+
.tcx
890+
.normalize_erasing_late_bound_regions(fx.typing_env(), from_ty.fn_sig(fx.tcx));
891891
let FnSig {
892892
inputs_and_output: types_from,
893893
c_variadic: c_variadic_from,
894894
safety: unsafety_from,
895895
abi: abi_from,
896896
} = from_sig;
897-
let to_sig = fx
898-
.tcx
899-
.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), to_ty.fn_sig(fx.tcx));
897+
let to_sig =
898+
fx.tcx.normalize_erasing_late_bound_regions(fx.typing_env(), to_ty.fn_sig(fx.tcx));
900899
let FnSig {
901900
inputs_and_output: types_to,
902901
c_variadic: c_variadic_to,
@@ -932,9 +931,8 @@ pub(crate) fn assert_assignable<'tcx>(
932931
(&ty::Dynamic(from_traits, _, _from_kind), &ty::Dynamic(to_traits, _, _to_kind)) => {
933932
// FIXME(dyn-star): Do the right thing with DynKinds
934933
for (from, to) in from_traits.iter().zip(to_traits) {
935-
let from =
936-
fx.tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), from);
937-
let to = fx.tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), to);
934+
let from = fx.tcx.normalize_erasing_late_bound_regions(fx.typing_env(), from);
935+
let to = fx.tcx.normalize_erasing_late_bound_regions(fx.typing_env(), to);
938936
assert_eq!(
939937
from, to,
940938
"Can't write trait object of incompatible traits {:?} to place with traits {:?}\n\n{:#?}",

0 commit comments

Comments
 (0)