Skip to content

Commit

Permalink
Eliminate glob imports from codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Feb 19, 2024
1 parent 896a730 commit 6eb82a9
Show file tree
Hide file tree
Showing 15 changed files with 4,257 additions and 3,830 deletions.
14 changes: 6 additions & 8 deletions codegen/src/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn expand_impl_body(defs: &Definitions, node: &Node) -> TokenStream {
let variant = Ident::new(variant_name, Span::call_site());
if fields.is_empty() {
quote! {
#ident::#variant => #ident::#variant,
crate::#ident::#variant => crate::#ident::#variant,
}
} else {
let mut pats = Vec::new();
Expand All @@ -37,7 +37,7 @@ fn expand_impl_body(defs: &Definitions, node: &Node) -> TokenStream {
}
quote! {
#cfg
#ident::#variant(#(#pats),*) => #ident::#variant(#(#clones),*),
crate::#ident::#variant(#(#pats),*) => crate::#ident::#variant(#(#clones),*),
}
}
});
Expand All @@ -63,7 +63,7 @@ fn expand_impl_body(defs: &Definitions, node: &Node) -> TokenStream {
#ident: self.#ident.clone(),
}
});
quote!(#ident { #(#fields)* })
quote!(crate::#ident { #(#fields)* })
}
Data::Private => unreachable!(),
}
Expand All @@ -86,9 +86,9 @@ fn expand_impl(defs: &Definitions, node: &Node) -> TokenStream {
if copy {
return quote! {
#cfg_features
impl Copy for #ident {}
impl Copy for crate::#ident {}
#cfg_features
impl Clone for #ident {
impl Clone for crate::#ident {
fn clone(&self) -> Self {
*self
}
Expand All @@ -100,7 +100,7 @@ fn expand_impl(defs: &Definitions, node: &Node) -> TokenStream {

quote! {
#cfg_features
impl Clone for #ident {
impl Clone for crate::#ident {
fn clone(&self) -> Self {
#body
}
Expand All @@ -119,8 +119,6 @@ pub fn generate(defs: &Definitions) -> Result<()> {
quote! {
#![allow(clippy::clone_on_copy, clippy::expl_impl_clone_on_copy)]

use crate::*;

#impls
},
)?;
Expand Down
11 changes: 5 additions & 6 deletions codegen/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn expand_impl_body(
let variant = Ident::new(variant_name, Span::call_site());
if fields.is_empty() {
quote! {
#ident::#variant => formatter.write_str(#variant_name),
crate::#ident::#variant => formatter.write_str(#variant_name),
}
} else {
let mut cfg = None;
Expand All @@ -71,15 +71,15 @@ fn expand_impl_body(
if syntax_tree_enum(type_name, variant_name, fields).is_some() {
quote! {
#cfg
#ident::#variant(v0) => v0.debug(formatter, #variant_name),
crate::#ident::#variant(v0) => v0.debug(formatter, #variant_name),
}
} else {
let pats = (0..fields.len())
.map(|i| format_ident!("v{}", i))
.collect::<Vec<_>>();
quote! {
#cfg
#ident::#variant(#(#pats),*) => {
crate::#ident::#variant(#(#pats),*) => {
let mut formatter = formatter.debug_tuple(#variant_name);
#(formatter.field(#pats);)*
formatter.finish()
Expand Down Expand Up @@ -128,7 +128,7 @@ fn expand_impl_body(

if is_syntax_tree_variant {
quote! {
impl #ident {
impl crate::#ident {
fn debug(&self, formatter: &mut fmt::Formatter, name: &str) -> fmt::Result {
#body
}
Expand Down Expand Up @@ -156,7 +156,7 @@ fn expand_impl(defs: &Definitions, node: &Node, syntax_tree_variants: &Set<&str>

quote! {
#cfg_features
impl Debug for #ident {
impl Debug for crate::#ident {
fn fmt(&self, #formatter: &mut fmt::Formatter) -> fmt::Result {
#body
}
Expand Down Expand Up @@ -185,7 +185,6 @@ pub fn generate(defs: &Definitions) -> Result<()> {
file::write(
DEBUG_SRC,
quote! {
use crate::*;
use std::fmt::{self, Debug};

#impls
Expand Down
9 changes: 4 additions & 5 deletions codegen/src/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn expand_impl_body(defs: &Definitions, node: &Node) -> TokenStream {
let variant = Ident::new(variant_name, Span::call_site());
if fields.is_empty() {
quote! {
(#ident::#variant, #ident::#variant) => true,
(crate::#ident::#variant, crate::#ident::#variant) => true,
}
} else {
let mut this_pats = Vec::new();
Expand Down Expand Up @@ -66,7 +66,7 @@ fn expand_impl_body(defs: &Definitions, node: &Node) -> TokenStream {
}
quote! {
#cfg
(#ident::#variant(#(#this_pats),*), #ident::#variant(#(#other_pats),*)) => {
(crate::#ident::#variant(#(#this_pats),*), crate::#ident::#variant(#(#other_pats),*)) => {
#(#comparisons)&&*
}
}
Expand Down Expand Up @@ -118,7 +118,7 @@ fn expand_impl(defs: &Definitions, node: &Node) -> TokenStream {

let eq = quote! {
#cfg_features
impl Eq for #ident {}
impl Eq for crate::#ident {}
};

let manual_partial_eq = node.data == Data::Private;
Expand All @@ -137,7 +137,7 @@ fn expand_impl(defs: &Definitions, node: &Node) -> TokenStream {
#eq

#cfg_features
impl PartialEq for #ident {
impl PartialEq for crate::#ident {
fn eq(&self, #other: &Self) -> bool {
#body
}
Expand All @@ -156,7 +156,6 @@ pub fn generate(defs: &Definitions) -> Result<()> {
quote! {
#[cfg(any(feature = "derive", feature = "full"))]
use crate::tt::TokenStreamHelper;
use crate::*;

#impls
},
Expand Down
15 changes: 9 additions & 6 deletions codegen/src/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ fn visit(

fn node(traits: &mut TokenStream, impls: &mut TokenStream, s: &Node, defs: &Definitions) {
let under_name = gen::under_name(&s.ident);
let ty = Ident::new(&s.ident, Span::call_site());
let ident = Ident::new(&s.ident, Span::call_site());
let ty = if let "Ident" | "Span" = s.ident.as_str() {
quote!(proc_macro2::#ident)
} else {
quote!(crate::#ident)
};
let fold_fn = format_ident!("fold_{}", under_name);

let mut fold_impl = TokenStream::new();
Expand Down Expand Up @@ -149,7 +154,7 @@ fn node(traits: &mut TokenStream, impls: &mut TokenStream, s: &Node, defs: &Defi
}

if fields.is_empty() {
if ty == "Ident" {
if s.ident == "Ident" {
fold_impl.extend(quote! {
let mut node = node;
let span = f.fold_span(node.span());
Expand All @@ -168,7 +173,7 @@ fn node(traits: &mut TokenStream, impls: &mut TokenStream, s: &Node, defs: &Defi
}
}
Data::Private => {
if ty == "Ident" {
if s.ident == "Ident" {
fold_impl.extend(quote! {
let mut node = node;
let span = f.fold_span(node.span());
Expand Down Expand Up @@ -223,9 +228,7 @@ pub fn generate(defs: &Definitions) -> Result<()> {
)]

#[cfg(any(feature = "full", feature = "derive"))]
use crate::gen::helper::fold::*;
use crate::*;
use proc_macro2::Span;
use crate::gen::helper::fold::FoldHelper;

#full_macro

Expand Down
7 changes: 3 additions & 4 deletions codegen/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn expand_impl_body(defs: &Definitions, node: &Node) -> TokenStream {
let variant = Ident::new(variant_name, Span::call_site());
if fields.is_empty() {
quote! {
#ident::#variant => {
crate::#ident::#variant => {
state.write_u8(#i);
}
}
Expand Down Expand Up @@ -69,7 +69,7 @@ fn expand_impl_body(defs: &Definitions, node: &Node) -> TokenStream {
}
quote! {
#cfg
#ident::#variant(#(#pats),*) => {
crate::#ident::#variant(#(#pats),*) => {
state.write_u8(#i);
#(#hashes)*
}
Expand Down Expand Up @@ -135,7 +135,7 @@ fn expand_impl(defs: &Definitions, node: &Node) -> TokenStream {

quote! {
#cfg_features
impl Hash for #ident {
impl Hash for crate::#ident {
fn hash<H>(&self, #hasher: &mut H)
where
H: Hasher,
Expand All @@ -157,7 +157,6 @@ pub fn generate(defs: &Definitions) -> Result<()> {
quote! {
#[cfg(any(feature = "derive", feature = "full"))]
use crate::tt::TokenStreamHelper;
use crate::*;
use std::hash::{Hash, Hasher};

#impls
Expand Down
2 changes: 1 addition & 1 deletion codegen/src/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub enum Operand {
Owned(TokenStream),
}

pub use self::Operand::*;
pub use self::Operand::{Borrowed, Owned};

impl Operand {
pub fn tokens(&self) -> &TokenStream {
Expand Down
11 changes: 7 additions & 4 deletions codegen/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ fn visit(

fn node(traits: &mut TokenStream, impls: &mut TokenStream, s: &Node, defs: &Definitions) {
let under_name = gen::under_name(&s.ident);
let ty = Ident::new(&s.ident, Span::call_site());
let ident = Ident::new(&s.ident, Span::call_site());
let ty = if let "Ident" | "Span" = s.ident.as_str() {
quote!(proc_macro2::#ident)
} else {
quote!(crate::#ident)
};
let visit_fn = format_ident!("visit_{}", under_name);

let mut visit_impl = TokenStream::new();
Expand Down Expand Up @@ -164,7 +169,7 @@ fn node(traits: &mut TokenStream, impls: &mut TokenStream, s: &Node, defs: &Defi
}
}
Data::Private => {
if ty == "Ident" {
if s.ident == "Ident" {
visit_impl.extend(quote! {
v.visit_span(&node.span());
});
Expand Down Expand Up @@ -205,8 +210,6 @@ pub fn generate(defs: &Definitions) -> Result<()> {

#[cfg(any(feature = "full", feature = "derive"))]
use crate::punctuated::Punctuated;
use crate::*;
use proc_macro2::Span;

#full_macro

Expand Down
11 changes: 7 additions & 4 deletions codegen/src/visit_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ fn visit(

fn node(traits: &mut TokenStream, impls: &mut TokenStream, s: &Node, defs: &Definitions) {
let under_name = gen::under_name(&s.ident);
let ty = Ident::new(&s.ident, Span::call_site());
let ident = Ident::new(&s.ident, Span::call_site());
let ty = if let "Ident" | "Span" = s.ident.as_str() {
quote!(proc_macro2::#ident)
} else {
quote!(crate::#ident)
};
let visit_mut_fn = format_ident!("visit_{}_mut", under_name);

let mut visit_mut_impl = TokenStream::new();
Expand Down Expand Up @@ -164,7 +169,7 @@ fn node(traits: &mut TokenStream, impls: &mut TokenStream, s: &Node, defs: &Defi
}
}
Data::Private => {
if ty == "Ident" {
if s.ident == "Ident" {
visit_mut_impl.extend(quote! {
let mut span = node.span();
v.visit_span_mut(&mut span);
Expand Down Expand Up @@ -201,8 +206,6 @@ pub fn generate(defs: &Definitions) -> Result<()> {

#[cfg(any(feature = "full", feature = "derive"))]
use crate::punctuated::Punctuated;
use crate::*;
use proc_macro2::Span;

#full_macro

Expand Down
Loading

0 comments on commit 6eb82a9

Please sign in to comment.