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 bf6adec108
commit 9cba14b95b
240 changed files with 1739 additions and 1340 deletions

View file

@ -59,9 +59,9 @@ fn unwrap_fn_abi<'tcx>(
}
fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
let param_env = tcx.param_env(item_def_id);
let typing_env = ty::TypingEnv::post_analysis(tcx, item_def_id);
let args = GenericArgs::identity_for_item(tcx, item_def_id);
let instance = match Instance::try_resolve(tcx, param_env, item_def_id.into(), args) {
let instance = match Instance::try_resolve(tcx, typing_env, item_def_id.into(), args) {
Ok(Some(instance)) => instance,
Ok(None) => {
// Not sure what to do here, but `LayoutError::Unknown` seems reasonable?
@ -75,7 +75,9 @@ fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribut
Err(_guaranteed) => return,
};
let abi = unwrap_fn_abi(
tcx.fn_abi_of_instance(param_env.and((instance, /* extra_args */ ty::List::empty()))),
tcx.fn_abi_of_instance(
typing_env.as_query_input((instance, /* extra_args */ ty::List::empty())),
),
tcx,
item_def_id,
);
@ -117,10 +119,10 @@ fn test_abi_eq<'tcx>(abi1: &'tcx FnAbi<'tcx, Ty<'tcx>>, abi2: &'tcx FnAbi<'tcx,
}
fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
let param_env = tcx.param_env(item_def_id);
let typing_env = ty::TypingEnv::post_analysis(tcx, item_def_id);
let ty = tcx.type_of(item_def_id).instantiate_identity();
let span = tcx.def_span(item_def_id);
if !ensure_wf(tcx, param_env, ty, item_def_id, span) {
if !ensure_wf(tcx, typing_env, ty, item_def_id, span) {
return;
}
let meta_items = attr.meta_item_list().unwrap_or_default();
@ -134,10 +136,10 @@ fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribut
);
};
let abi = unwrap_fn_abi(
tcx.fn_abi_of_fn_ptr(
param_env
.and((sig_tys.with(*hdr), /* extra_args */ ty::List::empty())),
),
tcx.fn_abi_of_fn_ptr(typing_env.as_query_input((
sig_tys.with(*hdr),
/* extra_args */ ty::List::empty(),
))),
tcx,
item_def_id,
);
@ -165,10 +167,10 @@ fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribut
);
};
let abi1 = unwrap_fn_abi(
tcx.fn_abi_of_fn_ptr(
param_env
.and((sig_tys1.with(*hdr1), /* extra_args */ ty::List::empty())),
),
tcx.fn_abi_of_fn_ptr(typing_env.as_query_input((
sig_tys1.with(*hdr1),
/* extra_args */ ty::List::empty(),
))),
tcx,
item_def_id,
);
@ -179,10 +181,10 @@ fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribut
);
};
let abi2 = unwrap_fn_abi(
tcx.fn_abi_of_fn_ptr(
param_env
.and((sig_tys2.with(*hdr2), /* extra_args */ ty::List::empty())),
),
tcx.fn_abi_of_fn_ptr(typing_env.as_query_input((
sig_tys2.with(*hdr2),
/* extra_args */ ty::List::empty(),
))),
tcx,
item_def_id,
);

View file

