Skip to content

Commit bf2ba86

Browse files
authored
Rollup merge of #120206 - petrochenkov:somehir, r=compiler-errors
hir: Make sure all `HirId`s have corresponding HIR `Node`s And then remove `tcx.opt_hir_node(hir_id)` in favor of `tcx.hir_node(hir_id)`.
2 parents 9ec5960 + 3e8c8d8 commit bf2ba86

File tree

50 files changed

+348
-359
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+348
-359
lines changed

compiler/rustc_ast_lowering/src/index.rs

+37-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct NodeCollector<'a, 'hir> {
1515
bodies: &'a SortedMap<ItemLocalId, &'hir Body<'hir>>,
1616

1717
/// Outputs
18-
nodes: IndexVec<ItemLocalId, Option<ParentedNode<'hir>>>,
18+
nodes: IndexVec<ItemLocalId, ParentedNode<'hir>>,
1919
parenting: LocalDefIdMap<ItemLocalId>,
2020

2121
/// The parent of this node
@@ -29,16 +29,19 @@ pub(super) fn index_hir<'hir>(
2929
tcx: TyCtxt<'hir>,
3030
item: hir::OwnerNode<'hir>,
3131
bodies: &SortedMap<ItemLocalId, &'hir Body<'hir>>,
32-
) -> (IndexVec<ItemLocalId, Option<ParentedNode<'hir>>>, LocalDefIdMap<ItemLocalId>) {
33-
let mut nodes = IndexVec::new();
32+
num_nodes: usize,
33+
) -> (IndexVec<ItemLocalId, ParentedNode<'hir>>, LocalDefIdMap<ItemLocalId>) {
34+
let zero_id = ItemLocalId::new(0);
35+
let err_node = ParentedNode { parent: zero_id, node: Node::Err(item.span()) };
36+
let mut nodes = IndexVec::from_elem_n(err_node, num_nodes);
3437
// This node's parent should never be accessed: the owner's parent is computed by the
3538
// hir_owner_parent query. Make it invalid (= ItemLocalId::MAX) to force an ICE whenever it is
3639
// used.
37-
nodes.push(Some(ParentedNode { parent: ItemLocalId::INVALID, node: item.into() }));
40+
nodes[zero_id] = ParentedNode { parent: ItemLocalId::INVALID, node: item.into() };
3841
let mut collector = NodeCollector {
3942
tcx,
4043
owner: item.def_id(),
41-
parent_node: ItemLocalId::new(0),
44+
parent_node: zero_id,
4245
nodes,
4346
bodies,
4447
parenting: Default::default(),
@@ -54,6 +57,14 @@ pub(super) fn index_hir<'hir>(
5457
OwnerNode::ForeignItem(item) => collector.visit_foreign_item(item),
5558
};
5659

60+
for (local_id, node) in collector.nodes.iter_enumerated() {
61+
if let Node::Err(span) = node.node {
62+
let hir_id = HirId { owner: item.def_id(), local_id };
63+
let msg = format!("ID {hir_id} not encountered when visiting item HIR");
64+
tcx.dcx().span_delayed_bug(*span, msg);
65+
}
66+
}
67+
5768
(collector.nodes, collector.parenting)
5869
}
5970

@@ -88,7 +99,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
8899
}
89100
}
90101

91-
self.nodes.insert(hir_id.local_id, ParentedNode { parent: self.parent_node, node });
102+
self.nodes[hir_id.local_id] = ParentedNode { parent: self.parent_node, node };
92103
}
93104

94105
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_node_id: HirId, f: F) {
@@ -254,6 +265,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
254265
}
255266

256267
fn visit_path_segment(&mut self, path_segment: &'hir PathSegment<'hir>) {
268+
// FIXME: walk path segment with `path_segment.hir_id` parent.
257269
self.insert(path_segment.ident.span, path_segment.hir_id, Node::PathSegment(path_segment));
258270
intravisit::walk_path_segment(self, path_segment);
259271
}
@@ -348,4 +360,23 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
348360

