Skip to content

Commit de9163a

Browse files
authored
Rollup merge of rust-lang#120943 - petrochenkov:somehir3, r=oli-obk
Create some minimal HIR for associated opaque types `LocalDefId`s for opaque types in traits and impls are created after AST -> HIR lowering, so they don't have corresponding HIR and return their various properties through fed queries. In this PR I also feed some core HIR-related queries for these `LocalDefId`s (which happen to be HIR owners). As a result all `LocalDefId`s now have corresponding `HirId`s and HIR nodes, and "optional" methods like `opt_local_def_id_to_hir_id` and `opt_hir_node_by_def_id` can be removed. Follow up to rust-lang#120206.
2 parents 5bf23d9 + b6312eb commit de9163a

File tree

16 files changed

+67
-29
lines changed

16 files changed

+67
-29
lines changed

compiler/rustc_ast_lowering/src/index.rs

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub(super) fn index_hir<'hir>(
5555
OwnerNode::TraitItem(item) => collector.visit_trait_item(item),
5656
OwnerNode::ImplItem(item) => collector.visit_impl_item(item),
5757
OwnerNode::ForeignItem(item) => collector.visit_foreign_item(item),
58+
OwnerNode::AssocOpaqueTy(..) => unreachable!(),
5859
};
5960

6061
for (local_id, node) in collector.nodes.iter_enumerated() {

compiler/rustc_hir/src/hir.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -2553,6 +2553,11 @@ pub struct OpaqueTy<'hir> {
25532553
pub in_trait: bool,
25542554
}
25552555

2556+
#[derive(Copy, Clone, Debug, HashStable_Generic)]
2557+
pub struct AssocOpaqueTy {
2558+
// Add some data if necessary
2559+
}
2560+
25562561
/// From whence the opaque type came.
25572562
#[derive(Copy, Clone, PartialEq, Eq, Debug, HashStable_Generic)]
25582563
pub enum OpaqueTyOrigin {
@@ -3363,6 +3368,7 @@ pub enum OwnerNode<'hir> {
33633368
TraitItem(&'hir TraitItem<'hir>),
33643369
ImplItem(&'hir ImplItem<'hir>),
33653370
Crate(&'hir Mod<'hir>),
3371+
AssocOpaqueTy(&'hir AssocOpaqueTy),
33663372
}
33673373

33683374
impl<'hir> OwnerNode<'hir> {
@@ -3372,7 +3378,7 @@ impl<'hir> OwnerNode<'hir> {
33723378
| OwnerNode::ForeignItem(ForeignItem { ident, .. })
33733379
| OwnerNode::ImplItem(ImplItem { ident, .. })
33743380
| OwnerNode::TraitItem(TraitItem { ident, .. }) => Some(*ident),
3375-
OwnerNode::Crate(..) => None,
3381+
OwnerNode::Crate(..) | OwnerNode::AssocOpaqueTy(..) => None,
33763382
}
33773383
}
33783384

@@ -3385,6 +3391,7 @@ impl<'hir> OwnerNode<'hir> {
33853391
| OwnerNode::ImplItem(ImplItem { span, .. })
33863392
| OwnerNode::TraitItem(TraitItem { span, .. }) => span,
33873393
OwnerNode::Crate(Mod { spans: ModSpans { inner_span, .. }, .. }) => inner_span,
3394+
OwnerNode::AssocOpaqueTy(..) => unreachable!(),
33883395
}
33893396
}
33903397

@@ -3443,6 +3450,7 @@ impl<'hir> OwnerNode<'hir> {
34433450
| OwnerNode::ImplItem(ImplItem { owner_id, .. })
34443451
| OwnerNode::ForeignItem(ForeignItem { owner_id, .. }) => *owner_id,
34453452
OwnerNode::Crate(..) => crate::CRATE_HIR_ID.owner,
3453+
OwnerNode::AssocOpaqueTy(..) => unreachable!(),
34463454
}
34473455
}
34483456

