mirror of
https://github.com/swc-project/swc.git
synced 2024-12-24 06:05:02 +03:00
Merge #34
34: Use Span::def_site() instead of Span::call_site() r=kdy1 a=kdy1 * Now rustdoc contains derived implementations.
This commit is contained in:
commit
e128ba64c4
@ -36,6 +36,8 @@ notifications:
|
|||||||
env:
|
env:
|
||||||
global:
|
global:
|
||||||
- RUST_MIN_STACK=4194304
|
- RUST_MIN_STACK=4194304
|
||||||
|
- RUST_BACKTRACE=full
|
||||||
|
- CARGO_INCREMENTAL=0
|
||||||
- RUSTFLAGS="--cfg procmacro2_semver_exempt"
|
- RUSTFLAGS="--cfg procmacro2_semver_exempt"
|
||||||
- secure: Z4RSNlpg/12Qx2fIjS+7TToYxPJQgK70X7u9A5lJiCIa0JfzWCxr1ZEKXfAVLG9o4nQok+nWOZa+vxR1IgyUVnf6oSanjjWl1pSRbvccxMS799NvHmGzIAiqSKAlxiSJuxf7MQbs1XBuI3XahsWLfXGPo7vPT6sKe4AAf9gT6igJr61D5hpHkVIXK7P6rnrWQALQYplyaox0NlU9UlqSXXBjdJfp3138rl7FIeYRsMMow44unBNPvs+mhVP8PWpeFWeop0jxbNbTHwnJUbCm4ZWrvqnJ/m70IMlBMN1AskLmz4KeXOhPx+XR9VtdWBX4q8lJ7s9J0hMBxrEnxgiYVBPMlLoEX+wW3zwZ5F+DQs7uLpRHYRUpxpi/7ZuQjp+uT3mN9PMMSvbHwHLH2r/CC9olKYWySXIsGsGVyyMnZeUwvgzwxiYLoeCWe4zZY99zc7jvGKbSmk0RtPu6hApPwL5A6novXbXL2QsXzqqeWpgMLmZyb7KYhM5IGIAB1oPQIqI++Re9Z+/ea/DRSUJOsA96yRQ+vVbiuClrVgDhaAaJOGYCtR1XZ5N2zRb9+Spu/ECtfisLOb9Xs1584DyRbqG69nRdjuscjYOTFZUlOoOeFvuADY65Jt0kF6u7g8NIDkJ1ROb3heKQtY/bAQUrBNUJydOQnn5tBwn8Z618+Ac=
|
- secure: Z4RSNlpg/12Qx2fIjS+7TToYxPJQgK70X7u9A5lJiCIa0JfzWCxr1ZEKXfAVLG9o4nQok+nWOZa+vxR1IgyUVnf6oSanjjWl1pSRbvccxMS799NvHmGzIAiqSKAlxiSJuxf7MQbs1XBuI3XahsWLfXGPo7vPT6sKe4AAf9gT6igJr61D5hpHkVIXK7P6rnrWQALQYplyaox0NlU9UlqSXXBjdJfp3138rl7FIeYRsMMow44unBNPvs+mhVP8PWpeFWeop0jxbNbTHwnJUbCm4ZWrvqnJ/m70IMlBMN1AskLmz4KeXOhPx+XR9VtdWBX4q8lJ7s9J0hMBxrEnxgiYVBPMlLoEX+wW3zwZ5F+DQs7uLpRHYRUpxpi/7ZuQjp+uT3mN9PMMSvbHwHLH2r/CC9olKYWySXIsGsGVyyMnZeUwvgzwxiYLoeCWe4zZY99zc7jvGKbSmk0RtPu6hApPwL5A6novXbXL2QsXzqqeWpgMLmZyb7KYhM5IGIAB1oPQIqI++Re9Z+/ea/DRSUJOsA96yRQ+vVbiuClrVgDhaAaJOGYCtR1XZ5N2zRb9+Spu/ECtfisLOb9Xs1584DyRbqG69nRdjuscjYOTFZUlOoOeFvuADY65Jt0kF6u7g8NIDkJ1ROb3heKQtY/bAQUrBNUJydOQnn5tBwn8Z618+Ac=
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ pub fn derive(input: DeriveInput) -> ItemImpl {
|
|||||||
.map(|f| f.ty.clone())
|
.map(|f| f.ty.clone())
|
||||||
.map(normalize_type_for_bound)
|
.map(normalize_type_for_bound)
|
||||||
.map(|ty| {
|
.map(|ty| {
|
||||||
Quote::new_call_site()
|
Quote::new(Span::def_site())
|
||||||
.quote_with(smart_quote!(
|
.quote_with(smart_quote!(
|
||||||
Vars { Type: &ty },
|
Vars { Type: &ty },
|
||||||
(Type: swc_common::FoldWith<__Folder>)
|
(Type: swc_common::FoldWith<__Folder>)
|
||||||
@ -45,18 +45,18 @@ pub fn derive(input: DeriveInput) -> ItemImpl {
|
|||||||
.unwrap_or_else(|| {
|
.unwrap_or_else(|| {
|
||||||
Index {
|
Index {
|
||||||
index: binding.idx() as _,
|
index: binding.idx() as _,
|
||||||
span: call_site(),
|
span: def_site(),
|
||||||
}.dump()
|
}.dump()
|
||||||
});
|
});
|
||||||
|
|
||||||
let value = match should_skip_field(binding.field()) {
|
let value = match should_skip_field(binding.field()) {
|
||||||
true => Quote::new_call_site().quote_with(smart_quote!(
|
true => Quote::new(Span::def_site()).quote_with(smart_quote!(
|
||||||
Vars {
|
Vars {
|
||||||
binded_field: binding.name(),
|
binded_field: binding.name(),
|
||||||
},
|
},
|
||||||
{ binded_field }
|
{ binded_field }
|
||||||
)),
|
)),
|
||||||
false => Quote::new_call_site().quote_with(smart_quote!(
|
false => Quote::new(Span::def_site()).quote_with(smart_quote!(
|
||||||
Vars {
|
Vars {
|
||||||
FieldType: &binding.field().ty,
|
FieldType: &binding.field().ty,
|
||||||
binded_field: binding.name(),
|
binded_field: binding.name(),
|
||||||
@ -65,7 +65,7 @@ pub fn derive(input: DeriveInput) -> ItemImpl {
|
|||||||
)),
|
)),
|
||||||
};
|
};
|
||||||
|
|
||||||
let v = Quote::new_call_site()
|
let v = Quote::new(Span::def_site())
|
||||||
.quote_with(smart_quote!(
|
.quote_with(smart_quote!(
|
||||||
Vars { field_name, value },
|
Vars { field_name, value },
|
||||||
(field_name: value)
|
(field_name: value)
|
||||||
@ -82,19 +82,19 @@ pub fn derive(input: DeriveInput) -> ItemImpl {
|
|||||||
..v
|
..v
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.map(|t| Element::Punctuated(t, call_site()))
|
.map(|t| Element::Punctuated(t, def_site()))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let body = match *v.data() {
|
let body = match *v.data() {
|
||||||
// Handle unit like structs separately
|
// Handle unit like structs separately
|
||||||
Fields::Unit => box Quote::new_call_site()
|
Fields::Unit => box Quote::new(Span::def_site())
|
||||||
.quote_with(smart_quote!(Vars { Name: qual_name }, {
|
.quote_with(smart_quote!(Vars { Name: qual_name }, {
|
||||||
{
|
{
|
||||||
return Name;
|
return Name;
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
.parse(),
|
.parse(),
|
||||||
_ => box Quote::new_call_site()
|
_ => box Quote::new(Span::def_site())
|
||||||
.quote_with(smart_quote!(
|
.quote_with(smart_quote!(
|
||||||
Vars {
|
Vars {
|
||||||
Name: qual_name,
|
Name: qual_name,
|
||||||
@ -118,23 +118,23 @@ pub fn derive(input: DeriveInput) -> ItemImpl {
|
|||||||
.collect(),
|
.collect(),
|
||||||
pats: vec![Element::End(pat)].into_iter().collect(),
|
pats: vec![Element::End(pat)].into_iter().collect(),
|
||||||
guard: None,
|
guard: None,
|
||||||
rocket_token: call_site(),
|
rocket_token: def_site(),
|
||||||
comma: Some(call_site()),
|
comma: Some(def_site()),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let body = Expr::Match(ExprMatch {
|
let body = Expr::Match(ExprMatch {
|
||||||
attrs: Default::default(),
|
attrs: Default::default(),
|
||||||
match_token: call_site(),
|
match_token: def_site(),
|
||||||
brace_token: call_site(),
|
brace_token: def_site(),
|
||||||
expr: box Quote::new_call_site()
|
expr: box Quote::new(Span::def_site())
|
||||||
.quote_with(smart_quote!(Vars {}, { self }))
|
.quote_with(smart_quote!(Vars {}, { self }))
|
||||||
.parse(),
|
.parse(),
|
||||||
arms,
|
arms,
|
||||||
});
|
});
|
||||||
|
|
||||||
let item = Quote::new_call_site()
|
let item = Quote::new(Span::def_site())
|
||||||
.quote_with(smart_quote!(
|
.quote_with(smart_quote!(
|
||||||
Vars {
|
Vars {
|
||||||
Type: &input.ident,
|
Type: &input.ident,
|
||||||
|
@ -15,11 +15,10 @@ mod fold;
|
|||||||
#[proc_macro_derive(Fold, attributes(fold))]
|
#[proc_macro_derive(Fold, attributes(fold))]
|
||||||
pub fn derive_fold(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
pub fn derive_fold(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||||
let input = parse::<DeriveInput>(input).expect("failed to parse input as DeriveInput");
|
let input = parse::<DeriveInput>(input).expect("failed to parse input as DeriveInput");
|
||||||
let type_name = input.ident.clone();
|
|
||||||
|
|
||||||
let item = self::fold::derive(input);
|
let item = self::fold::derive(input);
|
||||||
|
|
||||||
wrap_in_const(&format!("DERIVE_FOLD_FOR_{}", type_name), item.dump())
|
print_item(item.dump())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[proc_macro_derive(AstNode)]
|
#[proc_macro_derive(AstNode)]
|
||||||
@ -27,14 +26,14 @@ pub fn derive_ast_node(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
|
|||||||
let input = parse::<DeriveInput>(input).expect("failed to parse input as DeriveInput");
|
let input = parse::<DeriveInput>(input).expect("failed to parse input as DeriveInput");
|
||||||
let type_name = &input.ident;
|
let type_name = &input.ident;
|
||||||
|
|
||||||
let item = Quote::new(Span::call_site())
|
let item = Quote::new(Span::def_site())
|
||||||
.quote_with(smart_quote!(Vars { Type: type_name }, {
|
.quote_with(smart_quote!(Vars { Type: type_name }, {
|
||||||
impl swc_common::AstNode for Type {}
|
impl swc_common::AstNode for Type {}
|
||||||
}))
|
}))
|
||||||
.parse::<ItemImpl>()
|
.parse::<ItemImpl>()
|
||||||
.with_generics(input.generics);
|
.with_generics(input.generics);
|
||||||
|
|
||||||
wrap_in_const(&format!("DERIVE_AST_NODE_FOR_{}", type_name), item.dump())
|
print_item(item.dump())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Alias for
|
/// Alias for
|
||||||
@ -62,19 +61,10 @@ pub fn ast_node(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Workarounds https://github.com/rust-lang/rust/issues/44925
|
/// Workarounds https://github.com/rust-lang/rust/issues/44925
|
||||||
fn wrap_in_const<T: Into<TokenStream>>(const_name: &str, item: T) -> proc_macro::TokenStream {
|
fn print_item<T: Into<TokenStream>>(item: T) -> proc_macro::TokenStream {
|
||||||
let item = Quote::new(Span::call_site()).quote_with(smart_quote!(
|
let item = Quote::new(Span::def_site()).quote_with(smart_quote!(Vars { item: item.into() }, {
|
||||||
Vars {
|
extern crate swc_common;
|
||||||
item: item.into(),
|
item
|
||||||
CONST_NAME: Ident::new(const_name, call_site()),
|
}));
|
||||||
},
|
|
||||||
{
|
|
||||||
#[allow(dead_code, non_upper_case_globals)]
|
|
||||||
const CONST_NAME: () = {
|
|
||||||
extern crate swc_common;
|
|
||||||
item
|
|
||||||
};
|
|
||||||
}
|
|
||||||
));
|
|
||||||
item.into()
|
item.into()
|
||||||
}
|
}
|
||||||
|
@ -116,7 +116,7 @@ impl<'a> VariantBinder<'a> {
|
|||||||
/// `EnumName::VariantName` for enum, and `StructName` for struct.
|
/// `EnumName::VariantName` for enum, and `StructName` for struct.
|
||||||
pub fn qual_path(&self) -> Path {
|
pub fn qual_path(&self) -> Path {
|
||||||
match self.enum_name {
|
match self.enum_name {
|
||||||
Some(enum_name) => Quote::new_call_site()
|
Some(enum_name) => Quote::new(Span::def_site())
|
||||||
.quote_with(smart_quote!(
|
.quote_with(smart_quote!(
|
||||||
Vars {
|
Vars {
|
||||||
EnumName: enum_name,
|
EnumName: enum_name,
|
||||||
@ -214,7 +214,7 @@ impl<'a> VariantBinder<'a> {
|
|||||||
.map(|(idx, f)| {
|
.map(|(idx, f)| {
|
||||||
f.map_item(|f| {
|
f.map_item(|f| {
|
||||||
let binded_ident =
|
let binded_ident =
|
||||||
Span::call_site().new_ident(format!("{}{}", prefix, idx));
|
Span::def_site().new_ident(format!("{}{}", prefix, idx));
|
||||||
bindings.push(BindedField {
|
bindings.push(BindedField {
|
||||||
idx,
|
idx,
|
||||||
binded_ident: binded_ident.clone(),
|
binded_ident: binded_ident.clone(),
|
||||||
|
@ -73,21 +73,21 @@ impl<'a> Derive<'a> {
|
|||||||
where
|
where
|
||||||
I: IntoIterator<Item = WherePredicate>,
|
I: IntoIterator<Item = WherePredicate>,
|
||||||
{
|
{
|
||||||
let preds = preds.into_iter().map(|t| Pair::Punctuated(t, call_site()));
|
let preds = preds.into_iter().map(|t| Pair::Punctuated(t, def_site()));
|
||||||
|
|
||||||
match self.out.generics.where_clause {
|
match self.out.generics.where_clause {
|
||||||
Some(WhereClause {
|
Some(WhereClause {
|
||||||
ref mut predicates, ..
|
ref mut predicates, ..
|
||||||
}) => {
|
}) => {
|
||||||
if !predicates.empty_or_trailing() {
|
if !predicates.empty_or_trailing() {
|
||||||
predicates.push_punct(call_site());
|
predicates.push_punct(def_site());
|
||||||
}
|
}
|
||||||
|
|
||||||
predicates.extend(preds)
|
predicates.extend(preds)
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
self.out.generics.where_clause = Some(WhereClause {
|
self.out.generics.where_clause = Some(WhereClause {
|
||||||
where_token: call_site(),
|
where_token: def_site(),
|
||||||
predicates: preds.collect(),
|
predicates: preds.collect(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use call_site;
|
use def_site;
|
||||||
use pmutil::ToTokensExt;
|
use pmutil::ToTokensExt;
|
||||||
use quote::{ToTokens, Tokens};
|
use quote::{ToTokens, Tokens};
|
||||||
use std::iter;
|
use std::iter;
|
||||||
@ -62,8 +62,8 @@ impl<'a> Derive<'a> {
|
|||||||
input,
|
input,
|
||||||
out: ItemImpl {
|
out: ItemImpl {
|
||||||
attrs: vec![],
|
attrs: vec![],
|
||||||
impl_token: call_site(),
|
impl_token: def_site(),
|
||||||
brace_token: call_site(),
|
brace_token: def_site(),
|
||||||
defaultness: None,
|
defaultness: None,
|
||||||
unsafety: None,
|
unsafety: None,
|
||||||
generics,
|
generics,
|
||||||
@ -90,7 +90,7 @@ impl<'a> Derive<'a> {
|
|||||||
pub fn append_to(mut self, item: ItemImpl) -> ItemImpl {
|
pub fn append_to(mut self, item: ItemImpl) -> ItemImpl {
|
||||||
assert_eq!(self.out.trait_, None);
|
assert_eq!(self.out.trait_, None);
|
||||||
if !self.out.generics.params.empty_or_trailing() {
|
if !self.out.generics.params.empty_or_trailing() {
|
||||||
self.out.generics.params.push_punct(call_site());
|
self.out.generics.params.push_punct(def_site());
|
||||||
}
|
}
|
||||||
|
|
||||||
self.out
|
self.out
|
||||||
|
@ -23,6 +23,10 @@ pub fn call_site<T: FromSpan>() -> T {
|
|||||||
Span::call_site().as_token()
|
Span::call_site().as_token()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn def_site<T: FromSpan>() -> T {
|
||||||
|
Span::def_site().as_token()
|
||||||
|
}
|
||||||
|
|
||||||
/// `attr` - tokens inside `#[]`. e.g. `derive(EqIgnoreSpan)`, ast_node
|
/// `attr` - tokens inside `#[]`. e.g. `derive(EqIgnoreSpan)`, ast_node
|
||||||
pub fn print<T: Into<proc_macro2::TokenStream>>(
|
pub fn print<T: Into<proc_macro2::TokenStream>>(
|
||||||
attr: &'static str,
|
attr: &'static str,
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
pub use super::{call_site, is_attr_name, print};
|
pub use super::{call_site, def_site, is_attr_name, print};
|
||||||
pub use super::binder::{Binder, VariantBinder};
|
pub use super::binder::{Binder, VariantBinder};
|
||||||
pub use super::derive::Derive;
|
pub use super::derive::Derive;
|
||||||
pub use super::syn_ext::{ItemImplExt, PairExt};
|
pub use super::syn_ext::{ItemImplExt, PairExt};
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use call_site;
|
use def_site;
|
||||||
use pmutil::prelude::*;
|
use pmutil::prelude::*;
|
||||||
use syn::*;
|
use syn::*;
|
||||||
use syn::punctuated::Pair;
|
use syn::punctuated::Pair;
|
||||||
@ -10,7 +10,7 @@ pub trait ItemImplExt {
|
|||||||
/// ```rust,ignore
|
/// ```rust,ignore
|
||||||
/// let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
|
/// let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
|
||||||
///
|
///
|
||||||
/// let item: Item = Quote::new_call_site()
|
/// let item: Item = Quote::new(Span::def_site())
|
||||||
/// .quote_with(smart_quote!(
|
/// .quote_with(smart_quote!(
|
||||||
/// Vars {
|
/// Vars {
|
||||||
/// Type: type_name,
|
/// Type: type_name,
|
||||||
@ -29,7 +29,7 @@ pub trait ItemImplExt {
|
|||||||
/// You can use this like
|
/// You can use this like
|
||||||
///
|
///
|
||||||
/// ```rust,ignore
|
/// ```rust,ignore
|
||||||
// let item = Quote::new_call_site()
|
// let item = Quote::new(Span::def_site())
|
||||||
/// .quote_with(smart_quote!(Vars { Type: type_name }, {
|
/// .quote_with(smart_quote!(Vars { Type: type_name }, {
|
||||||
/// impl ::swc_common::AstNode for Type {}
|
/// impl ::swc_common::AstNode for Type {}
|
||||||
/// }))
|
/// }))
|
||||||
@ -45,7 +45,7 @@ impl ItemImplExt for ItemImpl {
|
|||||||
|
|
||||||
let need_new_punct = !generics.params.empty_or_trailing();
|
let need_new_punct = !generics.params.empty_or_trailing();
|
||||||
if need_new_punct {
|
if need_new_punct {
|
||||||
generics.params.push_punct(call_site());
|
generics.params.push_punct(def_site());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Respan
|
// Respan
|
||||||
|
@ -66,7 +66,7 @@ impl FnDef {
|
|||||||
attrs: Default::default(),
|
attrs: Default::default(),
|
||||||
lit: Lit::Bool(LitBool {
|
lit: Lit::Bool(LitBool {
|
||||||
value: false,
|
value: false,
|
||||||
span: Span::call_site(),
|
span: Span::def_site(),
|
||||||
}),
|
}),
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user