Inherited -> TypeckRootCtxt

This commit is contained in:
Michael Goulet 2024-03-26 14:11:51 -04:00
parent 536606bc5d
commit bf7a745077
11 changed files with 32 additions and 33 deletions

View file

@ -243,7 +243,7 @@ fn suggest_removing_semicolon_for_coerce(
let can_coerce_to_return_ty = match self.ret_coercion.as_ref() { let can_coerce_to_return_ty = match self.ret_coercion.as_ref() {
Some(ret_coercion) => { Some(ret_coercion) => {
let ret_ty = ret_coercion.borrow().expected_ty(); let ret_ty = ret_coercion.borrow().expected_ty();
let ret_ty = self.inh.infcx.shallow_resolve(ret_ty); let ret_ty = self.infcx.shallow_resolve(ret_ty);
self.can_coerce(arm_ty, ret_ty) self.can_coerce(arm_ty, ret_ty)
&& prior_arm.map_or(true, |(_, ty, _)| self.can_coerce(ty, ret_ty)) && prior_arm.map_or(true, |(_, ty, _)| self.can_coerce(ty, ret_ty))
// The match arms need to unify for the case of `impl Trait`. // The match arms need to unify for the case of `impl Trait`.

View file

@ -848,7 +848,7 @@ fn supplied_sig_of_closure(
bound_vars, bound_vars,
); );
let c_result = self.inh.infcx.canonicalize_response(result); let c_result = self.infcx.canonicalize_response(result);
self.typeck_results.borrow_mut().user_provided_sigs.insert(expr_def_id, c_result); self.typeck_results.borrow_mut().user_provided_sigs.insert(expr_def_id, c_result);
// Normalize only after registering in `user_provided_sigs`. // Normalize only after registering in `user_provided_sigs`.

View file

@ -347,7 +347,6 @@ fn calculate_diverging_fallback(
.any(|n| roots_reachable_from_non_diverging.visited(n)); .any(|n| roots_reachable_from_non_diverging.visited(n));
let infer_var_infos: UnordBag<_> = self let infer_var_infos: UnordBag<_> = self
.inh
.infer_var_info .infer_var_info
.borrow() .borrow()
.items() .items()

View file

@ -526,7 +526,7 @@ pub fn field_ty(
pub(in super::super) fn resolve_rvalue_scopes(&self, def_id: DefId) { pub(in super::super) fn resolve_rvalue_scopes(&self, def_id: DefId) {
let scope_tree = self.tcx.region_scope_tree(def_id); let scope_tree = self.tcx.region_scope_tree(def_id);
let rvalue_scopes = { rvalue_scopes::resolve_rvalue_scopes(self, scope_tree, def_id) }; let rvalue_scopes = { rvalue_scopes::resolve_rvalue_scopes(self, scope_tree, def_id) };
let mut typeck_results = self.inh.typeck_results.borrow_mut(); let mut typeck_results = self.typeck_results.borrow_mut();
typeck_results.rvalue_scopes = rvalue_scopes; typeck_results.rvalue_scopes = rvalue_scopes;
} }

View file

@ -7,7 +7,7 @@
use crate::coercion::DynamicCoerceMany; use crate::coercion::DynamicCoerceMany;
use crate::fallback::DivergingFallbackBehavior; use crate::fallback::DivergingFallbackBehavior;
use crate::fn_ctxt::checks::DivergingBlockBehavior; use crate::fn_ctxt::checks::DivergingBlockBehavior;
use crate::{CoroutineTypes, Diverges, EnclosingBreakables, Inherited}; use crate::{CoroutineTypes, Diverges, EnclosingBreakables, TypeckRootCtxt};
use hir::def_id::CRATE_DEF_ID; use hir::def_id::CRATE_DEF_ID;
use rustc_errors::{DiagCtxt, ErrorGuaranteed}; use rustc_errors::{DiagCtxt, ErrorGuaranteed};
use rustc_hir as hir; use rustc_hir as hir;
@ -108,7 +108,7 @@ pub struct FnCtxt<'a, 'tcx> {
pub(super) enclosing_breakables: RefCell<EnclosingBreakables<'tcx>>, pub(super) enclosing_breakables: RefCell<EnclosingBreakables<'tcx>>,
pub(super) inh: &'a Inherited<'tcx>, pub(super) root_ctxt: &'a TypeckRootCtxt<'tcx>,
pub(super) fallback_has_occurred: Cell<bool>, pub(super) fallback_has_occurred: Cell<bool>,
@ -118,12 +118,12 @@ pub struct FnCtxt<'a, 'tcx> {
impl<'a, 'tcx> FnCtxt<'a, 'tcx> { impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
pub fn new( pub fn new(
inh: &'a Inherited<'tcx>, root_ctxt: &'a TypeckRootCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
body_id: LocalDefId, body_id: LocalDefId,
) -> FnCtxt<'a, 'tcx> { ) -> FnCtxt<'a, 'tcx> {
let (diverging_fallback_behavior, diverging_block_behavior) = let (diverging_fallback_behavior, diverging_block_behavior) =
parse_never_type_options_attr(inh.tcx); parse_never_type_options_attr(root_ctxt.tcx);
FnCtxt { FnCtxt {
body_id, body_id,
param_env, param_env,
@ -137,7 +137,7 @@ pub fn new(
stack: Vec::new(), stack: Vec::new(),
by_id: Default::default(), by_id: Default::default(),
}), }),
inh, root_ctxt,
fallback_has_occurred: Cell::new(false), fallback_has_occurred: Cell::new(false),
diverging_fallback_behavior, diverging_fallback_behavior,
diverging_block_behavior, diverging_block_behavior,
@ -206,9 +206,9 @@ pub fn err_ctxt(&'a self) -> TypeErrCtxt<'a, 'tcx> {
} }
impl<'a, 'tcx> Deref for FnCtxt<'a, 'tcx> { impl<'a, 'tcx> Deref for FnCtxt<'a, 'tcx> {
type Target = Inherited<'tcx>; type Target = TypeckRootCtxt<'tcx>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
self.inh self.root_ctxt
} }
} }

View file

@ -95,8 +95,7 @@ fn declare(&mut self, decl: Declaration<'tcx>) {
Some(ref ty) => { Some(ref ty) => {
let o_ty = self.fcx.lower_ty(ty); let o_ty = self.fcx.lower_ty(ty);
let c_ty = let c_ty = self.fcx.infcx.canonicalize_user_type_annotation(UserType::Ty(o_ty.raw));
self.fcx.inh.infcx.canonicalize_user_type_annotation(UserType::Ty(o_ty.raw));
debug!("visit_local: ty.hir_id={:?} o_ty={:?} c_ty={:?}", ty.hir_id, o_ty, c_ty); debug!("visit_local: ty.hir_id={:?} o_ty={:?} c_ty={:?}", ty.hir_id, o_ty, c_ty);
self.fcx self.fcx
.typeck_results .typeck_results

View file

@ -31,7 +31,6 @@
mod fallback; mod fallback;
mod fn_ctxt; mod fn_ctxt;
mod gather_locals; mod gather_locals;
mod inherited;
mod intrinsicck; mod intrinsicck;
mod mem_categorization; mod mem_categorization;
mod method; mod method;
@ -39,11 +38,12 @@
mod pat; mod pat;
mod place_op; mod place_op;
mod rvalue_scopes; mod rvalue_scopes;
mod typeck_root_ctxt;
mod upvar; mod upvar;
mod writeback; mod writeback;
pub use fn_ctxt::FnCtxt; pub use fn_ctxt::FnCtxt;
pub use inherited::Inherited; pub use typeck_root_ctxt::TypeckRootCtxt;
use crate::check::check_fn; use crate::check::check_fn;
use crate::coercion::DynamicCoerceMany; use crate::coercion::DynamicCoerceMany;
@ -170,11 +170,11 @@ fn typeck_with_fallback<'tcx>(
let param_env = tcx.param_env(def_id); let param_env = tcx.param_env(def_id);
let inh = Inherited::new(tcx, def_id); let root_ctxt = TypeckRootCtxt::new(tcx, def_id);
if let Some(inspector) = inspector { if let Some(inspector) = inspector {
inh.infcx.attach_obligation_inspector(inspector); root_ctxt.infcx.attach_obligation_inspector(inspector);
} }
let mut fcx = FnCtxt::new(&inh, param_env, def_id); let mut fcx = FnCtxt::new(&root_ctxt, param_env, def_id);
if let Some(hir::FnSig { header, decl, .. }) = fn_sig { if let Some(hir::FnSig { header, decl, .. }) = fn_sig {
let fn_sig = if decl.output.get_infer_ret_ty().is_some() { let fn_sig = if decl.output.get_infer_ret_ty().is_some() {

View file

@ -388,8 +388,7 @@ fn peel_off_references(
if !pat_adjustments.is_empty() { if !pat_adjustments.is_empty() {
debug!("default binding mode is now {:?}", def_bm); debug!("default binding mode is now {:?}", def_bm);
self.inh self.typeck_results
.typeck_results
.borrow_mut() .borrow_mut()
.pat_adjustments_mut() .pat_adjustments_mut()
.insert(pat.hir_id, pat_adjustments); .insert(pat.hir_id, pat_adjustments);
@ -614,7 +613,7 @@ fn check_pat_ident(
_ => BindingMode::convert(ba), _ => BindingMode::convert(ba),
}; };
// ...and store it in a side table: // ...and store it in a side table:
self.inh.typeck_results.borrow_mut().pat_binding_modes_mut().insert(pat.hir_id, bm); self.typeck_results.borrow_mut().pat_binding_modes_mut().insert(pat.hir_id, bm);
debug!("check_pat_ident: pat.hir_id={:?} bm={:?}", pat.hir_id, bm); debug!("check_pat_ident: pat.hir_id={:?} bm={:?}", pat.hir_id, bm);

View file

@ -16,7 +16,8 @@
use std::cell::RefCell; use std::cell::RefCell;
use std::ops::Deref; use std::ops::Deref;
/// Closures defined within the function. For example: // Data shared between a "typeck root" and its nested bodies,
/// e.g. closures defined within the function. For example:
/// ```ignore (illustrative) /// ```ignore (illustrative)
/// fn foo() { /// fn foo() {
/// bar(move|| { ... }) /// bar(move|| { ... })
@ -24,8 +25,9 @@
/// ``` /// ```
/// Here, the function `foo()` and the closure passed to /// Here, the function `foo()` and the closure passed to
/// `bar()` will each have their own `FnCtxt`, but they will /// `bar()` will each have their own `FnCtxt`, but they will
/// share the inherited fields. /// share the inference context, will process obligations together,
pub struct Inherited<'tcx> { /// can access each other's local types (scoping permitted), etc.
pub struct TypeckRootCtxt<'tcx> {
pub(super) infcx: InferCtxt<'tcx>, pub(super) infcx: InferCtxt<'tcx>,
pub(super) typeck_results: RefCell<ty::TypeckResults<'tcx>>, pub(super) typeck_results: RefCell<ty::TypeckResults<'tcx>>,
@ -65,14 +67,14 @@ pub struct Inherited<'tcx> {
pub(super) infer_var_info: RefCell<UnordMap<ty::TyVid, ty::InferVarInfo>>, pub(super) infer_var_info: RefCell<UnordMap<ty::TyVid, ty::InferVarInfo>>,
} }
impl<'tcx> Deref for Inherited<'tcx> { impl<'tcx> Deref for TypeckRootCtxt<'tcx> {
type Target = InferCtxt<'tcx>; type Target = InferCtxt<'tcx>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.infcx &self.infcx
} }
} }
impl<'tcx> Inherited<'tcx> { impl<'tcx> TypeckRootCtxt<'tcx> {
pub fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self { pub fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
let hir_owner = tcx.local_def_id_to_hir_id(def_id).owner; let hir_owner = tcx.local_def_id_to_hir_id(def_id).owner;
@ -83,7 +85,7 @@ pub fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
.build(); .build();
let typeck_results = RefCell::new(ty::TypeckResults::new(hir_owner)); let typeck_results = RefCell::new(ty::TypeckResults::new(hir_owner));
Inherited { TypeckRootCtxt {
typeck_results, typeck_results,
fulfillment_cx: RefCell::new(<dyn TraitEngine<'_>>::new(&infcx)), fulfillment_cx: RefCell::new(<dyn TraitEngine<'_>>::new(&infcx)),
infcx, infcx,

View file

@ -12,7 +12,7 @@
use rustc_hir::def::{DefKind, Res}; use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_hir::{BorrowKind, Expr, ExprKind, ItemKind, LangItem, Node}; use rustc_hir::{BorrowKind, Expr, ExprKind, ItemKind, LangItem, Node};
use rustc_hir_typeck::{FnCtxt, Inherited}; use rustc_hir_typeck::{FnCtxt, TypeckRootCtxt};
use rustc_infer::infer::TyCtxtInferExt; use rustc_infer::infer::TyCtxtInferExt;
use rustc_lint::LateContext; use rustc_lint::LateContext;
use rustc_middle::mir::Mutability; use rustc_middle::mir::Mutability;
@ -438,8 +438,8 @@ fn can_change_type<'a>(cx: &LateContext<'a>, mut expr: &'a Expr<'a>, mut ty: Ty<
Node::Item(item) => { Node::Item(item) => {
if let ItemKind::Fn(_, _, body_id) = &item.kind if let ItemKind::Fn(_, _, body_id) = &item.kind
&& let output_ty = return_ty(cx, item.owner_id) && let output_ty = return_ty(cx, item.owner_id)
&& let inherited = Inherited::new(cx.tcx, item.owner_id.def_id) && let root_ctxt = TypeckRootCtxt::new(cx.tcx, item.owner_id.def_id)
&& let fn_ctxt = FnCtxt::new(&inherited, cx.param_env, item.owner_id.def_id) && let fn_ctxt = FnCtxt::new(&root_ctxt, cx.param_env, item.owner_id.def_id)
&& fn_ctxt.can_coerce(ty, output_ty) && fn_ctxt.can_coerce(ty, output_ty)
{ {
if has_lifetime(output_ty) && has_lifetime(ty) { if has_lifetime(output_ty) && has_lifetime(ty) {

View file

@ -1,6 +1,6 @@
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::Expr; use rustc_hir::Expr;
use rustc_hir_typeck::{cast, FnCtxt, Inherited}; use rustc_hir_typeck::{cast, FnCtxt, TypeckRootCtxt};
use rustc_lint::LateContext; use rustc_lint::LateContext;
use rustc_middle::ty::cast::CastKind; use rustc_middle::ty::cast::CastKind;
use rustc_middle::ty::Ty; use rustc_middle::ty::Ty;
@ -34,8 +34,8 @@ pub(super) fn check_cast<'tcx>(
let hir_id = e.hir_id; let hir_id = e.hir_id;
let local_def_id = hir_id.owner.def_id; let local_def_id = hir_id.owner.def_id;
let inherited = Inherited::new(cx.tcx, local_def_id); let root_ctxt = TypeckRootCtxt::new(cx.tcx, local_def_id);
let fn_ctxt = FnCtxt::new(&inherited, cx.param_env, local_def_id); let fn_ctxt = FnCtxt::new(&root_ctxt, cx.param_env, local_def_id);
if let Ok(check) = cast::CastCheck::new( if let Ok(check) = cast::CastCheck::new(
&fn_ctxt, &fn_ctxt,