1
Fork 0

use TypingEnv when no infcx is available

the behavior of the type system not only depends on the current
assumptions, but also the currentnphase of the compiler. This is
mostly necessary as we need to decide whether and how to reveal
opaque types. We track this via the `TypingMode`.
This commit is contained in:
lcnr 2024-11-15 13:53:31 +01:00
parent 1a27566d20
commit 19a8eb2a7f
11 changed files with 55 additions and 42 deletions

View file

@ -376,7 +376,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
let instance = if let ty::FnDef(def_id, fn_args) = *func.layout().ty.kind() { let instance = if let ty::FnDef(def_id, fn_args) = *func.layout().ty.kind() {
let instance = ty::Instance::expect_resolve( let instance = ty::Instance::expect_resolve(
fx.tcx, fx.tcx,
ty::ParamEnv::reveal_all(), ty::TypingEnv::fully_monomorphized(),
def_id, def_id,
fn_args, fn_args,
source_info.span, source_info.span,

View file

@ -666,7 +666,7 @@ fn codegen_stmt<'tcx>(
let func_ref = fx.get_function_ref( let func_ref = fx.get_function_ref(
Instance::resolve_for_fn_ptr( Instance::resolve_for_fn_ptr(
fx.tcx, fx.tcx,
ParamEnv::reveal_all(), ty::TypingEnv::fully_monomorphized(),
def_id, def_id,
args, args,
) )
@ -841,14 +841,18 @@ fn codegen_stmt<'tcx>(
lval.write_cvalue(fx, CValue::by_val(operand, box_layout)); lval.write_cvalue(fx, CValue::by_val(operand, box_layout));
} }
Rvalue::NullaryOp(ref null_op, ty) => { Rvalue::NullaryOp(ref null_op, ty) => {
assert!(lval.layout().ty.is_sized(fx.tcx, ParamEnv::reveal_all())); assert!(lval.layout().ty.is_sized(fx.tcx, ty::ParamEnv::reveal_all()));
let layout = fx.layout_of(fx.monomorphize(ty)); let layout = fx.layout_of(fx.monomorphize(ty));
let val = match null_op { let val = match null_op {
NullOp::SizeOf => layout.size.bytes(), NullOp::SizeOf => layout.size.bytes(),
NullOp::AlignOf => layout.align.abi.bytes(), NullOp::AlignOf => layout.align.abi.bytes(),
NullOp::OffsetOf(fields) => fx NullOp::OffsetOf(fields) => fx
.tcx .tcx
.offset_of_subfield(ParamEnv::reveal_all(), layout, fields.iter()) .offset_of_subfield(
ty::TypingEnv::fully_monomorphized(),
layout,
fields.iter(),
)
.bytes(), .bytes(),
NullOp::UbChecks => { NullOp::UbChecks => {
let val = fx.tcx.sess.ub_checks(); let val = fx.tcx.sess.ub_checks();

View file

@ -103,11 +103,11 @@ fn clif_pair_type_from_ty<'tcx>(
/// Is a pointer to this type a wide ptr? /// Is a pointer to this type a wide ptr?
pub(crate) fn has_ptr_meta<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool { pub(crate) fn has_ptr_meta<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
if ty.is_sized(tcx, ParamEnv::reveal_all()) { if ty.is_sized(tcx, ty::ParamEnv::reveal_all()) {
return false; return false;
} }
let tail = tcx.struct_tail_for_codegen(ty, ParamEnv::reveal_all()); let tail = tcx.struct_tail_for_codegen(ty, ty::TypingEnv::fully_monomorphized());
match tail.kind() { match tail.kind() {
ty::Foreign(..) => false, ty::Foreign(..) => false,
ty::Str | ty::Slice(..) | ty::Dynamic(..) => true, ty::Str | ty::Slice(..) | ty::Dynamic(..) => true,
@ -339,9 +339,9 @@ impl<'tcx> rustc_abi::HasDataLayout for FunctionCx<'_, '_, 'tcx> {
} }
} }
impl<'tcx> layout::HasParamEnv<'tcx> for FunctionCx<'_, '_, 'tcx> { impl<'tcx> layout::HasTypingEnv<'tcx> for FunctionCx<'_, '_, 'tcx> {
fn param_env(&self) -> ParamEnv<'tcx> { fn typing_env(&self) -> ty::TypingEnv<'tcx> {
ParamEnv::reveal_all() ty::TypingEnv::fully_monomorphized()
} }
} }
@ -358,7 +358,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
{ {
self.instance.instantiate_mir_and_normalize_erasing_regions( self.instance.instantiate_mir_and_normalize_erasing_regions(
self.tcx, self.tcx,
ty::ParamEnv::reveal_all(), ty::TypingEnv::fully_monomorphized(),
ty::EarlyBinder::bind(value), ty::EarlyBinder::bind(value),
) )
} }
@ -497,9 +497,9 @@ impl<'tcx> rustc_abi::HasDataLayout for RevealAllLayoutCx<'tcx> {
} }
} }
impl<'tcx> layout::HasParamEnv<'tcx> for RevealAllLayoutCx<'tcx> { impl<'tcx> layout::HasTypingEnv<'tcx> for RevealAllLayoutCx<'tcx> {
fn param_env(&self) -> ParamEnv<'tcx> { fn typing_env(&self) -> ty::TypingEnv<'tcx> {
ParamEnv::reveal_all() ty::TypingEnv::fully_monomorphized()
} }
} }

View file

@ -78,7 +78,7 @@ pub(crate) fn eval_mir_constant<'tcx>(
let cv = fx.monomorphize(constant.const_); let cv = fx.monomorphize(constant.const_);
// This cannot fail because we checked all required_consts in advance. // This cannot fail because we checked all required_consts in advance.
let val = cv let val = cv
.eval(fx.tcx, ty::ParamEnv::reveal_all(), constant.span) .eval(fx.tcx, ty::TypingEnv::fully_monomorphized(), constant.span)
.expect("erroneous constant missed by mono item collection"); .expect("erroneous constant missed by mono item collection");
(val, cv.ty()) (val, cv.ty())
} }
@ -265,8 +265,13 @@ fn data_id_for_static(
assert!(!definition); assert!(!definition);
assert!(!tcx.is_mutable_static(def_id)); assert!(!tcx.is_mutable_static(def_id));
let ty = instance.ty(tcx, ParamEnv::reveal_all()); let ty = instance.ty(tcx, ty::TypingEnv::fully_monomorphized());
let align = tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap().align.pref.bytes(); let align = tcx
.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(ty))
.unwrap()
.align
.pref
.bytes();
let linkage = if import_linkage == rustc_middle::mir::mono::Linkage::ExternalWeak let linkage = if import_linkage == rustc_middle::mir::mono::Linkage::ExternalWeak
|| import_linkage == rustc_middle::mir::mono::Linkage::WeakAny || import_linkage == rustc_middle::mir::mono::Linkage::WeakAny

View file

@ -210,7 +210,7 @@ impl DebugContext {
type_names::push_generic_params( type_names::push_generic_params(
tcx, tcx,
tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), args), tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), args),
&mut name, &mut name,
); );
@ -275,8 +275,10 @@ impl DebugContext {
let span = tcx.def_span(def_id); let span = tcx.def_span(def_id);
let (file_id, line, _column) = self.get_span_loc(tcx, span, span); let (file_id, line, _column) = self.get_span_loc(tcx, span, span);
let static_type = Instance::mono(tcx, def_id).ty(tcx, ty::ParamEnv::reveal_all()); let static_type = Instance::mono(tcx, def_id).ty(tcx, ty::TypingEnv::fully_monomorphized());
let static_layout = tcx.layout_of(ty::ParamEnv::reveal_all().and(static_type)).unwrap(); let static_layout = tcx
.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(static_type))
.unwrap();
// FIXME use the actual type layout // FIXME use the actual type layout
let type_id = self.debug_type(tcx, type_dbg, static_type); let type_id = self.debug_type(tcx, type_dbg, static_type);

