Skip to content

Commit d9dc960

Browse files
committed
Make ConstArg be its own hir::Node with distinct HirId
1 parent 9810f40 commit d9dc960

File tree

6 files changed

+24
-8
lines changed

6 files changed

+24
-8
lines changed

compiler/rustc_ast_lowering/src/index.rs

+10
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,23 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
229229
}
230230

231231
fn visit_anon_const(&mut self, constant: &'hir AnonConst) {
232+
// FIXME: use real span?
232233
self.insert(DUMMY_SP, constant.hir_id, Node::AnonConst(constant));
233234

234235
self.with_parent(constant.hir_id, |this| {
235236
intravisit::walk_anon_const(this, constant);
236237
});
237238
}
238239

240+
fn visit_const_arg(&mut self, const_arg: &'hir ConstArg<'hir>) {
241+
// FIXME: use real span?
242+
self.insert(DUMMY_SP, const_arg.hir_id, Node::ConstArg(const_arg));
243+
244+
self.with_parent(const_arg.hir_id, |this| {
245+
intravisit::walk_const_arg(this, const_arg);
246+
});
247+
}
248+
239249
fn visit_expr(&mut self, expr: &'hir Expr<'hir>) {
240250
self.insert(expr.span, expr.hir_id, Node::Expr(expr));
241251

compiler/rustc_ast_lowering/src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11611161
None,
11621162
);
11631163
return GenericArg::Const(ConstArg {
1164-
hir_id: self.lower_node_id(ty.id),
1164+
hir_id: self.next_id(),
11651165
kind: ConstArgKind::Path(qpath),
11661166
is_desugared_from_effects: false,
11671167
});
@@ -1202,7 +1202,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12021202

12031203
let lowered_anon = self.lower_anon_const(anon);
12041204
ConstArg {
1205-
hir_id: lowered_anon.hir_id,
1205+
hir_id: self.next_id(),
12061206
kind: ConstArgKind::Anon(lowered_anon),
12071207
is_desugared_from_effects: false,
12081208
}
@@ -2624,7 +2624,7 @@ impl<'hir> GenericArgsCtor<'hir> {
26242624

26252625
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
26262626
(
2627-
hir_id,
2627+
lcx.next_id(),
26282628
hir::ConstArgKind::Anon(lcx.arena.alloc(hir::AnonConst {
26292629
def_id,
26302630
hir_id,
@@ -2634,7 +2634,6 @@ impl<'hir> GenericArgsCtor<'hir> {
26342634
)
26352635
}
26362636
BoundConstness::Maybe(span) => {
2637-
let hir_id = lcx.next_id();
26382637
let span = lcx.lower_span(span);
26392638

26402639
let Some(host_param_id) = lcx.host_param_id else {
@@ -2646,19 +2645,20 @@ impl<'hir> GenericArgsCtor<'hir> {
26462645
};
26472646

26482647
let res = Res::Def(DefKind::ConstParam, host_param_id.to_def_id());
2648+
let path_seg_hir_id = lcx.next_id();
26492649
let path = lcx.arena.alloc(hir::Path {
26502650
span,
26512651
res,
26522652
segments: arena_vec![
26532653
lcx;
26542654
hir::PathSegment::new(
26552655
Ident { name: sym::host, span },
2656-
hir_id,
2656+
path_seg_hir_id,
26572657
res
26582658
)
26592659
],
26602660
});
2661-
(hir_id, hir::ConstArgKind::Path(hir::QPath::Resolved(None, path)))
2661+
(lcx.next_id(), hir::ConstArgKind::Path(hir::QPath::Resolved(None, path)))
26622662
}
26632663
};
26642664

compiler/rustc_hir/src/hir.rs

+2
Original file line numberDiff line numberDiff line change
@@ -3661,6 +3661,7 @@ pub enum Node<'hir> {
36613661
Variant(&'hir Variant<'hir>),
36623662
Field(&'hir FieldDef<'hir>),
36633663
AnonConst(&'hir AnonConst),
3664+
ConstArg(&'hir ConstArg<'hir>),
36643665
Expr(&'hir Expr<'hir>),
36653666
ExprField(&'hir ExprField<'hir>),
36663667
Stmt(&'hir Stmt<'hir>),
@@ -3721,6 +3722,7 @@ impl<'hir> Node<'hir> {
37213722
Node::PreciseCapturingNonLifetimeArg(a) => Some(a.ident),
37223723
Node::Param(..)
37233724
| Node::AnonConst(..)
3725+
| Node::ConstArg(..)
37243726
| Node::Expr(..)
37253727
| Node::Stmt(..)
37263728
| Node::Block(..)

compiler/rustc_hir_pretty/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ impl<'a> State<'a> {
8484
Node::ImplItem(a) => self.print_impl_item(a),
8585
Node::Variant(a) => self.print_variant(a),
8686
Node::AnonConst(a) => self.print_anon_const(a),
87+
Node::ConstArg(a) => self.print_const_arg(a),
8788
Node::Expr(a) => self.print_expr(a),
8889
Node::ExprField(a) => self.print_expr_field(a),
8990
Node::Stmt(a) => self.print_stmt(a),

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

+2
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,7 @@ impl<'hir> Map<'hir> {
916916
Node::Variant(variant) => variant.span,
917917
Node::Field(field) => field.span,
918918
Node::AnonConst(constant) => constant.span,
919+
Node::ConstArg(const_arg) => const_arg.span(),
919920
Node::Expr(expr) => expr.span,
920921
Node::ExprField(field) => field.span,
921922
Node::Stmt(stmt) => stmt.span,
@@ -1185,6 +1186,7 @@ fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
11851186
format!("{id} (field `{}` in {})", field.ident, path_str(field.def_id))
11861187
}
11871188
Node::AnonConst(_) => node_str("const"),
1189+
Node::ConstArg(_) => node_str("const"),
11881190
Node::Expr(_) => node_str("expr"),
11891191
Node::ExprField(_) => node_str("expr field"),
11901192
Node::Stmt(_) => node_str("stmt"),

compiler/rustc_middle/src/ty/consts.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,10 @@ impl<'tcx> Const<'tcx> {
222222
pub fn from_anon_const(tcx: TyCtxt<'tcx>, def: LocalDefId) -> Self {
223223
let body_id = match tcx.hir_node_by_def_id(def) {
224224
hir::Node::AnonConst(ac) => ac.body,
225-
_ => span_bug!(
225+
node => span_bug!(
226226
tcx.def_span(def.to_def_id()),
227-
"from_anon_const can only process anonymous constants"
227+
"from_anon_const can only process anonymous constants, not {:?}",
228+
node,
228229
),
229230
};
230231

0 commit comments

Comments
 (0)