From eae60113afd213cfd9a3dfb653cc015b19b4ad40 Mon Sep 17 00:00:00 2001 From: Noa Date: Sun, 16 Jun 2024 20:01:14 -0400 Subject: [PATCH] Update some stuff for inline const & associated type bounds --- common/src/str.rs | 13 ++++++++----- compiler/core/src/frozen.rs | 3 +-- vm/src/builtins/dict.rs | 7 ++----- vm/src/function/builtin.rs | 19 +++++++------------ vm/src/intern.rs | 8 ++------ vm/src/object/traverse_object.rs | 31 ++++++++++++------------------- vm/src/vm/mod.rs | 11 ++++------- 7 files changed, 36 insertions(+), 56 deletions(-) diff --git a/common/src/str.rs b/common/src/str.rs index 48fdb0f95..1606ebc07 100644 --- a/common/src/str.rs +++ b/common/src/str.rs @@ -331,12 +331,15 @@ pub mod levenshtein { /// ``` #[macro_export] macro_rules! ascii { - ($x:literal) => {{ - const STR: &str = $x; - const _: () = if !STR.is_ascii() { - panic!("ascii!() argument is not an ascii string"); + ($x:expr $(,)?) => {{ + let s = const { + let s: &str = $x; + if !s.is_ascii() { + panic!("ascii!() argument is not an ascii string"); + } + s }; - unsafe { $crate::vendored::ascii::AsciiStr::from_ascii_unchecked(STR.as_bytes()) } + unsafe { $crate::vendored::ascii::AsciiStr::from_ascii_unchecked(s.as_bytes()) } }}; } pub use ascii; diff --git a/compiler/core/src/frozen.rs b/compiler/core/src/frozen.rs index 4565eac8a..991898c1e 100644 --- a/compiler/core/src/frozen.rs +++ b/compiler/core/src/frozen.rs @@ -102,8 +102,7 @@ impl FrozenLib> { /// Encode the given iterator of frozen modules into a compressed vector of bytes pub fn encode<'a, I, B: AsRef<[u8]>>(lib: I) -> FrozenLib> where - I: IntoIterator)>, - I::IntoIter: ExactSizeIterator + Clone, + I: IntoIterator), IntoIter: ExactSizeIterator + Clone>, { let iter = lib.into_iter(); let mut bytes = Vec::new(); diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index 0fa2e18c3..68567e493 100644 --- a/vm/src/builtins/dict.rs +++ b/vm/src/builtins/dict.rs @@ -728,11 +728,8 @@ impl ExactSizeIterator for DictIter<'_> { } #[pyclass] -trait DictView: PyPayload + PyClassDef + Iterable + Representable -where - Self::ReverseIter: PyPayload, -{ - type ReverseIter; +trait DictView: PyPayload + PyClassDef + Iterable + Representable { + type ReverseIter: PyPayload; fn dict(&self) -> &PyDictRef; fn item(vm: &VirtualMachine, key: PyObjectRef, value: PyObjectRef) -> PyObjectRef; diff --git a/vm/src/function/builtin.rs b/vm/src/function/builtin.rs index 41e04d252..bf9a5ed34 100644 --- a/vm/src/function/builtin.rs +++ b/vm/src/function/builtin.rs @@ -55,19 +55,14 @@ const fn zst_ref_out_of_thin_air(x: T) -> &'static T { // would never get called anyway if we consider this semantically a Box::leak(Box::new(x))-type // operation. if T isn't zero-sized, we don't have to worry about it because we'll fail to compile. std::mem::forget(x); - trait Zst: Sized + 'static { - const THIN_AIR: &'static Self = { - if std::mem::size_of::() == 0 { - // SAFETY: we just confirmed that Self is zero-sized, so we can - // pull a value of it out of thin air. - unsafe { std::ptr::NonNull::::dangling().as_ref() } - } else { - panic!("can't use a non-zero-sized type here") - } - }; + const { + if std::mem::size_of::() != 0 { + panic!("can't use a non-zero-sized type here") + } + // SAFETY: we just confirmed that T is zero-sized, so we can + // pull a value of it out of thin air. + unsafe { std::ptr::NonNull::::dangling().as_ref() } } - impl Zst for T {} - ::THIN_AIR } /// Get the [`STATIC_FUNC`](IntoPyNativeFn::STATIC_FUNC) of the passed function. The same diff --git a/vm/src/intern.rs b/vm/src/intern.rs index 1f6fb09f2..45bd45d96 100644 --- a/vm/src/intern.rs +++ b/vm/src/intern.rs @@ -228,12 +228,8 @@ mod sealed { } /// A sealed marker trait for `DictKey` types that always become an exact instance of `str` -pub trait InternableString -where - Self: sealed::SealedInternable + ToPyObject + AsRef, - Self::Interned: MaybeInternedString, -{ - type Interned: ?Sized; +pub trait InternableString: sealed::SealedInternable + ToPyObject + AsRef { + type Interned: MaybeInternedString + ?Sized; fn into_pyref_exact(self, str_type: PyTypeRef) -> PyRefExact; } diff --git a/vm/src/object/traverse_object.rs b/vm/src/object/traverse_object.rs index b7476c849..b690b80f7 100644 --- a/vm/src/object/traverse_object.rs +++ b/vm/src/object/traverse_object.rs @@ -1,4 +1,4 @@ -use std::{fmt, marker::PhantomData}; +use std::fmt; use crate::{ object::{ @@ -16,25 +16,18 @@ pub(in crate::object) struct PyObjVTable { } impl PyObjVTable { - pub fn of() -> &'static Self { - struct Helper(PhantomData); - trait VtableHelper { - const VTABLE: PyObjVTable; + pub const fn of() -> &'static Self { + &PyObjVTable { + drop_dealloc: drop_dealloc_obj::, + debug: debug_obj::, + trace: const { + if T::IS_TRACE { + Some(try_trace_obj::) + } else { + None + } + }, } - impl VtableHelper for Helper { - const VTABLE: PyObjVTable = PyObjVTable { - drop_dealloc: drop_dealloc_obj::, - debug: debug_obj::, - trace: { - if T::IS_TRACE { - Some(try_trace_obj::) - } else { - None - } - }, - }; - } - &Helper::::VTABLE } } diff --git a/vm/src/vm/mod.rs b/vm/src/vm/mod.rs index 1a5f40e76..295e599b7 100644 --- a/vm/src/vm/mod.rs +++ b/vm/src/vm/mod.rs @@ -133,13 +133,10 @@ impl VirtualMachine { let import_func = ctx.none(); let profile_func = RefCell::new(ctx.none()); let trace_func = RefCell::new(ctx.none()); - // hack to get around const array repeat expressions, rust issue #79270 - const NONE: Option = None; - // putting it in a const optimizes better, prevents linear initialization of the array - #[allow(clippy::declare_interior_mutable_const)] - const SIGNAL_HANDLERS: RefCell<[Option; signal::NSIG]> = - RefCell::new([NONE; signal::NSIG]); - let signal_handlers = Some(Box::new(SIGNAL_HANDLERS)); + let signal_handlers = Some(Box::new( + // putting it in a const optimizes better, prevents linear initialization of the array + const { RefCell::new([const { None }; signal::NSIG]) }, + )); let module_inits = stdlib::get_module_inits();