Skip to content

Commit dd1e585

Browse files
committed
Make ConstArg be its own hir::Node with distinct HirId
1 parent d4ee73d commit dd1e585

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
@@ -1156,7 +1156,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11561156
None,
11571157
);
11581158
return GenericArg::Const(ConstArg {
1159-
hir_id: self.lower_node_id(ty.id),
1159+
hir_id: self.next_id(),
11601160
kind: ConstArgKind::Path(qpath),
11611161
is_desugared_from_effects: false,
11621162
});
@@ -1189,15 +1189,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11891189
None,
11901190
);
11911191
return ConstArg {
1192-
hir_id: self.lower_node_id(anon.id),
1192+
hir_id: self.next_id(),
11931193
kind: ConstArgKind::Path(qpath),
11941194
is_desugared_from_effects: false,
11951195
};
11961196
}
11971197

11981198
let lowered_anon = self.lower_anon_const(anon);
11991199
ConstArg {
1200-
hir_id: lowered_anon.hir_id,
1200+
hir_id: self.next_id(),
12011201
kind: ConstArgKind::Anon(lowered_anon),
12021202
is_desugared_from_effects: false,
12031203
}
@@ -2597,7 +2597,7 @@ impl<'hir> GenericArgsCtor<'hir> {
25972597
return;
25982598
}
25992599

2600-
let (hir_id, const_arg_kind) = match constness {
2600+
let const_arg_kind = match constness {
26012601
BoundConstness::Never => return,
26022602
BoundConstness::Always(span) => {
26032603
let id = lcx.next_node_id();
@@ -2618,18 +2618,14 @@ impl<'hir> GenericArgsCtor<'hir> {
26182618
);
26192619

26202620
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
2621-
(
2621+
hir::ConstArgKind::Anon(lcx.arena.alloc(hir::AnonConst {
2622+
def_id,
26222623
hir_id,
2623-
hir::ConstArgKind::Anon(lcx.arena.alloc(hir::AnonConst {
2624-
def_id,
2625-
hir_id,
2626-
body,
2627-
span,
2628-
})),
2629-
)
2624+
body,
2625+
span,
2626+
}))
26302627
}
26312628
BoundConstness::Maybe(span) => {
2632-
let hir_id = lcx.next_id();
26332629
let span = lcx.lower_span(span);
26342630

26352631
let Some(host_param_id) = lcx.host_param_id else {
@@ -2641,6 +2637,7 @@ impl<'hir> GenericArgsCtor<'hir> {
26412637
};
26422638

26432639
let res = Res::Def(DefKind::ConstParam, host_param_id.to_def_id());
2640+
let hir_id = lcx.next_id();
26442641
let path = lcx.arena.alloc(hir::Path {
26452642
span,
26462643
res,
@@ -2653,12 +2650,12 @@ impl<'hir> GenericArgsCtor<'hir> {
26532650
)
26542651
],
26552652
});
2656-
(hir_id, hir::ConstArgKind::Path(hir::QPath::Resolved(None, path)))
2653+
hir::ConstArgKind::Path(hir::QPath::Resolved(None, path))
26572654
}
26582655
};
26592656

26602657
self.args.push(hir::GenericArg::Const(hir::ConstArg {
2661-
hir_id,
2658+
hir_id: lcx.next_id(),
26622659
kind: const_arg_kind,
26632660
is_desugared_from_effects: true,
26642661
}))

compiler/rustc_hir/src/hir.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3673,6 +3673,7 @@ pub enum Node<'hir> {
36733673
Field(&'hir FieldDef<'hir>),
36743674
AnonConst(&'hir AnonConst),
36753675
ConstBlock(&'hir ConstBlock),
3676+
ConstArg(&'hir ConstArg<'hir>),
36763677
Expr(&'hir Expr<'hir>),
36773678
ExprField(&'hir ExprField<'hir>),
36783679
Stmt(&'hir Stmt<'hir>),
@@ -3734,6 +3735,7 @@ impl<'hir> Node<'hir> {
37343735
Node::Param(..)
37353736
| Node::AnonConst(..)
37363737
| Node::ConstBlock(..)
3738+
| Node::ConstArg(..)
37373739
| Node::Expr(..)
37383740
| Node::Stmt(..)
37393741
| Node::Block(..)

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ impl<'a> State<'a> {
8585
Node::Variant(a) => self.print_variant(a),
8686
Node::AnonConst(a) => self.print_anon_const(a),
8787
Node::ConstBlock(a) => self.print_inline_const(a),
88+
Node::ConstArg(a) => self.print_const_arg(a),
8889
Node::Expr(a) => self.print_expr(a),
8990
Node::ExprField(a) => self.print_expr_field(a),
9091
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)