@ -273,7 +273,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
data.get(expr.hir_id).expect("no offset_of_data for offset_of");
let body_did = self.typeck_results().hir_owner.to_def_id();
let param_env = self.tcx.param_env(body_did);
let typing_env = ty::TypingEnv::non_body_analysis(self.tcx, body_did);
let mut current_ty = container;
@ -285,13 +285,13 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
self.insert_def_id(field.did);
let field_ty = field.ty(self.tcx, args);
current_ty = self.tcx.normalize_erasing_regions(param_env, field_ty);
current_ty = self.tcx.normalize_erasing_regions(typing_env, field_ty);
}
// we don't need to mark tuple fields as live,
// but we may need to mark subfields
ty::Tuple(tys) => {
current_ty =
self.tcx.normalize_erasing_regions(param_env, tys[field.as_usize()]);
self.tcx.normalize_erasing_regions(typing_env, tys[field.as_usize()]);
}
_ => span_bug!(expr.span, "named field access on non-ADT"),
}
@ -944,7 +944,10 @@ impl<'tcx> DeadVisitor<'tcx> {
if is_positional
&& self
.tcx
.layout_of(self.tcx.param_env(field.did).and(field_type))
.layout_of(
ty::TypingEnv::non_body_analysis(self.tcx, field.did)
.as_query_input(field_type),
)
.map_or(true, |layout| layout.is_zst())
{
return ShouldWarnAboutField::No;

View file

@ -2,10 +2,9 @@ use rustc_abi::{HasDataLayout, TargetDataLayout};
use rustc_ast::Attribute;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::LocalDefId;
use rustc_middle::infer::canonical::ir::TypingMode;
use rustc_middle::span_bug;
use rustc_middle::ty::layout::{HasParamEnv, HasTyCtxt, LayoutError, LayoutOfHelpers};
use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt};
use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutError, LayoutOfHelpers};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::Span;
use rustc_span::source_map::Spanned;
use rustc_span::symbol::sym;
@ -39,11 +38,13 @@ pub fn test_layout(tcx: TyCtxt<'_>) {
pub fn ensure_wf<'tcx>(
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
typing_env: ty::TypingEnv<'tcx>,
ty: Ty<'tcx>,
def_id: LocalDefId,
span: Span,
) -> bool {
let (infcx, param_env) = tcx.infer_ctxt().build_with_typing_env(typing_env);
let ocx = traits::ObligationCtxt::new_with_diagnostics(&infcx);
let pred = ty::ClauseKind::WellFormed(ty.into());
let obligation = traits::Obligation::new(
tcx,
@ -55,8 +56,6 @@ pub fn ensure_wf<'tcx>(
param_env,
pred,
);
let infcx = tcx.infer_ctxt().build(TypingMode::from_param_env(param_env));
let ocx = traits::ObligationCtxt::new_with_diagnostics(&infcx);
ocx.register_obligation(obligation);
let errors = ocx.select_all_or_error();
if !errors.is_empty() {
@ -69,13 +68,13 @@ pub fn ensure_wf<'tcx>(
}
fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
let param_env = tcx.param_env(item_def_id);
let typing_env = ty::TypingEnv::post_analysis(tcx, item_def_id);
let ty = tcx.type_of(item_def_id).instantiate_identity();
let span = tcx.def_span(item_def_id.to_def_id());
if !ensure_wf(tcx, param_env, ty, item_def_id, span) {
if !ensure_wf(tcx, typing_env, ty, item_def_id, span) {
return;
}
match tcx.layout_of(param_env.and(ty)) {
match tcx.layout_of(typing_env.as_query_input(ty)) {
Ok(ty_layout) => {
// Check out the `#[rustc_layout(..)]` attribute to tell what to dump.
// The `..` are the names of fields to dump.
@ -107,19 +106,15 @@ fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
span,
homogeneous_aggregate: format!(
"{:?}",
ty_layout.homogeneous_aggregate(&UnwrapLayoutCx { tcx, param_env })
ty_layout
.homogeneous_aggregate(&UnwrapLayoutCx { tcx, typing_env })
),
});
}
sym::debug => {
let normalized_ty = format!(
"{}",
tcx.normalize_erasing_regions(
param_env.with_reveal_all_normalized(tcx),
ty,
)
);
let normalized_ty =
format!("{}", tcx.normalize_erasing_regions(typing_env, ty));
// FIXME: using the `Debug` impl here isn't ideal.
let ty_layout = format!("{:#?}", *ty_layout);
tcx.dcx().emit_err(LayoutOf { span, normalized_ty, ty_layout });
@ -140,7 +135,7 @@ fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
struct UnwrapLayoutCx<'tcx> {
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
typing_env: ty::TypingEnv<'tcx>,
}
impl<'tcx> LayoutOfHelpers<'tcx> for UnwrapLayoutCx<'tcx> {
@ -155,9 +150,9 @@ impl<'tcx> HasTyCtxt<'tcx> for UnwrapLayoutCx<'tcx> {
}
}
impl<'tcx> HasParamEnv<'tcx> for UnwrapLayoutCx<'tcx> {
fn param_env(&self) -> ParamEnv<'tcx> {
self.param_env
impl<'tcx> HasTypingEnv<'tcx> for UnwrapLayoutCx<'tcx> {
fn typing_env(&self) -> ty::TypingEnv<'tcx> {
self.typing_env
}
}

View file

@ -1297,7 +1297,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
fn check_is_ty_uninhabited(&mut self, expr: &Expr<'_>, succ: LiveNode) -> LiveNode {
let ty = self.typeck_results.expr_ty(expr);
let m = self.ir.tcx.parent_module(expr.hir_id).to_def_id();
if ty.is_inhabited_from(self.ir.tcx, m, self.param_env) {
if ty.is_inhabited_from(self.ir.tcx, m, ty::TypingEnv::from_param_env(self.param_env)) {
return succ;
}
match self.ir.lnks[succ] {