From 3a0aae44209d4c35f8369b10af98b8a2c9501090 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Fri, 2 Feb 2024 14:47:59 +0000 Subject: [PATCH 1/4] Support safe intrinsics with fallback bodies Turn `is_val_statically_known` into such an intrinsic to demonstrate. It is perfectly safe to call after all. --- tests/pass/intrinsics.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/pass/intrinsics.rs b/tests/pass/intrinsics.rs index 8e46bd7ad4..0dda5aadce 100644 --- a/tests/pass/intrinsics.rs +++ b/tests/pass/intrinsics.rs @@ -37,7 +37,7 @@ fn main() { let mut saw_false = false; for _ in 0..50 { - if unsafe { intrinsics::is_val_statically_known(0) } { + if intrinsics::is_val_statically_known(0) { saw_true = true; } else { saw_false = true; From 3ce0218930fabbe5da88f454030ef477f207837f Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Mon, 29 Jan 2024 23:59:09 +0100 Subject: [PATCH 2/4] Use generic `NonZero` internally. --- src/bin/miri.rs | 5 +++-- src/borrow_tracker/mod.rs | 14 +++++++------- src/concurrency/init_once.rs | 1 - src/concurrency/sync.rs | 9 ++++----- src/diagnostics.rs | 4 ++-- src/helpers.rs | 4 ++-- src/lib.rs | 1 + src/shims/foreign_items.rs | 2 +- 8 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/bin/miri.rs b/src/bin/miri.rs index de7a4b79d2..095ba17367 100644 --- a/src/bin/miri.rs +++ b/src/bin/miri.rs @@ -1,3 +1,4 @@ +#![feature(generic_nonzero)] #![feature(rustc_private, stmt_expr_attributes)] #![allow( clippy::manual_range_contains, @@ -17,7 +18,7 @@ extern crate rustc_middle; extern crate rustc_session; use std::env::{self, VarError}; -use std::num::NonZeroU64; +use std::num::NonZero; use std::path::PathBuf; use std::str::FromStr; @@ -528,7 +529,7 @@ fn main() { } } } else if let Some(param) = arg.strip_prefix("-Zmiri-track-alloc-id=") { - let ids: Vec = match parse_comma_list::(param) { + let ids: Vec = match parse_comma_list::>(param) { Ok(ids) => ids.into_iter().map(miri::AllocId).collect(), Err(err) => show_error!( diff --git a/src/borrow_tracker/mod.rs b/src/borrow_tracker/mod.rs index 46f96a715f..4424595ea1 100644 --- a/src/borrow_tracker/mod.rs +++ b/src/borrow_tracker/mod.rs @@ -1,6 +1,6 @@ use std::cell::RefCell; use std::fmt; -use std::num::NonZeroU64; +use std::num::NonZero; use log::trace; use smallvec::SmallVec; @@ -13,22 +13,22 @@ use crate::*; pub mod stacked_borrows; pub mod tree_borrows; -pub type CallId = NonZeroU64; +pub type CallId = NonZero; /// Tracking pointer provenance #[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] -pub struct BorTag(NonZeroU64); +pub struct BorTag(NonZero); impl BorTag { pub fn new(i: u64) -> Option { - NonZeroU64::new(i).map(BorTag) + NonZero::::new(i).map(BorTag) } pub fn get(&self) -> u64 { self.0.get() } - pub fn inner(&self) -> NonZeroU64 { + pub fn inner(&self) -> NonZero { self.0 } @@ -184,7 +184,7 @@ impl GlobalStateInner { borrow_tracker_method, next_ptr_tag: BorTag::one(), base_ptr_tags: FxHashMap::default(), - next_call_id: NonZeroU64::new(1).unwrap(), + next_call_id: NonZero::::new(1).unwrap(), protected_tags: FxHashMap::default(), tracked_pointer_tags, tracked_call_ids, @@ -206,7 +206,7 @@ impl GlobalStateInner { if self.tracked_call_ids.contains(&call_id) { machine.emit_diagnostic(NonHaltingDiagnostic::CreatedCallId(call_id)); } - self.next_call_id = NonZeroU64::new(call_id.get() + 1).unwrap(); + self.next_call_id = NonZero::::new(call_id.get() + 1).unwrap(); FrameState { call_id, protected_tags: SmallVec::new() } } diff --git a/src/concurrency/init_once.rs b/src/concurrency/init_once.rs index 9a848d5034..35dcfecbbe 100644 --- a/src/concurrency/init_once.rs +++ b/src/concurrency/init_once.rs @@ -1,5 +1,4 @@ use std::collections::VecDeque; -use std::num::NonZeroU32; use rustc_index::Idx; use rustc_middle::ty::layout::TyAndLayout; diff --git a/src/concurrency/sync.rs b/src/concurrency/sync.rs index b288b69e0c..c3130c8a8f 100644 --- a/src/concurrency/sync.rs +++ b/src/concurrency/sync.rs @@ -1,5 +1,4 @@ use std::collections::{hash_map::Entry, VecDeque}; -use std::num::NonZeroU32; use std::ops::Not; use log::trace; @@ -26,12 +25,12 @@ macro_rules! declare_id { /// 0 is used to indicate that the id was not yet assigned and, /// therefore, is not a valid identifier. #[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)] - pub struct $name(NonZeroU32); + pub struct $name(std::num::NonZero); impl SyncId for $name { // Panics if `id == 0`. fn from_u32(id: u32) -> Self { - Self(NonZeroU32::new(id).unwrap()) + Self(std::num::NonZero::::new(id).unwrap()) } fn to_u32(&self) -> u32 { self.0.get() @@ -44,11 +43,11 @@ macro_rules! declare_id { // therefore, need to shift by one when converting from an index // into a vector. let shifted_idx = u32::try_from(idx).unwrap().checked_add(1).unwrap(); - $name(NonZeroU32::new(shifted_idx).unwrap()) + $name(std::num::NonZero::::new(shifted_idx).unwrap()) } fn index(self) -> usize { // See the comment in `Self::new`. - // (This cannot underflow because self is NonZeroU32.) + // (This cannot underflow because `self.0` is `NonZero`.) usize::try_from(self.0.get() - 1).unwrap() } } diff --git a/src/diagnostics.rs b/src/diagnostics.rs index 7f91af59d5..19b29a4181 100644 --- a/src/diagnostics.rs +++ b/src/diagnostics.rs @@ -1,5 +1,5 @@ use std::fmt::{self, Write}; -use std::num::NonZeroU64; +use std::num::NonZero; use log::trace; @@ -115,7 +115,7 @@ pub enum NonHaltingDiagnostic { /// (new_tag, new_perm, (alloc_id, base_offset, orig_tag)) /// /// new_perm is `None` for base tags. - CreatedPointerTag(NonZeroU64, Option, Option<(AllocId, AllocRange, ProvenanceExtra)>), + CreatedPointerTag(NonZero, Option, Option<(AllocId, AllocRange, ProvenanceExtra)>), /// This `Item` was popped from the borrow stack. The string explains the reason. PoppedPointerTag(Item, String), CreatedCallId(CallId), diff --git a/src/helpers.rs b/src/helpers.rs index d6b1e13580..3cee4df588 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1,6 +1,6 @@ use std::cmp; use std::iter; -use std::num::NonZeroUsize; +use std::num::NonZero; use std::time::Duration; use log::trace; @@ -574,7 +574,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { fn visit_union( &mut self, _v: &MPlaceTy<'tcx, Provenance>, - _fields: NonZeroUsize, + _fields: NonZero, ) -> InterpResult<'tcx> { bug!("we should have already handled unions in `visit_value`") } diff --git a/src/lib.rs b/src/lib.rs index 5a2bb84f3e..305c71fb0f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ #![feature(rustc_private)] #![feature(cell_update)] #![feature(float_gamma)] +#![feature(generic_nonzero)] #![feature(map_try_insert)] #![feature(never_type)] #![feature(try_blocks)] diff --git a/src/shims/foreign_items.rs b/src/shims/foreign_items.rs index 25654248db..f0e6e0374d 100644 --- a/src/shims/foreign_items.rs +++ b/src/shims/foreign_items.rs @@ -477,7 +477,7 @@ trait EvalContextExtPriv<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let [id, show_unnamed] = this.check_shim(abi, Abi::Rust, link_name, args)?; let id = this.read_scalar(id)?.to_u64()?; let show_unnamed = this.read_scalar(show_unnamed)?.to_bool()?; - if let Some(id) = std::num::NonZeroU64::new(id) { + if let Some(id) = std::num::NonZero::::new(id) { this.print_borrow_state(AllocId(id), show_unnamed)?; } } From fc4253f7077a114fcea85cbd4d108778ffa9ced7 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Thu, 8 Feb 2024 23:03:25 +0100 Subject: [PATCH 3/4] Replace `NonZero::<_>::new` with `NonZero::new`. --- src/borrow_tracker/mod.rs | 6 +++--- src/concurrency/sync.rs | 4 ++-- src/shims/foreign_items.rs | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/borrow_tracker/mod.rs b/src/borrow_tracker/mod.rs index 4424595ea1..45240edea4 100644 --- a/src/borrow_tracker/mod.rs +++ b/src/borrow_tracker/mod.rs @@ -21,7 +21,7 @@ pub struct BorTag(NonZero); impl BorTag { pub fn new(i: u64) -> Option { - NonZero::::new(i).map(BorTag) + NonZero::new(i).map(BorTag) } pub fn get(&self) -> u64 { @@ -184,7 +184,7 @@ impl GlobalStateInner { borrow_tracker_method, next_ptr_tag: BorTag::one(), base_ptr_tags: FxHashMap::default(), - next_call_id: NonZero::::new(1).unwrap(), + next_call_id: NonZero::new(1).unwrap(), protected_tags: FxHashMap::default(), tracked_pointer_tags, tracked_call_ids, @@ -206,7 +206,7 @@ impl GlobalStateInner { if self.tracked_call_ids.contains(&call_id) { machine.emit_diagnostic(NonHaltingDiagnostic::CreatedCallId(call_id)); } - self.next_call_id = NonZero::::new(call_id.get() + 1).unwrap(); + self.next_call_id = NonZero::new(call_id.get() + 1).unwrap(); FrameState { call_id, protected_tags: SmallVec::new() } } diff --git a/src/concurrency/sync.rs b/src/concurrency/sync.rs index c3130c8a8f..b948ecb834 100644 --- a/src/concurrency/sync.rs +++ b/src/concurrency/sync.rs @@ -30,7 +30,7 @@ macro_rules! declare_id { impl SyncId for $name { // Panics if `id == 0`. fn from_u32(id: u32) -> Self { - Self(std::num::NonZero::::new(id).unwrap()) + Self(std::num::NonZero::new(id).unwrap()) } fn to_u32(&self) -> u32 { self.0.get() @@ -43,7 +43,7 @@ macro_rules! declare_id { // therefore, need to shift by one when converting from an index // into a vector. let shifted_idx = u32::try_from(idx).unwrap().checked_add(1).unwrap(); - $name(std::num::NonZero::::new(shifted_idx).unwrap()) + $name(std::num::NonZero::new(shifted_idx).unwrap()) } fn index(self) -> usize { // See the comment in `Self::new`. diff --git a/src/shims/foreign_items.rs b/src/shims/foreign_items.rs index f0e6e0374d..bf90d1468b 100644 --- a/src/shims/foreign_items.rs +++ b/src/shims/foreign_items.rs @@ -477,7 +477,7 @@ trait EvalContextExtPriv<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let [id, show_unnamed] = this.check_shim(abi, Abi::Rust, link_name, args)?; let id = this.read_scalar(id)?.to_u64()?; let show_unnamed = this.read_scalar(show_unnamed)?.to_bool()?; - if let Some(id) = std::num::NonZero::::new(id) { + if let Some(id) = std::num::NonZero::new(id) { this.print_borrow_state(AllocId(id), show_unnamed)?; } } From c8ed87f4172109a79f29551a5c32581a182a1bad Mon Sep 17 00:00:00 2001 From: The Miri Conjob Bot Date: Sat, 17 Feb 2024 05:10:27 +0000 Subject: [PATCH 4/4] Preparing for merge from rustc --- rust-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-version b/rust-version index 2115e482c6..ab6f899cd3 100644 --- a/rust-version +++ b/rust-version @@ -1 +1 @@ -0f806a9812b62c36bdab08d33c14cf2d3ecf4355 +4316d0c6252cb1f833e582dfa68adb98efd5ddfb