Skip to content

Commit 25b0e84

Browse files
authored
Rollup merge of #123419 - petrochenkov:zeroindex, r=compiler-errors
rustc_index: Add a `ZERO` constant to index types It is commonly used.
2 parents 202509b + b40ea03 commit 25b0e84

File tree

42 files changed

+80
-90
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+80
-90
lines changed

compiler/rustc_ast_lowering/src/index.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_hir as hir;
33
use rustc_hir::def_id::{LocalDefId, LocalDefIdMap};
44
use rustc_hir::intravisit::Visitor;
55
use rustc_hir::*;
6-
use rustc_index::{Idx, IndexVec};
6+
use rustc_index::IndexVec;
77
use rustc_middle::span_bug;
88
use rustc_middle::ty::TyCtxt;
99
use rustc_span::{Span, DUMMY_SP};
@@ -31,7 +31,7 @@ pub(super) fn index_hir<'hir>(
3131
bodies: &SortedMap<ItemLocalId, &'hir Body<'hir>>,
3232
num_nodes: usize,
3333
) -> (IndexVec<ItemLocalId, ParentedNode<'hir>>, LocalDefIdMap<ItemLocalId>) {
34-
let zero_id = ItemLocalId::new(0);
34+
let zero_id = ItemLocalId::ZERO;
3535
let err_node = ParentedNode { parent: zero_id, node: Node::Err(item.span()) };
3636
let mut nodes = IndexVec::from_elem_n(err_node, num_nodes);
3737
// This node's parent should never be accessed: the owner's parent is computed by the

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_hir as hir;
1111
use rustc_hir::def::{DefKind, Res};
1212
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
1313
use rustc_hir::PredicateOrigin;
14-
use rustc_index::{Idx, IndexSlice, IndexVec};
14+
use rustc_index::{IndexSlice, IndexVec};
1515
use rustc_middle::span_bug;
1616
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
1717
use rustc_span::edit_distance::find_best_match_for_name;
@@ -563,7 +563,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
563563
let kind =
564564
this.lower_use_tree(use_tree, &prefix, id, vis_span, &mut ident, attrs);
565565
if let Some(attrs) = attrs {
566-
this.attrs.insert(hir::ItemLocalId::new(0), attrs);
566+
this.attrs.insert(hir::ItemLocalId::ZERO, attrs);
567567
}
568568

569569
let item = hir::Item {

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
157157
attrs: SortedMap::default(),
158158
children: Vec::default(),
159159
current_hir_id_owner: hir::CRATE_OWNER_ID,
160-
item_local_id_counter: hir::ItemLocalId::new(0),
160+
item_local_id_counter: hir::ItemLocalId::ZERO,
161161
node_id_to_local_id: Default::default(),
162162
trait_map: Default::default(),
163163

@@ -583,7 +583,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
583583
// and the caller to refer to some of the subdefinitions' nodes' `LocalDefId`s.
584584

585585
// Always allocate the first `HirId` for the owner itself.
586-
let _old = self.node_id_to_local_id.insert(owner, hir::ItemLocalId::new(0));
586+
let _old = self.node_id_to_local_id.insert(owner, hir::ItemLocalId::ZERO);
587587
debug_assert_eq!(_old, None);
588588

589589
let item = f(self);
@@ -677,7 +677,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
677677
v.insert(local_id);
678678
self.item_local_id_counter.increment_by(1);
679679

680-
assert_ne!(local_id, hir::ItemLocalId::new(0));
680+
assert_ne!(local_id, hir::ItemLocalId::ZERO);
681681
if let Some(def_id) = self.opt_local_def_id(ast_node_id) {
682682
self.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
683683
}
@@ -696,7 +696,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
696696
fn next_id(&mut self) -> hir::HirId {
697697
let owner = self.current_hir_id_owner;
698698
let local_id = self.item_local_id_counter;
699-
assert_ne!(local_id, hir::ItemLocalId::new(0));
699+
assert_ne!(local_id, hir::ItemLocalId::ZERO);
700700
self.item_local_id_counter.increment_by(1);
701701
hir::HirId { owner, local_id }
702702
}

compiler/rustc_borrowck/src/borrow_set.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl<'tcx> BorrowSet<'tcx> {
159159
}
160160

161161
pub(crate) fn indices(&self) -> impl Iterator<Item = BorrowIndex> {
162-
BorrowIndex::from_usize(0)..BorrowIndex::from_usize(self.len())
162+
BorrowIndex::ZERO..BorrowIndex::from_usize(self.len())
163163
}
164164

165165
pub(crate) fn iter_enumerated(&self) -> impl Iterator<Item = (BorrowIndex, &BorrowData<'tcx>)> {

compiler/rustc_codegen_cranelift/src/intrinsics/llvm_x86.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,7 @@ fn llvm_add_sub<'tcx>(
13931393

13941394
// c + carry -> c + first intermediate carry or borrow respectively
13951395
let int0 = crate::num::codegen_checked_int_binop(fx, bin_op, a, b);
1396-
let c = int0.value_field(fx, FieldIdx::new(0));
1396+
let c = int0.value_field(fx, FieldIdx::ZERO);
13971397
let cb0 = int0.value_field(fx, FieldIdx::new(1)).load_scalar(fx);
13981398

13991399
// c + carry -> c + second intermediate carry or borrow respectively

compiler/rustc_codegen_cranelift/src/vtable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub(crate) fn get_ptr_and_method_ref<'tcx>(
6161
if ty.is_dyn_star() {
6262
let inner_layout = fx.layout_of(arg.layout().ty.builtin_deref(true).unwrap().ty);
6363
let dyn_star = CPlace::for_ptr(Pointer::new(arg.load_scalar(fx)), inner_layout);
64-
let ptr = dyn_star.place_field(fx, FieldIdx::new(0)).to_ptr();
64+
let ptr = dyn_star.place_field(fx, FieldIdx::ZERO).to_ptr();
6565
let vtable =
6666
dyn_star.place_field(fx, FieldIdx::new(1)).to_cvalue(fx).load_scalar(fx);
6767
break 'block (ptr, vtable);

compiler/rustc_data_structures/src/graph/dominators/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,16 @@ fn dominators_impl<G: ControlFlowGraph>(graph: &G) -> Inner<G::Node> {
7272
IndexVec::with_capacity(graph.num_nodes());
7373

7474
let mut stack = vec![PreOrderFrame {
75-
pre_order_idx: PreorderIndex::new(0),
75+
pre_order_idx: PreorderIndex::ZERO,
7676
iter: graph.successors(graph.start_node()),
7777
}];
7878
let mut pre_order_to_real: IndexVec<PreorderIndex, G::Node> =
7979
IndexVec::with_capacity(graph.num_nodes());
8080
let mut real_to_pre_order: IndexVec<G::Node, Option<PreorderIndex>> =
8181
IndexVec::from_elem_n(None, graph.num_nodes());
8282
pre_order_to_real.push(graph.start_node());
83-
parent.push(PreorderIndex::new(0)); // the parent of the root node is the root for now.
84-
real_to_pre_order[graph.start_node()] = Some(PreorderIndex::new(0));
83+
parent.push(PreorderIndex::ZERO); // the parent of the root node is the root for now.
84+
real_to_pre_order[graph.start_node()] = Some(PreorderIndex::ZERO);
8585
let mut post_order_idx = 0;
8686

8787
// Traverse the graph, collecting a number of things:
@@ -111,7 +111,7 @@ fn dominators_impl<G: ControlFlowGraph>(graph: &G) -> Inner<G::Node> {
111111

112112
let reachable_vertices = pre_order_to_real.len();
113113

114-
let mut idom = IndexVec::from_elem_n(PreorderIndex::new(0), reachable_vertices);
114+
let mut idom = IndexVec::from_elem_n(PreorderIndex::ZERO, reachable_vertices);
115115
let mut semi = IndexVec::from_fn_n(std::convert::identity, reachable_vertices);
116116
let mut label = semi.clone();
117117
let mut bucket = IndexVec::from_elem_n(vec![], reachable_vertices);

compiler/rustc_hir/src/hir.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -846,17 +846,16 @@ pub struct OwnerNodes<'tcx> {
846846

847847
impl<'tcx> OwnerNodes<'tcx> {
848848
pub fn node(&self) -> OwnerNode<'tcx> {
849-
use rustc_index::Idx;
850849
// Indexing must ensure it is an OwnerNode.
851-
self.nodes[ItemLocalId::new(0)].node.as_owner().unwrap()
850+
self.nodes[ItemLocalId::ZERO].node.as_owner().unwrap()
852851
}
853852
}
854853

855854
impl fmt::Debug for OwnerNodes<'_> {
856855
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
857856
f.debug_struct("OwnerNodes")
858857
// Do not print all the pointers to all the nodes, as it would be unreadable.
859-
.field("node", &self.nodes[ItemLocalId::from_u32(0)])
858+
.field("node", &self.nodes[ItemLocalId::ZERO])
860859
.field(
861860
"parents",
862861
&self

compiler/rustc_hir/src/hir_id.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl Debug for OwnerId {
1717

1818
impl From<OwnerId> for HirId {
1919
fn from(owner: OwnerId) -> HirId {
20-
HirId { owner, local_id: ItemLocalId::from_u32(0) }
20+
HirId { owner, local_id: ItemLocalId::ZERO }
2121
}
2222
}
2323

@@ -110,7 +110,7 @@ impl HirId {
110110

111111
#[inline]
112112
pub fn make_owner(owner: LocalDefId) -> Self {
113-
Self { owner: OwnerId { def_id: owner }, local_id: ItemLocalId::from_u32(0) }
113+
Self { owner: OwnerId { def_id: owner }, local_id: ItemLocalId::ZERO }
114114
}
115115

116116
pub fn index(self) -> (usize, usize) {
@@ -172,6 +172,6 @@ unsafe impl StableOrd for ItemLocalId {
172172

173173
/// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_ID`.
174174
pub const CRATE_HIR_ID: HirId =
175-
HirId { owner: OwnerId { def_id: CRATE_DEF_ID }, local_id: ItemLocalId::from_u32(0) };
175+
HirId { owner: OwnerId { def_id: CRATE_DEF_ID }, local_id: ItemLocalId::ZERO };
176176

177177
pub const CRATE_OWNER_ID: OwnerId = OwnerId { def_id: CRATE_DEF_ID };

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ pub fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) {
899899
struct_span_code_err!(tcx.dcx(), sp, E0075, "SIMD vector cannot be empty").emit();
900900
return;
901901
}
902-
let e = fields[FieldIdx::from_u32(0)].ty(tcx, args);
902+
let e = fields[FieldIdx::ZERO].ty(tcx, args);
903903
if !fields.iter().all(|f| f.ty(tcx, args) == e) {
904904
struct_span_code_err!(tcx.dcx(), sp, E0076, "SIMD vector should be homogeneous")
905905
.with_span_label(sp, "SIMD elements must have the same type")

0 commit comments

Comments
 (0)