@@ -3486,6 +3494,7 @@ impl<'hir> Into<Node<'hir>> for OwnerNode<'hir> {
34863494
OwnerNode::ImplItem(n) => Node::ImplItem(n),
34873495
OwnerNode::TraitItem(n) => Node::TraitItem(n),
34883496
OwnerNode::Crate(n) => Node::Crate(n),
3497+
OwnerNode::AssocOpaqueTy(n) => Node::AssocOpaqueTy(n),
34893498
}
34903499
}
34913500
}
@@ -3523,6 +3532,7 @@ pub enum Node<'hir> {
35233532
WhereBoundPredicate(&'hir WhereBoundPredicate<'hir>),
35243533
// FIXME: Merge into `Node::Infer`.
35253534
ArrayLenInfer(&'hir InferArg),
3535+
AssocOpaqueTy(&'hir AssocOpaqueTy),
35263536
// Span by reference to minimize `Node`'s size
35273537
#[allow(rustc::pass_by_value)]
35283538
Err(&'hir Span),
@@ -3573,6 +3583,7 @@ impl<'hir> Node<'hir> {
35733583
| Node::Infer(..)
35743584
| Node::WhereBoundPredicate(..)
35753585
| Node::ArrayLenInfer(..)
3586+
| Node::AssocOpaqueTy(..)
35763587
| Node::Err(..) => None,
35773588
}
35783589
}
@@ -3678,6 +3689,7 @@ impl<'hir> Node<'hir> {
36783689
Node::TraitItem(i) => Some(OwnerNode::TraitItem(i)),
36793690
Node::ImplItem(i) => Some(OwnerNode::ImplItem(i)),
36803691
Node::Crate(i) => Some(OwnerNode::Crate(i)),
3692+
Node::AssocOpaqueTy(i) => Some(OwnerNode::AssocOpaqueTy(i)),
36813693
_ => None,
36823694
}
36833695
}

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ fn check_well_formed(tcx: TyCtxt<'_>, def_id: hir::OwnerId) -> Result<(), ErrorG
196196
hir::OwnerNode::TraitItem(item) => check_trait_item(tcx, item),
197197
hir::OwnerNode::ImplItem(item) => check_impl_item(tcx, item),
198198
hir::OwnerNode::ForeignItem(item) => check_foreign_item(tcx, item),
199+
hir::OwnerNode::AssocOpaqueTy(..) => unreachable!(),
199200
};
200201

201202
if let Some(generics) = node.generics() {

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+1
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ fn resolve_bound_vars(tcx: TyCtxt<'_>, local_def_id: hir::OwnerId) -> ResolveBou
262262
visitor.visit_impl_item(item)
263263
}
264264
hir::OwnerNode::Crate(_) => {}
265+
hir::OwnerNode::AssocOpaqueTy(..) => unreachable!(),
265266
}
266267

267268
let mut rl = ResolveBoundVars::default();

compiler/rustc_hir_pretty/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ impl<'a> State<'a> {
121121
self.print_bounds(":", pred.bounds);
122122
}
123123
Node::ArrayLenInfer(_) => self.word("_"),
124+
Node::AssocOpaqueTy(..) => unreachable!(),
124125
Node::Err(_) => self.word("/*ERROR*/"),
125126
}
126127
}

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2174,7 +2174,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21742174
let mut call_finder = FindClosureArg { tcx: self.tcx, calls: vec![] };
21752175
let node = self
21762176
.tcx
2177-
.opt_local_def_id_to_hir_id(self.tcx.hir().get_parent_item(call_expr.hir_id))
2177+
.opt_local_def_id_to_hir_id(
2178+
self.tcx.hir().get_parent_item(call_expr.hir_id).def_id,
2179+
)
21782180
.map(|hir_id| self.tcx.hir_node(hir_id));
21792181
match node {
21802182
Some(hir::Node::Item(item)) => call_finder.visit_item(item),

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2554,6 +2554,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
25542554
hir::OwnerNode::ImplItem(i) => visitor.visit_impl_item(i),
25552555
hir::OwnerNode::TraitItem(i) => visitor.visit_trait_item(i),
25562556
hir::OwnerNode::Crate(_) => bug!("OwnerNode::Crate doesn't not have generics"),
2557+
hir::OwnerNode::AssocOpaqueTy(..) => unreachable!(),
25572558
}
25582559

