Skip to content

Commit 87cf891

Browse files
authored
Make PyMethodDef construction const (RustPython#5117)
* Make PyMethodDef construction const * Remove iter_chain![] Obsolete since arrays now impl IntoIterator
1 parent 6d23daa commit 87cf891

7 files changed

Lines changed: 149 additions & 85 deletions

File tree

‎derive-impl/src/pyclass.rs‎

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::util::{
44
ClassItemMeta, ContentItem, ContentItemInner, ErrorVec, ExceptionItemMeta, ItemMeta,
55
ItemMetaInner, ItemNursery, SimpleItemMeta, ALL_ALLOWED_NAMES,
66
};
7-
use proc_macro2::{Span, TokenStream};
7+
use proc_macro2::{Delimiter, Group, Span, TokenStream, TokenTree};
88
use quote::{quote, quote_spanned, ToTokens};
99
use std::collections::{HashMap, HashSet};
1010
use std::str::FromStr;
@@ -172,14 +172,9 @@ pub(crate) fn impl_pyclass_impl(attr: AttributeArgs, item: Item) -> Result<Token
172172
let slots_impl = context.extend_slots_items.validate()?;
173173
let class_extensions = &context.class_extensions;
174174

175-
let extra_methods = iter_chain![
175+
let extra_methods = [
176176
parse_quote! {
177-
#[allow(clippy::ptr_arg)]
178-
fn __extend_method_def(
179-
method_defs: &mut Vec<::rustpython_vm::function::PyMethodDef>,
180-
) {
181-
#method_def
182-
}
177+
const __OWN_METHOD_DEFS: &'static [::rustpython_vm::function::PyMethodDef] = &#method_def;
183178
},
184179
parse_quote! {
185180
fn __extend_py_class(
@@ -201,6 +196,15 @@ pub(crate) fn impl_pyclass_impl(attr: AttributeArgs, item: Item) -> Result<Token
201196
imp.items.extend(extra_methods);
202197
let is_main_impl = impl_ty == payload_ty;
203198
if is_main_impl {
199+
let method_defs = if with_method_defs.is_empty() {
200+
quote!(#impl_ty::__OWN_METHOD_DEFS)
201+
} else {
202+
quote!(
203+
rustpython_vm::function::PyMethodDef::__const_concat_arrays::<
204+
{ #impl_ty::__OWN_METHOD_DEFS.len() #(+ #with_method_defs.len())* },
205+
>(&[#impl_ty::__OWN_METHOD_DEFS, #(#with_method_defs,)*])
206+
)
207+
};
204208
quote! {
205209
#imp
206210
impl ::rustpython_vm::class::PyClassImpl for #payload_ty {
@@ -214,13 +218,7 @@ pub(crate) fn impl_pyclass_impl(attr: AttributeArgs, item: Item) -> Result<Token
214218
#with_impl
215219
}
216220

217-
#[allow(clippy::ptr_arg)]
218-
fn impl_extend_method_def(
219-
method_defs: &mut Vec<::rustpython_vm::function::PyMethodDef>,
220-
) {
221-
#impl_ty::__extend_method_def(method_defs);
222-
#with_method_defs
223-
}
221+
const METHOD_DEFS: &'static [::rustpython_vm::function::PyMethodDef] = &#method_defs;
224222

225223
fn extend_slots(slots: &mut ::rustpython_vm::types::PyTypeSlots) {
226224
#impl_ty::__extend_slots(slots);
@@ -268,14 +266,9 @@ pub(crate) fn impl_pyclass_impl(attr: AttributeArgs, item: Item) -> Result<Token
268266
} else {
269267
quote! {}
270268
};
271-
let extra_methods = iter_chain![
269+
let extra_methods = [
272270
parse_quote! {
273-
#[allow(clippy::ptr_arg)]
274-
fn __extend_method_def(
275-
method_defs: &mut Vec<::rustpython_vm::function::PyMethodDef>,
276-
) {
277-
#method_def
278-
}
271+
const __OWN_METHOD_DEFS: &'static [::rustpython_vm::function::PyMethodDef] = &#method_def;
279272
},
280273
parse_quote! {
281274
fn __extend_py_class(
@@ -983,6 +976,7 @@ impl MethodNursery {
983976

984977
impl ToTokens for MethodNursery {
985978
fn to_tokens(&self, tokens: &mut TokenStream) {
979+
let mut inner_tokens = TokenStream::new();
986980
for item in &self.items {
987981
let py_name = &item.py_name;
988982
let ident = &item.ident;
@@ -1011,16 +1005,18 @@ impl ToTokens for MethodNursery {
10111005
// } else {
10121006
// quote_spanned! { ident.span() => #py_name }
10131007
// };
1014-
tokens.extend(quote! {
1008+
inner_tokens.extend(quote! [
10151009
#(#cfgs)*
1016-
method_defs.push(rustpython_vm::function::PyMethodDef {
1017-
name: #py_name,
1018-
func: rustpython_vm::function::IntoPyNativeFn::into_func(Self::#ident),
1019-
flags: #flags,
1020-
doc: #doc,
1021-
});
1022-
});
1010+
rustpython_vm::function::PyMethodDef::new_const(
1011+
#py_name,
1012+
Self::#ident,
1013+
#flags,
1014+
#doc,
1015+
),
1016+
]);
10231017
}
1018+
let array: TokenTree = Group::new(Delimiter::Bracket, inner_tokens).into();
1019+
tokens.extend([array]);
10241020
}
10251021
}
10261022

@@ -1436,7 +1432,7 @@ struct ExtractedImplAttrs {
14361432
payload: Option<Ident>,
14371433
flags: TokenStream,
14381434
with_impl: TokenStream,
1439-
with_method_defs: TokenStream,
1435+
with_method_defs: Vec<TokenStream>,
14401436
with_slots: TokenStream,
14411437
}
14421438

@@ -1465,28 +1461,26 @@ fn extract_impl_attrs(attr: AttributeArgs, item: &Ident) -> Result<ExtractedImpl
14651461
let NestedMeta::Meta(Meta::Path(path)) = meta else {
14661462
bail_span!(meta, "#[pyclass(with(...))] arguments should be paths")
14671463
};
1468-
let (extend_class, extend_method_def, extend_slots) =
1464+
let (extend_class, method_defs, extend_slots) =
14691465
if path.is_ident("PyRef") || path.is_ident("Py") {
14701466
// special handling for PyRef
14711467
(
14721468
quote!(#path::<Self>::__extend_py_class),
1473-
quote!(#path::<Self>::__extend_method_def),
1469+
quote!(#path::<Self>::__OWN_METHOD_DEFS),
14741470
quote!(#path::<Self>::__extend_slots),
14751471
)
14761472
} else {
14771473
(
14781474
quote!(<Self as #path>::__extend_py_class),
1479-
quote!(<Self as #path>::__extend_method_def),
1475+
quote!(<Self as #path>::__OWN_METHOD_DEFS),
14801476
quote!(<Self as #path>::__extend_slots),
14811477
)
14821478
};
14831479
let item_span = item.span().resolved_at(Span::call_site());
14841480
withs.push(quote_spanned! { path.span() =>
14851481
#extend_class(ctx, class);
14861482
});
1487-
with_method_defs.push(quote_spanned! { path.span() =>
1488-
#extend_method_def(method_defs);
1489-
});
1483+
with_method_defs.push(method_defs);
14901484
with_slots.push(quote_spanned! { item_span =>
14911485
#extend_slots(slots);
14921486
});
@@ -1530,9 +1524,7 @@ fn extract_impl_attrs(attr: AttributeArgs, item: &Ident) -> Result<ExtractedImpl
15301524
with_impl: quote! {
15311525
#(#withs)*
15321526
},
1533-
with_method_defs: quote! {
1534-
#(#with_method_defs)*
1535-
},
1527+
with_method_defs,
15361528
with_slots: quote! {
15371529
#(#with_slots)*
15381530
},

‎derive-impl/src/pymodule.rs‎

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::util::{
44
AttributeExt, ClassItemMeta, ContentItem, ContentItemInner, ErrorVec, ItemMeta, ItemNursery,
55
ModuleItemMeta, SimpleItemMeta, ALL_ALLOWED_NAMES,
66
};
7-
use proc_macro2::TokenStream;
7+
use proc_macro2::{Delimiter, Group, TokenStream, TokenTree};
88
use quote::{quote, quote_spanned, ToTokens};
99
use std::{collections::HashSet, str::FromStr};
1010
use syn::{parse_quote, spanned::Spanned, Attribute, AttributeArgs, Ident, Item, Result};
@@ -110,7 +110,7 @@ pub fn impl_pymodule(attr: AttributeArgs, module_item: Item) -> Result<TokenStre
110110
let is_submodule = module_meta.sub()?;
111111
let withs = module_meta.with()?;
112112
if !is_submodule {
113-
items.extend(iter_chain![
113+
items.extend([
114114
parse_quote! {
115115
pub(crate) const MODULE_NAME: &'static str = #module_name;
116116
},
@@ -122,16 +122,10 @@ pub fn impl_pymodule(attr: AttributeArgs, module_item: Item) -> Result<TokenStre
122122
ctx: &::rustpython_vm::Context,
123123
) -> &'static ::rustpython_vm::builtins::PyModuleDef {
124124
DEF.get_or_init(|| {
125-
#[allow(clippy::ptr_arg)]
126-
let method_defs = {
127-
let mut method_defs = Vec::new();
128-
extend_method_def(ctx, &mut method_defs);
129-
method_defs
130-
};
131125
let mut def = ::rustpython_vm::builtins::PyModuleDef {
132126
name: ctx.intern_str(MODULE_NAME),
133127
doc: DOC.map(|doc| ctx.intern_str(doc)),
134-
methods: Box::leak(method_defs.into_boxed_slice()),
128+
methods: METHOD_DEFS,
135129
slots: Default::default(),
136130
};
137131
def.slots.exec = Some(extend_module);
@@ -161,23 +155,24 @@ pub fn impl_pymodule(attr: AttributeArgs, module_item: Item) -> Result<TokenStre
161155
}
162156
});
163157
}
164-
items.extend(iter_chain![
158+
let method_defs = if withs.is_empty() {
159+
quote!(#function_items)
160+
} else {
161+
quote!({
162+
const OWN_METHODS: &'static [::rustpython_vm::function::PyMethodDef] = &#function_items;
163+
rustpython_vm::function::PyMethodDef::__const_concat_arrays::<
164+
{ OWN_METHODS.len() #(+ super::#withs::METHOD_DEFS.len())* },
165+
>(&[#(super::#withs::METHOD_DEFS,)* OWN_METHODS])
166+
})
167+
};
168+
items.extend([
165169
parse_quote! {
166170
::rustpython_vm::common::static_cell! {
167171
pub(crate) static DEF: ::rustpython_vm::builtins::PyModuleDef;
168172
}
169173
},
170174
parse_quote! {
171-
#[allow(clippy::ptr_arg)]
172-
pub(crate) fn extend_method_def(
173-
ctx: &::rustpython_vm::Context,
174-
method_defs: &mut Vec<::rustpython_vm::function::PyMethodDef>,
175-
) {
176-
#(
177-
super::#withs::extend_method_def(ctx, method_defs);
178-
)*
179-
#function_items
180-
}
175+
pub(crate) const METHOD_DEFS: &'static [::rustpython_vm::function::PyMethodDef] = &#method_defs;
181176
},
182177
parse_quote! {
183178
pub(crate) fn __init_attributes(
@@ -361,26 +356,30 @@ struct ValidatedFunctionNursery(FunctionNursery);
361356

362357
impl ToTokens for ValidatedFunctionNursery {
363358
fn to_tokens(&self, tokens: &mut TokenStream) {
359+
let mut inner_tokens = TokenStream::new();
360+
let flags = quote! { rustpython_vm::function::PyMethodFlags::empty() };
364361
for item in &self.0.items {
365362
let ident = &item.ident;
366363
let cfgs = &item.cfgs;
364+
let cfgs = quote!(#(#cfgs)*);
367365
let py_names = &item.py_names;
368366
let doc = &item.doc;
369-
let flags = quote! { rustpython_vm::function::PyMethodFlags::empty() };
370-
371-
tokens.extend(quote! {
372-
#(#cfgs)*
373-
{
374-
let doc = Some(#doc);
375-
#(method_defs.push(rustpython_vm::function::PyMethodDef::new(
376-
(#py_names),
367+
let doc = quote!(Some(#doc));
368+
369+
inner_tokens.extend(quote![
370+
#(
371+
#cfgs
372+
rustpython_vm::function::PyMethodDef::new_const(
373+
#py_names,
377374
#ident,
378375
#flags,
379-
doc,
380-
));)*
381-
}
382-
});
376+
#doc,
377+
),
378+
)*
379+
]);
383380
}
381+
let array: TokenTree = Group::new(Delimiter::Bracket, inner_tokens).into();
382+
tokens.extend([array]);
384383
}
385384
}
386385

‎derive-impl/src/util.rs‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -639,13 +639,6 @@ impl ErrorVec for Vec<syn::Error> {
639639
}
640640
}
641641

642-
macro_rules! iter_chain {
643-
($($it:expr),*$(,)?) => {
644-
::std::iter::empty()
645-
$(.chain(::std::iter::once($it)))*
646-
};
647-
}
648-
649642
pub(crate) fn iter_use_idents<'a, F, R: 'a>(item_use: &'a syn::ItemUse, mut f: F) -> Result<Vec<R>>
650643
where
651644
F: FnMut(&'a syn::Ident, bool) -> Result<R>,

‎vm/src/class.rs‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,16 @@ pub trait PyClassImpl: PyClassDef {
135135
}
136136

137137
fn impl_extend_class(ctx: &Context, class: &'static Py<PyType>);
138-
fn impl_extend_method_def(method_defs: &mut Vec<PyMethodDef>);
138+
const METHOD_DEFS: &'static [PyMethodDef];
139139
fn extend_slots(slots: &mut PyTypeSlots);
140140

141141
fn make_slots() -> PyTypeSlots {
142-
let mut method_defs = Vec::new();
143-
Self::impl_extend_method_def(&mut method_defs);
144-
145142
let mut slots = PyTypeSlots {
146143
flags: Self::TP_FLAGS,
147144
name: Self::TP_NAME,
148145
basicsize: Self::BASICSIZE,
149146
doc: Self::DOC,
150-
methods: Box::leak(method_defs.into_boxed_slice()),
147+
methods: Self::METHOD_DEFS,
151148
..Default::default()
152149
};
153150

‎vm/src/function/builtin.rs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,44 @@ pub type PyNativeFn = py_dyn_fn!(dyn Fn(&VirtualMachine, FuncArgs) -> PyResult);
3030
/// `fn foo<F, FKind>(f: F) where F: IntoPyNativeFn<FKind>`
3131
pub trait IntoPyNativeFn<Kind>: Sized + PyThreadingConstraint + 'static {
3232
fn call(&self, vm: &VirtualMachine, args: FuncArgs) -> PyResult;
33+
3334
/// `IntoPyNativeFn::into_func()` generates a PyNativeFn that performs the
3435
/// appropriate type and arity checking, any requested conversions, and then if
3536
/// successful calls the function with the extracted parameters.
3637
fn into_func(self) -> &'static PyNativeFn {
3738
let boxed = Box::new(move |vm: &VirtualMachine, args| self.call(vm, args));
3839
Box::leak(boxed)
3940
}
41+
42+
/// Equivalent to `into_func()`, but accessible as a constant. This is only
43+
/// valid if this function is zero-sized, i.e. that
44+
/// `std::mem::size_of::<F>() == 0`. If it isn't, use of this constant will
45+
/// raise a compile error.
46+
const STATIC_FUNC: &'static PyNativeFn = {
47+
if std::mem::size_of::<Self>() == 0 {
48+
&|vm, args| {
49+
// SAFETY: we just confirmed that Self is zero-sized, so there
50+
// aren't any bytes in it that could be uninit.
51+
#[allow(clippy::uninit_assumed_init)]
52+
let f = unsafe { std::mem::MaybeUninit::<Self>::uninit().assume_init() };
53+
f.call(vm, args)
54+
}
55+
} else {
56+
panic!("function must be zero-sized to access STATIC_FUNC")
57+
}
58+
};
59+
}
60+
61+
/// Get the [`STATIC_FUNC`](IntoPyNativeFn::STATIC_FUNC) of the passed function. The same
62+
/// requirements of zero-sizedness apply, see that documentation for details.
63+
#[inline(always)]
64+
pub const fn static_func<Kind, F: IntoPyNativeFn<Kind>>(f: F) -> &'static PyNativeFn {
65+
// if f is zero-sized, there's no issue forgetting it - even if a capture of f does have a Drop
66+
// impl, it would never get called anyway. If you passed it to into_func, it would just get
67+
// Box::leak'd, and as a 'static reference it'll never be dropped. and if f isn't zero-sized,
68+
// we'll never reach this point anyway because we'll fail to compile.
69+
std::mem::forget(f);
70+
F::STATIC_FUNC
4071
}
4172

4273
// TODO: once higher-rank trait bounds are stabilized, remove the `Kind` type
@@ -186,5 +217,6 @@ mod tests {
186217
check_zst(py_func.into_func());
187218
let empty_closure = || "foo".to_owned();
188219
check_zst(empty_closure.into_func());
220+
check_zst(static_func(empty_closure));
189221
}
190222
}

0 commit comments

Comments
 (0)