Skip to content

Commit abc1178

Browse files
committed
[WIP] Create some minimal HIR for associated opaque types
1 parent 980cf08 commit abc1178

File tree

16 files changed

+71
-24
lines changed

16 files changed

+71
-24
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
@@ -2548,6 +2548,11 @@ pub struct OpaqueTy<'hir> {
25482548
pub in_trait: bool,
25492549
}
25502550

2551+
#[derive(Copy, Clone, Debug, HashStable_Generic)]
2552+
pub struct AssocOpaqueTy {
2553+
// Maybe add some data if necessary
2554+
}
2555+
25512556
/// From whence the opaque type came.
25522557
#[derive(Copy, Clone, PartialEq, Eq, Debug, HashStable_Generic)]
25532558
pub enum OpaqueTyOrigin {
@@ -3331,6 +3336,7 @@ pub enum OwnerNode<'hir> {
33313336
TraitItem(&'hir TraitItem<'hir>),
33323337
ImplItem(&'hir ImplItem<'hir>),
33333338
Crate(&'hir Mod<'hir>),
3339+
AssocOpaqueTy(&'hir AssocOpaqueTy),
33343340
}
33353341

33363342
impl<'hir> OwnerNode<'hir> {
@@ -3340,7 +3346,7 @@ impl<'hir> OwnerNode<'hir> {
33403346
| OwnerNode::ForeignItem(ForeignItem { ident, .. })
33413347
| OwnerNode::ImplItem(ImplItem { ident, .. })
33423348
| OwnerNode::TraitItem(TraitItem { ident, .. }) => Some(*ident),
3343-
OwnerNode::Crate(..) => None,
3349+
OwnerNode::Crate(..) | OwnerNode::AssocOpaqueTy(..) => None,
33443350
}
33453351
}
33463352

@@ -3353,6 +3359,7 @@ impl<'hir> OwnerNode<'hir> {
33533359
| OwnerNode::ImplItem(ImplItem { span, .. })
33543360
| OwnerNode::TraitItem(TraitItem { span, .. }) => span,
33553361
OwnerNode::Crate(Mod { spans: ModSpans { inner_span, .. }, .. }) => inner_span,
3362+
OwnerNode::AssocOpaqueTy(..) => unreachable!(),
33563363
}
33573364
}
33583365

@@ -3411,6 +3418,7 @@ impl<'hir> OwnerNode<'hir> {
34113418
| OwnerNode::ImplItem(ImplItem { owner_id, .. })
34123419
| OwnerNode::ForeignItem(ForeignItem { owner_id, .. }) => *owner_id,
34133420
OwnerNode::Crate(..) => crate::CRATE_HIR_ID.owner,
3421+
OwnerNode::AssocOpaqueTy(..) => unreachable!(),
34143422
}
34153423
}
34163424

@@ -3454,6 +3462,7 @@ impl<'hir> Into<Node<'hir>> for OwnerNode<'hir> {
34543462
OwnerNode::ImplItem(n) => Node::ImplItem(n),
34553463
OwnerNode::TraitItem(n) => Node::TraitItem(n),
34563464
OwnerNode::Crate(n) => Node::Crate(n),
3465+
OwnerNode::AssocOpaqueTy(n) => Node::AssocOpaqueTy(n),
34573466
}
34583467
}
34593468
}
@@ -3491,6 +3500,7 @@ pub enum Node<'hir> {
34913500
WhereBoundPredicate(&'hir WhereBoundPredicate<'hir>),
34923501
// FIXME: Merge into `Node::Infer`.
34933502
ArrayLenInfer(&'hir InferArg),
3503+
AssocOpaqueTy(&'hir AssocOpaqueTy),
34943504
// Span by reference to minimize `Node`'s size
34953505
#[allow(rustc::pass_by_value)]
34963506
Err(&'hir Span),
@@ -3541,6 +3551,7 @@ impl<'hir> Node<'hir> {
35413551
| Node::Infer(..)
35423552
| Node::WhereBoundPredicate(..)
35433553
| Node::ArrayLenInfer(..)
3554+
| Node::AssocOpaqueTy(..)
35443555
| Node::Err(..) => None,
35453556
}
35463557
}
@@ -3646,6 +3657,7 @@ impl<'hir> Node<'hir> {
36463657
Node::TraitItem(i) => Some(OwnerNode::TraitItem(i)),
36473658
Node::ImplItem(i) => Some(OwnerNode::ImplItem(i)),
36483659
Node::Crate(i) => Some(OwnerNode::Crate(i)),
3660+
Node::AssocOpaqueTy(i) => Some(OwnerNode::AssocOpaqueTy(i)),
36493661
_ => None,
36503662
}
36513663
}

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ fn check_well_formed(tcx: TyCtxt<'_>, def_id: hir::OwnerId) -> Result<(), ErrorG
194194
hir::OwnerNode::TraitItem(item) => check_trait_item(tcx, item),
195195
hir::OwnerNode::ImplItem(item) => check_impl_item(tcx, item),
196196
hir::OwnerNode::ForeignItem(item) => check_foreign_item(tcx, item),
197+
hir::OwnerNode::AssocOpaqueTy(..) => unreachable!(),
197198
};
198199

199200
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
@@ -270,6 +270,7 @@ fn resolve_bound_vars(tcx: TyCtxt<'_>, local_def_id: hir::OwnerId) -> ResolveBou
270270
visitor.visit_impl_item(item)
271271
}
272272
hir::OwnerNode::Crate(_) => {}
273+
hir::OwnerNode::AssocOpaqueTy(..) => unreachable!(),
273274
}
274275

