Skip to content

Commit f32739b

Browse files
committed
Obtain ident in print without forcing delayed lowering
1 parent abac65c commit f32739b

5 files changed

Lines changed: 55 additions & 35 deletions

File tree

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
5454
use rustc_hir::definitions::{DefPathData, DisambiguatorState};
5555
use rustc_hir::lints::{AttributeLint, DelayedLint};
5656
use rustc_hir::{
57-
self as hir, AngleBrackets, ConstArg, GenericArg, HirId, ItemLocalMap, LifetimeSource,
58-
LifetimeSyntax, ParamName, Target, TraitCandidate, find_attr,
57+
self as hir, AngleBrackets, ConstArg, DelayedOwner, GenericArg, HirId, ItemLocalMap,
58+
LifetimeSource, LifetimeSyntax, ParamName, Target, TraitCandidate, find_attr,
5959
};
6060
use rustc_index::{Idx, IndexSlice, IndexVec};
6161
use rustc_macros::extension;
@@ -634,25 +634,31 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> mid_hir::Crate<'_> {
634634
let mut delayed_ids: FxIndexSet<LocalDefId> = Default::default();
635635

636636
for def_id in ast_index.indices() {
637-
let kind = match &ast_index[def_id] {
638-
AstOwner::Item(Item { kind: ItemKind::Delegation { .. }, .. }) => {
639-
Some(hir::DelayedOwnerKind::Item)
640-
}
641-
AstOwner::AssocItem(Item { kind: AssocItemKind::Delegation { .. }, .. }, ctx) => {
642-
match ctx {
643-
AssocCtxt::Trait => Some(hir::DelayedOwnerKind::TraitItem),
644-
AssocCtxt::Impl { .. } => Some(hir::DelayedOwnerKind::ImplItem),
645-
}
637+
let delayed_owner = match &ast_index[def_id] {
638+
AstOwner::Item(Item {
639+
kind: ItemKind::Delegation(box Delegation { ident, .. }),
640+
..
641+
}) => Some(DelayedOwner { kind: hir::DelayedOwnerKind::Item, ident: Some(*ident) }),
642+
AstOwner::AssocItem(
643+
Item { kind: AssocItemKind::Delegation(box Delegation { ident, .. }), .. },
644+
ctx,
645+
) => {
646+
let kind = match ctx {
647+
AssocCtxt::Trait => hir::DelayedOwnerKind::TraitItem,
648+
AssocCtxt::Impl { .. } => hir::DelayedOwnerKind::ImplItem,
649+
};
650+
651+
Some(DelayedOwner { kind, ident: Some(*ident) })
646652
}
647653
_ => None,
648654
};
649655

650-
if let Some(kind) = kind {
656+
if let Some(delayed_owner) = delayed_owner {
651657
delayed_ids.insert(def_id);
652658

653659
let owner = lowerer.owners.get_or_insert_mut(def_id);
654660
if let hir::MaybeOwner::Phantom = owner {
655-
*owner = hir::MaybeOwner::Delayed(kind)
661+
*owner = hir::MaybeOwner::Delayed(delayed_owner)
656662
}
657663
} else {
658664
lowerer.lower_node(def_id);

compiler/rustc_hir/src/hir.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,11 +1648,17 @@ pub enum DelayedOwnerKind {
16481648
TraitItem,
16491649
}
16501650

1651+
#[derive(Copy, Clone, Debug, HashStable_Generic)]
1652+
pub struct DelayedOwner {
1653+
pub ident: Option<Ident>,
1654+
pub kind: DelayedOwnerKind,
1655+
}
1656+
16511657
#[derive(Copy, Clone, Debug, HashStable_Generic)]
16521658
pub enum MaybeOwner<'tcx> {
16531659
Owner(&'tcx OwnerInfo<'tcx>),
16541660
NonOwner(HirId),
1655-
Delayed(DelayedOwnerKind),
1661+
Delayed(DelayedOwner),
16561662
/// Used as a placeholder for unused LocalDefId.
16571663
Phantom,
16581664
}
@@ -1666,10 +1672,10 @@ impl<'tcx> MaybeOwner<'tcx> {
16661672
}
16671673

16681674
pub fn unwrap(self) -> &'tcx OwnerInfo<'tcx> {
1669-
self.as_owner().unwrap_or_else(|| panic!("Not a HIR owner"))
1675+
self.as_owner().unwrap_or_else(|| panic!("not a HIR owner"))
16701676
}
16711677

1672-
pub fn expect_delayed(&self) -> DelayedOwnerKind {
1678+
pub fn expect_delayed(&self) -> DelayedOwner {
16731679
if let MaybeOwner::Delayed(kind) = self { *kind } else { panic!("not a delayed owner") }
16741680
}
16751681
}

compiler/rustc_middle/src/hir/map.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -835,18 +835,8 @@ impl<'tcx> TyCtxt<'tcx> {
835835
}
836836

837837
#[inline]
838-
fn hir_opt_ident(self, id: HirId) -> Option<Ident> {
839-
match self.hir_node(id) {
840-
Node::Pat(&Pat { kind: PatKind::Binding(_, _, ident, _), .. }) => Some(ident),
841-
// A `Ctor` doesn't have an identifier itself, but its parent
842-
// struct/variant does. Compare with `hir::Map::span`.
843-
Node::Ctor(..) => match self.parent_hir_node(id) {
844-
Node::Item(item) => Some(item.kind.ident().unwrap()),
845-
Node::Variant(variant) => Some(variant.ident),
846-
_ => unreachable!(),
847-
},
848-
node => node.ident(),
849-
}
838+
pub fn hir_opt_ident(self, id: HirId) -> Option<Ident> {
839+
self.hir_crate(()).opt_ident(self, id)
850840
}
851841

852842
#[inline]

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_hir::lints::DelayedLint;
2121
use rustc_hir::*;
2222
use rustc_index::IndexVec;
2323
use rustc_macros::{Decodable, Encodable, HashStable};
24-
use rustc_span::{ErrorGuaranteed, ExpnId, Span};
24+
use rustc_span::{ErrorGuaranteed, ExpnId, Ident, Span};
2525

2626
use crate::query::Providers;
2727
use crate::ty::{ResolverAstLowering, TyCtxt};
@@ -78,7 +78,26 @@ impl<'hir> Crate<'hir> {
7878
}
7979

8080
pub fn delayed_owners_kinds(&self) -> impl Iterator<Item = (LocalDefId, DelayedOwnerKind)> {
81-
self.delayed_ids.iter().copied().map(|id| (id, self.owners[id].expect_delayed()))
81+
self.delayed_ids.iter().copied().map(|id| (id, self.owners[id].expect_delayed().kind))
82+
}
83+
84+
pub fn opt_ident(&self, tcx: TyCtxt<'hir>, id: HirId) -> Option<Ident> {
85+
// If possible don't force lowering of delayed owner, as it can lead to cycles.
86+
if let MaybeOwner::Delayed(delayed_owner) = self.owners[id.owner.def_id] {
87+
return delayed_owner.ident;
88+
}
89+
90+
match tcx.hir_node(id) {
91+
Node::Pat(&Pat { kind: PatKind::Binding(_, _, ident, _), .. }) => Some(ident),
92+
// A `Ctor` doesn't have an identifier itself, but its parent
93+
// struct/variant does. Compare with `hir::Map::span`.
94+
Node::Ctor(..) => match tcx.parent_hir_node(id) {
95+
Node::Item(item) => Some(item.kind.ident().unwrap()),
96+
Node::Variant(variant) => Some(variant.ident),
97+
_ => unreachable!(),
98+
},
99+
node => node.ident(),
100+
}
82101
}
83102
}
84103

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ use rustc_apfloat::Float;
88
use rustc_apfloat::ieee::{Double, Half, Quad, Single};
99
use rustc_data_structures::fx::{FxIndexMap, IndexEntry};
1010
use rustc_data_structures::unord::UnordMap;
11-
use rustc_hir as hir;
12-
use rustc_hir::LangItem;
1311
use rustc_hir::def::{self, CtorKind, DefKind, Namespace};
1412
use rustc_hir::def_id::{DefIdMap, DefIdSet, LOCAL_CRATE, ModDefId};
1513
use rustc_hir::definitions::{DefKey, DefPathDataName};
1614
use rustc_hir::limit::Limit;
15+
use rustc_hir::{self as hir, HirId, LangItem};
1716
use rustc_macros::{Lift, extension};
1817
use rustc_session::cstore::{ExternCrate, ExternCrateSource};
1918
use rustc_span::{Ident, RemapPathScopeComponents, Symbol, kw, sym};
@@ -3395,10 +3394,10 @@ fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, N
33953394
continue;
33963395
}
33973396

3398-
let item = tcx.hir_item(id);
3399-
let Some(ident) = item.kind.ident() else { continue };
3397+
let def_id = id.owner_id.def_id;
3398+
let Some(ident) = tcx.hir_opt_ident(HirId::make_owner(def_id)) else { continue };
34003399

3401-
let def_id = item.owner_id.to_def_id();
3400+
let def_id = def_id.to_def_id();
34023401
let ns = tcx.def_kind(def_id).ns().unwrap_or(Namespace::TypeNS);
34033402
collect_fn(&ident, ns, def_id);
34043403
}

0 commit comments

Comments
 (0)