349361
self.visit_nested_foreign_item(id);
350362
}
363+
364+
fn visit_where_predicate(&mut self, predicate: &'hir WherePredicate<'hir>) {
365+
match predicate {
366+
WherePredicate::BoundPredicate(pred) => {
367+
self.insert(pred.span, pred.hir_id, Node::WhereBoundPredicate(pred));
368+
self.with_parent(pred.hir_id, |this| {
369+
intravisit::walk_where_predicate(this, predicate)
370+
})
371+
}
372+
_ => intravisit::walk_where_predicate(self, predicate),
373+
}
374+
}
375+
376+
fn visit_array_length(&mut self, len: &'hir ArrayLen) {
377+
match len {
378+
ArrayLen::Infer(inf) => self.insert(inf.span, inf.hir_id, Node::ArrayLenInfer(inf)),
379+
ArrayLen::Body(..) => intravisit::walk_array_len(self, len),
380+
}
381+
}
351382
}

compiler/rustc_ast_lowering/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
675675
} else {
676676
(None, None)
677677
};
678-
let (nodes, parenting) = index::index_hir(self.tcx, node, &bodies);
678+
let num_nodes = self.item_local_id_counter.as_usize();
679+
let (nodes, parenting) = index::index_hir(self.tcx, node, &bodies, num_nodes);
679680
let nodes = hir::OwnerNodes { opt_hash_including_bodies, nodes, bodies };
680681
let attrs = hir::AttributeMap { map: attrs, opt_hash: attrs_hash };
681682

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+57-64
Original file line numberDiff line numberDiff line change
@@ -401,66 +401,60 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
401401
}
402402
let typeck = self.infcx.tcx.typeck(self.mir_def_id());
403403
let hir_id = hir.parent_id(expr.hir_id);
404-
if let Some(parent) = self.infcx.tcx.opt_hir_node(hir_id) {
405-
let (def_id, args, offset) = if let hir::Node::Expr(parent_expr) = parent
406-
&& let hir::ExprKind::MethodCall(_, _, args, _) = parent_expr.kind
407-
&& let Some(def_id) = typeck.type_dependent_def_id(parent_expr.hir_id)
408-
{
409-
(def_id.as_local(), args, 1)
410-
} else if let hir::Node::Expr(parent_expr) = parent
411-
&& let hir::ExprKind::Call(call, args) = parent_expr.kind
412-
&& let ty::FnDef(def_id, _) = typeck.node_type(call.hir_id).kind()
413-
{
414-
(def_id.as_local(), args, 0)
415-
} else {
416-
(None, &[][..], 0)
404+
let parent = self.infcx.tcx.hir_node(hir_id);
405+
let (def_id, args, offset) = if let hir::Node::Expr(parent_expr) = parent
406+
&& let hir::ExprKind::MethodCall(_, _, args, _) = parent_expr.kind
407+
&& let Some(def_id) = typeck.type_dependent_def_id(parent_expr.hir_id)
408+
{
409+
(def_id.as_local(), args, 1)
410+
} else if let hir::Node::Expr(parent_expr) = parent
411+
&& let hir::ExprKind::Call(call, args) = parent_expr.kind
412+
&& let ty::FnDef(def_id, _) = typeck.node_type(call.hir_id).kind()
413+
{
414+
(def_id.as_local(), args, 0)
415+
} else {
416+
(None, &[][..], 0)
417+
};
418+
if let Some(def_id) = def_id
419+
&& let node =
420+
self.infcx.tcx.hir_node(self.infcx.tcx.local_def_id_to_hir_id(def_id))
421+
&& let Some(fn_sig) = node.fn_sig()
422+
&& let Some(ident) = node.ident()
423+
&& let Some(pos) = args.iter().position(|arg| arg.hir_id == expr.hir_id)
424+
&& let Some(arg) = fn_sig.decl.inputs.get(pos + offset)
425+
{
426+
let mut span: MultiSpan = arg.span.into();
427+
span.push_span_label(
428+
arg.span,
429+
"this parameter takes ownership of the value".to_string(),
430+
);
431+
let descr = match node.fn_kind() {
432+
Some(hir::intravisit::FnKind::ItemFn(..)) | None => "function",
433+
Some(hir::intravisit::FnKind::Method(..)) => "method",
434+
Some(hir::intravisit::FnKind::Closure) => "closure",
417435
};
418-
if let Some(def_id) = def_id
419-
&& let Some(node) = self
420-
.infcx
421-
.tcx
422-
.opt_hir_node(self.infcx.tcx.local_def_id_to_hir_id(def_id))
423-
&& let Some(fn_sig) = node.fn_sig()
424-
&& let Some(ident) = node.ident()
425-
&& let Some(pos) = args.iter().position(|arg| arg.hir_id == expr.hir_id)
426-
&& let Some(arg) = fn_sig.decl.inputs.get(pos + offset)
427-
{
428-
let mut span: MultiSpan = arg.span.into();
429-
span.push_span_label(
430-
arg.span,
431-
"this parameter takes ownership of the value".to_string(),
432-
);
433-
let descr = match node.fn_kind() {
434-
Some(hir::intravisit::FnKind::ItemFn(..)) | None => "function",
435-
Some(hir::intravisit::FnKind::Method(..)) => "method",
436-
Some(hir::intravisit::FnKind::Closure) => "closure",
437-
};
438-
span.push_span_label(ident.span, format!("in this {descr}"));
439-
err.span_note(
440-
span,
441-
format!(
442-
"consider changing this parameter type in {descr} `{ident}` to \
436+
span.push_span_label(ident.span, format!("in this {descr}"));
437+
err.span_note(
438+
span,
439+
format!(
440+
"consider changing this parameter type in {descr} `{ident}` to \
443441
borrow instead if owning the value isn't necessary",
444-
),
445-
);
446-
}
447-
let place = &self.move_data.move_paths[mpi].place;
448-
let ty = place.ty(self.body, self.infcx.tcx).ty;
449-
if let hir::Node::Expr(parent_expr) = parent
450-
&& let hir::ExprKind::Call(call_expr, _) = parent_expr.kind
451-
&& let hir::ExprKind::Path(hir::QPath::LangItem(
452-
LangItem::IntoIterIntoIter,
453-
_,
454-
)) = call_expr.kind
455-
{
456-
// Do not suggest `.clone()` in a `for` loop, we already suggest borrowing.
457-
} else if let UseSpans::FnSelfUse { kind: CallKind::Normal { .. }, .. } =
458-
move_spans
459-
{
460-
// We already suggest cloning for these cases in `explain_captures`.
461-
} else {
462-
self.suggest_cloning(err, ty, expr, move_span);
463-
}
442+
),
443+
);
444+
}
445+
let place = &self.move_data.move_paths[mpi].place;
446+
let ty = place.ty(self.body, self.infcx.tcx).ty;
447+
if let hir::Node::Expr(parent_expr) = parent
448+
&& let hir::ExprKind::Call(call_expr, _) = parent_expr.kind
449+
&& let hir::ExprKind::Path(hir::QPath::LangItem(LangItem::IntoIterIntoIter, _)) =
450+
call_expr.kind
451+
{
452+
// Do not suggest `.clone()` in a `for` loop, we already suggest borrowing.
453+
} else if let UseSpans::FnSelfUse { kind: CallKind::Normal { .. }, .. } = move_spans
454+
{
455+
// We already suggest cloning for these cases in `explain_captures`.
456+
} else {
457+
self.suggest_cloning(err, ty, expr, move_span);
464458
}
465459
}
466460
if let Some(pat) = finder.pat {
@@ -1762,7 +1756,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
17621756
fn_decl: hir::FnDecl { inputs, .. },
17631757
..
17641758
}) = e.kind
1765-
&& let Some(hir::Node::Expr(body)) = self.tcx.opt_hir_node(body.hir_id)
1759+
&& let hir::Node::Expr(body) = self.tcx.hir_node(body.hir_id)
17661760
{
17671761
self.suggest_arg = "this: &Self".to_string();
17681762
if inputs.len() > 0 {
@@ -1828,11 +1822,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
18281822
}
18291823
}
18301824

