Directly construct Inherited.

This commit is contained in:
Camille GILLOT 2023-03-09 19:53:59 +00:00
parent 35a0961bbc
commit 83da9a89d8
4 changed files with 148 additions and 173 deletions

View File

@ -4,7 +4,6 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir as hir;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::HirIdMap;
use rustc_infer::infer;
use rustc_infer::infer::{DefiningAnchor, InferCtxt, InferOk, TyCtxtInferExt};
use rustc_middle::ty::visit::TypeVisitableExt;
use rustc_middle::ty::{self, Ty, TyCtxt};
@ -73,40 +72,16 @@ impl<'tcx> Deref for Inherited<'tcx> {
}
}
/// A temporary returned by `Inherited::build(...)`. This is necessary
/// for multiple `InferCtxt` to share the same `typeck_results`
/// without using `Rc` or something similar.
pub struct InheritedBuilder<'tcx> {
infcx: infer::InferCtxtBuilder<'tcx>,
typeck_results: RefCell<ty::TypeckResults<'tcx>>,
}
impl<'tcx> Inherited<'tcx> {
pub fn build(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> InheritedBuilder<'tcx> {
pub fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
let hir_owner = tcx.hir().local_def_id_to_hir_id(def_id).owner;
InheritedBuilder {
infcx: tcx
let infcx = tcx
.infer_ctxt()
.ignoring_regions()
.with_opaque_type_inference(DefiningAnchor::Bind(hir_owner.def_id)),
typeck_results: RefCell::new(ty::TypeckResults::new(hir_owner)),
}
}
}
impl<'tcx> InheritedBuilder<'tcx> {
pub fn enter<F, R>(mut self, f: F) -> R
where
F: FnOnce(&Inherited<'tcx>) -> R,
{
f(&Inherited::new(self.infcx.build(), self.typeck_results))
}
}
impl<'tcx> Inherited<'tcx> {
fn new(infcx: InferCtxt<'tcx>, typeck_results: RefCell<ty::TypeckResults<'tcx>>) -> Self {
let tcx = infcx.tcx;
.with_opaque_type_inference(DefiningAnchor::Bind(hir_owner.def_id))
.build();
let typeck_results = RefCell::new(ty::TypeckResults::new(hir_owner));
Inherited {
typeck_results,

View File

@ -45,13 +45,14 @@ mod rvalue_scopes;
mod upvar;
mod writeback;
pub use diverges::Diverges;
pub use expectation::Expectation;
pub use fn_ctxt::*;
pub use inherited::{Inherited, InheritedBuilder};
pub use fn_ctxt::FnCtxt;
pub use inherited::Inherited;
use crate::check::check_fn;
use crate::coercion::DynamicCoerceMany;
use crate::diverges::Diverges;
use crate::expectation::Expectation;
use crate::fn_ctxt::RawTy;
use crate::gather_locals::GatherLocalsVisitor;
use rustc_data_structures::unord::UnordSet;
use rustc_errors::{
@ -206,13 +207,13 @@ fn typeck_with_fallback<'tcx>(
});
let body = tcx.hir().body(body_id);
let typeck_results = Inherited::build(tcx, def_id).enter(|inh| {
let param_env = tcx.param_env(def_id);
let param_env = if tcx.has_attr(def_id.to_def_id(), sym::rustc_do_not_const_check) {
param_env.without_const()
} else {
param_env
};
let inh = Inherited::new(tcx, def_id);
let mut fcx = FnCtxt::new(&inh, param_env, def_id);
if let Some(hir::FnSig { header, decl, .. }) = fn_sig {
@ -244,16 +245,17 @@ fn typeck_with_fallback<'tcx>(
kind: TypeVariableOriginKind::TypeInference,
span,
}),
Node::Ty(&hir::Ty {
kind: hir::TyKind::Typeof(ref anon_const), ..
}) if anon_const.hir_id == id => fcx.next_ty_var(TypeVariableOrigin {
Node::Ty(&hir::Ty { kind: hir::TyKind::Typeof(ref anon_const), .. })
if anon_const.hir_id == id =>
{
fcx.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeInference,
span,
}),
})
}
Node::Expr(&hir::Expr { kind: hir::ExprKind::InlineAsm(asm), .. })
| Node::Item(&hir::Item { kind: hir::ItemKind::GlobalAsm(asm), .. }) => {
let operand_ty =
asm.operands.iter().find_map(|(op, _op_sp)| match op {
let operand_ty = asm.operands.iter().find_map(|(op, _op_sp)| match op {
hir::InlineAsmOperand::Const { anon_const }
if anon_const.hir_id == id =>
{
@ -333,8 +335,7 @@ fn typeck_with_fallback<'tcx>(
fcx.infcx.skip_region_resolution();
fcx.resolve_type_vars_in_body(body)
});
let typeck_results = fcx.resolve_type_vars_in_body(body);
// Consistency check our TypeckResults instance can hold all ItemLocalIds
// it will need to hold.

View File

@ -369,10 +369,10 @@ fn can_change_type<'a>(cx: &LateContext<'a>, mut expr: &'a Expr<'a>, mut ty: Ty<
Node::Item(item) => {
if let ItemKind::Fn(_, _, body_id) = &item.kind
&& let output_ty = return_ty(cx, item.owner_id)
&& Inherited::build(cx.tcx, item.owner_id.def_id).enter(|inherited| {
let fn_ctxt = FnCtxt::new(inherited, cx.param_env, item.owner_id.def_id);
fn_ctxt.can_coerce(ty, output_ty)
}) {
&& let inherited = Inherited::new(cx.tcx, item.owner_id.def_id)
&& let fn_ctxt = FnCtxt::new(&inherited, cx.param_env, item.owner_id.def_id)
&& fn_ctxt.can_coerce(ty, output_ty)
{
if has_lifetime(output_ty) && has_lifetime(ty) {
return false;
}

View File

@ -33,8 +33,8 @@ pub(super) fn check_cast<'tcx>(
let hir_id = e.hir_id;
let local_def_id = hir_id.owner.def_id;
Inherited::build(cx.tcx, local_def_id).enter(|inherited| {
let fn_ctxt = FnCtxt::new(inherited, cx.param_env, local_def_id);
let inherited = Inherited::new(cx.tcx, local_def_id);
let fn_ctxt = FnCtxt::new(&inherited, cx.param_env, local_def_id);
// If we already have errors, we can't be sure we can pointer cast.
assert!(
@ -66,5 +66,4 @@ pub(super) fn check_cast<'tcx>(
} else {
None
}
})
}