| 1 |
#![allow( |
| 2 |
clippy::assertions_on_result_states, |
| 3 |
clippy::elidable_lifetime_names, |
| 4 |
clippy::needless_lifetimes, |
| 5 |
clippy::non_ascii_literal, |
| 6 |
clippy::uninlined_format_args |
| 7 |
)] |
| 8 |
|
| 9 |
#[macro_use] |
| 10 |
mod snapshot; |
| 11 |
|
| 12 |
mod debug; |
| 13 |
|
| 14 |
use proc_macro2::{Delimiter, Group, Ident, Span, TokenStream, TokenTree}; |
| 15 |
use quote::{quote, ToTokens as _}; |
| 16 |
use syn::parse::Parser as _; |
| 17 |
use syn::{Block, Stmt}; |
| 18 |
|
| 19 |
#[test] |
| 20 |
fn test_raw_operator() { |
| 21 |
let stmt = syn::parse_str::<Stmt>("let _ = &raw const x;").unwrap(); |
| 22 |
|
| 23 |
snapshot!(stmt, @r#" |
| 24 |
Stmt::Local { |
| 25 |
pat: Pat::Wild, |
| 26 |
init: Some(LocalInit { |
| 27 |
expr: Expr::RawAddr { |
| 28 |
mutability: PointerMutability::Const, |
| 29 |
expr: Expr::Path { |
| 30 |
path: Path { |
| 31 |
segments: [ |
| 32 |
PathSegment { |
| 33 |
ident: "x", |
| 34 |
}, |
| 35 |
], |
| 36 |
}, |
| 37 |
}, |
| 38 |
}, |
| 39 |
}), |
| 40 |
} |
| 41 |
"#); |
| 42 |
} |
| 43 |
|
| 44 |
#[test] |
| 45 |
fn test_raw_variable() { |
| 46 |
let stmt = syn::parse_str::<Stmt>("let _ = &raw;").unwrap(); |
| 47 |
|
| 48 |
snapshot!(stmt, @r#" |
| 49 |
Stmt::Local { |
| 50 |
pat: Pat::Wild, |
| 51 |
init: Some(LocalInit { |
| 52 |
expr: Expr::Reference { |
| 53 |
expr: Expr::Path { |
| 54 |
path: Path { |
| 55 |
segments: [ |
| 56 |
PathSegment { |
| 57 |
ident: "raw", |
| 58 |
}, |
| 59 |
], |
| 60 |
}, |
| 61 |
}, |
| 62 |
}, |
| 63 |
}), |
| 64 |
} |
| 65 |
"#); |
| 66 |
} |
| 67 |
|
| 68 |
#[test] |
| 69 |
fn test_raw_invalid() { |
| 70 |
assert!(syn::parse_str::<Stmt>("let _ = &raw x;").is_err()); |
| 71 |
} |
| 72 |
|
| 73 |
#[test] |
| 74 |
fn test_none_group() { |
| 75 |
// «∅ async fn f() {} ∅» |
| 76 |
let tokens = TokenStream::from_iter([TokenTree::Group(Group::new( |
| 77 |
Delimiter::None, |
| 78 |
TokenStream::from_iter([ |
| 79 |
TokenTree::Ident(Ident::new("async", Span::call_site())), |
| 80 |
TokenTree::Ident(Ident::new("fn", Span::call_site())), |
| 81 |
TokenTree::Ident(Ident::new("f", Span::call_site())), |
| 82 |
TokenTree::Group(Group::new(Delimiter::Parenthesis, TokenStream::new())), |
| 83 |
TokenTree::Group(Group::new(Delimiter::Brace, TokenStream::new())), |
| 84 |
]), |
| 85 |
))]); |
| 86 |
snapshot!(tokens as Stmt, @r#" |
| 87 |
Stmt::Item(Item::Fn { |
| 88 |
vis: Visibility::Inherited, |
| 89 |
sig: Signature { |
| 90 |
asyncness: Some, |
| 91 |
ident: "f", |
| 92 |
generics: Generics, |
| 93 |
output: ReturnType::Default, |
| 94 |
}, |
| 95 |
block: Block { |
| 96 |
stmts: [], |
| 97 |
}, |
| 98 |
}) |
| 99 |
"#); |
| 100 |
|
| 101 |
let tokens = Group::new(Delimiter::None, quote!(let None = None)).to_token_stream(); |
| 102 |
let stmts = Block::parse_within.parse2(tokens).unwrap(); |
| 103 |
snapshot!(stmts, @r#" |
| 104 |
[ |
| 105 |
Stmt::Expr( |
| 106 |
Expr::Group { |
| 107 |
expr: Expr::Let { |
| 108 |
pat: Pat::Ident { |
| 109 |
ident: "None", |
| 110 |
}, |
| 111 |
expr: Expr::Path { |
| 112 |
path: Path { |
| 113 |
segments: [ |
| 114 |
PathSegment { |
| 115 |
ident: "None", |
| 116 |
}, |
| 117 |
], |
| 118 |
}, |
| 119 |
}, |
| 120 |
}, |
| 121 |
}, |
| 122 |
None, |
| 123 |
), |
| 124 |
] |
| 125 |
"#); |
| 126 |
} |
| 127 |
|
| 128 |
#[test] |
| 129 |
fn test_let_dot_dot() { |
| 130 |
let tokens = quote! { |
| 131 |
let .. = 10; |
| 132 |
}; |
| 133 |
|
| 134 |
snapshot!(tokens as Stmt, @r#" |
| 135 |
Stmt::Local { |
| 136 |
pat: Pat::Rest, |
| 137 |
init: Some(LocalInit { |
| 138 |
expr: Expr::Lit { |
| 139 |
lit: 10, |
| 140 |
}, |
| 141 |
}), |
| 142 |
} |
| 143 |
"#); |
| 144 |
} |
| 145 |
|
| 146 |
#[test] |
| 147 |
fn test_let_else() { |
| 148 |
let tokens = quote! { |
| 149 |
let Some(x) = None else { return 0; }; |
| 150 |
}; |
| 151 |
|
| 152 |
snapshot!(tokens as Stmt, @r#" |
| 153 |
Stmt::Local { |
| 154 |
pat: Pat::TupleStruct { |
| 155 |
path: Path { |
| 156 |
segments: [ |
| 157 |
PathSegment { |
| 158 |
ident: "Some", |
| 159 |
}, |
| 160 |
], |
| 161 |
}, |
| 162 |
elems: [ |
| 163 |
Pat::Ident { |
| 164 |
ident: "x", |
| 165 |
}, |
| 166 |
], |
| 167 |
}, |
| 168 |
init: Some(LocalInit { |
| 169 |
expr: Expr::Path { |
| 170 |
path: Path { |
| 171 |
segments: [ |
| 172 |
PathSegment { |
| 173 |
ident: "None", |
| 174 |
}, |
| 175 |
], |
| 176 |
}, |
| 177 |
}, |
| 178 |
diverge: Some(Expr::Block { |
| 179 |
block: Block { |
| 180 |
stmts: [ |
| 181 |
Stmt::Expr( |
| 182 |
Expr::Return { |
| 183 |
expr: Some(Expr::Lit { |
| 184 |
lit: 0, |
| 185 |
}), |
| 186 |
}, |
| 187 |
Some, |
| 188 |
), |
| 189 |
], |
| 190 |
}, |
| 191 |
}), |
| 192 |
}), |
| 193 |
} |
| 194 |
"#); |
| 195 |
} |
| 196 |
|
| 197 |
#[test] |
| 198 |
fn test_macros() { |
| 199 |
let tokens = quote! { |
| 200 |
fn main() { |
| 201 |
macro_rules! mac {} |
| 202 |
thread_local! { static FOO } |
| 203 |
println!(""); |
| 204 |
vec![] |
| 205 |
} |
| 206 |
}; |
| 207 |
|
| 208 |
snapshot!(tokens as Stmt, @r#" |
| 209 |
Stmt::Item(Item::Fn { |
| 210 |
vis: Visibility::Inherited, |
| 211 |
sig: Signature { |
| 212 |
ident: "main", |
| 213 |
generics: Generics, |
| 214 |
output: ReturnType::Default, |
| 215 |
}, |
| 216 |
block: Block { |
| 217 |
stmts: [ |
| 218 |
Stmt::Item(Item::Macro { |
| 219 |
ident: Some("mac"), |
| 220 |
mac: Macro { |
| 221 |
path: Path { |
| 222 |
segments: [ |
| 223 |
PathSegment { |
| 224 |
ident: "macro_rules", |
| 225 |
}, |
| 226 |
], |
| 227 |
}, |
| 228 |
delimiter: MacroDelimiter::Brace, |
| 229 |
tokens: TokenStream(``), |
| 230 |
}, |
| 231 |
}), |
| 232 |
Stmt::Macro { |
| 233 |
mac: Macro { |
| 234 |
path: Path { |
| 235 |
segments: [ |
| 236 |
PathSegment { |
| 237 |
ident: "thread_local", |
| 238 |
}, |
| 239 |
], |
| 240 |
}, |
| 241 |
delimiter: MacroDelimiter::Brace, |
| 242 |
tokens: TokenStream(`static FOO`), |
| 243 |
}, |
| 244 |
}, |
| 245 |
Stmt::Macro { |
| 246 |
mac: Macro { |
| 247 |
path: Path { |
| 248 |
segments: [ |
| 249 |
PathSegment { |
| 250 |
ident: "println", |
| 251 |
}, |
| 252 |
], |
| 253 |
}, |
| 254 |
delimiter: MacroDelimiter::Paren, |
| 255 |
tokens: TokenStream(`""`), |
| 256 |
}, |
| 257 |
semi_token: Some, |
| 258 |
}, |
| 259 |
Stmt::Expr( |
| 260 |
Expr::Macro { |
| 261 |
mac: Macro { |
| 262 |
path: Path { |
| 263 |
segments: [ |
| 264 |
PathSegment { |
| 265 |
ident: "vec", |
| 266 |
}, |
| 267 |
], |
| 268 |
}, |
| 269 |
delimiter: MacroDelimiter::Bracket, |
| 270 |
tokens: TokenStream(``), |
| 271 |
}, |
| 272 |
}, |
| 273 |
None, |
| 274 |
), |
| 275 |
], |
| 276 |
}, |
| 277 |
}) |
| 278 |
"#); |
| 279 |
} |
| 280 |
|
| 281 |
#[test] |
| 282 |
fn test_early_parse_loop() { |
| 283 |
// The following is an Expr::Loop followed by Expr::Tuple. It is not an |
| 284 |
// Expr::Call. |
| 285 |
let tokens = quote! { |
| 286 |
loop {} |
| 287 |
() |
| 288 |
}; |
| 289 |
|
| 290 |
let stmts = Block::parse_within.parse2(tokens).unwrap(); |
| 291 |
|
| 292 |
snapshot!(stmts, @r#" |
| 293 |
[ |
| 294 |
Stmt::Expr( |
| 295 |
Expr::Loop { |
| 296 |
body: Block { |
| 297 |
stmts: [], |
| 298 |
}, |
| 299 |
}, |
| 300 |
None, |
| 301 |
), |
| 302 |
Stmt::Expr( |
| 303 |
Expr::Tuple, |
| 304 |
None, |
| 305 |
), |
| 306 |
] |
| 307 |
"#); |
| 308 |
|
| 309 |
let tokens = quote! { |
| 310 |
'a: loop {} |
| 311 |
() |
| 312 |
}; |
| 313 |
|
| 314 |
let stmts = Block::parse_within.parse2(tokens).unwrap(); |
| 315 |
|
| 316 |
snapshot!(stmts, @r#" |
| 317 |
[ |
| 318 |
Stmt::Expr( |
| 319 |
Expr::Loop { |
| 320 |
label: Some(Label { |
| 321 |
name: Lifetime { |
| 322 |
ident: "a", |
| 323 |
}, |
| 324 |
}), |
| 325 |
body: Block { |
| 326 |
stmts: [], |
| 327 |
}, |
| 328 |
}, |
| 329 |
None, |
| 330 |
), |
| 331 |
Stmt::Expr( |
| 332 |
Expr::Tuple, |
| 333 |
None, |
| 334 |
), |
| 335 |
] |
| 336 |
"#); |
| 337 |
} |