1831-
if let Some(hir::Node::ImplItem(hir::ImplItem {
1825+
if let hir::Node::ImplItem(hir::ImplItem {
18321826
kind: hir::ImplItemKind::Fn(_fn_sig, body_id),
18331827
..
1834-
})) = self.infcx.tcx.opt_hir_node(self.mir_hir_id())
1835-
&& let Some(hir::Node::Expr(expr)) = self.infcx.tcx.opt_hir_node(body_id.hir_id)
1828+
}) = self.infcx.tcx.hir_node(self.mir_hir_id())
1829+
&& let hir::Node::Expr(expr) = self.infcx.tcx.hir_node(body_id.hir_id)
18361830
{
18371831
let mut finder = ExpressionFinder {
18381832
capture_span: *capture_kind_span,
@@ -2400,8 +2394,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
24002394
let proper_span = proper_span.source_callsite();
24012395
if let Some(scope) = self.body.source_scopes.get(source_info.scope)
24022396
&& let ClearCrossCrate::Set(scope_data) = &scope.local_data
2403-
&& let Some(node) = self.infcx.tcx.opt_hir_node(scope_data.lint_root)
2404-
&& let Some(id) = node.body_id()
2397+
&& let Some(id) = self.infcx.tcx.hir_node(scope_data.lint_root).body_id()
24052398
&& let hir::ExprKind::Block(block, _) = self.infcx.tcx.hir().body(id).value.kind
24062399
{
24072400
for stmt in block.stmts {

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
8787
if let hir::ExprKind::Path(hir::QPath::Resolved(None, p)) = expr.kind
8888
&& let [hir::PathSegment { ident, args: None, .. }] = p.segments
8989
&& let hir::def::Res::Local(hir_id) = p.res
90-
&& let Some(hir::Node::Pat(pat)) = tcx.opt_hir_node(hir_id)
90+
&& let hir::Node::Pat(pat) = tcx.hir_node(hir_id)
9191
{
9292
err.span_label(pat.span, format!("binding `{ident}` declared here"));
9393
}

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
396396

397397
let upvar_hir_id = captured_place.get_root_variable();
398398

399-
if let Some(Node::Pat(pat)) = self.infcx.tcx.opt_hir_node(upvar_hir_id)
399+
if let Node::Pat(pat) = self.infcx.tcx.hir_node(upvar_hir_id)
400400
&& let hir::PatKind::Binding(hir::BindingAnnotation::NONE, _, upvar_ident, _) =
401401
pat.kind
402402
{
@@ -688,15 +688,15 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
688688
break;
689689
}
690690
f_in_trait_opt.and_then(|f_in_trait| {
691-
match self.infcx.tcx.opt_hir_node(f_in_trait) {
692-
Some(Node::TraitItem(hir::TraitItem {
691+
match self.infcx.tcx.hir_node(f_in_trait) {
692+
Node::TraitItem(hir::TraitItem {
693693
kind:
694694
hir::TraitItemKind::Fn(
695695
hir::FnSig { decl: hir::FnDecl { inputs, .. }, .. },
696696
_,
697697
),
698698
..
699-
})) => {
699+
}) => {
700700
let hir::Ty { span, .. } = inputs[local.index() - 1];
701701
Some(span)
702702
}
@@ -759,10 +759,10 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
759759
//
760760
// `let &b = a;` -> `let &(mut b) = a;`
761761
if let Some(hir_id) = hir_id
762-
&& let Some(hir::Node::Local(hir::Local {
762+
&& let hir::Node::Local(hir::Local {
763763
pat: hir::Pat { kind: hir::PatKind::Ref(_, _), .. },
764764
..
765-
})) = self.infcx.tcx.opt_hir_node(hir_id)
765+
}) = self.infcx.tcx.hir_node(hir_id)
766766
&& let Ok(name) =
767767
self.infcx.tcx.sess.source_map().span_to_snippet(local_decl.source_info.span)
768768
{
@@ -1206,7 +1206,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
12061206
};
12071207

12081208
if let Some(hir_id) = hir_id
1209-
&& let Some(hir::Node::Local(local)) = self.infcx.tcx.opt_hir_node(hir_id)
1209+
&& let hir::Node::Local(local) = self.infcx.tcx.hir_node(hir_id)
12101210
{
12111211
let tables = self.infcx.tcx.typeck(def_id.as_local().unwrap());
12121212
if let Some(clone_trait) = self.infcx.tcx.lang_items().clone_trait()

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
216216
if let Some(id) = placeholder.bound.kind.get_id()
217217
&& let Some(placeholder_id) = id.as_local()
218218
&& let gat_hir_id = self.infcx.tcx.local_def_id_to_hir_id(placeholder_id)
219-
&& let Some(generics_impl) = hir.get_parent(gat_hir_id).generics()
219+
&& let Some(generics_impl) =
220+
hir.get_parent(hir.parent_id(gat_hir_id)).generics()
220221
{
221222
Some((gat_hir_id, generics_impl))
222223
} else {

0 commit comments

Comments
 (0)