Skip to content

Commit

Permalink
Avoid lots of hir::HirId{,Map,Set} qualifiers.
Browse files Browse the repository at this point in the history
Because they're a bit redundant.
  • Loading branch information
nnethercote committed Mar 21, 2024
1 parent eba9934 commit 55a92cf
Show file tree
Hide file tree
Showing 39 changed files with 301 additions and 337 deletions.
11 changes: 6 additions & 5 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use rustc_ast::*;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::HirId;
use rustc_middle::span_bug;
use rustc_session::errors::report_lit_error;
use rustc_span::source_map::{respan, Spanned};
Expand Down Expand Up @@ -698,8 +699,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
pub(super) fn maybe_forward_track_caller(
&mut self,
span: Span,
outer_hir_id: hir::HirId,
inner_hir_id: hir::HirId,
outer_hir_id: HirId,
inner_hir_id: HirId,
) {
if self.tcx.features().async_fn_track_caller
&& let Some(attrs) = self.attrs.get(&outer_hir_id.local_id)
Expand Down Expand Up @@ -1045,7 +1046,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
binder: &ClosureBinder,
capture_clause: CaptureBy,
closure_id: NodeId,
closure_hir_id: hir::HirId,
closure_hir_id: HirId,
coroutine_kind: CoroutineKind,
decl: &FnDecl,
body: &Expr,
Expand Down Expand Up @@ -2033,7 +2034,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
&mut self,
sp: Span,
ident: Ident,
binding: hir::HirId,
binding: HirId,
) -> &'hir hir::Expr<'hir> {
self.arena.alloc(self.expr_ident_mut(sp, ident, binding))
}
Expand All @@ -2042,7 +2043,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
&mut self,
span: Span,
ident: Ident,
binding: hir::HirId,
binding: HirId,
) -> hir::Expr<'hir> {
let hir_id = self.next_id();
let res = Res::Local(binding);
Expand Down
37 changes: 15 additions & 22 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use rustc_hir as hir;
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
use rustc_hir::def_id::{LocalDefId, LocalDefIdMap, CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::{
ConstArg, GenericArg, ItemLocalMap, MissingLifetimeKind, ParamName, TraitCandidate,
ConstArg, GenericArg, HirId, ItemLocalMap, MissingLifetimeKind, ParamName, TraitCandidate,
};
use rustc_index::{Idx, IndexSlice, IndexVec};
use rustc_macros::extension;
Expand Down Expand Up @@ -107,7 +107,7 @@ struct LoweringContext<'a, 'hir> {

/// When inside an `async` context, this is the `HirId` of the
/// `task_context` local bound to the resume argument of the coroutine.
task_context: Option<hir::HirId>,
task_context: Option<HirId>,

/// Used to get the current `fn`'s def span to point to when using `await`
/// outside of an `async fn`.
Expand Down Expand Up @@ -661,18 +661,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
/// `HirIdValidator` later on, which makes sure that all `NodeId`s got mapped
/// properly. Calling the method twice with the same `NodeId` is fine though.
#[instrument(level = "debug", skip(self), ret)]
fn lower_node_id(&mut self, ast_node_id: NodeId) -> hir::HirId {
fn lower_node_id(&mut self, ast_node_id: NodeId) -> HirId {
assert_ne!(ast_node_id, DUMMY_NODE_ID);

match self.node_id_to_local_id.entry(ast_node_id) {
Entry::Occupied(o) => {
hir::HirId { owner: self.current_hir_id_owner, local_id: *o.get() }
}
Entry::Occupied(o) => HirId { owner: self.current_hir_id_owner, local_id: *o.get() },
Entry::Vacant(v) => {
// Generate a new `HirId`.
let owner = self.current_hir_id_owner;
let local_id = self.item_local_id_counter;
let hir_id = hir::HirId { owner, local_id };
let hir_id = HirId { owner, local_id };

v.insert(local_id);
self.item_local_id_counter.increment_by(1);
Expand All @@ -693,20 +691,20 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {

/// Generate a new `HirId` without a backing `NodeId`.
#[instrument(level = "debug", skip(self), ret)]
fn next_id(&mut self) -> hir::HirId {
fn next_id(&mut self) -> HirId {
let owner = self.current_hir_id_owner;
let local_id = self.item_local_id_counter;
assert_ne!(local_id, hir::ItemLocalId::new(0));
self.item_local_id_counter.increment_by(1);
hir::HirId { owner, local_id }
HirId { owner, local_id }
}

#[instrument(level = "trace", skip(self))]
fn lower_res(&mut self, res: Res<NodeId>) -> Res {
let res: Result<Res, ()> = res.apply_id(|id| {
let owner = self.current_hir_id_owner;
let local_id = self.node_id_to_local_id.get(&id).copied().ok_or(())?;
Ok(hir::HirId { owner, local_id })
Ok(HirId { owner, local_id })
});
trace!(?res);

Expand Down Expand Up @@ -889,7 +887,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
ret
}

fn lower_attrs(&mut self, id: hir::HirId, attrs: &[Attribute]) -> Option<&'hir [Attribute]> {
fn lower_attrs(&mut self, id: HirId, attrs: &[Attribute]) -> Option<&'hir [Attribute]> {
if attrs.is_empty() {
None
} else {
Expand Down Expand Up @@ -921,7 +919,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
Attribute { kind, id: attr.id, style: attr.style, span: self.lower_span(attr.span) }
}

fn alias_attrs(&mut self, id: hir::HirId, target_id: hir::HirId) {
fn alias_attrs(&mut self, id: HirId, target_id: HirId) {
debug_assert_eq!(id.owner, self.current_hir_id_owner);
debug_assert_eq!(target_id.owner, self.current_hir_id_owner);
if let Some(&a) = self.attrs.get(&target_id.local_id) {
Expand Down Expand Up @@ -2418,11 +2416,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
self.pat(span, hir::PatKind::Struct(qpath, fields, false))
}

fn pat_ident(&mut self, span: Span, ident: Ident) -> (&'hir hir::Pat<'hir>, hir::HirId) {
fn pat_ident(&mut self, span: Span, ident: Ident) -> (&'hir hir::Pat<'hir>, HirId) {
self.pat_ident_binding_mode(span, ident, hir::BindingAnnotation::NONE)
}

fn pat_ident_mut(&mut self, span: Span, ident: Ident) -> (hir::Pat<'hir>, hir::HirId) {
fn pat_ident_mut(&mut self, span: Span, ident: Ident) -> (hir::Pat<'hir>, HirId) {
self.pat_ident_binding_mode_mut(span, ident, hir::BindingAnnotation::NONE)
}

Expand All @@ -2431,7 +2429,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
span: Span,
ident: Ident,
bm: hir::BindingAnnotation,
) -> (&'hir hir::Pat<'hir>, hir::HirId) {
) -> (&'hir hir::Pat<'hir>, HirId) {
let (pat, hir_id) = self.pat_ident_binding_mode_mut(span, ident, bm);
(self.arena.alloc(pat), hir_id)
}
Expand All @@ -2441,7 +2439,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
span: Span,
ident: Ident,
bm: hir::BindingAnnotation,
) -> (hir::Pat<'hir>, hir::HirId) {
) -> (hir::Pat<'hir>, HirId) {
let hir_id = self.next_id();

(
Expand Down Expand Up @@ -2473,12 +2471,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
}

fn ty_path(
&mut self,
mut hir_id: hir::HirId,
span: Span,
qpath: hir::QPath<'hir>,
) -> hir::Ty<'hir> {
fn ty_path(&mut self, mut hir_id: HirId, span: Span, qpath: hir::QPath<'hir>) -> hir::Ty<'hir> {
let kind = match qpath {
hir::QPath::Resolved(None, path) => {
// Turn trait object paths into `TyKind::TraitObject` instead.
Expand Down
35 changes: 12 additions & 23 deletions compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use rustc_hir as hir;
use rustc_hir::def::{CtorOf, DefKind, Namespace, Res};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::{walk_generics, Visitor as _};
use rustc_hir::{GenericArg, GenericArgs};
use rustc_hir::{GenericArg, GenericArgs, HirId};
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
use rustc_infer::traits::ObligationCause;
use rustc_middle::middle::stability::AllowUnstable;
Expand Down Expand Up @@ -137,7 +137,7 @@ pub trait AstConv<'tcx> {
/// report.
fn set_tainted_by_errors(&self, e: ErrorGuaranteed);

fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span);
fn record_ty(&self, hir_id: HirId, ty: Ty<'tcx>, span: Span);

fn astconv(&self) -> &dyn AstConv<'tcx>
where
Expand Down Expand Up @@ -943,7 +943,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
#[instrument(level = "debug", skip(self, hir_ref_id, span, qself, assoc_segment), fields(assoc_ident=?assoc_segment.ident), ret)]
pub fn associated_path_to_ty(
&self,
hir_ref_id: hir::HirId,
hir_ref_id: HirId,
span: Span,
qself_ty: Ty<'tcx>,
qself: &hir::Ty<'_>,
Expand Down Expand Up @@ -1226,7 +1226,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
segment: &hir::PathSegment<'tcx>,
adt_did: DefId,
self_ty: Ty<'tcx>,
block: hir::HirId,
block: HirId,
span: Span,
) -> Result<Option<(Ty<'tcx>, DefId)>, ErrorGuaranteed> {
let tcx = self.tcx();
Expand Down Expand Up @@ -1377,7 +1377,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
fn lookup_assoc_ty(
&self,
name: Ident,
block: hir::HirId,
block: HirId,
span: Span,
scope: DefId,
) -> Option<DefId> {
Expand All @@ -1389,7 +1389,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
fn lookup_assoc_ty_unchecked(
&self,
name: Ident,
block: hir::HirId,
block: HirId,
scope: DefId,
) -> Option<(DefId, DefId)> {
let tcx = self.tcx();
Expand All @@ -1406,14 +1406,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
Some((item.def_id, def_scope))
}

fn check_assoc_ty(
&self,
item: DefId,
name: Ident,
def_scope: DefId,
block: hir::HirId,
span: Span,
) {
fn check_assoc_ty(&self, item: DefId, name: Ident, def_scope: DefId, block: HirId, span: Span) {
let tcx = self.tcx();
let kind = DefKind::AssocTy;

Expand Down Expand Up @@ -1816,7 +1809,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
&self,
opt_self_ty: Option<Ty<'tcx>>,
path: &hir::Path<'tcx>,
hir_id: hir::HirId,
hir_id: HirId,
permit_variants: bool,
) -> Ty<'tcx> {
let tcx = self.tcx();
Expand Down Expand Up @@ -2054,7 +2047,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {

// Converts a hir id corresponding to a type parameter to
// a early-bound `ty::Param` or late-bound `ty::Bound`.
pub(crate) fn hir_id_to_bound_ty(&self, hir_id: hir::HirId) -> Ty<'tcx> {
pub(crate) fn hir_id_to_bound_ty(&self, hir_id: HirId) -> Ty<'tcx> {
let tcx = self.tcx();
match tcx.named_bound_var(hir_id) {
Some(rbv::ResolvedArg::LateBound(debruijn, index, def_id)) => {
Expand All @@ -2079,11 +2072,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {

// Converts a hir id corresponding to a const parameter to
// a early-bound `ConstKind::Param` or late-bound `ConstKind::Bound`.
pub(crate) fn hir_id_to_bound_const(
&self,
hir_id: hir::HirId,
param_ty: Ty<'tcx>,
) -> Const<'tcx> {
pub(crate) fn hir_id_to_bound_const(&self, hir_id: HirId, param_ty: Ty<'tcx>) -> Const<'tcx> {
let tcx = self.tcx();
match tcx.named_bound_var(hir_id) {
Some(rbv::ResolvedArg::EarlyBound(def_id)) => {
Expand Down Expand Up @@ -2417,7 +2406,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
#[instrument(level = "debug", skip(self, hir_id, unsafety, abi, decl, generics, hir_ty), ret)]
pub fn ty_of_fn(
&self,
hir_id: hir::HirId,
hir_id: HirId,
unsafety: hir::Unsafety,
abi: abi::Abi,
decl: &hir::FnDecl<'tcx>,
Expand Down Expand Up @@ -2551,7 +2540,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
/// corresponds to the return type.
fn suggest_trait_fn_ty_for_impl_fn_infer(
&self,
fn_hir_id: hir::HirId,
fn_hir_id: HirId,
arg_idx: Option<usize>,
) -> Option<Ty<'tcx>> {
let tcx = self.tcx();
Expand Down
20 changes: 8 additions & 12 deletions compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::LocalDefId;
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{GenericArg, GenericParam, GenericParamKind, HirIdMap, LifetimeName, Node};
use rustc_hir::{GenericArg, GenericParam, GenericParamKind, HirId, HirIdMap, LifetimeName, Node};
use rustc_macros::extension;
use rustc_middle::bug;
use rustc_middle::hir::nested_filter;
Expand Down Expand Up @@ -108,7 +108,7 @@ enum Scope<'a> {
/// queried later. However, if we enter an elision scope, we have to
/// later append the elided bound vars to the list and need to know what
/// to append to.
hir_id: hir::HirId,
hir_id: HirId,

s: ScopeRef<'a>,

Expand Down Expand Up @@ -775,7 +775,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
}
}

fn visit_path(&mut self, path: &hir::Path<'tcx>, hir_id: hir::HirId) {
fn visit_path(&mut self, path: &hir::Path<'tcx>, hir_id: HirId) {
for (i, segment) in path.segments.iter().enumerate() {
let depth = path.segments.len() - i - 1;
if let Some(args) = segment.args {
Expand Down Expand Up @@ -994,7 +994,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
}
}

fn record_late_bound_vars(&mut self, hir_id: hir::HirId, binder: Vec<ty::BoundVariableKind>) {
fn record_late_bound_vars(&mut self, hir_id: HirId, binder: Vec<ty::BoundVariableKind>) {
if let Some(old) = self.map.late_bound_vars.insert(hir_id, binder) {
bug!(
"overwrote bound vars for {hir_id:?}:\nold={old:?}\nnew={:?}",
Expand All @@ -1021,12 +1021,8 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
/// already in scope (for a fn item, that will be 0, but for a method it might not be). Late
/// bound lifetimes are resolved by name and associated with a binder ID (`binder_id`), so the
/// ordering is not important there.
fn visit_early_late<F>(
&mut self,
hir_id: hir::HirId,
generics: &'tcx hir::Generics<'tcx>,
walk: F,
) where
fn visit_early_late<F>(&mut self, hir_id: HirId, generics: &'tcx hir::Generics<'tcx>, walk: F)
where
F: for<'b, 'c> FnOnce(&'b mut BoundVarContext<'c, 'tcx>),
{
let mut named_late_bound_vars = 0;
Expand Down Expand Up @@ -1073,7 +1069,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
self.with(scope, walk);
}

fn visit_early<F>(&mut self, hir_id: hir::HirId, generics: &'tcx hir::Generics<'tcx>, walk: F)
fn visit_early<F>(&mut self, hir_id: HirId, generics: &'tcx hir::Generics<'tcx>, walk: F)
where
F: for<'b, 'c> FnOnce(&'b mut BoundVarContext<'c, 'tcx>),
{
Expand Down Expand Up @@ -1299,7 +1295,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
);
}

fn resolve_type_ref(&mut self, param_def_id: LocalDefId, hir_id: hir::HirId) {
fn resolve_type_ref(&mut self, param_def_id: LocalDefId, hir_id: HirId) {
// Walk up the scope chain, tracking the number of fn scopes
// that we pass through, until we find a lifetime with the
// given name or we run out of scopes.
Expand Down

0 comments on commit 55a92cf

Please sign in to comment.