Skip to content

Commit 85c6904

Browse files
committed
Remove dependency on syn/visit feature
The builtin visitor is fairly expensive to compile (3700 lines of code), particularly if something else in the dependency graph also enables syn/full. For the usage in serde_derive, it turns out to be easy to replace.
1 parent 2fd5212 commit 85c6904

3 files changed

Lines changed: 96 additions & 5 deletions

File tree

‎serde_derive/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ proc-macro = true
2222
[dependencies]
2323
proc-macro2 = "1.0"
2424
quote = "1.0"
25-
syn = { version = "1.0.58", features = ["visit"] }
25+
syn = "1.0.60"
2626

2727
[dev-dependencies]
2828
serde = { version = "1.0", path = "../serde" }

‎serde_derive/src/bound.rs‎

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::collections::HashSet;
22

33
use syn;
44
use syn::punctuated::{Pair, Punctuated};
5-
use syn::visit::{self, Visit};
65

76
use internals::ast::{Container, Data};
87
use internals::{attr, ungroup};
@@ -112,7 +111,8 @@ pub fn with_bound(
112111
// parameters.
113112
associated_type_usage: Vec<&'ast syn::TypePath>,
114113
}
115-
impl<'ast> Visit<'ast> for FindTyParams<'ast> {
114+
115+
impl<'ast> FindTyParams<'ast> {
116116
fn visit_field(&mut self, field: &'ast syn::Field) {
117117
if let syn::Type::Path(ty) = ungroup(&field.ty) {
118118
if let Some(Pair::Punctuated(t, _)) = ty.path.segments.pairs().next() {
@@ -138,7 +138,98 @@ pub fn with_bound(
138138
self.relevant_type_params.insert(id.clone());
139139
}
140140
}
141-
visit::visit_path(self, path);
141+
for segment in &path.segments {
142+
self.visit_path_segment(segment);
143+
}
144+
}
145+
146+
// Everything below is simply traversing the syntax tree.
147+
148+
fn visit_type(&mut self, ty: &'ast syn::Type) {
149+
match ty {
150+
syn::Type::Array(ty) => self.visit_type(&ty.elem),
151+
syn::Type::BareFn(ty) => {
152+
for arg in &ty.inputs {
153+
self.visit_type(&arg.ty);
154+
}
155+
self.visit_return_type(&ty.output);
156+
}
157+
syn::Type::Group(ty) => self.visit_type(&ty.elem),
158+
syn::Type::ImplTrait(ty) => {
159+
for bound in &ty.bounds {
160+
self.visit_type_param_bound(bound);
161+
}
162+
}
163+
syn::Type::Macro(ty) => self.visit_macro(&ty.mac),
164+
syn::Type::Paren(ty) => self.visit_type(&ty.elem),
165+
syn::Type::Path(ty) => {
166+
if let Some(qself) = &ty.qself {
167+
self.visit_type(&qself.ty);
168+
}
169+
self.visit_path(&ty.path);
170+
}
171+
syn::Type::Ptr(ty) => self.visit_type(&ty.elem),
172+
syn::Type::Reference(ty) => self.visit_type(&ty.elem),
173+
syn::Type::Slice(ty) => self.visit_type(&ty.elem),
174+
syn::Type::TraitObject(ty) => {
175+
for bound in &ty.bounds {
176+
self.visit_type_param_bound(bound);
177+
}
178+
}
179+
syn::Type::Tuple(ty) => {
180+
for elem in &ty.elems {
181+
self.visit_type(elem);
182+
}
183+
}
184+
185+
syn::Type::Infer(_) | syn::Type::Never(_) | syn::Type::Verbatim(_) => {}
186+
187+
#[cfg(test)]
188+
syn::Type::__TestExhaustive(_) => unimplemented!(),
189+
#[cfg(not(test))]
190+
_ => {}
191+
}
192+
}
193+
194+
fn visit_path_segment(&mut self, segment: &'ast syn::PathSegment) {
195+
self.visit_path_arguments(&segment.arguments);
196+
}
197+
198+
fn visit_path_arguments(&mut self, arguments: &'ast syn::PathArguments) {
199+
match arguments {
200+
syn::PathArguments::None => {}
201+
syn::PathArguments::AngleBracketed(arguments) => {
202+
for arg in &arguments.args {
203+
match arg {
204+
syn::GenericArgument::Type(arg) => self.visit_type(arg),
205+
syn::GenericArgument::Binding(arg) => self.visit_type(&arg.ty),
206+
syn::GenericArgument::Lifetime(_)
207+
| syn::GenericArgument::Constraint(_)
208+
| syn::GenericArgument::Const(_) => {}
209+
}
210+
}
211+
}
212+
syn::PathArguments::Parenthesized(arguments) => {
213+
for argument in &arguments.inputs {
214+
self.visit_type(argument);
215+
}
216+
self.visit_return_type(&arguments.output);
217+
}
218+
}
219+
}
220+
221+
fn visit_return_type(&mut self, return_type: &'ast syn::ReturnType) {
222+
match return_type {
223+
syn::ReturnType::Default => {}
224+
syn::ReturnType::Type(_, output) => self.visit_type(output),
225+
}
226+
}
227+
228+
fn visit_type_param_bound(&mut self, bound: &'ast syn::TypeParamBound) {
229+
match bound {
230+
syn::TypeParamBound::Trait(bound) => self.visit_path(&bound.path),
231+
syn::TypeParamBound::Lifetime(_) => {}
232+
}
142233
}
143234

144235
// Type parameter should not be considered used by a macro path.

‎serde_derive_internals/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ path = "lib.rs"
1616
[dependencies]
1717
proc-macro2 = "1.0"
1818
quote = "1.0"
19-
syn = { version = "1.0.33", default-features = false, features = ["derive", "parsing", "printing", "clone-impls"] }
19+
syn = { version = "1.0.60", default-features = false, features = ["derive", "parsing", "printing", "clone-impls"] }
2020

2121
[package.metadata.docs.rs]
2222
targets = ["x86_64-unknown-linux-gnu"]

0 commit comments

Comments
 (0)