Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2018,7 +2018,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
let typeck = self.typeck_results.borrow();
for (rcvr, args) in call_finder.calls {
if rcvr.hir_id.owner == typeck.hir_owner
if Some(rcvr.hir_id.owner) == typeck.hir_owner
&& let Some(rcvr_ty) = typeck.node_type_opt(rcvr.hir_id)
&& let ty::Closure(call_def_id, _) = rcvr_ty.kind()
&& def_id == *call_def_id
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ fn typeck_with_inspect<'tcx>(

// Consistency check our TypeckResults instance can hold all ItemLocalIds
// it will need to hold.
assert_eq!(typeck_results.hir_owner, id.owner);
assert_eq!(typeck_results.hir_owner, Some(id.owner));

typeck_results
}
Expand Down
57 changes: 28 additions & 29 deletions compiler/rustc_hir_typeck/src/writeback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use rustc_data_structures::unord::ExtendUnord;
use rustc_errors::{E0720, ErrorGuaranteed};
use rustc_hir::def_id::LocalDefId;
use rustc_hir::intravisit::{self, InferKind, Visitor};
use rustc_hir::{self as hir, AmbigArg, HirId};
use rustc_hir::{self as hir, AmbigArg, HirId, ItemLocalId};
use rustc_infer::traits::solve::Goal;
use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::adjustment::{Adjust, Adjustment, PointerCoercion};
Expand Down Expand Up @@ -451,16 +451,16 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
fn visit_closures(&mut self) {
let fcx_typeck_results = self.fcx.typeck_results.borrow();
assert_eq!(fcx_typeck_results.hir_owner, self.typeck_results.hir_owner);
let common_hir_owner = fcx_typeck_results.hir_owner;

let fcx_closure_kind_origins =
fcx_typeck_results.closure_kind_origins().items_in_stable_order();

for (local_id, origin) in fcx_closure_kind_origins {
let hir_id = HirId { owner: common_hir_owner, local_id };
let place_span = origin.0;
let place = self.resolve(origin.1.clone(), &place_span);
self.typeck_results.closure_kind_origins_mut().insert(hir_id, (place_span, place));
self.typeck_results
.closure_kind_origins_mut()
.insert_local(local_id, (place_span, place));
}
}

Expand All @@ -478,9 +478,10 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
fn visit_user_provided_tys(&mut self) {
let fcx_typeck_results = self.fcx.typeck_results.borrow();
assert_eq!(fcx_typeck_results.hir_owner, self.typeck_results.hir_owner);
let common_hir_owner = fcx_typeck_results.hir_owner;

if self.rustc_dump_user_args {
let common_hir_owner = fcx_typeck_results.hir_owner.unwrap();

let sorted_user_provided_types =
fcx_typeck_results.user_provided_types().items_in_stable_order();

Expand All @@ -507,11 +508,8 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
}
}

self.typeck_results.user_provided_types_mut().extend(
fcx_typeck_results.user_provided_types().items().map(|(local_id, c_ty)| {
let hir_id = HirId { owner: common_hir_owner, local_id };
(hir_id, *c_ty)
}),
self.typeck_results.user_provided_types_mut().extend_local(
fcx_typeck_results.user_provided_types().items().map(|(id, &c_ty)| (id, c_ty)),
);
}

Expand Down Expand Up @@ -764,43 +762,37 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
fn visit_liberated_fn_sigs(&mut self) {
let fcx_typeck_results = self.fcx.typeck_results.borrow();
assert_eq!(fcx_typeck_results.hir_owner, self.typeck_results.hir_owner);
let common_hir_owner = fcx_typeck_results.hir_owner;

let fcx_liberated_fn_sigs = fcx_typeck_results.liberated_fn_sigs().items_in_stable_order();

for (local_id, &fn_sig) in fcx_liberated_fn_sigs {
let hir_id = HirId { owner: common_hir_owner, local_id };
let fn_sig = self.resolve(fn_sig, &hir_id);
self.typeck_results.liberated_fn_sigs_mut().insert(hir_id, fn_sig);
let fn_sig = self.resolve(fn_sig, &local_id);
self.typeck_results.liberated_fn_sigs_mut().insert_local(local_id, fn_sig);
}
}

fn visit_fru_field_types(&mut self) {
let fcx_typeck_results = self.fcx.typeck_results.borrow();
assert_eq!(fcx_typeck_results.hir_owner, self.typeck_results.hir_owner);
let common_hir_owner = fcx_typeck_results.hir_owner;

let fcx_fru_field_types = fcx_typeck_results.fru_field_types().items_in_stable_order();

for (local_id, ftys) in fcx_fru_field_types {
let hir_id = HirId { owner: common_hir_owner, local_id };
let ftys = self.resolve(ftys.clone(), &hir_id);
self.typeck_results.fru_field_types_mut().insert(hir_id, ftys);
let ftys = self.resolve(ftys.clone(), &local_id);
self.typeck_results.fru_field_types_mut().insert_local(local_id, ftys);
}
}

