Skip to content

Commit

Permalink
Migrate from ParamEnv to TypingEnv
Browse files Browse the repository at this point in the history
  • Loading branch information
nbdd0121 committed Nov 20, 2024
1 parent e84e929 commit 839e6de
Show file tree
Hide file tree
Showing 8 changed files with 372 additions and 280 deletions.
19 changes: 8 additions & 11 deletions src/atomic_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use rustc_hir::def_id::LocalDefId;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::mir::mono::MonoItem;
use rustc_middle::ty::{GenericArgs, Instance, ParamEnv, TyCtxt};
use rustc_middle::ty::{GenericArgs, Instance, TyCtxt, TypingEnv};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::Span;

Expand Down Expand Up @@ -402,13 +402,10 @@ impl<'tcx> LateLintPass<'tcx> for AtomicContext<'tcx> {
.tcx
.erase_regions(GenericArgs::identity_for_item(self.cx.tcx, def_id));
let instance = Instance::new(def_id.into(), identity);
let param_and_instance = self
.cx
.param_env_reveal_all_normalized(def_id)
.and(instance);
let _ = self.cx.instance_adjustment(param_and_instance);
let _ = self.cx.instance_expectation(param_and_instance);
let _ = self.cx.instance_check(param_and_instance);
let poly_instance = TypingEnv::post_analysis(*self.cx, def_id).as_query_input(instance);
let _ = self.cx.instance_adjustment(poly_instance);
let _ = self.cx.instance_expectation(poly_instance);
let _ = self.cx.instance_check(poly_instance);
}

fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
Expand All @@ -420,11 +417,11 @@ impl<'tcx> LateLintPass<'tcx> for AtomicContext<'tcx> {

