Skip to content

Commit 98df547

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

File tree

6 files changed

+30
-17
lines changed

6 files changed

+30
-17
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

+12-15
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
});
@@ -1194,15 +1194,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11941194
None,
11951195
);
11961196
return ConstArg {
1197-
hir_id: self.lower_node_id(anon.id),
1197+
hir_id: self.next_id(),
11981198
kind: ConstArgKind::Path(qpath),
11991199
is_desugared_from_effects: false,
12001200
};
12011201
}
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
}
@@ -2602,7 +2602,7 @@ impl<'hir> GenericArgsCtor<'hir> {
26022602
return;
26032603
}
26042604

2605-
let (hir_id, const_arg_kind) = match constness {
2605+
let const_arg_kind = match constness {
26062606
BoundConstness::Never => return,
26072607
BoundConstness::Always(span) => {
26082608
let id = lcx.next_node_id();
@@ -2623,18 +2623,14 @@ impl<'hir> GenericArgsCtor<'hir> {
26232623
);
26242624

26252625
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
2626-
(
2626+
hir::ConstArgKind::Anon(lcx.arena.alloc(hir::AnonConst {
2627+
def_id,
26272628
hir_id,
2628-
hir::ConstArgKind::Anon(lcx.arena.alloc(hir::AnonConst {
2629-
def_id,
2630-
hir_id,
2631-
body,
2632-
span,
2633-
})),
2634-
)
2629+
body,
2630+
span,
2631+
}))
26352632
}
26362633
BoundConstness::Maybe(span) => {
2637-
let hir_id = lcx.next_id();
26382634
let span = lcx.lower_span(span);
26392635

26402636
let Some(host_param_id) = lcx.host_param_id else {
@@ -2646,6 +2642,7 @@ impl<'hir> GenericArgsCtor<'hir> {
26462642
};
26472643

26482644
let res = Res::Def(DefKind::ConstParam, host_param_id.to_def_id());
2645+
let hir_id = lcx.next_id();
26492646
let path = lcx.arena.alloc(hir::Path {
26502647
span,
26512648
res,
@@ -2658,12 +2655,12 @@ impl<'hir> GenericArgsCtor<'hir> {
26582655
)
26592656
],
26602657
});
2661-
(hir_id, hir::ConstArgKind::Path(hir::QPath::Resolved(None, path)))
2658+
hir::ConstArgKind::Path(hir::QPath::Resolved(None, path))
26622659
}
26632660
};
26642661

26652662
self.args.push(hir::GenericArg::Const(hir::ConstArg {
2666-
hir_id,
2663+
hir_id: lcx.next_id(),
26672664
kind: const_arg_kind,
26682665
is_desugared_from_effects: true,
26692666
}))

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)