Skip to content

Commit 8b6a4d8

Browse files
committed
Make ConstArg be its own hir::Node with distinct HirId
1 parent 9da3bc0 commit 8b6a4d8

File tree

6 files changed

+30
-17
lines changed

6 files changed

+30
-17
lines changed

compiler/rustc_ast_lowering/src/index.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ 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| {
@@ -244,6 +245,15 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
244245
});
245246
}
246247

248+
fn visit_const_arg(&mut self, const_arg: &'hir ConstArg<'hir>) {
249+
// FIXME: use real span?
250+
self.insert(DUMMY_SP, const_arg.hir_id, Node::ConstArg(const_arg));
251+
252+
self.with_parent(const_arg.hir_id, |this| {
253+
intravisit::walk_const_arg(this, const_arg);
254+
});
255+
}
256+
247257
fn visit_expr(&mut self, expr: &'hir Expr<'hir>) {
248258
self.insert(expr.span, expr.hir_id, Node::Expr(expr));
249259

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11581158
None,
11591159
);
11601160
return GenericArg::Const(ConstArg {
1161-
hir_id: self.lower_node_id(ty.id),
1161+
hir_id: self.next_id(),
11621162
kind: ConstArgKind::Path(qpath),
11631163
is_desugared_from_effects: false,
11641164
});
@@ -1191,15 +1191,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11911191
None,
11921192
);
11931193
return ConstArg {
1194-
hir_id: self.lower_node_id(anon.id),
1194+
hir_id: self.next_id(),
11951195
kind: ConstArgKind::Path(qpath),
11961196
is_desugared_from_effects: false,
11971197
};
11981198
}
11991199

12001200
let lowered_anon = self.lower_anon_const(anon);
12011201
ConstArg {
1202-
hir_id: lowered_anon.hir_id,
1202+
hir_id: self.next_id(),
12031203
kind: ConstArgKind::Anon(lowered_anon),
12041204
is_desugared_from_effects: false,
12051205
}
@@ -2599,7 +2599,7 @@ impl<'hir> GenericArgsCtor<'hir> {
25992599
return;
26002600
}
26012601

2602-
let (hir_id, const_arg_kind) = match constness {
2602+
let const_arg_kind = match constness {
26032603
BoundConstness::Never => return,
26042604
BoundConstness::Always(span) => {
26052605
let id = lcx.next_node_id();
@@ -2620,18 +2620,14 @@ impl<'hir> GenericArgsCtor<'hir> {
26202620
);
26212621

26222622
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
2623-
(
2623+
hir::ConstArgKind::Anon(lcx.arena.alloc(hir::AnonConst {
2624+
def_id,
26242625
hir_id,
2625-
hir::ConstArgKind::Anon(lcx.arena.alloc(hir::AnonConst {
2626-
def_id,
2627-
hir_id,
2628-
body,
2629-
span,
2630-
})),
2631-
)
2626+
body,
2627+
span,
2628+
}))
26322629
}
26332630
BoundConstness::Maybe(span) => {
2634-
let hir_id = lcx.next_id();
26352631
let span = lcx.lower_span(span);
26362632

26372633
let Some(host_param_id) = lcx.host_param_id else {
@@ -2643,6 +2639,7 @@ impl<'hir> GenericArgsCtor<'hir> {
26432639
};
26442640

26452641
let res = Res::Def(DefKind::ConstParam, host_param_id.to_def_id());
2642+
let hir_id = lcx.next_id();
26462643
let path = lcx.arena.alloc(hir::Path {
26472644
span,
26482645
res,
@@ -2655,12 +2652,12 @@ impl<'hir> GenericArgsCtor<'hir> {
26552652
)
26562653
],
26572654
});
2658-
(hir_id, hir::ConstArgKind::Path(hir::QPath::Resolved(None, path)))
2655+
hir::ConstArgKind::Path(hir::QPath::Resolved(None, path))
26592656
}
26602657
};
26612658

26622659
self.args.push(hir::GenericArg::Const(hir::ConstArg {
2663-
hir_id,
2660+
hir_id: lcx.next_id(),
26642661
kind: const_arg_kind,
26652662
is_desugared_from_effects: true,
26662663
}))

compiler/rustc_hir/src/hir.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3688,6 +3688,7 @@ pub enum Node<'hir> {
36883688
Field(&'hir FieldDef<'hir>),
36893689
AnonConst(&'hir AnonConst),
36903690
ConstBlock(&'hir ConstBlock),
3691+
ConstArg(&'hir ConstArg<'hir>),
36913692
Expr(&'hir Expr<'hir>),
36923693
ExprField(&'hir ExprField<'hir>),
36933694
Stmt(&'hir Stmt<'hir>),
@@ -3749,6 +3750,7 @@ impl<'hir> Node<'hir> {
37493750
Node::Param(..)
37503751
| Node::AnonConst(..)
37513752
| Node::ConstBlock(..)
3753+
| Node::ConstArg(..)
37523754
| Node::Expr(..)
37533755
| Node::Stmt(..)
37543756
| Node::Block(..)

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ impl<'a> State<'a> {
8787
Node::Variant(a) => self.print_variant(a),
8888
Node::AnonConst(a) => self.print_anon_const(a),
8989
Node::ConstBlock(a) => self.print_inline_const(a),
90+
Node::ConstArg(a) => self.print_const_arg(a),
9091
Node::Expr(a) => self.print_expr(a),
9192
Node::ExprField(a) => self.print_expr_field(a),
9293
Node::Stmt(a) => self.print_stmt(a),

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,7 @@ impl<'hir> Map<'hir> {
894894
Node::Field(field) => field.span,
895895
Node::AnonConst(constant) => constant.span,
896896
Node::ConstBlock(constant) => self.body(constant.body).value.span,
897+
Node::ConstArg(const_arg) => const_arg.span(),
897898
Node::Expr(expr) => expr.span,
898899
Node::ExprField(field) => field.span,
899900
Node::Stmt(stmt) => stmt.span,
@@ -1164,6 +1165,7 @@ fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
11641165
}
11651166
Node::AnonConst(_) => node_str("const"),
11661167
Node::ConstBlock(_) => node_str("const"),
1168+
Node::ConstArg(_) => node_str("const"),
11671169
Node::Expr(_) => node_str("expr"),
11681170
Node::ExprField(_) => node_str("expr field"),
11691171
Node::Stmt(_) => node_str("stmt"),

compiler/rustc_middle/src/ty/consts.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,10 @@ impl<'tcx> Const<'tcx> {
185185
pub fn from_anon_const(tcx: TyCtxt<'tcx>, def: LocalDefId) -> Self {
186186
let body_id = match tcx.hir_node_by_def_id(def) {
187187
hir::Node::AnonConst(ac) => ac.body,
188-
_ => span_bug!(
188+
node => span_bug!(
189189
tcx.def_span(def.to_def_id()),
190-
"from_anon_const can only process anonymous constants"
190+
"from_anon_const can only process anonymous constants, not {:?}",
191+
node,
191192
),
192193
};
193194

0 commit comments

Comments
 (0)