275276
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
@@ -2059,7 +2059,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20592059
let mut call_finder = FindClosureArg { tcx: self.tcx, calls: vec![] };
20602060
let node = self
20612061
.tcx
2062-
.opt_local_def_id_to_hir_id(self.tcx.hir().get_parent_item(call_expr.hir_id))
2062+
.opt_local_def_id_to_hir_id(
2063+
self.tcx.hir().get_parent_item(call_expr.hir_id).def_id,
2064+
)
20632065
.map(|hir_id| self.tcx.hir_node(hir_id));
20642066
match node {
20652067
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
@@ -2564,6 +2564,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
25642564
hir::OwnerNode::ImplItem(i) => visitor.visit_impl_item(i),
25652565
hir::OwnerNode::TraitItem(i) => visitor.visit_trait_item(i),
25662566
hir::OwnerNode::Crate(_) => bug!("OwnerNode::Crate doesn't not have generics"),
2567+
hir::OwnerNode::AssocOpaqueTy(..) => unreachable!(),
25672568
}
25682569

25692570
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
@@ -160,20 +160,18 @@ impl<'tcx> TyCtxt<'tcx> {
160160
/// Retrieves the `hir::Node` corresponding to `id`, returning `None` if cannot be found.
161161
#[inline]
162162
pub fn opt_hir_node_by_def_id(self, id: LocalDefId) -> Option<Node<'tcx>> {
163-
Some(self.hir_node(self.opt_local_def_id_to_hir_id(id)?))
163+
Some(self.hir_node_by_def_id(id))
164164
}
165165

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

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

