2024-04-02 08:50:13 +00:00
|
|
|
#![allow(clippy::uninlined_format_args)]
|
|
|
|
|
2020-06-20 20:20:53 +00:00
|
|
|
#[macro_use]
|
|
|
|
mod macros;
|
|
|
|
|
|
|
|
use proc_macro2::{Delimiter, Group, Ident, Span, TokenStream, TokenTree};
|
|
|
|
use quote::quote;
|
2021-01-06 13:06:38 +00:00
|
|
|
use syn::{Item, ItemTrait};
|
2020-06-20 20:20:53 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_macro_variable_attr() {
|
|
|
|
// mimics the token stream corresponding to `$attr fn f() {}`
|
|
|
|
let tokens = TokenStream::from_iter(vec![
|
|
|
|
TokenTree::Group(Group::new(Delimiter::None, quote! { #[test] })),
|
|
|
|
TokenTree::Ident(Ident::new("fn", Span::call_site())),
|
|
|
|
TokenTree::Ident(Ident::new("f", Span::call_site())),
|
|
|
|
TokenTree::Group(Group::new(Delimiter::Parenthesis, TokenStream::new())),
|
|
|
|
TokenTree::Group(Group::new(Delimiter::Brace, TokenStream::new())),
|
|
|
|
]);
|
|
|
|
|
|
|
|
snapshot!(tokens as Item, @r###"
|
|
|
|
Item::Fn {
|
|
|
|
attrs: [
|
|
|
|
Attribute {
|
2024-04-02 08:50:13 +00:00
|
|
|
style: AttrStyle::Outer,
|
|
|
|
meta: Meta::Path {
|
2020-06-20 20:20:53 +00:00
|
|
|
segments: [
|
|
|
|
PathSegment {
|
|
|
|
ident: "test",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2024-04-02 08:50:13 +00:00
|
|
|
vis: Visibility::Inherited,
|
2020-06-20 20:20:53 +00:00
|
|
|
sig: Signature {
|
|
|
|
ident: "f",
|
|
|
|
generics: Generics,
|
2024-04-02 08:50:13 +00:00
|
|
|
output: ReturnType::Default,
|
|
|
|
},
|
|
|
|
block: Block {
|
|
|
|
stmts: [],
|
2020-06-20 20:20:53 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
"###);
|
|
|
|
}
|
2020-11-30 04:48:58 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_negative_impl() {
|
|
|
|
// Rustc parses all of the following.
|
|
|
|
|
|
|
|
#[cfg(any())]
|
|
|
|
impl ! {}
|
|
|
|
let tokens = quote! {
|
|
|
|
impl ! {}
|
|
|
|
};
|
|
|
|
snapshot!(tokens as Item, @r###"
|
|
|
|
Item::Impl {
|
|
|
|
generics: Generics,
|
|
|
|
self_ty: Type::Never,
|
|
|
|
}
|
|
|
|
"###);
|
|
|
|
|
|
|
|
#[cfg(any())]
|
2020-11-30 05:15:05 +00:00
|
|
|
#[rustfmt::skip]
|
2020-11-30 04:48:58 +00:00
|
|
|
impl !Trait {}
|
|
|
|
let tokens = quote! {
|
|
|
|
impl !Trait {}
|
|
|
|
};
|
|
|
|
snapshot!(tokens as Item, @r###"
|
|
|
|
Item::Impl {
|
|
|
|
generics: Generics,
|
2024-04-02 08:50:13 +00:00
|
|
|
self_ty: Type::Verbatim(`! Trait`),
|
2020-11-30 04:48:58 +00:00
|
|
|
}
|
|
|
|
"###);
|
|
|
|
|
|
|
|
#[cfg(any())]
|
|
|
|
impl !Trait for T {}
|
|
|
|
let tokens = quote! {
|
|
|
|
impl !Trait for T {}
|
|
|
|
};
|
|
|
|
snapshot!(tokens as Item, @r###"
|
|
|
|
Item::Impl {
|
|
|
|
generics: Generics,
|
|
|
|
trait_: Some((
|
|
|
|
Some,
|
|
|
|
Path {
|
|
|
|
segments: [
|
|
|
|
PathSegment {
|
|
|
|
ident: "Trait",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)),
|
|
|
|
self_ty: Type::Path {
|
|
|
|
path: Path {
|
|
|
|
segments: [
|
|
|
|
PathSegment {
|
|
|
|
ident: "T",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
"###);
|
|
|
|
|
|
|
|
#[cfg(any())]
|
2020-11-30 05:15:05 +00:00
|
|
|
#[rustfmt::skip]
|
2020-11-30 04:48:58 +00:00
|
|
|
impl !! {}
|
|
|
|
let tokens = quote! {
|
|
|
|
impl !! {}
|
|
|
|
};
|
|
|
|
snapshot!(tokens as Item, @r###"
|
|
|
|
Item::Impl {
|
|
|
|
generics: Generics,
|
2024-04-02 08:50:13 +00:00
|
|
|
self_ty: Type::Verbatim(`! !`),
|
2020-11-30 04:48:58 +00:00
|
|
|
}
|
|
|
|
"###);
|
|
|
|
}
|
2020-12-07 18:31:57 +00:00
|
|
|
|
2020-12-07 18:31:06 +00:00
|
|
|
#[test]
|
|
|
|
fn test_macro_variable_impl() {
|
|
|
|
// mimics the token stream corresponding to `impl $trait for $ty {}`
|
|
|
|
let tokens = TokenStream::from_iter(vec![
|
|
|
|
TokenTree::Ident(Ident::new("impl", Span::call_site())),
|
|
|
|
TokenTree::Group(Group::new(Delimiter::None, quote!(Trait))),
|
|
|
|
TokenTree::Ident(Ident::new("for", Span::call_site())),
|
|
|
|
TokenTree::Group(Group::new(Delimiter::None, quote!(Type))),
|
|
|
|
TokenTree::Group(Group::new(Delimiter::Brace, TokenStream::new())),
|
|
|
|
]);
|
|
|
|
|
|
|
|
snapshot!(tokens as Item, @r###"
|
|
|
|
Item::Impl {
|
|
|
|
generics: Generics,
|
|
|
|
trait_: Some((
|
|
|
|
None,
|
|
|
|
Path {
|
|
|
|
segments: [
|
|
|
|
PathSegment {
|
|
|
|
ident: "Trait",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)),
|
|
|
|
self_ty: Type::Group {
|
|
|
|
elem: Type::Path {
|
|
|
|
path: Path {
|
|
|
|
segments: [
|
|
|
|
PathSegment {
|
|
|
|
ident: "Type",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
"###);
|
|
|
|
}
|
2021-01-06 13:06:38 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_supertraits() {
|
|
|
|
// Rustc parses all of the following.
|
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let tokens = quote!(trait Trait where {});
|
|
|
|
snapshot!(tokens as ItemTrait, @r###"
|
|
|
|
ItemTrait {
|
2024-04-02 08:50:13 +00:00
|
|
|
vis: Visibility::Inherited,
|
2021-01-06 13:06:38 +00:00
|
|
|
ident: "Trait",
|
|
|
|
generics: Generics {
|
|
|
|
where_clause: Some(WhereClause),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
"###);
|
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let tokens = quote!(trait Trait: where {});
|
|
|
|
snapshot!(tokens as ItemTrait, @r###"
|
|
|
|
ItemTrait {
|
2024-04-02 08:50:13 +00:00
|
|
|
vis: Visibility::Inherited,
|
2021-01-06 13:06:38 +00:00
|
|
|
ident: "Trait",
|
|
|
|
generics: Generics {
|
|
|
|
where_clause: Some(WhereClause),
|
|
|
|
},
|
|
|
|
colon_token: Some,
|
|
|
|
}
|
|
|
|
"###);
|
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let tokens = quote!(trait Trait: Sized where {});
|
|
|
|
snapshot!(tokens as ItemTrait, @r###"
|
|
|
|
ItemTrait {
|
2024-04-02 08:50:13 +00:00
|
|
|
vis: Visibility::Inherited,
|
2021-01-06 13:06:38 +00:00
|
|
|
ident: "Trait",
|
|
|
|
generics: Generics {
|
|
|
|
where_clause: Some(WhereClause),
|
|
|
|
},
|
|
|
|
colon_token: Some,
|
|
|
|
supertraits: [
|
2024-04-02 08:50:13 +00:00
|
|
|
TypeParamBound::Trait(TraitBound {
|
2021-01-06 13:06:38 +00:00
|
|
|
path: Path {
|
|
|
|
segments: [
|
|
|
|
PathSegment {
|
|
|
|
ident: "Sized",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"###);
|
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let tokens = quote!(trait Trait: Sized + where {});
|
|
|
|
snapshot!(tokens as ItemTrait, @r###"
|
|
|
|
ItemTrait {
|
2024-04-02 08:50:13 +00:00
|
|
|
vis: Visibility::Inherited,
|
2021-01-06 13:06:38 +00:00
|
|
|
ident: "Trait",
|
|
|
|
generics: Generics {
|
|
|
|
where_clause: Some(WhereClause),
|
|
|
|
},
|
|
|
|
colon_token: Some,
|
|
|
|
supertraits: [
|
2024-04-02 08:50:13 +00:00
|
|
|
TypeParamBound::Trait(TraitBound {
|
2021-01-06 13:06:38 +00:00
|
|
|
path: Path {
|
|
|
|
segments: [
|
|
|
|
PathSegment {
|
|
|
|
ident: "Sized",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}),
|
2024-04-02 08:50:13 +00:00
|
|
|
Token![+],
|
2021-01-06 13:06:38 +00:00
|
|
|
],
|
|
|
|
}
|
|
|
|
"###);
|
|
|
|
}
|
2021-03-09 18:45:44 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_type_empty_bounds() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let tokens = quote! {
|
|
|
|
trait Foo {
|
|
|
|
type Bar: ;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
snapshot!(tokens as ItemTrait, @r###"
|
|
|
|
ItemTrait {
|
2024-04-02 08:50:13 +00:00
|
|
|
vis: Visibility::Inherited,
|
2021-03-09 18:45:44 +00:00
|
|
|
ident: "Foo",
|
|
|
|
generics: Generics,
|
|
|
|
items: [
|
|
|
|
TraitItem::Type {
|
|
|
|
ident: "Bar",
|
|
|
|
generics: Generics,
|
|
|
|
colon_token: Some,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
"###);
|
|
|
|
}
|
2021-04-27 05:21:58 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_impl_visibility() {
|
|
|
|
let tokens = quote! {
|
|
|
|
pub default unsafe impl union {}
|
|
|
|
};
|
|
|
|
|
2024-04-02 08:50:13 +00:00
|
|
|
snapshot!(tokens as Item, @"Item::Verbatim(`pub default unsafe impl union { }`)");
|
2021-04-27 05:21:58 +00:00
|
|
|
}
|
2021-04-27 13:55:35 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_impl_type_parameter_defaults() {
|
|
|
|
#[cfg(any())]
|
|
|
|
impl<T = ()> () {}
|
|
|
|
let tokens = quote! {
|
|
|
|
impl<T = ()> () {}
|
|
|
|
};
|
|
|
|
snapshot!(tokens as Item, @r###"
|
|
|
|
Item::Impl {
|
|
|
|
generics: Generics {
|
|
|
|
lt_token: Some,
|
|
|
|
params: [
|
2024-04-02 08:50:13 +00:00
|
|
|
GenericParam::Type(TypeParam {
|
2021-04-27 13:55:35 +00:00
|
|
|
ident: "T",
|
|
|
|
eq_token: Some,
|
|
|
|
default: Some(Type::Tuple),
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
gt_token: Some,
|
|
|
|
},
|
|
|
|
self_ty: Type::Tuple,
|
2024-04-02 08:50:13 +00:00
|
|
|
}
|
|
|
|
"###);
|
2021-04-27 13:55:35 +00:00
|
|
|
}
|
2021-10-03 01:08:43 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_impl_trait_trailing_plus() {
|
|
|
|
let tokens = quote! {
|
|
|
|
fn f() -> impl Sized + {}
|
|
|
|
};
|
|
|
|
|
|
|
|
snapshot!(tokens as Item, @r###"
|
|
|
|
Item::Fn {
|
2024-04-02 08:50:13 +00:00
|
|
|
vis: Visibility::Inherited,
|
2021-10-03 01:08:43 +00:00
|
|
|
sig: Signature {
|
|
|
|
ident: "f",
|
|
|
|
generics: Generics,
|
2024-04-02 08:50:13 +00:00
|
|
|
output: ReturnType::Type(
|
2021-10-03 01:08:43 +00:00
|
|
|
Type::ImplTrait {
|
|
|
|
bounds: [
|
2024-04-02 08:50:13 +00:00
|
|
|
TypeParamBound::Trait(TraitBound {
|
2021-10-03 01:08:43 +00:00
|
|
|
path: Path {
|
|
|
|
segments: [
|
|
|
|
PathSegment {
|
|
|
|
ident: "Sized",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}),
|
2024-04-02 08:50:13 +00:00
|
|
|
Token![+],
|
2021-10-03 01:08:43 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
2024-04-02 08:50:13 +00:00
|
|
|
block: Block {
|
|
|
|
stmts: [],
|
|
|
|
},
|
2021-10-03 01:08:43 +00:00
|
|
|
}
|
|
|
|
"###);
|
|
|
|
}
|