Skip to content

Commit e62b34b

Browse files
committed
Auto merge of #120486 - reitermarkus:use-generic-nonzero, r=dtolnay
Use generic `NonZero` internally. Tracking issue: rust-lang/rust#120257
2 parents 8e81bf1 + fc4253f commit e62b34b

File tree

8 files changed

+20
-20
lines changed

8 files changed

+20
-20
lines changed

src/bin/miri.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(generic_nonzero)]
12
#![feature(rustc_private, stmt_expr_attributes)]
23
#![allow(
34
clippy::manual_range_contains,
@@ -17,7 +18,7 @@ extern crate rustc_middle;
1718
extern crate rustc_session;
1819

1920
use std::env::{self, VarError};
20-
use std::num::NonZeroU64;
21+
use std::num::NonZero;
2122
use std::path::PathBuf;
2223
use std::str::FromStr;
2324

@@ -528,7 +529,7 @@ fn main() {
528529
}
529530
}
530531
} else if let Some(param) = arg.strip_prefix("-Zmiri-track-alloc-id=") {
531-
let ids: Vec<miri::AllocId> = match parse_comma_list::<NonZeroU64>(param) {
532+
let ids: Vec<miri::AllocId> = match parse_comma_list::<NonZero<u64>>(param) {
532533
Ok(ids) => ids.into_iter().map(miri::AllocId).collect(),
533534
Err(err) =>
534535
show_error!(

src/borrow_tracker/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::cell::RefCell;
22
use std::fmt;
3-
use std::num::NonZeroU64;
3+
use std::num::NonZero;
44

55
use log::trace;
66
use smallvec::SmallVec;
@@ -13,22 +13,22 @@ use crate::*;
1313
pub mod stacked_borrows;
1414
pub mod tree_borrows;
1515

16-
pub type CallId = NonZeroU64;
16+
pub type CallId = NonZero<u64>;
1717

1818
/// Tracking pointer provenance
1919
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
20-
pub struct BorTag(NonZeroU64);
20+
pub struct BorTag(NonZero<u64>);
2121

2222
impl BorTag {
2323
pub fn new(i: u64) -> Option<Self> {
24-
NonZeroU64::new(i).map(BorTag)
24+
NonZero::new(i).map(BorTag)
2525
}
2626

2727
pub fn get(&self) -> u64 {
2828
self.0.get()
2929
}
3030

31-
pub fn inner(&self) -> NonZeroU64 {
31+
pub fn inner(&self) -> NonZero<u64> {
3232
self.0
3333
}
3434

@@ -184,7 +184,7 @@ impl GlobalStateInner {
184184
borrow_tracker_method,
185185
next_ptr_tag: BorTag::one(),
186186
base_ptr_tags: FxHashMap::default(),
187-
next_call_id: NonZeroU64::new(1).unwrap(),
187+
next_call_id: NonZero::new(1).unwrap(),
188188
protected_tags: FxHashMap::default(),
189189
tracked_pointer_tags,
190190
tracked_call_ids,
@@ -206,7 +206,7 @@ impl GlobalStateInner {
206206
if self.tracked_call_ids.contains(&call_id) {
207207
machine.emit_diagnostic(NonHaltingDiagnostic::CreatedCallId(call_id));
208208
}
209-
self.next_call_id = NonZeroU64::new(call_id.get() + 1).unwrap();
209+
self.next_call_id = NonZero::new(call_id.get() + 1).unwrap();
210210
FrameState { call_id, protected_tags: SmallVec::new() }
211211
}
212212

src/concurrency/init_once.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::collections::VecDeque;
2-
use std::num::NonZeroU32;
32

43
use rustc_index::Idx;
54
use rustc_middle::ty::layout::TyAndLayout;

src/concurrency/sync.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::collections::{hash_map::Entry, VecDeque};
2-
use std::num::NonZeroU32;
32
use std::ops::Not;
43

54
use log::trace;
@@ -26,12 +25,12 @@ macro_rules! declare_id {
2625
/// 0 is used to indicate that the id was not yet assigned and,
2726
/// therefore, is not a valid identifier.
2827
#[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
29-
pub struct $name(NonZeroU32);
28+
pub struct $name(std::num::NonZero<u32>);
3029

3130
impl SyncId for $name {
3231
// Panics if `id == 0`.
3332
fn from_u32(id: u32) -> Self {
34-
Self(NonZeroU32::new(id).unwrap())
33+
Self(std::num::NonZero::new(id).unwrap())
3534
}
3635
fn to_u32(&self) -> u32 {
3736
self.0.get()
@@ -44,11 +43,11 @@ macro_rules! declare_id {
4443
// therefore, need to shift by one when converting from an index
4544
// into a vector.
4645
let shifted_idx = u32::try_from(idx).unwrap().checked_add(1).unwrap();
47-
$name(NonZeroU32::new(shifted_idx).unwrap())
46+
$name(std::num::NonZero::new(shifted_idx).unwrap())
4847
}
4948
fn index(self) -> usize {
5049
// See the comment in `Self::new`.
51-
// (This cannot underflow because self is NonZeroU32.)
50+
// (This cannot underflow because `self.0` is `NonZero<u32>`.)
5251
usize::try_from(self.0.get() - 1).unwrap()
5352
}
5453
}

src/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::fmt::{self, Write};
2-
use std::num::NonZeroU64;
2+
use std::num::NonZero;
33

44
use log::trace;
55

@@ -115,7 +115,7 @@ pub enum NonHaltingDiagnostic {
115115
/// (new_tag, new_perm, (alloc_id, base_offset, orig_tag))
116116
///
117117
/// new_perm is `None` for base tags.
118-
CreatedPointerTag(NonZeroU64, Option<String>, Option<(AllocId, AllocRange, ProvenanceExtra)>),
118+
CreatedPointerTag(NonZero<u64>, Option<String>, Option<(AllocId, AllocRange, ProvenanceExtra)>),
119119
/// This `Item` was popped from the borrow stack. The string explains the reason.
120120
PoppedPointerTag(Item, String),
121121
CreatedCallId(CallId),

src/helpers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::cmp;
22
use std::iter;
3-
use std::num::NonZeroUsize;
3+
use std::num::NonZero;
44
use std::time::Duration;
55

66
use log::trace;
@@ -574,7 +574,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
574574
fn visit_union(
575575
&mut self,
576576
_v: &MPlaceTy<'tcx, Provenance>,
577-
_fields: NonZeroUsize,
577+
_fields: NonZero<usize>,
578578
) -> InterpResult<'tcx> {
579579
bug!("we should have already handled unions in `visit_value`")
580580
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![feature(rustc_private)]
22
#![feature(cell_update)]
33
#![feature(float_gamma)]
4+
#![feature(generic_nonzero)]
45
#![feature(map_try_insert)]
56
#![feature(never_type)]
67
#![feature(try_blocks)]

src/shims/foreign_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ trait EvalContextExtPriv<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
477477
let [id, show_unnamed] = this.check_shim(abi, Abi::Rust, link_name, args)?;
478478
let id = this.read_scalar(id)?.to_u64()?;
479479
let show_unnamed = this.read_scalar(show_unnamed)?.to_bool()?;
480-
if let Some(id) = std::num::NonZeroU64::new(id) {
480+
if let Some(id) = std::num::NonZero::new(id) {
481481
this.print_borrow_state(AllocId(id), show_unnamed)?;
482482
}
483483
}

0 commit comments

Comments
 (0)