Skip to content

Commit 827f830

Browse files
authored
Merge pull request RustPython#836 from RustPython/coolreader18/no-proc-macro-hack
Remove the __inside_vm hack for procedural macros
2 parents 78db5ea + 08babef commit 827f830

File tree

10 files changed

+32
-77
lines changed

10 files changed

+32
-77
lines changed

derive/src/from_args.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use super::rustpython_path_derive;
21
use proc_macro2::TokenStream as TokenStream2;
32
use quote::quote;
43
use syn::{Attribute, Data, DeriveInput, Expr, Field, Fields, Ident, Lit, Meta, NestedMeta};
@@ -109,7 +108,7 @@ impl ArgAttribute {
109108
}
110109
}
111110

112-
fn generate_field(field: &Field, rp_path: &syn::Path) -> TokenStream2 {
111+
fn generate_field(field: &Field) -> TokenStream2 {
113112
let mut pyarg_attrs = field
114113
.attrs
115114
.iter()
@@ -132,24 +131,24 @@ fn generate_field(field: &Field, rp_path: &syn::Path) -> TokenStream2 {
132131

133132
let name = &field.ident;
134133
let middle = quote! {
135-
.map(|x| #rp_path::pyobject::TryFromObject::try_from_object(vm, x)).transpose()?
134+
.map(|x| ::rustpython_vm::pyobject::TryFromObject::try_from_object(vm, x)).transpose()?
136135
};
137136
let ending = if let Some(default) = attr.default {
138137
quote! {
139138
.unwrap_or_else(|| #default)
140139
}
141140
} else if attr.optional {
142141
quote! {
143-
.map(#rp_path::function::OptionalArg::Present)
144-
.unwrap_or(#rp_path::function::OptionalArg::Missing)
142+
.map(::rustpython_vm::function::OptionalArg::Present)
143+
.unwrap_or(::rustpython_vm::function::OptionalArg::Missing)
145144
}
146145
} else {
147146
let err = match attr.kind {
148147
ParameterKind::PositionalOnly | ParameterKind::PositionalOrKeyword => quote! {
149-
#rp_path::function::ArgumentError::TooFewArgs
148+
::rustpython_vm::function::ArgumentError::TooFewArgs
150149
},
151150
ParameterKind::KeywordOnly => quote! {
152-
#rp_path::function::ArgumentError::RequiredKeywordArgument(tringify!(#name))
151+
::rustpython_vm::function::ArgumentError::RequiredKeywordArgument(tringify!(#name))
153152
},
154153
};
155154
quote! {
@@ -177,14 +176,10 @@ fn generate_field(field: &Field, rp_path: &syn::Path) -> TokenStream2 {
177176
}
178177

179178
pub fn impl_from_args(input: DeriveInput) -> TokenStream2 {
180-
let rp_path = rustpython_path_derive(&input);
181179
let fields = match input.data {
182180
Data::Struct(ref data) => {
183181
match data.fields {
184-
Fields::Named(ref fields) => fields
185-
.named
186-
.iter()
187-
.map(|field| generate_field(field, &rp_path)),
182+
Fields::Named(ref fields) => fields.named.iter().map(generate_field),
188183
Fields::Unnamed(_) | Fields::Unit => unimplemented!(), // TODO: better error message
189184
}
190185
}
@@ -193,11 +188,11 @@ pub fn impl_from_args(input: DeriveInput) -> TokenStream2 {
193188

194189
let name = &input.ident;
195190
quote! {
196-
impl #rp_path::function::FromArgs for #name {
191+
impl ::rustpython_vm::function::FromArgs for #name {
197192
fn from_args(
198-
vm: &#rp_path::VirtualMachine,
199-
args: &mut #rp_path::function::PyFuncArgs
200-
) -> Result<Self, #rp_path::function::ArgumentError> {
193+
vm: &::rustpython_vm::VirtualMachine,
194+
args: &mut ::rustpython_vm::function::PyFuncArgs
195+
) -> Result<Self, ::rustpython_vm::function::ArgumentError> {
201196
Ok(#name { #(#fields)* })
202197
}
203198
}

derive/src/lib.rs

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,12 @@
11
extern crate proc_macro;
22

33
use proc_macro::TokenStream;
4-
use quote::quote;
54
use syn::{parse_macro_input, AttributeArgs, DeriveInput, Item};
65

76
mod from_args;
87
mod pyclass;
98

10-
fn rustpython_path(inside_vm: bool) -> syn::Path {
11-
let path = if inside_vm {
12-
quote!(crate)
13-
} else {
14-
quote!(::rustpython_vm)
15-
};
16-
syn::parse2(path).unwrap()
17-
}
18-
19-
/// Does the item have the #[__inside_vm] attribute on it, signifying that the derive target is
20-
/// being derived from inside the `rustpython_vm` crate.
21-
fn rustpython_path_derive(input: &DeriveInput) -> syn::Path {
22-
rustpython_path(
23-
input
24-
.attrs
25-
.iter()
26-
.any(|attr| attr.path.is_ident("__inside_vm")),
27-
)
28-
}
29-
30-
fn rustpython_path_attr(attr: &AttributeArgs) -> syn::Path {
31-
rustpython_path(attr.iter().any(|meta| {
32-
if let syn::NestedMeta::Meta(meta) = meta {
33-
if let syn::Meta::Word(ident) = meta {
34-
ident == "__inside_vm"
35-
} else {
36-
false
37-
}
38-
} else {
39-
false
40-
}
41-
}))
42-
}
43-
44-
#[proc_macro_derive(FromArgs, attributes(__inside_vm, pyarg))]
9+
#[proc_macro_derive(FromArgs, attributes(pyarg))]
4510
pub fn derive_from_args(input: TokenStream) -> TokenStream {
4611
let ast: DeriveInput = syn::parse(input).unwrap();
4712

derive/src/pyclass.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use super::rustpython_path_attr;
21
use proc_macro2::TokenStream as TokenStream2;
32
use quote::quote;
43
use std::collections::HashMap;
@@ -138,15 +137,13 @@ impl ClassItem {
138137
}
139138
}
140139

141-
pub fn impl_pyimpl(attr: AttributeArgs, item: Item) -> TokenStream2 {
140+
pub fn impl_pyimpl(_attr: AttributeArgs, item: Item) -> TokenStream2 {
142141
let mut imp = if let Item::Impl(imp) = item {
143142
imp
144143
} else {
145144
return quote!(#item);
146145
};
147146

148-
let rp_path = rustpython_path_attr(&attr);
149-
150147
let items = imp
151148
.items
152149
.iter_mut()
@@ -199,7 +196,7 @@ pub fn impl_pyimpl(attr: AttributeArgs, item: Item) -> TokenStream2 {
199196
quote! {
200197
class.set_str_attr(
201198
#name,
202-
#rp_path::obj::objproperty::PropertyBuilder::new(ctx)
199+
::rustpython_vm::obj::objproperty::PropertyBuilder::new(ctx)
203200
.add_getter(Self::#getter)
204201
#add_setter
205202
.create(),
@@ -209,10 +206,10 @@ pub fn impl_pyimpl(attr: AttributeArgs, item: Item) -> TokenStream2 {
209206

210207
quote! {
211208
#imp
212-
impl #rp_path::pyobject::PyClassImpl for #ty {
209+
impl ::rustpython_vm::pyobject::PyClassImpl for #ty {
213210
fn impl_extend_class(
214-
ctx: &#rp_path::pyobject::PyContext,
215-
class: &#rp_path::obj::objtype::PyClassRef,
211+
ctx: &::rustpython_vm::pyobject::PyContext,
212+
class: &::rustpython_vm::obj::objtype::PyClassRef,
216213
) {
217214
#(#methods)*
218215
#(#properties)*
@@ -228,8 +225,6 @@ pub fn impl_pyclass(attr: AttributeArgs, item: Item) -> TokenStream2 {
228225
_ => panic!("#[pyclass] can only be on a struct or enum declaration"),
229226
};
230227

231-
let rp_path = rustpython_path_attr(&attr);
232-
233228
let mut class_name = None;
234229
for attr in attr {
235230
if let NestedMeta::Meta(meta) = attr {
@@ -273,7 +268,7 @@ pub fn impl_pyclass(attr: AttributeArgs, item: Item) -> TokenStream2 {
273268

274269
quote! {
275270
#item
276-
impl #rp_path::pyobject::PyClassDef for #ident {
271+
impl ::rustpython_vm::pyobject::PyClassDef for #ident {
277272
const NAME: &'static str = #class_name;
278273
const DOC: Option<&'static str> = #doc;
279274
}

vm/src/builtins.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,6 @@ fn builtin_pow(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
530530
}
531531

532532
#[derive(Debug, FromArgs)]
533-
#[__inside_vm]
534533
pub struct PrintOptions {
535534
#[pyarg(keyword_only, default = "None")]
536535
sep: Option<PyStringRef>,

vm/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ extern crate rustpython_parser;
3232
#[macro_use]
3333
extern crate rustpython_derive;
3434

35+
extern crate self as rustpython_vm;
36+
3537
pub use rustpython_derive::*;
3638

3739
//extern crate eval; use eval::eval::*;

vm/src/obj/objbytes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use super::objtype::PyClassRef;
2121
/// - a text string encoded using the specified encoding\n \
2222
/// - any object implementing the buffer API.\n \
2323
/// - an integer";
24-
#[pyclass(name = "bytes", __inside_vm)]
24+
#[pyclass(name = "bytes")]
2525
#[derive(Clone, Debug)]
2626
pub struct PyBytes {
2727
inner: PyByteInner,
@@ -71,7 +71,7 @@ pub fn init(context: &PyContext) {
7171
});
7272
}
7373

74-
#[pyimpl(__inside_vm)]
74+
#[pyimpl]
7575
impl PyBytesRef {
7676
#[pymethod(name = "__new__")]
7777
fn bytes_new(

vm/src/obj/objdict.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,13 @@ impl Iterator for DictIter {
327327

328328
macro_rules! dict_iterator {
329329
( $name: ident, $iter_name: ident, $class: ident, $iter_class: ident, $class_name: literal, $iter_class_name: literal, $result_fn: expr) => {
330-
#[pyclass(name = $class_name, __inside_vm)]
330+
#[pyclass(name = $class_name)]
331331
#[derive(Debug)]
332332
struct $name {
333333
pub dict: PyDictRef,
334334
}
335335

336-
#[pyimpl(__inside_vm)]
336+
#[pyimpl]
337337
impl $name {
338338
fn new(dict: PyDictRef) -> Self {
339339
$name { dict: dict }
@@ -356,15 +356,15 @@ macro_rules! dict_iterator {
356356
}
357357
}
358358

359-
#[pyclass(name = $iter_class_name, __inside_vm)]
359+
#[pyclass(name = $iter_class_name)]
360360
#[derive(Debug)]
361361
struct $iter_name {
362362
pub dict: PyDictRef,
363363
pub size: dictdatatype::DictSize,
364364
pub position: Cell<usize>,
365365
}
366366

367-
#[pyimpl(__inside_vm)]
367+
#[pyimpl]
368368
impl $iter_name {
369369
fn new(dict: PyDictRef) -> Self {
370370
$iter_name {

vm/src/obj/objgenerator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::vm::VirtualMachine;
99

1010
pub type PyGeneratorRef = PyRef<PyGenerator>;
1111

12-
#[pyclass(name = "generator", __inside_vm)]
12+
#[pyclass(name = "generator")]
1313
#[derive(Debug)]
1414
pub struct PyGenerator {
1515
frame: FrameRef,
@@ -21,7 +21,7 @@ impl PyValue for PyGenerator {
2121
}
2222
}
2323

24-
#[pyimpl(__inside_vm)]
24+
#[pyimpl]
2525
impl PyGenerator {
2626
pub fn new(frame: FrameRef, vm: &VirtualMachine) -> PyGeneratorRef {
2727
PyGenerator { frame }.into_ref(vm)

vm/src/obj/objint.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::obj::objtype::PyClassRef;
3232
/// Base 0 means to interpret the base from the string as an integer literal.
3333
/// >>> int('0b100', base=0)
3434
/// 4
35-
#[pyclass(__inside_vm)]
35+
#[pyclass]
3636
#[derive(Debug)]
3737
pub struct PyInt {
3838
value: BigInt,
@@ -111,7 +111,7 @@ impl_try_from_object_int!(
111111
(u64, to_u64),
112112
);
113113

114-
#[pyimpl(__inside_vm)]
114+
#[pyimpl]
115115
impl PyInt {
116116
#[pymethod(name = "__eq__")]
117117
fn eq(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
@@ -476,7 +476,6 @@ impl PyInt {
476476
}
477477

478478
#[derive(FromArgs)]
479-
#[__inside_vm]
480479
struct IntOptions {
481480
#[pyarg(positional_only, optional = true)]
482481
val_options: OptionalArg<PyObjectRef>,

vm/src/obj/objstr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use super::objtype::{self, PyClassRef};
3030
/// or repr(object).
3131
/// encoding defaults to sys.getdefaultencoding().
3232
/// errors defaults to 'strict'."
33-
#[pyclass(name = "str", __inside_vm)]
33+
#[pyclass(name = "str")]
3434
#[derive(Clone, Debug)]
3535
pub struct PyString {
3636
// TODO: shouldn't be public
@@ -74,7 +74,7 @@ impl TryIntoRef<PyString> for &str {
7474
}
7575
}
7676

77-
#[pyimpl(__inside_vm)]
77+
#[pyimpl]
7878
impl PyString {
7979
// TODO: should with following format
8080
// class str(object='')

0 commit comments

Comments
 (0)