Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Commit e83de1e

Browse files
committed
Upgrade to syn 0.14
1 parent 6e15664 commit e83de1e

File tree

12 files changed

+118
-100
lines changed

12 files changed

+118
-100
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pyo3 = { path = "../pyo3", features = ["extension-module"], optional = true }
1818
pyo3cls = { path = "../pyo3/pyo3cls", optional = true }
1919
helix = { path = "helix", optional = true }
2020
capybara-derive = { path = "capybara-derive" }
21-
wasm-bindgen = { version = "=0.2.10", optional = true }
21+
wasm-bindgen = { version = "0.2.11", optional = true }
2222

2323
[workspace]
2424
members = [

capybara-derive/Cargo.toml

+5-6
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ name = "capybara-derive"
44
version = "0.1.0"
55

66
[dependencies]
7-
quote = "0.5"
8-
pyo3 = { path = "../../pyo3", features = ["extension-module"], optional = true }
7+
quote = "0.6"
98
pyo3-derive-backend = { path = "../../pyo3/pyo3-derive-backend", optional = true }
109
helix = { path = "../helix", optional = true }
11-
wasm-bindgen-backend = { version = "=0.2.10", optional = true }
10+
wasm-bindgen-backend = { version = "0.2.11", optional = true }
1211

13-
syn = {features = ["full", "extra-traits", "visit-mut"], version = "0.13"}
14-
proc-macro2 = { version = "0.4", features = ["nightly"] }
12+
syn = {features = ["full", "extra-traits", "visit-mut"], version = "0.14"}
13+
proc-macro2 = { version = "0.4" }
1514

1615
[lib]
1716
proc-macro = true
1817

1918
[features]
20-
python = ["pyo3", "pyo3-derive-backend"]
19+
python = ["pyo3-derive-backend"]
2120
ruby = ["helix"]
2221
wasm = ["wasm-bindgen-backend"]

capybara-derive/src/helix_builder.rs

+27-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::BindingBuilder;
2-
use proc_macro::TokenStream;
3-
use quote::Tokens;
2+
use proc_macro2::{Span, TokenStream};
43
use syn;
54

65
pub struct HelixBuilder;
@@ -15,7 +14,7 @@ const INITIALIZE_HELIX: &'static str = "initialize";
1514

1615
impl HelixBuilder {
1716
/// Parses the function into the form codegen_ruby_init wants
18-
fn parse_into_macro_part(&self, method: &syn::ImplItemMethod, is_new: bool) -> Tokens {
17+
fn parse_into_macro_part(&self, method: &syn::ImplItemMethod, is_new: bool) -> TokenStream {
1918
let output = match method.sig.decl.output {
2019
syn::ReturnType::Default => quote!(()),
2120
syn::ReturnType::Type(_, ref ty) => quote!(#ty),
@@ -28,23 +27,27 @@ impl HelixBuilder {
2827
.first()
2928
.map(syn::punctuated::Pair::into_value);
3029

31-
let method_type = match first {
32-
Some(syn::FnArg::SelfRef(_)) => quote!(instance_method),
33-
Some(syn::FnArg::SelfValue(_)) => quote!(instance_method),
34-
_ => if is_new {
35-
quote!(initializer)
36-
} else {
37-
quote!(class_method)
38-
},
39-
};
40-
30+
let method_type;
31+
let args;
4132
let inputs = &method.sig.decl.inputs;
42-
// Helix expects the argument list to be without self
43-
let args = if method_type == quote!(instance_method) {
44-
let inputs = inputs.iter().skip(1);
45-
quote!([#(#inputs),*])
46-
} else {
47-
quote!([#(#inputs),*])
33+
34+
match first {
35+
Some(syn::FnArg::SelfRef(_)) | Some(syn::FnArg::SelfValue(_)) => {
36+
// Helix expects the argument list to be without self
37+
let inputs = inputs.iter().skip(1);
38+
args = quote!([#(#inputs),*]);
39+
40+
method_type = quote!(instance_method);
41+
}
42+
_ => {
43+
args = quote!([#(#inputs),*]);
44+
45+
if is_new {
46+
method_type = quote!(initializer);
47+
} else {
48+
method_type = quote!(class_method);
49+
}
50+
}
4851
};
4952

5053
let self_tt = match first {
@@ -90,11 +93,11 @@ impl HelixBuilder {
9093
}
9194

9295
/// Mainly rewriting the new function into the initialize function helix wants
93-
fn method(&self, mut method: syn::ImplItemMethod) -> (syn::ImplItemMethod, Tokens) {
94-
let is_new = method.sig.ident == syn::Ident::from("new");
96+
fn method(&self, mut method: syn::ImplItemMethod) -> (syn::ImplItemMethod, TokenStream) {
97+
let is_new = method.sig.ident == "new";
9598

9699
if is_new {
97-
method.sig.ident = syn::Ident::from(INITIALIZE_HELIX);
100+
method.sig.ident = syn::Ident::new(INITIALIZE_HELIX, Span::call_site());
98101

99102
super::remove_constructor_attribute(&mut method);
100103
}
@@ -131,7 +134,7 @@ impl HelixBuilder {
131134
impl BindingBuilder for HelixBuilder {
132135
/// Calls codegen_from_struct!
133136
fn class(&self, _: TokenStream, input: TokenStream) -> TokenStream {
134-
let class: syn::ItemStruct = syn::parse(input).unwrap();
137+
let class: syn::ItemStruct = syn::parse2(input).unwrap();
135138
let rust_name = &class.ident;
136139
let struct_body = class.struct_token;
137140

@@ -159,7 +162,7 @@ impl BindingBuilder for HelixBuilder {
159162
/// Handles some parsing boilerplate and the invocation of codegen_ruby_init!. The actual work
160163
/// is done by [HelixBuilder::method] and [HelixBuild::parse_into_macro_part]
161164
fn methods(&self, _: TokenStream, input: TokenStream) -> TokenStream {
162-
let mut impl_block: syn::ItemImpl = syn::parse(input).unwrap();
165+
let mut impl_block: syn::ItemImpl = syn::parse2(input).unwrap();
163166
let rust_name = impl_block.self_ty.clone();
164167

165168
let mut methods_tokens = vec![];

capybara-derive/src/lib.rs

+18-13
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ extern crate proc_macro2;
1111

1212
#[macro_use]
1313
extern crate quote;
14-
14+
#[macro_use]
1515
extern crate syn;
1616

17-
use proc_macro::TokenStream;
17+
use proc_macro2::TokenStream;
1818

1919
#[cfg(feature = "python")]
2020
mod pyo3_builder;
@@ -30,24 +30,30 @@ mod stub_builder;
3030
/// The heart of capybara: This attribute can be added to a struct to generate bindings for that struct,
3131
/// and then also to a plain impl block (i.e. not a trait implementation).
3232
#[proc_macro_attribute]
33-
pub fn capybara_bindgen(attr: TokenStream, input: TokenStream) -> TokenStream {
33+
pub fn capybara_bindgen(
34+
attr: proc_macro::TokenStream,
35+
input: proc_macro::TokenStream,
36+
) -> proc_macro::TokenStream {
3437
capybara_bindgen_impl(attr, input)
3538
}
3639

37-
fn capybara_bindgen_impl(attr: TokenStream, input: TokenStream) -> TokenStream {
40+
fn capybara_bindgen_impl(
41+
attr: proc_macro::TokenStream,
42+
input: proc_macro::TokenStream,
43+
) -> proc_macro::TokenStream {
3844
let item: syn::Item = syn::parse(input.clone()).unwrap();
3945

4046
let builder = get_builder();
4147

4248
let generated = match item {
43-
syn::Item::ForeignMod(_) => builder.foreign_mod(attr, input),
44-
syn::Item::Struct(_) => builder.class(attr, input),
45-
syn::Item::Impl(_) => builder.methods(attr, input),
46-
syn::Item::Fn(_) => builder.function(attr, input),
49+
syn::Item::ForeignMod(_) => builder.foreign_mod(attr.into(), input.into()),
50+
syn::Item::Struct(_) => builder.class(attr.into(), input.into()),
51+
syn::Item::Impl(_) => builder.methods(attr.into(), input.into()),
52+
syn::Item::Fn(_) => builder.function(attr.into(), input.into()),
4753
_ => panic!("This kind of item isn't supported"),
4854
};
4955

50-
generated
56+
generated.into()
5157
}
5258

5359
/// A workaround for getting feaature-independent typings
@@ -93,10 +99,9 @@ trait BindingBuilder {
9399

94100
#[allow(dead_code)]
95101
fn remove_constructor_attribute(method: &mut syn::ImplItemMethod) {
96-
let attribute_pos = method
97-
.attrs
98-
.iter()
99-
.position(|x| quote!(#x) == quote!(#[capybara_bindgen(constructor)]));
102+
let constructor_attr: syn::Attribute = parse_quote!(#[capybara_bindgen(constructor)]);
103+
104+
let attribute_pos = method.attrs.iter().position(|x| x == &constructor_attr);
100105

101106
match attribute_pos {
102107
None => panic!("A constructor must have a #[capybara_bindgen(constructor)] annotation"),

capybara-derive/src/pyo3_builder.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
//! Beware, lands of ugly syn 0.11 code wrapping an ugly library lie ahead of you
1+
//! Beware, this code is mostly workarounds directly ported from syn 0.11
22
3-
extern crate pyo3;
43
extern crate pyo3_derive_backend;
54
extern crate syn;
65

76
use super::BindingBuilder;
8-
use proc_macro::TokenStream;
7+
use proc_macro2::TokenStream;
98
use syn::buffer::TokenBuffer;
109
use syn::punctuated::Punctuated;
1110
use syn::token::Comma;
@@ -21,10 +20,10 @@ fn attribute_from_str(attr_str: &str) -> syn::Attribute {
2120
/// This is the same boilerplate that pyo3 uses
2221
impl BindingBuilder for Pyo3Builder {
2322
fn class(&self, attr: TokenStream, input: TokenStream) -> TokenStream {
24-
let mut class = syn::parse(input).unwrap();
23+
let mut class = syn::parse2(input).unwrap();
2524

2625
let args: Vec<syn::Expr> = {
27-
let buffer = TokenBuffer::new(attr);
26+
let buffer = TokenBuffer::new2(attr);
2827
let punc = Punctuated::<syn::Expr, Comma>::parse_terminated(buffer.begin());
2928
punc.expect("could not parse macro arguments")
3029
.0
@@ -42,7 +41,7 @@ impl BindingBuilder for Pyo3Builder {
4241
}
4342

4443
fn methods(&self, _: TokenStream, input: TokenStream) -> TokenStream {
45-
let mut impl_block: syn::ItemImpl = syn::parse(input).expect("Expected an impl block");
44+
let mut impl_block: syn::ItemImpl = syn::parse2(input).expect("Expected an impl block");
4645

4746
let classname = impl_block.self_ty.clone();
4847
let constructor = Pyo3Builder::constructor(&mut impl_block.items, &classname);
@@ -82,7 +81,7 @@ impl BindingBuilder for Pyo3Builder {
8281
}
8382

8483
fn function(&self, _: TokenStream, input: TokenStream) -> TokenStream {
85-
let mut item_fn: syn::ItemFn = syn::parse(input).unwrap();
84+
let mut item_fn: syn::ItemFn = syn::parse2(input).unwrap();
8685

8786
let python_name = item_fn.ident.clone();
8887
let expanded =
@@ -111,7 +110,7 @@ impl Pyo3Builder {
111110

112111
// Pyo3 currently can't handle the implicit return type
113112
if method.sig.decl.output == syn::ReturnType::Default {
114-
method.sig.decl.output = syn::parse_str("-> ()").unwrap();
113+
method.sig.decl.output = parse_quote!(-> ());
115114
}
116115

117116
if is_static {
@@ -136,7 +135,7 @@ impl Pyo3Builder {
136135

137136
for (pos, impl_item) in impl_items.iter().enumerate() {
138137
if let syn::ImplItem::Method(ref method) = impl_item {
139-
if method.sig.ident.as_ref() == "new" {
138+
if method.sig.ident == "new" {
140139
rust_new_pos = Some(pos);
141140
rust_new = Some(method.clone());
142141
}
@@ -189,7 +188,7 @@ impl Pyo3Builder {
189188
}
190189
);
191190

192-
let pyo3_new: syn::ImplItem = syn::parse(pyo3_new.into()).unwrap();
191+
let pyo3_new: syn::ImplItem = syn::parse2(pyo3_new.into()).unwrap();
193192

194193
Some((syn::ImplItem::Method(rust_new), pyo3_new, rust_new_pos))
195194
}

capybara-derive/src/stub_builder.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::BindingBuilder;
2-
use proc_macro::TokenStream;
3-
use quote::{ToTokens, Tokens};
2+
use proc_macro2::{Span, TokenStream};
3+
use quote::ToTokens;
44
use syn;
55

66
/// This is stub target that does not emitt any bindings
@@ -14,26 +14,30 @@ impl BindingBuilder for StubBuilder {
1414

1515
/// Removes all the capybara_bindgen attributes
1616
fn methods(&self, _: TokenStream, input: TokenStream) -> TokenStream {
17-
let mut impl_block: syn::ItemImpl = syn::parse(input).unwrap();
17+
let mut impl_block: syn::ItemImpl = syn::parse2(input).unwrap();
1818

1919
struct Walk;
2020

2121
impl<'ast> syn::visit_mut::VisitMut for Walk {
2222
fn visit_impl_item_method_mut(&mut self, method: &mut syn::ImplItemMethod) {
23+
// For some unknown reason parse_quote fails here
24+
let path_segment = syn::PathSegment {
25+
ident: syn::Ident::new("capybara_bindgen", Span::call_site()),
26+
arguments: syn::PathArguments::None,
27+
};
28+
let path: syn::Path = path_segment.into();
2329
method.attrs = method
2430
.attrs
2531
.clone()
2632
.into_iter()
27-
.filter(|attr| attr.path != syn::parse_str("capybara_bindgen").unwrap())
33+
.filter(|attr| attr.path != path)
2834
.collect();
2935
}
3036
}
3137

3238
syn::visit_mut::VisitMut::visit_item_impl_mut(&mut Walk, &mut impl_block);
3339

34-
let mut tokens = Tokens::new();
35-
impl_block.to_tokens(&mut tokens);
36-
tokens.into()
40+
impl_block.into_token_stream()
3741
}
3842

3943
/// no-op

0 commit comments

Comments
 (0)