25592560
let ast_generics = self.tcx.hir().get_generics(lifetime_scope).unwrap();

compiler/rustc_lint/src/late.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>(
356356
cached_typeck_results: Cell::new(None),
357357
param_env: ty::ParamEnv::empty(),
358358
effective_visibilities: tcx.effective_visibilities(()),
359-
last_node_with_lint_attrs: tcx.local_def_id_to_hir_id(module_def_id.into()),
359+
last_node_with_lint_attrs: tcx.local_def_id_to_hir_id(module_def_id),
360360
generics: None,
361361
only_module: true,
362362
};

compiler/rustc_lint/src/levels.rs

+1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ fn shallow_lint_levels_on(tcx: TyCtxt<'_>, owner: hir::OwnerId) -> ShallowLintLe
190190
levels.add_id(hir::CRATE_HIR_ID);
191191
levels.visit_mod(mod_, mod_.spans.inner_span, hir::CRATE_HIR_ID)
192192
}
193+
hir::OwnerNode::AssocOpaqueTy(..) => unreachable!(),
193194
},
194195
}
195196

compiler/rustc_middle/src/arena.rs

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ macro_rules! arena_types {
115115
[] features: rustc_feature::Features,
116116
[decode] specialization_graph: rustc_middle::traits::specialization_graph::Graph,
117117
[] crate_inherent_impls: rustc_middle::ty::CrateInherentImpls,
118+
[] hir_owner_nodes: rustc_hir::OwnerNodes<'tcx>,
118119
]);
119120
)
120121
}

compiler/rustc_middle/src/hir/map/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -161,20 +161,18 @@ impl<'tcx> TyCtxt<'tcx> {
161161
/// Retrieves the `hir::Node` corresponding to `id`, returning `None` if cannot be found.
162162
#[inline]
163163
pub fn opt_hir_node_by_def_id(self, id: LocalDefId) -> Option<Node<'tcx>> {
164-
Some(self.hir_node(self.opt_local_def_id_to_hir_id(id)?))
164+
Some(self.hir_node_by_def_id(id))
165165
}
166166

167167
/// Retrieves the `hir::Node` corresponding to `id`.
168168
pub fn hir_node(self, id: HirId) -> Node<'tcx> {
169169
self.hir_owner_nodes(id.owner).nodes[id.local_id].node
170170
}
171171

172-
/// Retrieves the `hir::Node` corresponding to `id`, panicking if it cannot be found.
172+
/// Retrieves the `hir::Node` corresponding to `id`.
173173
#[inline]
174-
#[track_caller]
175174
pub fn hir_node_by_def_id(self, id: LocalDefId) -> Node<'tcx> {
176-
self.opt_hir_node_by_def_id(id)
177-
.unwrap_or_else(|| bug!("couldn't find HIR node for def id {id:?}"))
175+
self.hir_node(self.local_def_id_to_hir_id(id))
178176
}
179177

180178
/// Returns `HirId` of the parent HIR node of node with this `hir_id`.
@@ -963,6 +961,7 @@ impl<'hir> Map<'hir> {
963961
Node::Crate(item) => item.spans.inner_span,
964962
Node::WhereBoundPredicate(pred) => pred.span,
965963
Node::ArrayLenInfer(inf) => inf.span,
964+
Node::AssocOpaqueTy(..) => unreachable!(),
966965
Node::Err(span) => *span,
967966
}
968967
}
@@ -1227,6 +1226,7 @@ fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
12271226
Node::Crate(..) => String::from("(root_crate)"),
12281227
Node::WhereBoundPredicate(_) => node_str("where bound predicate"),
12291228
Node::ArrayLenInfer(_) => node_str("array len infer"),
1229+
Node::AssocOpaqueTy(..) => unreachable!(),
12301230
Node::Err(_) => node_str("error"),
12311231
}
12321232
}