for mono_item in mono_items {
if let MonoItem::Fn(instance) = mono_item {
let param_and_instance = ParamEnv::reveal_all().and(instance);
if let Err(Error::TooGeneric) = self.cx.instance_adjustment(param_and_instance) {
let poly_instance = TypingEnv::fully_monomorphized().as_query_input(instance);
if let Err(Error::TooGeneric) = self.cx.instance_adjustment(poly_instance) {
bug!("monomorphized function should not be too generic");
}
if let Err(Error::TooGeneric) = self.cx.instance_expectation(param_and_instance) {
if let Err(Error::TooGeneric) = self.cx.instance_expectation(poly_instance) {
bug!("monomorphized function should not be too generic");
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/mir/drop_shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc_hir::def_id::DefId;
use rustc_index::{Idx, IndexVec};
use rustc_middle::mir::patch::MirPatch;
use rustc_middle::mir::*;
use rustc_middle::ty::{self, EarlyBinder, ParamEnv, Ty, TyCtxt};
use rustc_middle::ty::{self, EarlyBinder, Ty, TyCtxt, TypingEnv};
use rustc_mir_dataflow::elaborate_drops::{self, *};
use rustc_span::Span;
use rustc_target::abi::{FieldIdx, VariantIdx};
Expand All @@ -34,7 +34,7 @@ fn local_decls_for_sig<'tcx>(
pub fn build_drop_shim<'tcx>(
cx: &AnalysisCtxt<'tcx>,
def_id: DefId,
param_env: ParamEnv<'tcx>,
typing_env: TypingEnv<'tcx>,
ty: Ty<'tcx>,
) -> Body<'tcx> {
if let ty::Coroutine(gen_def_id, args) = ty.kind() {
Expand Down Expand Up @@ -83,7 +83,7 @@ pub fn build_drop_shim<'tcx>(
body: &body,
patch: MirPatch::new(&body),
tcx: cx.tcx,
param_env,
typing_env,
};
let dropee = cx.mk_place_deref(dropee_ptr);
let resume_block = elaborator.patch.resume_block();
Expand Down Expand Up @@ -137,7 +137,7 @@ pub struct DropShimElaborator<'a, 'tcx> {
pub body: &'a Body<'tcx>,
pub patch: MirPatch<'tcx>,
pub tcx: TyCtxt<'tcx>,
pub param_env: ty::ParamEnv<'tcx>,
pub typing_env: ty::TypingEnv<'tcx>,
}

impl fmt::Debug for DropShimElaborator<'_, '_> {
Expand All @@ -158,8 +158,8 @@ impl<'a, 'tcx> DropElaborator<'a, 'tcx> for DropShimElaborator<'a, 'tcx> {
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}
fn param_env(&self) -> ty::ParamEnv<'tcx> {
self.param_env
fn typing_env(&self) -> ty::TypingEnv<'tcx> {
self.typing_env
}

fn drop_style(&self, _path: Self::Path, mode: DropFlagMode) -> DropStyle {
Expand Down
53 changes: 32 additions & 21 deletions src/monomorphize_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use std::path::PathBuf;
// From rustc_monomorphize/lib.rs
fn custom_coerce_unsize_info<'tcx>(
tcx: TyCtxtAt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
typing_env: ty::TypingEnv<'tcx>,
source_ty: Ty<'tcx>,
target_ty: Ty<'tcx>,
) -> Result<CustomCoerceUnsized, ErrorGuaranteed> {
Expand All @@ -49,7 +49,7 @@ fn custom_coerce_unsize_info<'tcx>(
[source_ty, target_ty],
);

match tcx.codegen_select_candidate((param_env, trait_ref)) {
match tcx.codegen_select_candidate(typing_env.as_query_input(trait_ref)) {
Ok(traits::ImplSource::UserDefined(traits::ImplSourceUserDefinedData {
impl_def_id,
..
Expand Down Expand Up @@ -250,7 +250,7 @@ fn collect_items_rec<'tcx>(
// Sanity check whether this ended up being collected accidentally
debug_assert!(should_codegen_locally(tcx, &instance));

let ty = instance.ty(tcx, ty::ParamEnv::reveal_all());
let ty = instance.ty(tcx, ty::TypingEnv::fully_monomorphized());
visit_drop_use(tcx, ty, true, starting_item.span, &mut used_items);

recursion_depth_reset = None;
Expand Down Expand Up @@ -497,7 +497,7 @@ impl<'a, 'tcx> MirUsedCollector<'a, 'tcx> {
debug!("monomorphize: self.instance={:?}", self.instance);
self.instance.instantiate_mir_and_normalize_erasing_regions(
self.tcx,
ty::ParamEnv::reveal_all(),
ty::TypingEnv::fully_monomorphized(),
ty::EarlyBinder::bind(value),
)
}
Expand All @@ -524,7 +524,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
let source_ty = self.monomorphize(source_ty);
let (source_ty, target_ty) = find_vtable_types_for_unsizing(
self.tcx.at(span),
ty::ParamEnv::reveal_all(),
ty::TypingEnv::fully_monomorphized(),
source_ty,
target_ty,
);
Expand Down Expand Up @@ -589,8 +589,8 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
/// work, as some constants cannot be represented in the type system.
fn visit_const_operand(&mut self, constant: &mir::ConstOperand<'tcx>, location: Location) {
let const_ = self.monomorphize(constant.const_);
let param_env = ty::ParamEnv::reveal_all();
let val = match const_.eval(self.tcx, param_env, constant.span) {
let typing_env = ty::TypingEnv::fully_monomorphized();
let val = match const_.eval(self.tcx, typing_env, constant.span) {
Ok(v) => v,
Err(ErrorHandled::TooGeneric(..)) => span_bug!(
self.body.source_info(location).span,
Expand Down Expand Up @@ -704,9 +704,20 @@ fn visit_fn_use<'tcx>(
) {
if let ty::FnDef(def_id, args) = *ty.kind() {
let instance = if is_direct_call {
ty::Instance::expect_resolve(tcx, ty::ParamEnv::reveal_all(), def_id, args, source)
ty::Instance::expect_resolve(
tcx,
ty::TypingEnv::fully_monomorphized(),
def_id,
args,
source,
)
} else {
match ty::Instance::resolve_for_fn_ptr(tcx, ty::ParamEnv::reveal_all(), def_id, args) {
match ty::Instance::resolve_for_fn_ptr(
tcx,
ty::TypingEnv::fully_monomorphized(),
def_id,
args,
) {
Some(instance) => instance,
_ => bug!("failed to resolve instance for {ty}"),
}
Expand Down Expand Up @@ -839,16 +850,16 @@ pub fn should_codegen_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx>
/// smart pointers such as `Rc` and `Arc`.
pub fn find_vtable_types_for_unsizing<'tcx>(
tcx: TyCtxtAt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
typing_env: ty::TypingEnv<'tcx>,
source_ty: Ty<'tcx>,
target_ty: Ty<'tcx>,
) -> (Ty<'tcx>, Ty<'tcx>) {
let ptr_vtable = |inner_source: Ty<'tcx>, inner_target: Ty<'tcx>| {
let type_has_metadata = |ty: Ty<'tcx>| -> bool {
if ty.is_sized(tcx.tcx, param_env) {
if ty.is_sized(tcx.tcx, typing_env.param_env) {
return false;
}
let tail = tcx.struct_tail_for_codegen(ty, param_env);
let tail = tcx.struct_tail_for_codegen(ty, typing_env);
match tail.kind() {
ty::Foreign(..) => false,
ty::Str | ty::Slice(..) | ty::Dynamic(..) => true,
Expand All @@ -858,7 +869,7 @@ pub fn find_vtable_types_for_unsizing<'tcx>(
if type_has_metadata(inner_source) {
(inner_source, inner_target)
} else {
tcx.struct_lockstep_tails_for_codegen(inner_source, inner_target, param_env)
tcx.struct_lockstep_tails_for_codegen(inner_source, inner_target, typing_env)
}
};

Expand All @@ -879,7 +890,7 @@ pub fn find_vtable_types_for_unsizing<'tcx>(
assert_eq!(source_adt_def, target_adt_def);

let CustomCoerceUnsized::Struct(coerce_index) =
match custom_coerce_unsize_info(tcx, param_env, source_ty, target_ty) {
match custom_coerce_unsize_info(tcx, typing_env, source_ty, target_ty) {
Ok(ccu) => ccu,
Err(e) => {
let e = Ty::new_error(tcx.tcx, e);
Expand All @@ -897,7 +908,7 @@ pub fn find_vtable_types_for_unsizing<'tcx>(

find_vtable_types_for_unsizing(
tcx,
param_env,
typing_env,
source_fields[coerce_index].ty(*tcx, source_args),
target_fields[coerce_index].ty(*tcx, target_args),
)
Expand Down Expand Up @@ -980,7 +991,7 @@ impl<'v> RootCollector<'_, 'v> {

let item = self.tcx.hir().item(id);
let ty = Instance::new(item.owner_id.to_def_id(), GenericArgs::empty())
.ty(self.tcx, ty::ParamEnv::reveal_all());
.ty(self.tcx, ty::TypingEnv::fully_monomorphized());
visit_drop_use(self.tcx, ty, true, DUMMY_SP, self.output);
}
}
Expand Down Expand Up @@ -1078,13 +1089,13 @@ impl<'v> RootCollector<'_, 'v> {
// regions must appear in the argument
// listing.
let main_ret_ty = self.tcx.normalize_erasing_regions(
ty::ParamEnv::reveal_all(),
ty::TypingEnv::fully_monomorphized(),
main_ret_ty.no_bound_vars().unwrap(),
);

let start_instance = Instance::try_resolve(
self.tcx,
ty::ParamEnv::reveal_all(),
ty::TypingEnv::fully_monomorphized(),
start_def_id,
self.tcx.mk_args(&[main_ret_ty.into()]),
)
Expand Down Expand Up @@ -1124,8 +1135,8 @@ fn create_mono_items_for_default_impls<'tcx>(

let trait_ref = trait_ref.instantiate_identity();

let param_env = ty::ParamEnv::reveal_all();
let trait_ref = tcx.normalize_erasing_regions(param_env, trait_ref);
let typing_env = ty::TypingEnv::fully_monomorphized();
let trait_ref = tcx.normalize_erasing_regions(typing_env, trait_ref);
let overridden_methods = tcx.impl_item_implementor_ids(item.owner_id);

for method in tcx.provided_trait_methods(trait_ref.def_id) {
Expand All @@ -1146,7 +1157,7 @@ fn create_mono_items_for_default_impls<'tcx>(
trait_ref.args[param.index as usize]
}
});
let instance = ty::Instance::expect_resolve(tcx, param_env, method.def_id, args, DUMMY_SP);
let instance = ty::Instance::expect_resolve(tcx, typing_env, method.def_id, args, DUMMY_SP);

let mono_item = create_fn_mono_item(tcx, instance, DUMMY_SP);
if mono_item.node.is_instantiable(tcx) {
Expand Down
Loading

0 comments on commit 839e6de

Please sign in to comment.