View file

@ -92,7 +92,7 @@ pub(crate) fn codegen_inline_asm_terminator<'tcx>(
if let ty::FnDef(def_id, args) = *const_.ty().kind() { if let ty::FnDef(def_id, args) = *const_.ty().kind() {
let instance = ty::Instance::resolve_for_fn_ptr( let instance = ty::Instance::resolve_for_fn_ptr(
fx.tcx, fx.tcx,
ty::ParamEnv::reveal_all(), ty::TypingEnv::fully_monomorphized(),
def_id, def_id,
args, args,
) )
@ -227,11 +227,11 @@ pub(crate) fn codegen_naked_asm<'tcx>(
InlineAsmOperand::Const { ref value } => { InlineAsmOperand::Const { ref value } => {
let cv = instance.instantiate_mir_and_normalize_erasing_regions( let cv = instance.instantiate_mir_and_normalize_erasing_regions(
tcx, tcx,
ty::ParamEnv::reveal_all(), ty::TypingEnv::fully_monomorphized(),
ty::EarlyBinder::bind(value.const_), ty::EarlyBinder::bind(value.const_),
); );
let const_value = cv let const_value = cv
.eval(tcx, ty::ParamEnv::reveal_all(), value.span) .eval(tcx, ty::TypingEnv::fully_monomorphized(), value.span)
.expect("erroneous constant missed by mono item collection"); .expect("erroneous constant missed by mono item collection");
let value = rustc_codegen_ssa::common::asm_const_to_str( let value = rustc_codegen_ssa::common::asm_const_to_str(
@ -250,13 +250,13 @@ pub(crate) fn codegen_naked_asm<'tcx>(
let const_ = instance.instantiate_mir_and_normalize_erasing_regions( let const_ = instance.instantiate_mir_and_normalize_erasing_regions(
tcx, tcx,
ty::ParamEnv::reveal_all(), ty::TypingEnv::fully_monomorphized(),
ty::EarlyBinder::bind(value.const_), ty::EarlyBinder::bind(value.const_),
); );
if let ty::FnDef(def_id, args) = *const_.ty().kind() { if let ty::FnDef(def_id, args) = *const_.ty().kind() {
let instance = ty::Instance::resolve_for_fn_ptr( let instance = ty::Instance::resolve_for_fn_ptr(
tcx, tcx,
ty::ParamEnv::reveal_all(), ty::TypingEnv::fully_monomorphized(),
def_id, def_id,
args, args,
) )

View file

@ -20,7 +20,7 @@ mod simd;
use cranelift_codegen::ir::AtomicRmwOp; use cranelift_codegen::ir::AtomicRmwOp;
use rustc_middle::ty; use rustc_middle::ty;
use rustc_middle::ty::GenericArgsRef; use rustc_middle::ty::GenericArgsRef;
use rustc_middle::ty::layout::{HasParamEnv, ValidityRequirement}; use rustc_middle::ty::layout::ValidityRequirement;
use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths}; use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths};
use rustc_span::source_map::Spanned; use rustc_span::source_map::Spanned;
use rustc_span::symbol::{Symbol, sym}; use rustc_span::symbol::{Symbol, sym};
@ -682,7 +682,10 @@ fn codegen_regular_intrinsic_call<'tcx>(
if let Some(requirement) = requirement { if let Some(requirement) = requirement {
let do_panic = !fx let do_panic = !fx
.tcx .tcx
.check_validity_requirement((requirement, fx.param_env().and(ty))) .check_validity_requirement((
requirement,
ty::TypingEnv::fully_monomorphized().as_query_input(ty),
))
.expect("expect to have layout during codegen"); .expect("expect to have layout during codegen");
if do_panic { if do_panic {
@ -741,7 +744,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
let const_val = fx let const_val = fx
.tcx .tcx
.const_eval_instance(ParamEnv::reveal_all(), instance, source_info.span) .const_eval_instance(ty::ParamEnv::reveal_all(), instance, source_info.span)
.unwrap(); .unwrap();
let val = crate::constant::codegen_const_value(fx, const_val, ret.layout().ty); let val = crate::constant::codegen_const_value(fx, const_val, ret.layout().ty);
ret.write_cvalue(fx, val); ret.write_cvalue(fx, val);

View file

@ -98,7 +98,7 @@ mod prelude {
pub(crate) use rustc_middle::mir::{self, *}; pub(crate) use rustc_middle::mir::{self, *};
pub(crate) use rustc_middle::ty::layout::{LayoutOf, TyAndLayout}; pub(crate) use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
pub(crate) use rustc_middle::ty::{ pub(crate) use rustc_middle::ty::{
self, FloatTy, Instance, InstanceKind, IntTy, ParamEnv, Ty, TyCtxt, UintTy, self, FloatTy, Instance, InstanceKind, IntTy, Ty, TyCtxt, UintTy,
}; };
pub(crate) use rustc_span::Span; pub(crate) use rustc_span::Span;

View file

@ -49,7 +49,7 @@ pub(crate) fn maybe_create_entry_wrapper(
// regions must appear in the argument // regions must appear in the argument
// listing. // listing.
let main_ret_ty = tcx.normalize_erasing_regions( let main_ret_ty = tcx.normalize_erasing_regions(
ty::ParamEnv::reveal_all(), ty::TypingEnv::fully_monomorphized(),
main_ret_ty.no_bound_vars().unwrap(), main_ret_ty.no_bound_vars().unwrap(),
); );
@ -113,7 +113,7 @@ pub(crate) fn maybe_create_entry_wrapper(
.unwrap(); .unwrap();
let report = Instance::expect_resolve( let report = Instance::expect_resolve(
tcx, tcx,
ParamEnv::reveal_all(), ty::TypingEnv::fully_monomorphized(),
report.def_id, report.def_id,
tcx.mk_args(&[GenericArg::from(main_ret_ty)]), tcx.mk_args(&[GenericArg::from(main_ret_ty)]),
DUMMY_SP, DUMMY_SP,
@ -139,7 +139,7 @@ pub(crate) fn maybe_create_entry_wrapper(
let start_def_id = tcx.require_lang_item(LangItem::Start, None); let start_def_id = tcx.require_lang_item(LangItem::Start, None);
let start_instance = Instance::expect_resolve( let start_instance = Instance::expect_resolve(
tcx, tcx,
ParamEnv::reveal_all(), ty::TypingEnv::fully_monomorphized(),
start_def_id, start_def_id,
tcx.mk_args(&[main_ret_ty.into()]), tcx.mk_args(&[main_ret_ty.into()]),
DUMMY_SP, DUMMY_SP,

View file

@ -3,6 +3,7 @@
//! [`PointerCoercion::Unsize`]: `rustc_middle::ty::adjustment::PointerCoercion::Unsize` //! [`PointerCoercion::Unsize`]: `rustc_middle::ty::adjustment::PointerCoercion::Unsize`
use rustc_codegen_ssa::base::validate_trivial_unsize; use rustc_codegen_ssa::base::validate_trivial_unsize;
use rustc_middle::ty::layout::HasTypingEnv;
use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths}; use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths};
use crate::base::codegen_panic_nounwind; use crate::base::codegen_panic_nounwind;
@ -23,7 +24,7 @@ pub(crate) fn unsized_info<'tcx>(
old_info: Option<Value>, old_info: Option<Value>,
) -> Value { ) -> Value {
let (source, target) = let (source, target) =
fx.tcx.struct_lockstep_tails_for_codegen(source, target, ParamEnv::reveal_all()); fx.tcx.struct_lockstep_tails_for_codegen(source, target, fx.typing_env());
match (&source.kind(), &target.kind()) { match (&source.kind(), &target.kind()) {
(&ty::Array(_, len), &ty::Slice(_)) => fx.bcx.ins().iconst( (&ty::Array(_, len), &ty::Slice(_)) => fx.bcx.ins().iconst(
fx.pointer_type, fx.pointer_type,

View file

@ -4,6 +4,7 @@ use cranelift_codegen::entity::EntityRef;
use cranelift_codegen::ir::immediates::Offset32; use cranelift_codegen::ir::immediates::Offset32;
use cranelift_frontend::Variable; use cranelift_frontend::Variable;
use rustc_middle::ty::FnSig; use rustc_middle::ty::FnSig;
use rustc_middle::ty::layout::HasTypingEnv;
use crate::prelude::*; use crate::prelude::*;
@ -884,19 +885,17 @@ pub(crate) fn assert_assignable<'tcx>(
assert_assignable(fx, *a, *b, limit - 1); assert_assignable(fx, *a, *b, limit - 1);
} }
(ty::FnPtr(..), ty::FnPtr(..)) => { (ty::FnPtr(..), ty::FnPtr(..)) => {
let from_sig = fx.tcx.normalize_erasing_late_bound_regions( let from_sig = fx
ParamEnv::reveal_all(), .tcx
from_ty.fn_sig(fx.tcx), .normalize_erasing_late_bound_regions(fx.typing_env(), from_ty.fn_sig(fx.tcx));
);
let FnSig { let FnSig {
inputs_and_output: types_from, inputs_and_output: types_from,
c_variadic: c_variadic_from, c_variadic: c_variadic_from,
safety: unsafety_from, safety: unsafety_from,
abi: abi_from, abi: abi_from,
} = from_sig; } = from_sig;
let to_sig = fx let to_sig =
.tcx fx.tcx.normalize_erasing_late_bound_regions(fx.typing_env(), to_ty.fn_sig(fx.tcx));
.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), to_ty.fn_sig(fx.tcx));
let FnSig { let FnSig {
inputs_and_output: types_to, inputs_and_output: types_to,
c_variadic: c_variadic_to, c_variadic: c_variadic_to,
@ -932,9 +931,8 @@ pub(crate) fn assert_assignable<'tcx>(
(&ty::Dynamic(from_traits, _, _from_kind), &ty::Dynamic(to_traits, _, _to_kind)) => { (&ty::Dynamic(from_traits, _, _from_kind), &ty::Dynamic(to_traits, _, _to_kind)) => {
// FIXME(dyn-star): Do the right thing with DynKinds // FIXME(dyn-star): Do the right thing with DynKinds
for (from, to) in from_traits.iter().zip(to_traits) { for (from, to) in from_traits.iter().zip(to_traits) {
let from = let from = fx.tcx.normalize_erasing_late_bound_regions(fx.typing_env(), from);
fx.tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), from); let to = fx.tcx.normalize_erasing_late_bound_regions(fx.typing_env(), to);
let to = fx.tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), to);
assert_eq!( assert_eq!(
from, to, from, to,
"Can't write trait object of incompatible traits {:?} to place with traits {:?}\n\n{:#?}", "Can't write trait object of incompatible traits {:?} to place with traits {:?}\n\n{:#?}",