Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 5a27782

Browse files
committed
use newtype_index for abstract_const::NodeId
1 parent f24d532 commit 5a27782

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

compiler/rustc_middle/src/mir/abstract_const.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22
use crate::mir;
33
use crate::ty;
44

5-
/// An index into an `AbstractConst`.
6-
pub type NodeId = usize;
5+
rustc_index::newtype_index! {
6+
/// An index into an `AbstractConst`.
7+
pub struct NodeId {
8+
derive [HashStable]
9+
DEBUG_FORMAT = "n{}",
10+
}
11+
}
712

813
/// A node of an `AbstractConst`.
914
#[derive(Debug, Clone, Copy, PartialEq, Eq, HashStable)]

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,10 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
9595
/// and should not leak any information about desugarings.
9696
#[derive(Clone, Copy)]
9797
pub struct AbstractConst<'tcx> {
98-
pub inner: &'tcx [Node<'tcx>],
99-
pub substs: SubstsRef<'tcx>,
98+
// FIXME: Consider adding something like `IndexSlice`
99+
// and use this here.
100+
inner: &'tcx [Node<'tcx>],
101+
substs: SubstsRef<'tcx>,
100102
}
101103

102104
impl AbstractConst<'tcx> {
@@ -117,7 +119,7 @@ impl AbstractConst<'tcx> {
117119

118120
#[inline]
119121
pub fn subtree(self, node: NodeId) -> AbstractConst<'tcx> {
120-
AbstractConst { inner: &self.inner[..=node], substs: self.substs }
122+
AbstractConst { inner: &self.inner[..=node.index()], substs: self.substs }
121123
}
122124

123125
#[inline]
@@ -129,7 +131,7 @@ impl AbstractConst<'tcx> {
129131
struct AbstractConstBuilder<'a, 'tcx> {
130132
tcx: TyCtxt<'tcx>,
131133
body: &'a mir::Body<'tcx>,
132-
nodes: Vec<Node<'tcx>>,
134+
nodes: IndexVec<NodeId, Node<'tcx>>,
133135
locals: IndexVec<mir::Local, NodeId>,
134136
checked_op_locals: BitSet<mir::Local>,
135137
}
@@ -143,18 +145,12 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
143145
Some(AbstractConstBuilder {
144146
tcx,
145147
body,
146-
nodes: vec![],
148+
nodes: IndexVec::new(),
147149
locals: IndexVec::from_elem(NodeId::MAX, &body.local_decls),
148150
checked_op_locals: BitSet::new_empty(body.local_decls.len()),
149151
})
150152
}
151153

152-
fn add_node(&mut self, n: Node<'tcx>) -> NodeId {
153-
let len = self.nodes.len();
154-
self.nodes.push(n);
155-
len
156-
}
157-
158154
fn operand_to_node(&mut self, op: &mir::Operand<'tcx>) -> Option<NodeId> {
159155
debug!("operand_to_node: op={:?}", op);
160156
const ZERO_FIELD: mir::Field = mir::Field::from_usize(0);
@@ -174,7 +170,7 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
174170
None
175171
}
176172
}
177-
mir::Operand::Constant(ct) => Some(self.add_node(Node::Leaf(ct.literal))),
173+
mir::Operand::Constant(ct) => Some(self.nodes.push(Node::Leaf(ct.literal))),
178174
}
179175
}
180176

@@ -199,15 +195,15 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
199195
Rvalue::BinaryOp(op, ref lhs, ref rhs) if Self::check_binop(op) => {
200196
let lhs = self.operand_to_node(lhs)?;
201197
let rhs = self.operand_to_node(rhs)?;
202-
self.locals[local] = self.add_node(Node::Binop(op, lhs, rhs));
198+
self.locals[local] = self.nodes.push(Node::Binop(op, lhs, rhs));
203199
if op.is_checkable() {
204200
bug!("unexpected unchecked checkable binary operation");
205201
}
206202
}
207203
Rvalue::CheckedBinaryOp(op, ref lhs, ref rhs) if Self::check_binop(op) => {
208204
let lhs = self.operand_to_node(lhs)?;
209205
let rhs = self.operand_to_node(rhs)?;
210-
self.locals[local] = self.add_node(Node::Binop(op, lhs, rhs));
206+
self.locals[local] = self.nodes.push(Node::Binop(op, lhs, rhs));
211207
self.checked_op_locals.insert(local);
212208
}
213209
_ => return None,

0 commit comments

Comments
 (0)