Skip to content

Commit

Permalink
Make ConstArg be its own hir::Node with distinct HirId
Browse files Browse the repository at this point in the history
  • Loading branch information
camelid committed Jun 4, 2024
1 parent 9810f40 commit 98df547
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
10 changes: 10 additions & 0 deletions compiler/rustc_ast_lowering/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,23 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
}

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

self.with_parent(constant.hir_id, |this| {
intravisit::walk_anon_const(this, constant);
});
}

fn visit_const_arg(&mut self, const_arg: &'hir ConstArg<'hir>) {
// FIXME: use real span?
self.insert(DUMMY_SP, const_arg.hir_id, Node::ConstArg(const_arg));

self.with_parent(const_arg.hir_id, |this| {
intravisit::walk_const_arg(this, const_arg);
});
}

fn visit_expr(&mut self, expr: &'hir Expr<'hir>) {
self.insert(expr.span, expr.hir_id, Node::Expr(expr));

Expand Down
27 changes: 12 additions & 15 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
None,
);
return GenericArg::Const(ConstArg {
hir_id: self.lower_node_id(ty.id),
hir_id: self.next_id(),
kind: ConstArgKind::Path(qpath),
is_desugared_from_effects: false,
});
Expand Down Expand Up @@ -1194,15 +1194,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
None,
);
return ConstArg {
hir_id: self.lower_node_id(anon.id),
hir_id: self.next_id(),
kind: ConstArgKind::Path(qpath),
is_desugared_from_effects: false,
};
}

let lowered_anon = self.lower_anon_const(anon);
ConstArg {
hir_id: lowered_anon.hir_id,
hir_id: self.next_id(),
kind: ConstArgKind::Anon(lowered_anon),
is_desugared_from_effects: false,
}
Expand Down Expand Up @@ -2602,7 +2602,7 @@ impl<'hir> GenericArgsCtor<'hir> {
return;
}

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

lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
(
hir::ConstArgKind::Anon(lcx.arena.alloc(hir::AnonConst {
def_id,
hir_id,
hir::ConstArgKind::Anon(lcx.arena.alloc(hir::AnonConst {
def_id,
hir_id,
body,
span,
})),
)
body,
span,
}))
}
BoundConstness::Maybe(span) => {
let hir_id = lcx.next_id();
let span = lcx.lower_span(span);

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

let res = Res::Def(DefKind::ConstParam, host_param_id.to_def_id());
let hir_id = lcx.next_id();
let path = lcx.arena.alloc(hir::Path {
span,
res,
Expand All @@ -2658,12 +2655,12 @@ impl<'hir> GenericArgsCtor<'hir> {
)
],
});
(hir_id, hir::ConstArgKind::Path(hir::QPath::Resolved(None, path)))
hir::ConstArgKind::Path(hir::QPath::Resolved(None, path))
}
};

self.args.push(hir::GenericArg::Const(hir::ConstArg {
hir_id,
hir_id: lcx.next_id(),
kind: const_arg_kind,
is_desugared_from_effects: true,
}))
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3661,6 +3661,7 @@ pub enum Node<'hir> {
Variant(&'hir Variant<'hir>),
Field(&'hir FieldDef<'hir>),
AnonConst(&'hir AnonConst),
ConstArg(&'hir ConstArg<'hir>),
Expr(&'hir Expr<'hir>),
ExprField(&'hir ExprField<'hir>),
Stmt(&'hir Stmt<'hir>),
Expand Down Expand Up @@ -3721,6 +3722,7 @@ impl<'hir> Node<'hir> {
Node::PreciseCapturingNonLifetimeArg(a) => Some(a.ident),
Node::Param(..)
| Node::AnonConst(..)
| Node::ConstArg(..)
| Node::Expr(..)
| Node::Stmt(..)
| Node::Block(..)
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl<'a> State<'a> {
Node::ImplItem(a) => self.print_impl_item(a),
Node::Variant(a) => self.print_variant(a),
Node::AnonConst(a) => self.print_anon_const(a),
Node::ConstArg(a) => self.print_const_arg(a),
Node::Expr(a) => self.print_expr(a),
Node::ExprField(a) => self.print_expr_field(a),
Node::Stmt(a) => self.print_stmt(a),
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,7 @@ impl<'hir> Map<'hir> {
Node::Variant(variant) => variant.span,
Node::Field(field) => field.span,
Node::AnonConst(constant) => constant.span,
Node::ConstArg(const_arg) => const_arg.span(),
Node::Expr(expr) => expr.span,
Node::ExprField(field) => field.span,
Node::Stmt(stmt) => stmt.span,
Expand Down Expand Up @@ -1185,6 +1186,7 @@ fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
format!("{id} (field `{}` in {})", field.ident, path_str(field.def_id))
}
Node::AnonConst(_) => node_str("const"),
Node::ConstArg(_) => node_str("const"),
Node::Expr(_) => node_str("expr"),
Node::ExprField(_) => node_str("expr field"),
Node::Stmt(_) => node_str("stmt"),
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_middle/src/ty/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,10 @@ impl<'tcx> Const<'tcx> {
pub fn from_anon_const(tcx: TyCtxt<'tcx>, def: LocalDefId) -> Self {
let body_id = match tcx.hir_node_by_def_id(def) {
hir::Node::AnonConst(ac) => ac.body,
_ => span_bug!(
node => span_bug!(
tcx.def_span(def.to_def_id()),
"from_anon_const can only process anonymous constants"
"from_anon_const can only process anonymous constants, not {:?}",
node,
),
};

Expand Down

0 comments on commit 98df547

Please sign in to comment.