@@ -976,6 +974,7 @@ impl<'hir> Map<'hir> {
976974
Node::Crate(item) => item.spans.inner_span,
977975
Node::WhereBoundPredicate(pred) => pred.span,
978976
Node::ArrayLenInfer(inf) => inf.span,
977+
Node::AssocOpaqueTy(..) => unreachable!(),
979978
Node::Err(span) => *span,
980979
}
981980
}
@@ -1240,6 +1239,7 @@ fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
12401239
Node::Crate(..) => String::from("(root_crate)"),
12411240
Node::WhereBoundPredicate(_) => node_str("where bound predicate"),
12421241
Node::ArrayLenInfer(_) => node_str("array len infer"),
1242+
Node::AssocOpaqueTy(..) => unreachable!(),
12431243
Node::Err(_) => node_str("error"),
12441244
}
12451245
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ rustc_queries! {
177177
/// Gives access to the HIR ID for the given `LocalDefId` owner `key` if any.
178178
///
179179
/// 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>{
180+
query local_def_id_to_hir_id(key: LocalDefId) -> hir::HirId{
181181
desc { |tcx| "getting HIR ID of `{}`", tcx.def_path_str(key) }
182182
feedable
183183
}
@@ -196,6 +196,7 @@ rustc_queries! {
196196
/// Avoid calling this query directly.
197197
query opt_hir_owner_nodes(key: LocalDefId) -> Option<&'tcx hir::OwnerNodes<'tcx>> {
198198
desc { |tcx| "getting HIR owner items in `{}`", tcx.def_path_str(key) }
199+
feedable
199200
}
200201

201202
/// Gives access to the HIR attributes inside the HIR owner `key`.
@@ -204,6 +205,7 @@ rustc_queries! {
204205
/// Avoid calling this query directly.
205206
query hir_attrs(key: hir::OwnerId) -> &'tcx hir::AttributeMap<'tcx> {
206207
desc { |tcx| "getting HIR owner attributes in `{}`", tcx.def_path_str(key) }
208+
feedable
207209
}
208210

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

compiler/rustc_middle/src/ty/context.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,9 @@ impl<'tcx> TyCtxt<'tcx> {
525525
pub fn feed_local_def_id(self, key: LocalDefId) -> TyCtxtFeed<'tcx, LocalDefId> {
526526
TyCtxtFeed { tcx: self, key }
527527
}
528+
pub fn feed_owner_id(self, key: hir::OwnerId) -> TyCtxtFeed<'tcx, hir::OwnerId> {
529+
TyCtxtFeed { tcx: self, key }
530+
}
528531

529532
/// In order to break cycles involving `AnonConst`, we need to set the expected type by side
530533
/// effect. However, we do not want this as a general capability, so this interface restricts
@@ -2280,8 +2283,8 @@ impl<'tcx> TyCtxt<'tcx> {
22802283
)
22812284
}
22822285

2283-
pub fn local_def_id_to_hir_id(self, local_def_id: LocalDefId) -> HirId {
2284-
self.opt_local_def_id_to_hir_id(local_def_id).unwrap()
2286+
pub fn opt_local_def_id_to_hir_id(self, local_def_id: LocalDefId) -> Option<HirId> {
2287+
Some(self.local_def_id_to_hir_id(local_def_id))
22852288
}
22862289

22872290
pub fn next_trait_solver_globally(self) -> bool {

compiler/rustc_passes/src/reachable.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ impl<'tcx> ReachableContext<'tcx> {
256256
| Node::Ctor(..)
257257
| Node::Field(_)
258258
| Node::Ty(_)
259-
| Node::Crate(_) => {}
259+
| Node::Crate(_)
260+
| Node::AssocOpaqueTy(..) => {}
260261
_ => {
261262
bug!(
262263
"found unexpected node kind in worklist: {} ({:?})",

compiler/rustc_ty_utils/src/assoc.rs

+28-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
use hir::OwnerId;
12
use rustc_data_structures::fx::FxIndexSet;
23
use rustc_hir as hir;
34
use rustc_hir::def::DefKind;
45
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId};
56
use rustc_hir::intravisit::{self, Visitor};
7+
use rustc_index::IndexVec;
68
use rustc_middle::query::Providers;
79
use rustc_middle::ty::{self, GenericArgs, ImplTraitInTraitData, Ty, TyCtxt};
810
use rustc_span::symbol::kw;
@@ -258,9 +260,19 @@ fn associated_type_for_impl_trait_in_trait(
258260
let local_def_id = trait_assoc_ty.def_id();
259261
let def_id = local_def_id.to_def_id();
260262

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);
263+
trait_assoc_ty.local_def_id_to_hir_id(hir::HirId::make_owner(local_def_id));
264+
trait_assoc_ty.opt_hir_owner_nodes(Some(tcx.arena.alloc(hir::OwnerNodes {
265+
opt_hash_including_bodies: None,
266+
nodes: IndexVec::from_elem_n(
267+
hir::ParentedNode {
268+
parent: hir::ItemLocalId::INVALID,
269+
node: hir::Node::AssocOpaqueTy(&hir::AssocOpaqueTy {}),
270+
},
271+
1,
272+
),
273+
bodies: Default::default(),
274+
})));
275+
tcx.feed_owner_id(OwnerId { def_id: local_def_id }).hir_attrs(hir::AttributeMap::EMPTY);
264276

265277
// Copy span of the opaque.
266278
trait_assoc_ty.def_ident_span(Some(span));
@@ -358,9 +370,19 @@ fn associated_type_for_impl_trait_in_impl(
358370
let local_def_id = impl_assoc_ty.def_id();
359371
let def_id = local_def_id.to_def_id();
360372

361-
// There's no HIR associated with this new synthesized `def_id`, so feed
362-
// `opt_local_def_id_to_hir_id` with `None`.
363-
impl_assoc_ty.opt_local_def_id_to_hir_id(None);
373+
impl_assoc_ty.local_def_id_to_hir_id(hir::HirId::make_owner(local_def_id));
374+
impl_assoc_ty.opt_hir_owner_nodes(Some(tcx.arena.alloc(hir::OwnerNodes {
375+
opt_hash_including_bodies: None,
376+
nodes: IndexVec::from_elem_n(
377+
hir::ParentedNode {
378+
parent: hir::ItemLocalId::INVALID,
379+
node: hir::Node::AssocOpaqueTy(&hir::AssocOpaqueTy {}),
380+
},
381+
1,
382+
),
383+
bodies: Default::default(),
384+
})));
385+
tcx.feed_owner_id(OwnerId { def_id: local_def_id }).hir_attrs(hir::AttributeMap::EMPTY);
364386

365387
// Copy span of the opaque.
366388
impl_assoc_ty.def_ident_span(Some(span));

0 commit comments

Comments
 (0)