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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl<'tcx> TypeInformationCtxt<'tcx> for (&LateContext<'tcx>, LocalDefId) {
type Error = !;

fn typeck_results(&self) -> Self::TypeckResults<'_> {
self.0.maybe_typeck_results().expect("expected typeck results")
self.0.typeck_results
}

fn structurally_resolve_type(&self, _span: Span, ty: Ty<'tcx>) -> Ty<'tcx> {
Expand Down
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 @@ -1623,7 +1623,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 @@ -273,7 +273,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 @@ -16,7 +16,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, find_attr};
use rustc_hir::{self as hir, AmbigArg, HirId, ItemLocalId, find_attr};
use rustc_infer::traits::solve::Goal;
use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::adjustment::{Adjust, Adjustment, PointerCoercion};
Expand Down Expand Up @@ -449,16 +449,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 @@ -476,9 +476,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 @@ -505,11 +506,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 @@ -742,43 +740,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 @@ -835,7 +827,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 @@ -876,18 +868,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 @@ -920,7 +919,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 @@ -944,7 +943,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_id = tcx.hir_body_owner_def_id(self.body.id());
let cause = ObligationCause::misc(self.span.to_span(tcx), body_id);
let cause = ObligationCause::misc(self.span.to_span(self.fcx), body_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
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/autorefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitAutorefs {
_ => return,
};

let typeck = cx.typeck_results();
let typeck = cx.typeck_results;
let adjustments_table = typeck.adjustments();

if let Some(adjustments) = adjustments_table.get(inner.hir_id)
Expand All @@ -104,7 +104,7 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitAutorefs {
&& typeck.expr_ty(dereferenced).is_raw_ptr()
&& let method_did = match expr.kind {
// PERF: 5. b. A method call annotated with `#[rustc_no_implicit_refs]`
ExprKind::MethodCall(..) => cx.typeck_results().type_dependent_def_id(expr.hir_id),
ExprKind::MethodCall(..) => cx.typeck_results.type_dependent_def_id(expr.hir_id),
_ => None,
}
&& method_did.map(|did| find_attr!(cx.tcx, did, RustcNoImplicitAutorefs)).unwrap_or(true)
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ impl<'tcx> LateLintPass<'tcx> for NonShorthandFieldPatterns {
fn check_pat(&mut self, cx: &LateContext<'_>, pat: &hir::Pat<'_>) {
// The result shouldn't be tainted, otherwise it will cause ICE.
if let PatKind::Struct(ref qpath, field_pats, _) = pat.kind
&& cx.typeck_results().tainted_by_errors.is_none()
&& cx.typeck_results.tainted_by_errors.is_none()
{
let variant = cx
.typeck_results()
.typeck_results
.pat_ty(pat)
.ty_adt_def()
.expect("struct pattern type is not an ADT")
Expand All @@ -171,7 +171,7 @@ impl<'tcx> LateLintPass<'tcx> for NonShorthandFieldPatterns {
}
if let PatKind::Binding(binding_annot, _, ident, None) = fieldpat.pat.kind {
if cx.tcx.find_field_index(ident, variant)
== Some(cx.typeck_results().field_index(fieldpat.hir_id))
== Some(cx.typeck_results.field_index(fieldpat.hir_id))
{
cx.emit_span_lint(
NON_SHORTHAND_FIELD_PATTERNS,
Expand Down Expand Up @@ -1016,7 +1016,7 @@ impl<'tcx> LateLintPass<'tcx> for MutableTransmutes {
if !def_id_is_transmute(cx, did) {
return None;
}
let sig = cx.typeck_results().node_type(expr.hir_id).fn_sig(cx.tcx);
let sig = cx.typeck_results.node_type(expr.hir_id).fn_sig(cx.tcx);
let from = sig.inputs().skip_binder()[0];
let to = sig.output().skip_binder();
return Some((from, to));
Expand Down Expand Up @@ -2390,7 +2390,7 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
}
} else if let hir::ExprKind::MethodCall(_, receiver, ..) = expr.kind {
// Find problematic calls to `MaybeUninit::assume_init`.
let def_id = cx.typeck_results().type_dependent_def_id(expr.hir_id)?;
let def_id = cx.typeck_results.type_dependent_def_id(expr.hir_id)?;
if cx.tcx.is_diagnostic_item(sym::assume_init, def_id) {
// This is a call to *some* method named `assume_init`.
// See if the `self` parameter is one of the dangerous constructors.
Expand Down Expand Up @@ -2594,7 +2594,7 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
// This conjures an instance of a type out of nothing,
// using zeroed or uninitialized memory.
// We are extremely conservative with what we warn about.
let conjured_ty = cx.typeck_results().expr_ty(expr);
let conjured_ty = cx.typeck_results.expr_ty(expr);
if let Some(err) = with_no_trimmed_paths!(ty_find_init_error(cx, conjured_ty, init)) {
let msg = match init {
InitKind::Zeroed => {
Expand Down Expand Up @@ -2656,7 +2656,7 @@ impl<'tcx> LateLintPass<'tcx> for DerefNullPtr {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &hir::Expr<'_>) {
/// test if expression is a null ptr
fn is_null_ptr(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {
let pointer_ty = cx.typeck_results().expr_ty(expr);
let pointer_ty = cx.typeck_results.expr_ty(expr);
let ty::RawPtr(pointee, _) = pointer_ty.kind() else {
return false;
};
Expand Down
35 changes: 5 additions & 30 deletions compiler/rustc_lint/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//! See <https://rustc-dev-guide.rust-lang.org/diagnostics.html> for an
//! overview of how lints are implemented.

use std::cell::Cell;
use std::slice;

use rustc_abi as abi;
Expand Down Expand Up @@ -485,11 +484,8 @@ pub struct LateContext<'tcx> {
/// Current body, or `None` if outside a body.
pub enclosing_body: Option<hir::BodyId>,

/// Type-checking results for the current body. Access using the `typeck_results`
/// and `maybe_typeck_results` methods, which handle querying the typeck results on demand.
// FIXME(eddyb) move all the code accessing internal fields like this,
// to this module, to avoid exposing it to lint logic.
pub(super) cached_typeck_results: Cell<Option<&'tcx ty::TypeckResults<'tcx>>>,
/// Type-checking results for the current body.
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 @@ -664,35 +660,14 @@ 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,
/// or `None` if outside a body.
pub fn maybe_typeck_results(&self) -> Option<&'tcx ty::TypeckResults<'tcx>> {
self.cached_typeck_results.get().or_else(|| {
self.enclosing_body.map(|body| {
let typeck_results = self.tcx.typeck_body(body);
self.cached_typeck_results.set(Some(typeck_results));
typeck_results
})
})
}

/// 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).
#[track_caller]
pub fn typeck_results(&self) -> &'tcx ty::TypeckResults<'tcx> {
self.maybe_typeck_results().expect("`LateContext::typeck_results` called outside of body")
}

/// Returns the final resolution of a `QPath`, or `Res::Err` if unavailable.
/// Unlike `.typeck_results().qpath_res(qpath, id)`, this can be used even outside
/// Unlike `.typeck_results.qpath_res(qpath, id)`, this can be used even outside
/// bodies (e.g. for paths in `hir::Ty`), without any risk of ICE-ing.
pub fn qpath_res(&self, qpath: &hir::QPath<'_>, id: hir::HirId) -> Res {
match *qpath {
hir::QPath::Resolved(_, path) => path.res,
hir::QPath::TypeRelative(..) => self
.maybe_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
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/dangling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ impl DanglingPointerSearcher<'_, '_> {
fn lint_expr(cx: &LateContext<'_>, expr: &Expr<'_>) {
if let ExprKind::MethodCall(method, receiver, _args, _span) = expr.kind
&& is_temporary_rvalue(receiver)
&& let ty = cx.typeck_results().expr_ty(receiver)
&& let ty = cx.typeck_results.expr_ty(receiver)
&& owns_allocation(cx.tcx, ty)
&& let Some(fn_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
&& let Some(fn_id) = cx.typeck_results.type_dependent_def_id(expr.hir_id)
&& find_attr!(cx.tcx, fn_id, RustcAsPtr)
{
cx.tcx.emit_node_span_lint(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/drop_forget_useless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
&& let Some(fn_name) = cx.tcx.get_diagnostic_name(def_id)
{
let arg_ty = cx.typeck_results().expr_ty(arg);
let arg_ty = cx.typeck_results.expr_ty(arg);
let is_copy = cx.type_is_copy_modulo_regions(arg_ty);
let drop_is_single_call_in_arm = is_single_call_in_arm(cx, arg, expr);
let let_underscore_ignore_sugg = || {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/enum_intrinsics_non_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn enforce_mem_discriminant(
expr_span: Span,
args_span: Span,
) {
let ty_param = cx.typeck_results().node_args(func_expr.hir_id).type_at(0);
let ty_param = cx.typeck_results.node_args(func_expr.hir_id).type_at(0);
if is_non_enum(ty_param) {
cx.emit_span_lint(
ENUM_INTRINSICS_NON_ENUMS,
Expand All @@ -62,7 +62,7 @@ fn enforce_mem_discriminant(
}

fn enforce_mem_variant_count(cx: &LateContext<'_>, func_expr: &hir::Expr<'_>, span: Span) {
let ty_param = cx.typeck_results().node_args(func_expr.hir_id).type_at(0);
let ty_param = cx.typeck_results.node_args(func_expr.hir_id).type_at(0);
if is_non_enum(ty_param) {
cx.emit_span_lint(ENUM_INTRINSICS_NON_ENUMS, span, EnumIntrinsicsMemVariant { ty_param });
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_lint/src/for_loops_over_fallibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopsOverFallibles {

let arg_span = arg.span.source_callsite();

let ty = cx.typeck_results().expr_ty(arg);
let ty = cx.typeck_results.expr_ty(arg);

let (adt, args, ref_mutability) = match ty.kind() {
&ty::Adt(adt, args) => (adt, args, None),
Expand Down Expand Up @@ -136,7 +136,7 @@ fn extract_iterator_next_call<'tcx>(
// This won't work for `Iterator::next(iter)`, is this an issue?
if let hir::ExprKind::MethodCall(_, recv, _, _) = expr.kind
&& cx
.typeck_results()
.typeck_results
.type_dependent_def_id(expr.hir_id)
.is_some_and(|def_id| cx.tcx.is_lang_item(def_id, LangItem::IteratorNext))
{
Expand Down Expand Up @@ -164,7 +164,7 @@ fn suggest_question_mark<'tcx>(
// Check that the function/closure/constant we are in has a `Result` type.
// Otherwise suggesting using `?` may not be a good idea.
{
let ty = cx.typeck_results().expr_ty(cx.tcx.hir_body(body_id).value);
let ty = cx.typeck_results.expr_ty(cx.tcx.hir_body(body_id).value);
let ty::Adt(ret_adt, ..) = ty.kind() else { return false };
if !cx.tcx.is_diagnostic_item(sym::Result, ret_adt.did()) {
return false;
Expand Down
Loading
Loading