From c883f0ad8af76a981eaca511fa2c3e334117dbad Mon Sep 17 00:00:00 2001 From: Noa Date: Thu, 17 Oct 2024 16:31:41 -0500 Subject: [PATCH] Updates for Rust 1.82 --- Cargo.toml | 2 +- common/src/boxvec.rs | 25 +++---------------------- common/src/lock/cell_lock.rs | 6 +++--- stdlib/src/grp.rs | 2 +- stdlib/src/select.rs | 2 +- stdlib/src/socket.rs | 2 +- stdlib/src/sqlite.rs | 12 ++++-------- vm/src/object/core.rs | 4 ++-- vm/src/stdlib/pwd.rs | 2 +- 9 files changed, 17 insertions(+), 40 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6e90643ef..35ff85758 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -105,7 +105,7 @@ members = [ version = "0.4.0" authors = ["RustPython Team"] edition = "2021" -rust-version = "1.80.0" +rust-version = "1.82.0" repository = "https://github.com/RustPython/RustPython" license = "MIT" diff --git a/common/src/boxvec.rs b/common/src/boxvec.rs index ca81b7510..50f9ab271 100644 --- a/common/src/boxvec.rs +++ b/common/src/boxvec.rs @@ -1,7 +1,6 @@ //! An unresizable vector backed by a `Box<[T]>` use std::{ - alloc, borrow::{Borrow, BorrowMut}, cmp, fmt, mem::{self, MaybeUninit}, @@ -35,29 +34,11 @@ macro_rules! panic_oob { }; } -fn capacity_overflow() -> ! { - panic!("capacity overflow") -} - impl BoxVec { pub fn new(n: usize) -> BoxVec { - unsafe { - let layout = match alloc::Layout::array::(n) { - Ok(l) => l, - Err(_) => capacity_overflow(), - }; - let ptr = if mem::size_of::() == 0 { - ptr::NonNull::>::dangling().as_ptr() - } else { - let ptr = alloc::alloc(layout); - if ptr.is_null() { - alloc::handle_alloc_error(layout) - } - ptr as *mut MaybeUninit - }; - let ptr = ptr::slice_from_raw_parts_mut(ptr, n); - let xs = Box::from_raw(ptr); - BoxVec { xs, len: 0 } + BoxVec { + xs: Box::new_uninit_slice(n), + len: 0, } } diff --git a/common/src/lock/cell_lock.rs b/common/src/lock/cell_lock.rs index 5c31fcdbb..b10101f26 100644 --- a/common/src/lock/cell_lock.rs +++ b/common/src/lock/cell_lock.rs @@ -2,7 +2,7 @@ use lock_api::{ GetThreadId, RawMutex, RawRwLock, RawRwLockDowngrade, RawRwLockRecursive, RawRwLockUpgrade, RawRwLockUpgradeDowngrade, }; -use std::{cell::Cell, num::NonZeroUsize}; +use std::{cell::Cell, num::NonZero}; pub struct RawCellMutex { locked: Cell, @@ -203,7 +203,7 @@ fn deadlock(lock_kind: &str, ty: &str) -> ! { pub struct SingleThreadId(()); unsafe impl GetThreadId for SingleThreadId { const INIT: Self = SingleThreadId(()); - fn nonzero_thread_id(&self) -> NonZeroUsize { - NonZeroUsize::new(1).unwrap() + fn nonzero_thread_id(&self) -> NonZero { + NonZero::new(1).unwrap() } } diff --git a/stdlib/src/grp.rs b/stdlib/src/grp.rs index 959c984be..d3eb0848b 100644 --- a/stdlib/src/grp.rs +++ b/stdlib/src/grp.rs @@ -83,7 +83,7 @@ mod grp { #[pyfunction] fn getgrall(vm: &VirtualMachine) -> PyResult> { // setgrent, getgrent, etc are not thread safe. Could use fgetgrent_r, but this is easier - static GETGRALL: parking_lot::Mutex<()> = parking_lot::const_mutex(()); + static GETGRALL: parking_lot::Mutex<()> = parking_lot::Mutex::new(()); let _guard = GETGRALL.lock(); let mut list = Vec::new(); diff --git a/stdlib/src/select.rs b/stdlib/src/select.rs index 0e367bc35..af76d86c8 100644 --- a/stdlib/src/select.rs +++ b/stdlib/src/select.rs @@ -36,7 +36,7 @@ mod platform { // based off winsock2.h: https://gist.github.com/piscisaureus/906386#file-winsock2-h-L128-L141 pub unsafe fn FD_SET(fd: RawFd, set: *mut fd_set) { - let mut slot = std::ptr::addr_of_mut!((*set).fd_array).cast::(); + let mut slot = (&raw mut (*set).fd_array).cast::(); let fd_count = (*set).fd_count; for _ in 0..fd_count { if *slot == fd { diff --git a/stdlib/src/socket.rs b/stdlib/src/socket.rs index 864586685..bf2f5ecd3 100644 --- a/stdlib/src/socket.rs +++ b/stdlib/src/socket.rs @@ -2217,7 +2217,7 @@ mod _socket { fn as_slice(&self) -> &[netioapi::MIB_IF_ROW2] { unsafe { let p = self.ptr.as_ptr(); - let ptr = ptr::addr_of!((*p).Table) as *const netioapi::MIB_IF_ROW2; + let ptr = &raw const (*p).Table as *const netioapi::MIB_IF_ROW2; std::slice::from_raw_parts(ptr, (*p).NumEntries as usize) } } diff --git a/stdlib/src/sqlite.rs b/stdlib/src/sqlite.rs index e24b7e475..c47bc6420 100644 --- a/stdlib/src/sqlite.rs +++ b/stdlib/src/sqlite.rs @@ -76,7 +76,7 @@ mod _sqlite { ffi::{c_int, c_longlong, c_uint, c_void, CStr}, fmt::Debug, ops::Deref, - ptr::{addr_of_mut, null, null_mut}, + ptr::{null, null_mut}, thread::ThreadId, }; @@ -1178,14 +1178,10 @@ mod _sqlite { ) }; - // TODO: replace with Result.inspect_err when stable - if let Err(exc) = db.check(ret, vm) { + db.check(ret, vm).inspect_err(|_| { // create_collation do not call destructor if error occur let _ = unsafe { Box::from_raw(data) }; - Err(exc) - } else { - Ok(()) - } + }) } #[pymethod] @@ -2396,7 +2392,7 @@ mod _sqlite { let ret = unsafe { sqlite3_open_v2( path, - addr_of_mut!(db), + &raw mut db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | if uri { SQLITE_OPEN_URI } else { 0 }, diff --git a/vm/src/object/core.rs b/vm/src/object/core.rs index 6a8166b20..b32693546 100644 --- a/vm/src/object/core.rs +++ b/vm/src/object/core.rs @@ -322,7 +322,7 @@ unsafe impl Link for WeakLink { #[inline(always)] unsafe fn pointers(target: NonNull) -> NonNull> { - NonNull::new_unchecked(ptr::addr_of_mut!((*target.as_ptr()).0.payload.pointers)) + NonNull::new_unchecked(&raw mut (*target.as_ptr()).0.payload.pointers) } } @@ -1047,7 +1047,7 @@ impl PyRef { pub fn leak(pyref: Self) -> &'static Py { let ptr = pyref.ptr; std::mem::forget(pyref); - unsafe { &*ptr.as_ptr() } + unsafe { ptr.as_ref() } } } diff --git a/vm/src/stdlib/pwd.rs b/vm/src/stdlib/pwd.rs index f5d1e50a2..0edca9c0a 100644 --- a/vm/src/stdlib/pwd.rs +++ b/vm/src/stdlib/pwd.rs @@ -92,7 +92,7 @@ mod pwd { #[pyfunction] fn getpwall(vm: &VirtualMachine) -> PyResult> { // setpwent, getpwent, etc are not thread safe. Could use fgetpwent_r, but this is easier - static GETPWALL: parking_lot::Mutex<()> = parking_lot::const_mutex(()); + static GETPWALL: parking_lot::Mutex<()> = parking_lot::Mutex::new(()); let _guard = GETPWALL.lock(); let mut list = Vec::new();