From b6d09f95178ea7dc1d02e898cfd67cfa563bff79 Mon Sep 17 00:00:00 2001 From: lcnr Date: Sun, 4 Oct 2026 11:42:25 +0200 Subject: [PATCH] replace`fully_monomorphized` with `cx.typing_env()` --- .../rustc_codegen_cranelift/src/abi/mod.rs | 4 +- compiler/rustc_codegen_cranelift/src/base.rs | 5 +-- .../rustc_codegen_cranelift/src/common.rs | 6 +-- .../rustc_codegen_cranelift/src/constant.rs | 3 +- .../rustc_codegen_cranelift/src/inline_asm.rs | 4 +- .../src/intrinsics/mod.rs | 7 +--- compiler/rustc_codegen_cranelift/src/num.rs | 4 +- .../src/value_and_place.rs | 6 +-- compiler/rustc_codegen_gcc/src/consts.rs | 6 +-- compiler/rustc_codegen_gcc/src/int.rs | 8 ++-- .../rustc_codegen_gcc/src/intrinsic/simd.rs | 6 +-- .../src/builder/autodiff.rs | 12 ++---- compiler/rustc_codegen_llvm/src/intrinsic.rs | 4 +- compiler/rustc_codegen_ssa/src/base.rs | 2 +- compiler/rustc_codegen_ssa/src/mir/mod.rs | 2 +- .../src/builder/expr/as_rvalue.rs | 4 +- compiler/rustc_mir_build/src/builder/misc.rs | 4 +- compiler/rustc_public_bridge/src/alloc.rs | 4 +- .../src/context/helpers.rs | 5 --- .../rustc_public_bridge/src/context/impls.rs | 41 +++++-------------- 20 files changed, 52 insertions(+), 85 deletions(-) diff --git a/compiler/rustc_codegen_cranelift/src/abi/mod.rs b/compiler/rustc_codegen_cranelift/src/abi/mod.rs index 7f46b19f7568f..e2446d94993a7 100644 --- a/compiler/rustc_codegen_cranelift/src/abi/mod.rs +++ b/compiler/rustc_codegen_cranelift/src/abi/mod.rs @@ -16,7 +16,7 @@ use rustc_abi::{CanonAbi, ExternAbi, X86Call}; use rustc_codegen_ssa::base::is_call_from_compiler_builtins_to_upstream_monomorphization; use rustc_codegen_ssa::diagnostics::CompilerBuiltinsCannotCall; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; -use rustc_middle::ty::layout::FnAbiOf; +use rustc_middle::ty::layout::{FnAbiOf, HasTypingEnv as _}; use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_middle::ty::{ShimKind, TypeVisitableExt}; use rustc_session::Session; @@ -441,7 +441,7 @@ pub(crate) fn codegen_terminator_call<'tcx>( let instance = if let ty::FnDef(def_id, fn_args) = *func.layout().ty.kind() { let instance = ty::Instance::expect_resolve( fx.tcx, - ty::TypingEnv::fully_monomorphized(), + fx.typing_env(), def_id, fn_args.no_bound_vars().unwrap(), source_info.span, diff --git a/compiler/rustc_codegen_cranelift/src/base.rs b/compiler/rustc_codegen_cranelift/src/base.rs index e752189130290..a1810a8d494f0 100644 --- a/compiler/rustc_codegen_cranelift/src/base.rs +++ b/compiler/rustc_codegen_cranelift/src/base.rs @@ -720,7 +720,7 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt: let func_ref = fx.get_function_ref( Instance::resolve_for_fn_ptr( fx.tcx, - ty::TypingEnv::fully_monomorphized(), + fx.typing_env(), def_id, args.no_bound_vars().unwrap(), ) @@ -771,8 +771,7 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt: fn is_wide_ptr<'tcx>(fx: &FunctionCx<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool { ty.builtin_deref(true).is_some_and(|pointee_ty| { - fx.tcx - .type_has_metadata(pointee_ty, ty::TypingEnv::fully_monomorphized()) + fx.tcx.type_has_metadata(pointee_ty, fx.typing_env()) }) } diff --git a/compiler/rustc_codegen_cranelift/src/common.rs b/compiler/rustc_codegen_cranelift/src/common.rs index 30bd2f28af53f..867843a193718 100644 --- a/compiler/rustc_codegen_cranelift/src/common.rs +++ b/compiler/rustc_codegen_cranelift/src/common.rs @@ -4,8 +4,8 @@ use rustc_abi::{Float, Integer, Primitive}; use rustc_index::IndexVec; use rustc_middle::ty::TypeFoldable; use rustc_middle::ty::layout::{ - self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers, - codegen_handle_fn_abi_err, + self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasTypingEnv as _, LayoutError, + LayoutOfHelpers, codegen_handle_fn_abi_err, }; use rustc_span::Symbol; use rustc_target::callconv::FnAbi; @@ -350,7 +350,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> { { self.instance.instantiate_mir_and_normalize_erasing_regions( self.tcx, - ty::TypingEnv::fully_monomorphized(), + self.typing_env(), ty::EarlyBinder::bind(self.tcx, value), ) } diff --git a/compiler/rustc_codegen_cranelift/src/constant.rs b/compiler/rustc_codegen_cranelift/src/constant.rs index f1e4fd818ebeb..5fc02a19c0094 100644 --- a/compiler/rustc_codegen_cranelift/src/constant.rs +++ b/compiler/rustc_codegen_cranelift/src/constant.rs @@ -9,6 +9,7 @@ use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; use rustc_middle::mir::interpret::{ AllocId, GlobalAlloc, PointerArithmetic, Scalar, read_target_uint, }; +use rustc_middle::ty::layout::HasTypingEnv as _; use rustc_middle::ty::{ExistentialTraitRef, ScalarInt}; use crate::prelude::*; @@ -81,7 +82,7 @@ pub(crate) fn eval_mir_constant<'tcx>( let cv = fx.monomorphize(constant.const_); // This cannot fail because we checked all required_consts in advance. let val = cv - .eval(fx.tcx, ty::TypingEnv::fully_monomorphized(), constant.span) + .eval(fx.tcx, fx.typing_env(), constant.span) .expect("erroneous constant missed by mono item collection"); (val, cv.ty()) } diff --git a/compiler/rustc_codegen_cranelift/src/inline_asm.rs b/compiler/rustc_codegen_cranelift/src/inline_asm.rs index b2bf856e7bcee..c13af35f94108 100644 --- a/compiler/rustc_codegen_cranelift/src/inline_asm.rs +++ b/compiler/rustc_codegen_cranelift/src/inline_asm.rs @@ -7,7 +7,7 @@ use rustc_abi::CanonAbi; use rustc_ast::ast::{InlineAsmOptions, InlineAsmTemplatePiece}; use rustc_attr_ir::lang_items::LangItem; use rustc_middle::mir::interpret::{GlobalAlloc, PointerArithmetic, Scalar as ConstScalar}; -use rustc_middle::ty::layout::FnAbiOf; +use rustc_middle::ty::layout::{FnAbiOf, HasTypingEnv as _}; use rustc_span::sym; use rustc_target::asm::*; use rustc_target::spec::Arch; @@ -175,7 +175,7 @@ pub(crate) fn codegen_inline_asm_terminator<'tcx>( if let ty::FnDef(def_id, args) = *const_.ty().kind() { let instance = ty::Instance::resolve_for_fn_ptr( fx.tcx, - ty::TypingEnv::fully_monomorphized(), + fx.typing_env(), def_id, args.no_bound_vars().unwrap(), ) diff --git a/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs b/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs index 9c3182c944dc6..1437ea7445d24 100644 --- a/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs +++ b/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs @@ -22,7 +22,7 @@ use cranelift_codegen::ir::{ }; use rustc_middle::ty; use rustc_middle::ty::GenericArgsRef; -use rustc_middle::ty::layout::ValidityRequirement; +use rustc_middle::ty::layout::{HasTypingEnv as _, ValidityRequirement}; use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths}; use rustc_span::{Spanned, Symbol, sym}; use rustc_target::spec::PanicStrategy; @@ -729,10 +729,7 @@ fn codegen_regular_intrinsic_call<'tcx>( if let Some(requirement) = requirement { let do_panic = !fx .tcx - .check_validity_requirement(( - requirement, - ty::TypingEnv::fully_monomorphized().as_query_input(ty), - )) + .check_validity_requirement((requirement, fx.typing_env().as_query_input(ty))) .expect("expect to have layout during codegen"); if do_panic { diff --git a/compiler/rustc_codegen_cranelift/src/num.rs b/compiler/rustc_codegen_cranelift/src/num.rs index f1c44df1f6886..317e61031b34c 100644 --- a/compiler/rustc_codegen_cranelift/src/num.rs +++ b/compiler/rustc_codegen_cranelift/src/num.rs @@ -1,5 +1,7 @@ //! Various operations on integer and floating-point numbers +use rustc_middle::ty::layout::HasTypingEnv as _; + use crate::codegen_f16_f128; use crate::prelude::*; @@ -437,7 +439,7 @@ fn codegen_ptr_binop<'tcx>( .layout() .ty .builtin_deref(true) - .map(|ty| !fx.tcx.type_has_metadata(ty, ty::TypingEnv::fully_monomorphized())) + .map(|ty| !fx.tcx.type_has_metadata(ty, fx.typing_env())) .unwrap_or(true); if is_thin_ptr { diff --git a/compiler/rustc_codegen_cranelift/src/value_and_place.rs b/compiler/rustc_codegen_cranelift/src/value_and_place.rs index 440ae9c4b812e..a4af73cbc3f20 100644 --- a/compiler/rustc_codegen_cranelift/src/value_and_place.rs +++ b/compiler/rustc_codegen_cranelift/src/value_and_place.rs @@ -693,7 +693,7 @@ impl<'tcx> CPlace<'tcx> { }; let (field_ptr, field_layout) = codegen_field(fx, base, extra, layout, field); - if fx.tcx.type_has_metadata(field_layout.ty, ty::TypingEnv::fully_monomorphized()) { + if fx.tcx.type_has_metadata(field_layout.ty, fx.typing_env()) { CPlace::for_ptr_with_extra(field_ptr, extra.unwrap(), field_layout) } else { CPlace::for_ptr(field_ptr, field_layout) @@ -808,7 +808,7 @@ impl<'tcx> CPlace<'tcx> { pub(crate) fn place_deref(self, fx: &mut FunctionCx<'_, '_, 'tcx>) -> CPlace<'tcx> { let inner_layout = fx.layout_of(self.layout().ty.builtin_deref(true).unwrap()); - if fx.tcx.type_has_metadata(inner_layout.ty, ty::TypingEnv::fully_monomorphized()) { + if fx.tcx.type_has_metadata(inner_layout.ty, fx.typing_env()) { let (addr, extra) = self.to_cvalue(fx).load_scalar_pair(fx); CPlace::for_ptr_with_extra(Pointer::new(addr), extra, inner_layout) } else { @@ -821,7 +821,7 @@ impl<'tcx> CPlace<'tcx> { fx: &mut FunctionCx<'_, '_, 'tcx>, layout: TyAndLayout<'tcx>, ) -> CValue<'tcx> { - if fx.tcx.type_has_metadata(self.layout().ty, ty::TypingEnv::fully_monomorphized()) { + if fx.tcx.type_has_metadata(self.layout().ty, fx.typing_env()) { let (ptr, extra) = self.to_ptr_unsized(); CValue::by_val_pair(ptr.get_addr(fx), extra, layout) } else { diff --git a/compiler/rustc_codegen_gcc/src/consts.rs b/compiler/rustc_codegen_gcc/src/consts.rs index 0784064547e0f..c34367de422d4 100644 --- a/compiler/rustc_codegen_gcc/src/consts.rs +++ b/compiler/rustc_codegen_gcc/src/consts.rs @@ -16,8 +16,8 @@ use rustc_middle::mir::interpret::{ self, ConstAllocation, CtfeProvenance, ErrorHandled, Scalar as InterpScalar, read_target_uint, }; use rustc_middle::mono::MonoItem; -use rustc_middle::ty::layout::LayoutOf; -use rustc_middle::ty::{self, Instance}; +use rustc_middle::ty::Instance; +use rustc_middle::ty::layout::{HasTypingEnv as _, LayoutOf}; use rustc_span::def_id::DefId; use rustc_span::{bug, span_bug}; @@ -251,7 +251,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> { let gcc_type = if nested { self.type_i8() } else { - let ty = instance.ty(self.tcx, ty::TypingEnv::fully_monomorphized()); + let ty = instance.ty(self.tcx, self.typing_env()); self.layout_of(ty).gcc_type(self) }; diff --git a/compiler/rustc_codegen_gcc/src/int.rs b/compiler/rustc_codegen_gcc/src/int.rs index de8762d1c06b4..eaf44c35ded01 100644 --- a/compiler/rustc_codegen_gcc/src/int.rs +++ b/compiler/rustc_codegen_gcc/src/int.rs @@ -10,7 +10,8 @@ use gccjit::{ use rustc_abi::{CanonAbi, Endian, ExternAbi}; use rustc_codegen_ssa::common::{IntPredicate, TypeKind}; use rustc_codegen_ssa::traits::{BackendTypes, BaseTypeCodegenMethods, BuilderMethods, OverflowOp}; -use rustc_middle::ty::{self, Ty}; +use rustc_middle::ty::Ty; +use rustc_middle::ty::layout::HasTypingEnv as _; use rustc_target::callconv::{ArgAbi, ArgAttributes, FnAbi, PassMode}; use rustc_type_ir::{Interner, TyKind}; @@ -387,10 +388,7 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> { 128 => self.tcx.types.i128, _ => unreachable!("unexpected integer size"), }; - let layout = self - .tcx - .layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(res_ty)) - .unwrap(); + let layout = self.tcx.layout_of(self.cx.typing_env().as_query_input(res_ty)).unwrap(); let arg_abi = ArgAbi { layout, mode: PassMode::Direct(ArgAttributes::new()) }; let mut fn_abi = FnAbi { diff --git a/compiler/rustc_codegen_gcc/src/intrinsic/simd.rs b/compiler/rustc_codegen_gcc/src/intrinsic/simd.rs index 54013b6be6173..959ad826359f9 100644 --- a/compiler/rustc_codegen_gcc/src/intrinsic/simd.rs +++ b/compiler/rustc_codegen_gcc/src/intrinsic/simd.rs @@ -16,7 +16,7 @@ use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods, BuilderMethods, LayoutTy use rustc_hir as hir; use rustc_middle::mir::BinOp; use rustc_middle::ty::consts::ConstExt; -use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf}; +use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv as _, LayoutOf}; use rustc_middle::ty::{self, Ty}; use rustc_span::{ErrorGuaranteed, Span, Symbol, span_bug, sym}; @@ -540,7 +540,7 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>( match *in_elem.kind() { ty::RawPtr(p_ty, _) => { let metadata = p_ty.ptr_metadata_ty(bx.tcx, |ty| { - bx.tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), ty) + bx.tcx.normalize_erasing_regions(bx.typing_env(), ty) }); require!( metadata.is_unit(), @@ -554,7 +554,7 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>( match *out_elem.kind() { ty::RawPtr(p_ty, _) => { let metadata = p_ty.ptr_metadata_ty(bx.tcx, |ty| { - bx.tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), ty) + bx.tcx.normalize_erasing_regions(bx.typing_env(), ty) }); require!( metadata.is_unit(), diff --git a/compiler/rustc_codegen_llvm/src/builder/autodiff.rs b/compiler/rustc_codegen_llvm/src/builder/autodiff.rs index 03058ba02d9dc..3b1e36265b941 100644 --- a/compiler/rustc_codegen_llvm/src/builder/autodiff.rs +++ b/compiler/rustc_codegen_llvm/src/builder/autodiff.rs @@ -10,7 +10,7 @@ use rustc_codegen_ssa::mir::place::PlaceValue; use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods, BuilderMethods, ReturnSlot}; use rustc_data_structures::thin_vec::ThinVec; use rustc_middle::ty; -use rustc_middle::ty::{PseudoCanonicalInput, Ty, TyCtxt, TypingEnv}; +use rustc_middle::ty::{Ty, TyCtxt, TypingEnv}; use rustc_span::bug; use rustc_target::callconv::PassMode; use tracing::debug; @@ -50,12 +50,8 @@ pub(crate) fn adjust_activity_to_abi<'tcx>( if let ty::Slice(element_ty) = tail_ty.kind() { // Now we need to figure out the size of each slice element in memory to allow // safety checks and usability improvements in the backend. - let pci = PseudoCanonicalInput { - typing_env: TypingEnv::fully_monomorphized(), - value: *element_ty, - }; - let layout = tcx.layout_of(pci); + let layout = tcx.layout_of(typing_env.as_query_input(*element_ty)); let elem_size = match layout { Ok(layout) => layout.size, Err(_) => { @@ -91,9 +87,7 @@ pub(crate) fn adjust_activity_to_abi<'tcx>( } } - let pci = PseudoCanonicalInput { typing_env: TypingEnv::fully_monomorphized(), value: *ty }; - - let layout = match tcx.layout_of(pci) { + let layout = match tcx.layout_of(typing_env.as_query_input(*ty)) { Ok(layout) => layout.layout, Err(_) => { bug!("failed to compute layout for type {:?}", ty); diff --git a/compiler/rustc_codegen_llvm/src/intrinsic.rs b/compiler/rustc_codegen_llvm/src/intrinsic.rs index da0460b2a9290..542d5fd7a088d 100644 --- a/compiler/rustc_codegen_llvm/src/intrinsic.rs +++ b/compiler/rustc_codegen_llvm/src/intrinsic.rs @@ -22,7 +22,7 @@ use rustc_middle::mir::BinOp; use rustc_middle::ty::consts::ConstExt; use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, HasTypingEnv, LayoutOf}; use rustc_middle::ty::offload_meta::OffloadMetadata; -use rustc_middle::ty::{self, GenericArgsRef, Instance, SimdAlign, Ty, TyCtxt, TypingEnv}; +use rustc_middle::ty::{self, GenericArgsRef, Instance, SimdAlign, Ty, TyCtxt}; use rustc_session::diagnostics::feature_err; use rustc_span::{ErrorGuaranteed, Span, Symbol, bug, span_bug, sym}; use rustc_structures::CrateType; @@ -1881,7 +1881,7 @@ fn codegen_autodiff<'ll, 'tcx>( adjust_activity_to_abi( tcx, source_fn_ptr_ty, - TypingEnv::fully_monomorphized(), + bx.cx.typing_env(), &mut diff_attrs.input_activity, ); diff --git a/compiler/rustc_codegen_ssa/src/base.rs b/compiler/rustc_codegen_ssa/src/base.rs index f9dfd04faf2ac..e7bdb30c370dc 100644 --- a/compiler/rustc_codegen_ssa/src/base.rs +++ b/compiler/rustc_codegen_ssa/src/base.rs @@ -451,7 +451,7 @@ where let instance = match ty.kind() { &ty::FnDef(def_id, args) => Instance::expect_resolve( cx.tcx(), - ty::TypingEnv::fully_monomorphized(), + cx.typing_env(), def_id, args.no_bound_vars().unwrap(), expr.span, diff --git a/compiler/rustc_codegen_ssa/src/mir/mod.rs b/compiler/rustc_codegen_ssa/src/mir/mod.rs index aefa8356536dc..969de7a2d2172 100644 --- a/compiler/rustc_codegen_ssa/src/mir/mod.rs +++ b/compiler/rustc_codegen_ssa/src/mir/mod.rs @@ -222,7 +222,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( if tcx.features().ergonomic_clones() { let monomorphized_mir = instance.instantiate_mir_and_normalize_erasing_regions( tcx, - ty::TypingEnv::fully_monomorphized(), + cx.typing_env(), ty::EarlyBinder::bind(tcx, mir.clone()), ); mir = tcx.arena.alloc(optimize_use_clone::(cx, monomorphized_mir)); diff --git a/compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs b/compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs index 09c76d6beb4a1..18dea6dafd2dd 100644 --- a/compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs +++ b/compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs @@ -736,7 +736,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // Helper to get a `-1` value of the appropriate type fn neg_1_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> { - let typing_env = ty::TypingEnv::fully_monomorphized(); + let typing_env = self.typing_env(); let size = self.tcx.layout_of(typing_env.as_query_input(ty)).unwrap().size; let literal = Const::from_bits(self.tcx, size.unsigned_int_max(), typing_env, ty); @@ -746,7 +746,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // Helper to get the minimum value of the appropriate type fn minval_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> { assert!(ty.is_signed()); - let typing_env = ty::TypingEnv::fully_monomorphized(); + let typing_env = self.typing_env(); let bits = self.tcx.layout_of(typing_env.as_query_input(ty)).unwrap().size.bits(); let n = 1 << (bits - 1); let literal = Const::from_bits(self.tcx, n, typing_env, ty); diff --git a/compiler/rustc_mir_build/src/builder/misc.rs b/compiler/rustc_mir_build/src/builder/misc.rs index 6e8e74fd4fc8d..101d07cea2ce7 100644 --- a/compiler/rustc_mir_build/src/builder/misc.rs +++ b/compiler/rustc_mir_build/src/builder/misc.rs @@ -2,7 +2,7 @@ //! kind of thing. use rustc_middle::mir::*; -use rustc_middle::ty::{self, Ty}; +use rustc_middle::ty::Ty; use rustc_span::Span; use rustc_trait_selection::infer::InferCtxtExt; use tracing::debug; @@ -32,7 +32,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { /// Returns a zero literal operand for the appropriate type, works for /// bool, char and integers. pub(crate) fn zero_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> { - let literal = Const::from_bits(self.tcx, 0, ty::TypingEnv::fully_monomorphized(), ty); + let literal = Const::from_bits(self.tcx, 0, self.typing_env(), ty); self.literal_operand(span, literal) } diff --git a/compiler/rustc_public_bridge/src/alloc.rs b/compiler/rustc_public_bridge/src/alloc.rs index 7e6af3425465a..675f0f4cf8671 100644 --- a/compiler/rustc_public_bridge/src/alloc.rs +++ b/compiler/rustc_public_bridge/src/alloc.rs @@ -8,6 +8,7 @@ use rustc_abi::{Size, TyAndLayout}; use rustc_middle::mir::interpret::{ AllocId, AllocInit, AllocRange, Allocation, ConstAllocation, Pointer, Scalar, alloc_range, }; +use rustc_middle::ty::layout::HasTypingEnv; use rustc_middle::ty::{Ty, layout}; use super::{CompilerCtxt, Tables}; @@ -18,8 +19,7 @@ pub fn create_ty_and_layout<'tcx, B: Bridge>( cx: &CompilerCtxt<'tcx, B>, ty: Ty<'tcx>, ) -> Result>, &'tcx layout::LayoutError<'tcx>> { - use crate::context::TypingEnvHelpers; - cx.tcx.layout_of(cx.fully_monomorphized().as_query_input(ty)) + cx.tcx.layout_of(cx.typing_env().as_query_input(ty)) } pub fn try_new_scalar<'tcx, B: Bridge>( diff --git a/compiler/rustc_public_bridge/src/context/helpers.rs b/compiler/rustc_public_bridge/src/context/helpers.rs index 21eef29e5f1cc..e5a3aa0457612 100644 --- a/compiler/rustc_public_bridge/src/context/helpers.rs +++ b/compiler/rustc_public_bridge/src/context/helpers.rs @@ -4,7 +4,6 @@ //! interface regardless of internal compiler changes. use rustc_middle::mir::interpret::AllocRange; -use rustc_middle::ty; use rustc_middle::ty::Ty; use rustc_span::def_id::DefId; @@ -12,10 +11,6 @@ pub trait TyHelpers<'tcx> { fn new_foreign(&self, def_id: DefId) -> Ty<'tcx>; } -pub trait TypingEnvHelpers<'tcx> { - fn fully_monomorphized(&self) -> ty::TypingEnv<'tcx>; -} - pub trait AllocRangeHelpers<'tcx> { fn alloc_range(&self, offset: rustc_abi::Size, size: rustc_abi::Size) -> AllocRange; } diff --git a/compiler/rustc_public_bridge/src/context/impls.rs b/compiler/rustc_public_bridge/src/context/impls.rs index f8ab16a54b451..742e863749518 100644 --- a/compiler/rustc_public_bridge/src/context/impls.rs +++ b/compiler/rustc_public_bridge/src/context/impls.rs @@ -12,7 +12,7 @@ use rustc_hir::def::DefKind; use rustc_middle::mir::interpret::{AllocId, ConstAllocation, ErrorHandled, GlobalAlloc, Scalar}; use rustc_middle::mir::{BinOp, Body, Const as MirConst, ConstValue, UnOp}; use rustc_middle::ty::consts::ConstExt; -use rustc_middle::ty::layout::{FnAbiOf, LayoutOf}; +use rustc_middle::ty::layout::{FnAbiOf, HasTypingEnv, LayoutOf}; use rustc_middle::ty::print::{ with_forced_trimmed_paths, with_no_trimmed_paths, with_resolve_crate_name, }; @@ -28,7 +28,7 @@ use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE}; use rustc_span::{Span, Symbol}; use rustc_target::callconv::FnAbi; -use super::{AllocRangeHelpers, CompilerCtxt, TyHelpers, TypingEnvHelpers}; +use super::{AllocRangeHelpers, CompilerCtxt, TyHelpers}; use crate::builder::BodyBuilder; use crate::{Bridge, Error, Tables, filter_def_ids}; @@ -38,12 +38,6 @@ impl<'tcx, B: Bridge> TyHelpers<'tcx> for CompilerCtxt<'tcx, B> { } } -impl<'tcx, B: Bridge> TypingEnvHelpers<'tcx> for CompilerCtxt<'tcx, B> { - fn fully_monomorphized(&self) -> ty::TypingEnv<'tcx> { - ty::TypingEnv::fully_monomorphized() - } -} - impl<'tcx, B: Bridge> AllocRangeHelpers<'tcx> for CompilerCtxt<'tcx, B> { fn alloc_range( &self, @@ -458,8 +452,7 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> { /// Evaluate constant as a target usize. pub fn eval_target_usize(&self, cnst: MirConst<'tcx>) -> Result { - use crate::context::TypingEnvHelpers; - cnst.try_eval_target_usize(self.tcx, self.fully_monomorphized()) + cnst.try_eval_target_usize(self.tcx, self.typing_env()) .ok_or_else(|| B::Error::new(format!("Const `{cnst:?}` cannot be encoded as u64"))) } @@ -471,7 +464,7 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> { pub fn try_new_const_zst(&self, ty_internal: Ty<'tcx>) -> Result, B::Error> { let size = self .tcx - .layout_of(self.fully_monomorphized().as_query_input(ty_internal)) + .layout_of(self.typing_env().as_query_input(ty_internal)) .map_err(|err| { B::Error::new(format!( "Cannot create a zero-sized constant for type `{ty_internal}`: {err}" @@ -523,11 +516,7 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> { value: u128, ty_internal: Ty<'tcx>, ) -> Result, B::Error> { - let size = self - .tcx - .layout_of(self.fully_monomorphized().as_query_input(ty_internal)) - .unwrap() - .size; + let size = self.tcx.layout_of(self.typing_env().as_query_input(ty_internal)).unwrap().size; let scalar = ScalarInt::try_from_uint(value, size).ok_or_else(|| { B::Error::new(format!("Value overflow: cannot convert `{value}` to `{ty_internal}`.")) })?; @@ -539,11 +528,7 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> { value: u128, ty_internal: Ty<'tcx>, ) -> Result, B::Error> { - let size = self - .tcx - .layout_of(self.fully_monomorphized().as_query_input(ty_internal)) - .unwrap() - .size; + let size = self.tcx.layout_of(self.typing_env().as_query_input(ty_internal)).unwrap().size; let scalar = ScalarInt::try_from_uint(value, size).ok_or_else(|| { B::Error::new(format!("Value overflow: cannot convert `{value}` to `{ty_internal}`.")) })?; @@ -585,11 +570,7 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> { /// Returns the type of given definition instantiated with the given arguments. pub fn def_ty_with_args(&self, item: DefId, args_ref: GenericArgsRef<'tcx>) -> Ty<'tcx> { let def_ty = self.tcx.type_of(item); - self.tcx.instantiate_and_normalize_erasing_regions( - args_ref, - self.fully_monomorphized(), - def_ty, - ) + self.tcx.instantiate_and_normalize_erasing_regions(args_ref, self.typing_env(), def_ty) } /// `Span` of a `DefId`. @@ -625,7 +606,7 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> { /// Get the instance type with generic instantiations applied and lifetimes erased. pub fn instance_ty(&self, instance: ty::Instance<'tcx>) -> Ty<'tcx> { assert!(!instance.has_non_region_param(), "{instance:?} needs further instantiation"); - instance.ty(self.tcx, self.fully_monomorphized()) + instance.ty(self.tcx, self.typing_env()) } /// Get the instantiation types. @@ -694,7 +675,7 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> { def_id: DefId, args_ref: GenericArgsRef<'tcx>, ) -> Option> { - match Instance::try_resolve(self.tcx, self.fully_monomorphized(), def_id, args_ref) { + match Instance::try_resolve(self.tcx, self.typing_env(), def_id, args_ref) { Ok(Some(instance)) => Some(instance), Ok(None) | Err(_) => None, } @@ -712,7 +693,7 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> { def_id: DefId, args_ref: GenericArgsRef<'tcx>, ) -> Option> { - Instance::resolve_for_fn_ptr(self.tcx, self.fully_monomorphized(), def_id, args_ref) + Instance::resolve_for_fn_ptr(self.tcx, self.typing_env(), def_id, args_ref) } /// Resolve instance for a closure with the requested type. @@ -728,7 +709,7 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> { /// Try to evaluate an instance into a constant. pub fn eval_instance(&self, instance: ty::Instance<'tcx>) -> Result { self.tcx.const_eval_instance( - self.fully_monomorphized(), + self.typing_env(), instance, self.tcx.def_span(instance.def_id()), )