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:
|
||||
global:
|
||||
- RUST_MIN_STACK=4194304
|
||||
- RUST_BACKTRACE=full
|
||||
- CARGO_INCREMENTAL=0
|
||||
- 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=
|
||||
|
||||
|
@ -14,7 +14,7 @@ pub fn derive(input: DeriveInput) -> ItemImpl {
|
||||
.map(|f| f.ty.clone())
|
||||
.map(normalize_type_for_bound)
|
||||
.map(|ty| {
|
||||
Quote::new_call_site()
|
||||
Quote::new(Span::def_site())
|
||||
.quote_with(smart_quote!(
|
||||
Vars { Type: &ty },
|
||||
(Type: swc_common::FoldWith<__Folder>)
|
||||
@ -45,18 +45,18 @@ pub fn derive(input: DeriveInput) -> ItemImpl {
|
||||
.unwrap_or_else(|| {
|
||||
Index {
|
||||
index: binding.idx() as _,
|
||||
span: call_site(),
|
||||
span: def_site(),
|
||||
}.dump()
|
||||
});
|
||||
|
||||
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 {
|
||||
binded_field: binding.name(),
|
||||
},
|
||||
{ binded_field }
|
||||
)),
|
||||
false => Quote::new_call_site().quote_with(smart_quote!(
|
||||
false => Quote::new(Span::def_site()).quote_with(smart_quote!(
|
||||
Vars {
|
||||
FieldType: &binding.field().ty,
|
||||
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!(
|
||||
Vars { field_name, value },
|
||||
(field_name: value)
|
||||
@ -82,19 +82,19 @@ pub fn derive(input: DeriveInput) -> ItemImpl {
|
||||
..v
|
||||
}
|
||||
})
|
||||
.map(|t| Element::Punctuated(t, call_site()))
|
||||
.map(|t| Element::Punctuated(t, def_site()))
|
||||
.collect();
|
||||
|
||||
let body = match *v.data() {
|
||||
// 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 }, {
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
}))
|
||||
.parse(),
|
||||
_ => box Quote::new_call_site()
|
||||
_ => box Quote::new(Span::def_site())
|
||||
.quote_with(smart_quote!(
|
||||
Vars {
|
||||
Name: qual_name,
|
||||
@ -118,23 +118,23 @@ pub fn derive(input: DeriveInput) -> ItemImpl {
|
||||
.collect(),
|
||||
pats: vec![Element::End(pat)].into_iter().collect(),
|
||||
guard: None,
|
||||
rocket_token: call_site(),
|
||||
comma: Some(call_site()),
|
||||
rocket_token: def_site(),
|
||||
comma: Some(def_site()),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let body = Expr::Match(ExprMatch {
|
||||
attrs: Default::default(),
|
||||
match_token: call_site(),
|
||||
brace_token: call_site(),
|
||||
expr: box Quote::new_call_site()
|
||||
match_token: def_site(),
|
||||
brace_token: def_site(),
|
||||
expr: box Quote::new(Span::def_site())
|
||||
.quote_with(smart_quote!(Vars {}, { self }))
|
||||
.parse(),
|
||||
arms,
|
||||
});
|
||||
|
||||
let item = Quote::new_call_site()
|
||||
let item = Quote::new(Span::def_site())
|
||||
.quote_with(smart_quote!(
|
||||
Vars {
|
||||
Type: &input.ident,
|
||||
|
@ -15,11 +15,10 @@ mod fold;
|
||||
#[proc_macro_derive(Fold, attributes(fold))]
|
||||
pub fn derive_fold(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let input = parse::<DeriveInput>(input).expect("failed to parse input as DeriveInput");
|
||||
let type_name = input.ident.clone();
|
||||
|
||||
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)]
|
||||
@ -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 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 }, {
|
||||
impl swc_common::AstNode for Type {}
|
||||
}))
|
||||
.parse::<ItemImpl>()
|
||||
.with_generics(input.generics);
|
||||
|
||||
wrap_in_const(&format!("DERIVE_AST_NODE_FOR_{}", type_name), item.dump())
|
||||
print_item(item.dump())
|
||||
}
|
||||
|
||||
/// Alias for
|
||||
@ -62,19 +61,10 @@ pub fn ast_node(
|
||||
}
|
||||
|
||||
/// Workarounds https://github.com/rust-lang/rust/issues/44925
|
||||
fn wrap_in_const<T: Into<TokenStream>>(const_name: &str, item: T) -> proc_macro::TokenStream {
|
||||
let item = Quote::new(Span::call_site()).quote_with(smart_quote!(
|
||||
Vars {
|
||||
item: item.into(),
|
||||
CONST_NAME: Ident::new(const_name, call_site()),
|
||||
},
|
||||
{
|
||||
#[allow(dead_code, non_upper_case_globals)]
|
||||
const CONST_NAME: () = {
|
||||
extern crate swc_common;
|
||||
item
|
||||
};
|
||||
}
|
||||
));
|
||||
fn print_item<T: Into<TokenStream>>(item: T) -> proc_macro::TokenStream {
|
||||
let item = Quote::new(Span::def_site()).quote_with(smart_quote!(Vars { item: item.into() }, {
|
||||
extern crate swc_common;
|
||||
item
|
||||
}));
|
||||
item.into()
|
||||
}
|
||||
|
@ -116,7 +116,7 @@ impl<'a> VariantBinder<'a> {
|
||||
/// `EnumName::VariantName` for enum, and `StructName` for struct.
|
||||
pub fn qual_path(&self) -> Path {
|
||||
match self.enum_name {
|
||||
Some(enum_name) => Quote::new_call_site()
|
||||
Some(enum_name) => Quote::new(Span::def_site())
|
||||
.quote_with(smart_quote!(
|
||||
Vars {
|
||||
EnumName: enum_name,
|
||||
@ -214,7 +214,7 @@ impl<'a> VariantBinder<'a> {
|
||||
.map(|(idx, f)| {
|
||||
f.map_item(|f| {
|
||||
let binded_ident =
|
||||
Span::call_site().new_ident(format!("{}{}", prefix, idx));
|
||||
Span::def_site().new_ident(format!("{}{}", prefix, idx));
|
||||
bindings.push(BindedField {
|
||||
idx,
|
||||
binded_ident: binded_ident.clone(),
|
||||
|
@ -73,21 +73,21 @@ impl<'a> Derive<'a> {
|
||||
where
|
||||
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 {
|
||||
Some(WhereClause {
|
||||
ref mut predicates, ..
|
||||
}) => {
|
||||
if !predicates.empty_or_trailing() {
|
||||
predicates.push_punct(call_site());
|
||||
predicates.push_punct(def_site());
|
||||
}
|
||||
|
||||
predicates.extend(preds)
|
||||
}
|
||||
None => {
|
||||
self.out.generics.where_clause = Some(WhereClause {
|
||||
where_token: call_site(),
|
||||
where_token: def_site(),
|
||||
predicates: preds.collect(),
|
||||
})
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
use call_site;
|
||||
use def_site;
|
||||
use pmutil::ToTokensExt;
|
||||
use quote::{ToTokens, Tokens};
|
||||
use std::iter;
|
||||
@ -62,8 +62,8 @@ impl<'a> Derive<'a> {
|
||||
input,
|
||||
out: ItemImpl {
|
||||
attrs: vec![],
|
||||
impl_token: call_site(),
|
||||
brace_token: call_site(),
|
||||
impl_token: def_site(),
|
||||
brace_token: def_site(),
|
||||
defaultness: None,
|
||||
unsafety: None,
|
||||
generics,
|
||||
@ -90,7 +90,7 @@ impl<'a> Derive<'a> {
|
||||
pub fn append_to(mut self, item: ItemImpl) -> ItemImpl {
|
||||
assert_eq!(self.out.trait_, None);
|
||||
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
|
||||
|
@ -23,6 +23,10 @@ pub fn call_site<T: FromSpan>() -> T {
|
||||
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
|
||||
pub fn print<T: Into<proc_macro2::TokenStream>>(
|
||||
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::derive::Derive;
|
||||
pub use super::syn_ext::{ItemImplExt, PairExt};
|
||||
|
@ -1,4 +1,4 @@
|
||||
use call_site;
|
||||
use def_site;
|
||||
use pmutil::prelude::*;
|
||||
use syn::*;
|
||||
use syn::punctuated::Pair;
|
||||
@ -10,7 +10,7 @@ pub trait ItemImplExt {
|
||||
/// ```rust,ignore
|
||||
/// 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!(
|
||||
/// Vars {
|
||||
/// Type: type_name,
|
||||
@ -29,7 +29,7 @@ pub trait ItemImplExt {
|
||||
/// You can use this like
|
||||
///
|
||||
/// ```rust,ignore
|
||||
// let item = Quote::new_call_site()
|
||||
// let item = Quote::new(Span::def_site())
|
||||
/// .quote_with(smart_quote!(Vars { Type: type_name }, {
|
||||
/// impl ::swc_common::AstNode for Type {}
|
||||
/// }))
|
||||
@ -45,7 +45,7 @@ impl ItemImplExt for ItemImpl {
|
||||
|
||||
let need_new_punct = !generics.params.empty_or_trailing();
|
||||
if need_new_punct {
|
||||
generics.params.push_punct(call_site());
|
||||
generics.params.push_punct(def_site());
|
||||
}
|
||||
|
||||
// Respan
|
||||
|
@ -66,7 +66,7 @@ impl FnDef {
|
||||
attrs: Default::default(),
|
||||
lit: Lit::Bool(LitBool {
|
||||
value: false,
|
||||
span: Span::call_site(),
|
||||
span: Span::def_site(),
|
||||
}),
|
||||
}));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user