fn visit_offset_of_container_types(&mut self) {
let fcx_typeck_results = self.fcx.typeck_results.borrow();
assert_eq!(fcx_typeck_results.hir_owner, self.typeck_results.hir_owner);
let common_hir_owner = fcx_typeck_results.hir_owner;

for (local_id, indices) in fcx_typeck_results.offset_of_data().items_in_stable_order() {
let hir_id = HirId { owner: common_hir_owner, local_id };
let indices = indices
.iter()
.map(|&(ty, variant, field)| (self.resolve(ty, &hir_id), variant, field))
.map(|&(ty, variant, field)| (self.resolve(ty, &local_id), variant, field))
.collect();
self.typeck_results.offset_of_data_mut().insert(hir_id, indices);
self.typeck_results.offset_of_data_mut().insert_local(local_id, indices);
}
}

Expand Down Expand Up @@ -858,7 +850,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
))
})
// FIXME: throwing away the param-env :(
.map(|goal| (goal.predicate, self.fcx.misc(span.to_span(self.fcx.tcx)))),
.map(|goal| (goal.predicate, self.fcx.misc(span.to_span(self.fcx)))),
);
assert_eq!(unexpected_goals, vec![]);

Expand Down Expand Up @@ -899,18 +891,25 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
}

pub(crate) trait Locatable {
fn to_span(&self, tcx: TyCtxt<'_>) -> Span;
fn to_span(&self, tcx: &FnCtxt<'_, '_>) -> Span;
}

impl Locatable for Span {
fn to_span(&self, _: TyCtxt<'_>) -> Span {
fn to_span(&self, _: &FnCtxt<'_, '_>) -> Span {
*self
}
}

impl Locatable for HirId {
fn to_span(&self, tcx: TyCtxt<'_>) -> Span {
tcx.hir_span(*self)
fn to_span(&self, fcx: &FnCtxt<'_, '_>) -> Span {
fcx.tcx.hir_span(*self)
}
}

impl Locatable for ItemLocalId {
fn to_span(&self, fcx: &FnCtxt<'_, '_>) -> Span {
fcx.tcx
.hir_span(HirId { owner: fcx.typeck_results.borrow().expect_owner(), local_id: *self })
}
}

Expand Down Expand Up @@ -943,7 +942,7 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> {
.err_ctxt()
.emit_inference_failure_err(
self.fcx.tcx.hir_body_owner_def_id(self.body.id()),
self.span.to_span(self.fcx.tcx),
self.span.to_span(self.fcx),
p.into(),
TypeAnnotationNeeded::E0282,
false,
Expand All @@ -967,7 +966,7 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> {
// that types that show up in the typeck are fully normalized.
let mut value = if self.should_normalize && self.fcx.next_trait_solver() {
let body_def_id = tcx.hir_body_owner_def_id(self.body.id());
let cause = ObligationCause::misc(self.span.to_span(tcx), body_def_id);
let cause = ObligationCause::misc(self.span.to_span(self.fcx), body_def_id);
let at = self.fcx.at(&cause, self.fcx.param_env);
let universes = vec![None; outer_exclusive_binder(&value).as_usize()];
match solve::deeply_normalize_with_skipped_universes_and_ambiguous_coroutine_goals(
Expand Down
13 changes: 4 additions & 9 deletions compiler/rustc_lint/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ pub struct LateContext<'tcx> {
pub enclosing_body: Option<hir::BodyId>,

/// Type-checking results for the current body.
pub typeck_results: Option<&'tcx ty::TypeckResults<'tcx>>,
pub typeck_results: &'tcx ty::TypeckResults<'tcx>,

/// Parameter environment for the item we are in.
pub param_env: ty::ParamEnv<'tcx>,
Expand Down Expand Up @@ -603,13 +603,9 @@ impl<'tcx> LateContext<'tcx> {
self.tcx.type_is_use_cloned_modulo_regions(self.typing_env(), ty)
}

/// Gets the type-checking results for the current body.
/// As this will ICE if called outside bodies, only call when working with
/// `Expr` or `Pat` nodes (they are guaranteed to be found only in bodies).
#[inline]
#[track_caller]
pub fn typeck_results(&self) -> &'tcx ty::TypeckResults<'tcx> {
self.typeck_results.expect("`LateContext::typeck_results` called outside of body")
self.typeck_results
}

/// Returns the final resolution of a `QPath`, or `Res::Err` if unavailable.
Expand All @@ -618,9 +614,8 @@ impl<'tcx> LateContext<'tcx> {
pub fn qpath_res(&self, qpath: &hir::QPath<'_>, id: hir::HirId) -> Res {
match *qpath {
hir::QPath::Resolved(_, path) => path.res,
hir::QPath::TypeRelative(..) => self
.typeck_results
.filter(|typeck_results| typeck_results.hir_owner == id.owner)
hir::QPath::TypeRelative(..) => Some(self.typeck_results)
.filter(|typeck_results| typeck_results.hir_owner == Some(id.owner))
.or_else(|| {
self.tcx
.has_typeck_results(id.owner.def_id)
Expand Down
11 changes: 6 additions & 5 deletions compiler/rustc_lint/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
self.context.typeck_results = old_typeck_results;
return;
}
self.context.typeck_results = Some(typeck_results);

self.context.typeck_results = typeck_results;
}

let body = self.context.tcx.hir_body(body_id);
Expand All @@ -131,7 +132,7 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
let generics = self.context.generics.take();
self.context.generics = it.kind.generics();
let old_typeck_results = self.context.typeck_results.take();
let old_typeck_results = self.context.typeck_results;
let old_enclosing_body = self.context.enclosing_body.take();
self.with_lint_attrs(it.hir_id(), |cx| {
cx.with_param_env(it.owner_id, |cx| {
Expand Down Expand Up @@ -206,7 +207,7 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
return;
}

self.context.typeck_results = Some(typeck_results);
self.context.typeck_results = typeck_results;
}
let body = self.context.tcx.hir_body(body_id);
lint_callback!(self, check_fn, fk, decl, body, span, id);
Expand Down Expand Up @@ -358,7 +359,7 @@ pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>(
let context = LateContext {
tcx,
enclosing_body: None,
typeck_results: None,
typeck_results: tcx.dummy_typeck_results,
param_env: ty::ParamEnv::empty(),
effective_visibilities: tcx.effective_visibilities(()),
last_node_with_lint_attrs: tcx.local_def_id_to_hir_id(mod_id),
Expand Down Expand Up @@ -436,7 +437,7 @@ fn late_lint_crate<'tcx>(tcx: TyCtxt<'tcx>) {
let context = LateContext {
tcx,
enclosing_body: None,
typeck_results: None,
typeck_results: tcx.dummy_typeck_results,
param_env: ty::ParamEnv::empty(),
effective_visibilities: tcx.effective_visibilities(()),
last_node_with_lint_attrs: hir::CRATE_HIR_ID,
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_lint/src/unit_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ impl<'tcx> LateLintPass<'tcx> for UnitBindings {
// - explicitly wrote `let pat = ();`
// - explicitly wrote `let () = init;`.
if !local.span.from_expansion()
&& let Some(tyck_results) = cx.typeck_results
&& let Some(init) = local.init
&& let init_ty = tyck_results.expr_ty(init)
&& let local_ty = tyck_results.node_type(local.hir_id)
&& let init_ty = cx.typeck_results.expr_ty(init)
&& let local_ty = cx.typeck_results.node_type(local.hir_id)
&& init_ty == cx.tcx.types.unit
&& local_ty == cx.tcx.types.unit
&& local.ty.is_none()
Expand Down
10 changes: 9 additions & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ use crate::ty::{
Const, ConstKind, FlagComputation, FnSigKind, GenericArg, GenericArgsUnsized,
GenericParamDefKind, List, ListWithCachedTypeInfo, ParamConst, Pattern, PatternKind,
PolyExistentialPredicate, PolyFnSig, Predicate, PredicateKind, Region, RegionKind, ReprOptions,
TraitObjectVisitor, Ty, TyKind, TyVid, ValTree, ValTreeKind, Visibility,
TraitObjectVisitor, Ty, TyKind, TyVid, TypeckResults, ValTree, ValTreeKind, Visibility,
};

impl<'tcx> rustc_type_ir::inherent::DefId<TyCtxt<'tcx>> for DefId {
Expand Down Expand Up @@ -975,6 +975,12 @@ pub struct GlobalCtxt<'tcx> {
/// Common consts, pre-interned for your convenience.
pub consts: CommonConsts<'tcx>,

/// The dummy ownerless typeck results. All attempts to lookup a HIR node in any of
/// the contained tables will panic.
///
/// FIXME: This cannot be a static as the lifetime argument is invariant.
pub dummy_typeck_results: &'tcx TypeckResults<'tcx>,

/// Hooks to be able to register functions in other crates that can then still
/// be called from rustc_middle.
pub(crate) hooks: crate::hooks::Providers,
Expand Down Expand Up @@ -1193,6 +1199,7 @@ impl<'tcx> TyCtxt<'tcx> {
let common_types = CommonTypes::new(&interners);
let common_lifetimes = CommonLifetimes::new(&interners);
let common_consts = CommonConsts::new(&interners, &common_types);
let dummy_typeck_results = arena.alloc(TypeckResults::new_maybe_ownerless(None));

let gcx = gcx_cell.get_or_init(|| GlobalCtxt {
sess,
Expand All @@ -1208,6 +1215,7 @@ impl<'tcx> TyCtxt<'tcx> {
types: common_types,
lifetimes: common_lifetimes,
consts: common_consts,
dummy_typeck_results,
untracked,
query_system,
caches: Default::default(),
Expand Down
Loading
Loading