compiler/rustc_middle/src/hir/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,10 @@ pub fn provide(providers: &mut Providers) {
127127
providers.hir_crate_items = map::hir_crate_items;
128128
providers.crate_hash = map::crate_hash;
129129
providers.hir_module_items = map::hir_module_items;
130-
providers.opt_local_def_id_to_hir_id = |tcx, def_id| {
131-
Some(match tcx.hir_crate(()).owners[def_id] {
132-
MaybeOwner::Owner(_) => HirId::make_owner(def_id),
133-
MaybeOwner::NonOwner(hir_id) => hir_id,
134-
MaybeOwner::Phantom => bug!("No HirId for {:?}", def_id),
135-
})
130+
providers.local_def_id_to_hir_id = |tcx, def_id| match tcx.hir_crate(()).owners[def_id] {
131+
MaybeOwner::Owner(_) => HirId::make_owner(def_id),
132+
MaybeOwner::NonOwner(hir_id) => hir_id,
133+
MaybeOwner::Phantom => bug!("No HirId for {:?}", def_id),
136134
};
137135
providers.opt_hir_owner_nodes =
138136
|tcx, id| tcx.hir_crate(()).owners.get(id)?.as_owner().map(|i| &i.nodes);

compiler/rustc_middle/src/query/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,8 @@ rustc_queries! {
174174
cache_on_disk_if { true }
175175
}
176176

177-
/// Gives access to the HIR ID for the given `LocalDefId` owner `key` if any.
178-
///
179-
/// Definitions that were generated with no HIR, would be fed to return `None`.
180-
query opt_local_def_id_to_hir_id(key: LocalDefId) -> Option<hir::HirId>{
177+
/// Returns HIR ID for the given `LocalDefId`.
178+
query local_def_id_to_hir_id(key: LocalDefId) -> hir::HirId {
181179
desc { |tcx| "getting HIR ID of `{}`", tcx.def_path_str(key) }
182180
feedable
183181
}
@@ -196,6 +194,7 @@ rustc_queries! {
196194
/// Avoid calling this query directly.
197195
query opt_hir_owner_nodes(key: LocalDefId) -> Option<&'tcx hir::OwnerNodes<'tcx>> {
198196
desc { |tcx| "getting HIR owner items in `{}`", tcx.def_path_str(key) }
197+
feedable
199198
}
200199

201200
/// Gives access to the HIR attributes inside the HIR owner `key`.
@@ -204,6 +203,7 @@ rustc_queries! {
204203
/// Avoid calling this query directly.
205204
query hir_attrs(key: hir::OwnerId) -> &'tcx hir::AttributeMap<'tcx> {
206205
desc { |tcx| "getting HIR owner attributes in `{}`", tcx.def_path_str(key) }
206+
feedable
207207
}
208208

209209
/// Given the def_id of a const-generic parameter, computes the associated default const

compiler/rustc_middle/src/ty/context.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,11 @@ impl<'tcx> TyCtxtFeed<'tcx, LocalDefId> {
589589
pub fn def_id(&self) -> LocalDefId {
590590
self.key
591591
}
592+
593+
// Caller must ensure that `self.key` ID is indeed an owner.
594+
pub fn feed_owner_id(&self) -> TyCtxtFeed<'tcx, hir::OwnerId> {
595+
TyCtxtFeed { tcx: self.tcx, key: hir::OwnerId { def_id: self.key } }
596+
}
592597
}
593598

594599
/// The central data structure of the compiler. It stores references
@@ -2350,8 +2355,8 @@ impl<'tcx> TyCtxt<'tcx> {
23502355
self.intrinsic_raw(def_id)
23512356
}
23522357

2353-
pub fn local_def_id_to_hir_id(self, local_def_id: LocalDefId) -> HirId {
2354-
self.opt_local_def_id_to_hir_id(local_def_id).unwrap()
2358+
pub fn opt_local_def_id_to_hir_id(self, local_def_id: LocalDefId) -> Option<HirId> {
2359+
Some(self.local_def_id_to_hir_id(local_def_id))
23552360
}
23562361

23572362
pub fn next_trait_solver_globally(self) -> bool {

compiler/rustc_passes/src/reachable.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ impl<'tcx> ReachableContext<'tcx> {
270270
| Node::Ctor(..)
271271
| Node::Field(_)
272272
| Node::Ty(_)
273-
| Node::Crate(_) => {}
273+
| Node::Crate(_)
274+
| Node::AssocOpaqueTy(..) => {}
274275
_ => {
275276
bug!(
276277
"found unexpected node kind in worklist: {} ({:?})",

compiler/rustc_ty_utils/src/assoc.rs

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use rustc_data_structures::fx::FxIndexSet;
2-
use rustc_hir as hir;
32
use rustc_hir::def::DefKind;
43
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId};
54
use rustc_hir::intravisit::{self, Visitor};
5+
use rustc_hir::{self as hir, HirId};
6+
use rustc_index::IndexVec;
67
use rustc_middle::query::Providers;
7-
use rustc_middle::ty::{self, ImplTraitInTraitData, TyCtxt};
8+
use rustc_middle::ty::{self, ImplTraitInTraitData, TyCtxt, TyCtxtFeed};
89
use rustc_span::symbol::kw;
910

1011
pub(crate) fn provide(providers: &mut Providers) {
@@ -237,6 +238,22 @@ fn associated_types_for_impl_traits_in_associated_fn(
237238
}
238239
}
239240

241+
fn feed_hir(feed: &TyCtxtFeed<'_, LocalDefId>) {
242+
feed.local_def_id_to_hir_id(HirId::make_owner(feed.def_id()));
243+
feed.opt_hir_owner_nodes(Some(feed.tcx.arena.alloc(hir::OwnerNodes {
244+
opt_hash_including_bodies: None,
245+
nodes: IndexVec::from_elem_n(
246+
hir::ParentedNode {
247+
parent: hir::ItemLocalId::INVALID,
248+
node: hir::Node::AssocOpaqueTy(&hir::AssocOpaqueTy {}),
249+
},
250+
1,
251+
),
252+
bodies: Default::default(),
253+
})));
254+
feed.feed_owner_id().hir_attrs(hir::AttributeMap::EMPTY);
255+
}
256+
240257
/// Given an `opaque_ty_def_id` corresponding to an `impl Trait` in an associated
241258
/// function from a trait, synthesize an associated type for that `impl Trait`
242259
/// that inherits properties that we infer from the method and the opaque type.
@@ -258,9 +275,7 @@ fn associated_type_for_impl_trait_in_trait(
258275
let local_def_id = trait_assoc_ty.def_id();
259276
let def_id = local_def_id.to_def_id();
260277

261-
// There's no HIR associated with this new synthesized `def_id`, so feed
262-
// `opt_local_def_id_to_hir_id` with `None`.
263-
trait_assoc_ty.opt_local_def_id_to_hir_id(None);
278+
feed_hir(&trait_assoc_ty);
264279

265280
// Copy span of the opaque.
266281
trait_assoc_ty.def_ident_span(Some(span));
@@ -318,9 +333,7 @@ fn associated_type_for_impl_trait_in_impl(
318333
let local_def_id = impl_assoc_ty.def_id();
319334
let def_id = local_def_id.to_def_id();
320335

321-
// There's no HIR associated with this new synthesized `def_id`, so feed
322-
// `opt_local_def_id_to_hir_id` with `None`.
323-
impl_assoc_ty.opt_local_def_id_to_hir_id(None);
336+
feed_hir(&impl_assoc_ty);
324337

325338
// Copy span of the opaque.
326339
impl_assoc_ty.def_ident_span(Some(span));

0 commit comments

Comments
 (0)