| 1 |
#![allow( |
| 2 |
clippy::elidable_lifetime_names, |
| 3 |
clippy::needless_lifetimes, |
| 4 |
clippy::uninlined_format_args |
| 5 |
)] |
| 6 |
|
| 7 |
#[macro_use] |
| 8 |
mod snapshot; |
| 9 |
|
| 10 |
mod debug; |
| 11 |
|
| 12 |
use proc_macro2::{Delimiter, Group, Ident, Span, TokenStream, TokenTree}; |
| 13 |
use quote::quote; |
| 14 |
use syn::{Item, ItemTrait}; |
| 15 |
|
| 16 |
#[test] |
| 17 |
fn test_macro_variable_attr() { |
| 18 |
// mimics the token stream corresponding to `$attr fn f() {}` |
| 19 |
let tokens = TokenStream::from_iter([ |
| 20 |
TokenTree::Group(Group::new(Delimiter::None, quote! { #[test] })), |
| 21 |
TokenTree::Ident(Ident::new("fn", Span::call_site())), |
| 22 |
TokenTree::Ident(Ident::new("f", Span::call_site())), |
| 23 |
TokenTree::Group(Group::new(Delimiter::Parenthesis, TokenStream::new())), |
| 24 |
TokenTree::Group(Group::new(Delimiter::Brace, TokenStream::new())), |
| 25 |
]); |
| 26 |
|
| 27 |
snapshot!(tokens as Item, @r#" |
| 28 |
Item::Fn { |
| 29 |
attrs: [ |
| 30 |
Attribute { |
| 31 |
style: AttrStyle::Outer, |
| 32 |
meta: Meta::Path { |
| 33 |
segments: [ |
| 34 |
PathSegment { |
| 35 |
ident: "test", |
| 36 |
}, |
| 37 |
], |
| 38 |
}, |
| 39 |
}, |
| 40 |
], |
| 41 |
vis: Visibility::Inherited, |
| 42 |
sig: Signature { |
| 43 |
ident: "f", |
| 44 |
generics: Generics, |
| 45 |
output: ReturnType::Default, |
| 46 |
}, |
| 47 |
block: Block { |
| 48 |
stmts: [], |
| 49 |
}, |
| 50 |
} |
| 51 |
"#); |
| 52 |
} |
| 53 |
|
| 54 |
#[test] |
| 55 |
fn test_negative_impl() { |
| 56 |
#[cfg(any())] |
| 57 |
impl ! {} |
| 58 |
let tokens = quote! { |
| 59 |
impl ! {} |
| 60 |
}; |
| 61 |
snapshot!(tokens as Item, @r#" |
| 62 |
Item::Impl { |
| 63 |
generics: Generics, |
| 64 |
self_ty: Type::Never, |
| 65 |
} |
| 66 |
"#); |
| 67 |
|
| 68 |
let tokens = quote! { |
| 69 |
impl !Trait {} |
| 70 |
}; |
| 71 |
let err = syn::parse2::<Item>(tokens).unwrap_err(); |
| 72 |
assert_eq!(err.to_string(), "inherent impls cannot be negative"); |
| 73 |
|
| 74 |
#[cfg(any())] |
| 75 |
impl !Trait for T {} |
| 76 |
let tokens = quote! { |
| 77 |
impl !Trait for T {} |
| 78 |
}; |
| 79 |
snapshot!(tokens as Item, @r#" |
| 80 |
Item::Impl { |
| 81 |
generics: Generics, |
| 82 |
trait_: Some(( |
| 83 |
Some, |
| 84 |
Path { |
| 85 |
segments: [ |
| 86 |
PathSegment { |
| 87 |
ident: "Trait", |
| 88 |
}, |
| 89 |
], |
| 90 |
}, |
| 91 |
)), |
| 92 |
self_ty: Type::Path { |
| 93 |
path: Path { |
| 94 |
segments: [ |
| 95 |
PathSegment { |
| 96 |
ident: "T", |
| 97 |
}, |
| 98 |
], |
| 99 |
}, |
| 100 |
}, |
| 101 |
} |
| 102 |
"#); |
| 103 |
} |
| 104 |
|
| 105 |
#[test] |
| 106 |
fn test_macro_variable_impl() { |
| 107 |
// mimics the token stream corresponding to `impl $trait for $ty {}` |
| 108 |
let tokens = TokenStream::from_iter([ |
| 109 |
TokenTree::Ident(Ident::new("impl", Span::call_site())), |
| 110 |
TokenTree::Group(Group::new(Delimiter::None, quote!(Trait))), |
| 111 |
TokenTree::Ident(Ident::new("for", Span::call_site())), |
| 112 |
TokenTree::Group(Group::new(Delimiter::None, quote!(Type))), |
| 113 |
TokenTree::Group(Group::new(Delimiter::Brace, TokenStream::new())), |
| 114 |
]); |
| 115 |
|
| 116 |
snapshot!(tokens as Item, @r#" |
| 117 |
Item::Impl { |
| 118 |
generics: Generics, |
| 119 |
trait_: Some(( |
| 120 |
None, |
| 121 |
Path { |
| 122 |
segments: [ |
| 123 |
PathSegment { |
| 124 |
ident: "Trait", |
| 125 |
}, |
| 126 |
], |
| 127 |
}, |
| 128 |
)), |
| 129 |
self_ty: Type::Group { |
| 130 |
elem: Type::Path { |
| 131 |
path: Path { |
| 132 |
segments: [ |
| 133 |
PathSegment { |
| 134 |
ident: "Type", |
| 135 |
}, |
| 136 |
], |
| 137 |
}, |
| 138 |
}, |
| 139 |
}, |
| 140 |
} |
| 141 |
"#); |
| 142 |
} |
| 143 |
|
| 144 |
#[test] |
| 145 |
fn test_supertraits() { |
| 146 |
// Rustc parses all of the following. |
| 147 |
|
| 148 |
#[rustfmt::skip] |
| 149 |
let tokens = quote!(trait Trait where {}); |
| 150 |
snapshot!(tokens as ItemTrait, @r#" |
| 151 |
ItemTrait { |
| 152 |
vis: Visibility::Inherited, |
| 153 |
ident: "Trait", |
| 154 |
generics: Generics { |
| 155 |
where_clause: Some(WhereClause), |
| 156 |
}, |
| 157 |
} |
| 158 |
"#); |
| 159 |
|
| 160 |
#[rustfmt::skip] |
| 161 |
let tokens = quote!(trait Trait: where {}); |
| 162 |
snapshot!(tokens as ItemTrait, @r#" |
| 163 |
ItemTrait { |
| 164 |
vis: Visibility::Inherited, |
| 165 |
ident: "Trait", |
| 166 |
generics: Generics { |
| 167 |
where_clause: Some(WhereClause), |
| 168 |
}, |
| 169 |
colon_token: Some, |
| 170 |
} |
| 171 |
"#); |
| 172 |
|
| 173 |
#[rustfmt::skip] |
| 174 |
let tokens = quote!(trait Trait: Sized where {}); |
| 175 |
snapshot!(tokens as ItemTrait, @r#" |
| 176 |
ItemTrait { |
| 177 |
vis: Visibility::Inherited, |
| 178 |
ident: "Trait", |
| 179 |
generics: Generics { |
| 180 |
where_clause: Some(WhereClause), |
| 181 |
}, |
| 182 |
colon_token: Some, |
| 183 |
supertraits: [ |
| 184 |
TypeParamBound::Trait(TraitBound { |
| 185 |
path: Path { |
| 186 |
segments: [ |
| 187 |
PathSegment { |
| 188 |
ident: "Sized", |
| 189 |
}, |
| 190 |
], |
| 191 |
}, |
| 192 |
}), |
| 193 |
], |
| 194 |
} |
| 195 |
"#); |
| 196 |
|
| 197 |
#[rustfmt::skip] |
| 198 |
let tokens = quote!(trait Trait: Sized + where {}); |
| 199 |
snapshot!(tokens as ItemTrait, @r#" |
| 200 |
ItemTrait { |
| 201 |
vis: Visibility::Inherited, |
| 202 |
ident: "Trait", |
| 203 |
generics: Generics { |
| 204 |
where_clause: Some(WhereClause), |
| 205 |
}, |
| 206 |
colon_token: Some, |
| 207 |
supertraits: [ |
| 208 |
TypeParamBound::Trait(TraitBound { |
| 209 |
path: Path { |
| 210 |
segments: [ |
| 211 |
PathSegment { |
| 212 |
ident: "Sized", |
| 213 |
}, |
| 214 |
], |
| 215 |
}, |
| 216 |
}), |
| 217 |
Token![+], |
| 218 |
], |
| 219 |
} |
| 220 |
"#); |
| 221 |
} |
| 222 |
|
| 223 |
#[test] |
| 224 |
fn test_type_empty_bounds() { |
| 225 |
#[rustfmt::skip] |
| 226 |
let tokens = quote! { |
| 227 |
trait Foo { |
| 228 |
type Bar: ; |
| 229 |
} |
| 230 |
}; |
| 231 |
|
| 232 |
snapshot!(tokens as ItemTrait, @r#" |
| 233 |
ItemTrait { |
| 234 |
vis: Visibility::Inherited, |
| 235 |
ident: "Foo", |
| 236 |
generics: Generics, |
| 237 |
items: [ |
| 238 |
TraitItem::Type { |
| 239 |
ident: "Bar", |
| 240 |
generics: Generics, |
| 241 |
colon_token: Some, |
| 242 |
}, |
| 243 |
], |
| 244 |
} |
| 245 |
"#); |
| 246 |
} |
| 247 |
|
| 248 |
#[test] |
| 249 |
fn test_impl_visibility() { |
| 250 |
let tokens = quote! { |
| 251 |
pub default unsafe impl union {} |
| 252 |
}; |
| 253 |
|
| 254 |
snapshot!(tokens as Item, @"Item::Verbatim(`pub default unsafe impl union { }`)"); |
| 255 |
} |
| 256 |
|
| 257 |
#[test] |
| 258 |
fn test_impl_type_parameter_defaults() { |
| 259 |
#[cfg(any())] |
| 260 |
impl<T = ()> () {} |
| 261 |
let tokens = quote! { |
| 262 |
impl<T = ()> () {} |
| 263 |
}; |
| 264 |
snapshot!(tokens as Item, @r#" |
| 265 |
Item::Impl { |
| 266 |
generics: Generics { |
| 267 |
lt_token: Some, |
| 268 |
params: [ |
| 269 |
GenericParam::Type(TypeParam { |
| 270 |
ident: "T", |
| 271 |
eq_token: Some, |
| 272 |
default: Some(Type::Tuple), |
| 273 |
}), |
| 274 |
], |
| 275 |
gt_token: Some, |
| 276 |
}, |
| 277 |
self_ty: Type::Tuple, |
| 278 |
} |
| 279 |
"#); |
| 280 |
} |
| 281 |
|
| 282 |
#[test] |
| 283 |
fn test_impl_trait_trailing_plus() { |
| 284 |
let tokens = quote! { |
| 285 |
fn f() -> impl Sized + {} |
| 286 |
}; |
| 287 |
|
| 288 |
snapshot!(tokens as Item, @r#" |
| 289 |
Item::Fn { |
| 290 |
vis: Visibility::Inherited, |
| 291 |
sig: Signature { |
| 292 |
ident: "f", |
| 293 |
generics: Generics, |
| 294 |
output: ReturnType::Type( |
| 295 |
Type::ImplTrait { |
| 296 |
bounds: [ |
| 297 |
TypeParamBound::Trait(TraitBound { |
| 298 |
path: Path { |
| 299 |
segments: [ |
| 300 |
PathSegment { |
| 301 |
ident: "Sized", |
| 302 |
}, |
| 303 |
], |
| 304 |
}, |
| 305 |
}), |
| 306 |
Token![+], |
| 307 |
], |
| 308 |
}, |
| 309 |
), |
| 310 |
}, |
| 311 |
block: Block { |
| 312 |
stmts: [], |
| 313 |
}, |
| 314 |
} |
| 315 |
"#); |
| 316 |
} |