Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/syn/src/expr.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / syn / src / expr.rs` 4245 lines · 144645 bytes Raw Blame History 1 use crate::attr::Attribute; 2 #[cfg(all(feature = "parsing", feature = "full"))] 3 use crate::error::Result; 4 #[cfg(feature = "parsing")] 5 use crate::ext::IdentExt as _; 6 #[cfg(feature = "full")] 7 use crate::generics::BoundLifetimes; 8 use crate::ident::Ident; 9 #[cfg(any(feature = "parsing", feature = "full"))] 10 use crate::lifetime::Lifetime; 11 use crate::lit::Lit; 12 use crate::mac::Macro; 13 use crate::op::{BinOp, UnOp}; 14 #[cfg(feature = "parsing")] 15 use crate::parse::ParseStream; 16 #[cfg(feature = "full")] 17 use crate::pat::Pat; 18 use crate::path::{AngleBracketedGenericArguments, Path, QSelf}; 19 use crate::punctuated::Punctuated; 20 #[cfg(feature = "full")] 21 use crate::stmt::Block; 22 use crate::token; 23 use crate::ty::Type; 24 #[cfg(feature = "full")] 25 use crate::ty::{PointerMutability, ReturnType}; 26 use alloc::boxed::Box; 27 use alloc::vec::Vec; 28 #[cfg(feature = "printing")] 29 use core::fmt::{self, Display}; 30 use core::hash::{Hash, Hasher}; 31 #[cfg(all(feature = "parsing", feature = "full"))] 32 use core::mem; 33 use proc_macro2::{Span, TokenStream}; 34 #[cfg(feature = "printing")] 35 use quote::IdentFragment; 36 37 ast_enum_of_structs! { 38 /// A Rust expression. 39 /// 40 /// *This type is available only if Syn is built with the `"derive"` or `"full"` 41 /// feature, but most of the variants are not available unless "full" is enabled.* 42 /// 43 /// # Syntax tree enums 44 /// 45 /// This type is a syntax tree enum. In Syn this and other syntax tree enums 46 /// are designed to be traversed using the following rebinding idiom. 47 /// 48 /// ``` 49 /// # use syn::Expr; 50 /// # 51 /// # fn example(expr: Expr) { 52 /// # const IGNORE: &str = stringify! { 53 /// let expr: Expr = /* ... */; 54 /// # }; 55 /// match expr { 56 /// Expr::MethodCall(expr) => { 57 /// /* ... */ 58 /// } 59 /// Expr::Cast(expr) => { 60 /// /* ... */ 61 /// } 62 /// Expr::If(expr) => { 63 /// /* ... */ 64 /// } 65 /// 66 /// /* ... */ 67 /// # _ => {} 68 /// # } 69 /// # } 70 /// ``` 71 /// 72 /// We begin with a variable `expr` of type `Expr` that has no fields 73 /// (because it is an enum), and by matching on it and rebinding a variable 74 /// with the same name `expr` we effectively imbue our variable with all of 75 /// the data fields provided by the variant that it turned out to be. So for 76 /// example above if we ended up in the `MethodCall` case then we get to use 77 /// `expr.receiver`, `expr.args` etc; if we ended up in the `If` case we get 78 /// to use `expr.cond`, `expr.then_branch`, `expr.else_branch`. 79 /// 80 /// This approach avoids repeating the variant names twice on every line. 81 /// 82 /// ``` 83 /// # use syn::{Expr, ExprMethodCall}; 84 /// # 85 /// # fn example(expr: Expr) { 86 /// // Repetitive; recommend not doing this. 87 /// match expr { 88 /// Expr::MethodCall(ExprMethodCall { method, args, .. }) => { 89 /// # } 90 /// # _ => {} 91 /// # } 92 /// # } 93 /// ``` 94 /// 95 /// In general, the name to which a syntax tree enum variant is bound should 96 /// be a suitable name for the complete syntax tree enum type. 97 /// 98 /// ``` 99 /// # use syn::{Expr, ExprField}; 100 /// # 101 /// # fn example(discriminant: ExprField) { 102 /// // Binding is called `base` which is the name I would use if I were 103 /// // assigning `*discriminant.base` without an `if let`. 104 /// if let Expr::Tuple(base) = *discriminant.base { 105 /// # } 106 /// # } 107 /// ``` 108 /// 109 /// A sign that you may not be choosing the right variable names is if you 110 /// see names getting repeated in your code, like accessing 111 /// `receiver.receiver` or `pat.pat` or `cond.cond`. 112 /// 113 /// # Exhaustive matching 114 /// 115 /// For testing exhaustiveness in downstream code, use the following idiom: 116 /// 117 /// ``` 118 /// # use syn::Expr; 119 /// # 120 /// # fn example(expr: Expr) { 121 /// match expr { 122 /// #![cfg_attr(test, deny(non_exhaustive_omitted_patterns))] 123 /// 124 /// Expr::Array(expr) => { /*...*/ } 125 /// Expr::Assign(expr) => { /*...*/ } 126 #[cfg_attr(not(doctest), doc = " ...")] 127 /// Expr::Yield(expr) => { /*...*/ } 128 /// 129 /// _ => { /* some sane fallback */ } 130 /// } 131 /// # } 132 /// ``` 133 /// 134 /// This way we fail your tests but don't break your library when adding a 135 /// variant. You will be notified by a test failure when a variant is added, 136 /// so that you can add code to handle it, but your library will continue to 137 /// compile and work for downstream users in the interim. 138 #[cfg_attr(docsrs, doc(cfg(any(feature = "full", feature = "derive"))))] 139 #[non_exhaustive] 140 pub enum Expr { 141 /// A slice literal expression: `[a, b, c, d]`. 142 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 143 Array(ExprArray), 144 145 /// An assignment expression: `a = compute()`. 146 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 147 Assign(ExprAssign), 148 149 /// An async block: `async { ... }`. 150 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 151 Async(ExprAsync), 152 153 /// An await expression: `fut.await`. 154 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 155 Await(ExprAwait), 156 157 /// A binary operation: `a + b`, `a += b`. 158 Binary(ExprBinary), 159 160 /// A braced block: `{ ... }`. 161 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 162 Block(ExprBlock), 163 164 /// A `break`, with an optional label to break and an optional 165 /// expression. 166 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 167 Break(ExprBreak), 168 169 /// A function call expression: `invoke(a, b)`. 170 Call(ExprCall), 171 172 /// A cast expression: `foo as f64`. 173 Cast(ExprCast), 174 175 /// A closure expression: `|a, b| a + b`. 176 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 177 Closure(ExprClosure), 178 179 /// A const block: `const { ... }`. 180 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 181 Const(ExprConst), 182 183 /// A `continue`, with an optional label. 184 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 185 Continue(ExprContinue), 186 187 /// Access of a named struct field (`obj.k`) or unnamed tuple struct 188 /// field (`obj.0`). 189 Field(ExprField), 190 191 /// A for loop: `for pat in expr { ... }`. 192 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 193 ForLoop(ExprForLoop), 194 195 /// An expression contained within invisible delimiters. 196 /// 197 /// This variant is important for faithfully representing the precedence 198 /// of expressions and is related to `None`-delimited spans in a 199 /// `TokenStream`. 200 Group(ExprGroup), 201 202 /// An `if` expression with an optional `else` block: `if expr { ... } 203 /// else { ... }`. 204 /// 205 /// The `else` branch expression may only be an `If` or `Block` 206 /// expression, not any of the other types of expression. 207 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 208 If(ExprIf), 209 210 /// A square bracketed indexing expression: `vector[2]`. 211 Index(ExprIndex), 212 213 /// The inferred value of a const generic argument, denoted `_`. 214 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 215 Infer(ExprInfer), 216 217 /// A pattern application: `let Some(x) = opt`. 218 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 219 Let(ExprLet), 220 221 /// A literal in place of an expression: `1`, `"foo"`. 222 Lit(ExprLit), 223 224 /// Conditionless loop: `loop { ... }`. 225 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 226 Loop(ExprLoop), 227 228 /// A macro invocation expression: `format!("{}", q)`. 229 Macro(ExprMacro), 230 231 /// A `match` expression: `match n { Some(n) => {}, None => {} }`. 232 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 233 Match(ExprMatch), 234 235 /// A method call expression: `x.foo::(a, b)`. 236 MethodCall(ExprMethodCall), 237 238 /// A parenthesized expression: `(a + b)`. 239 Paren(ExprParen), 240 241 /// A path like `core::mem::replace` possibly containing generic 242 /// parameters and a qualified self-type. 243 /// 244 /// A plain identifier like `x` is a path of length 1. 245 Path(ExprPath), 246 247 /// A range expression: `1..2`, `1..`, `..2`, `1..=2`, `..=2`. 248 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 249 Range(ExprRange), 250 251 /// Address-of operation: `&raw const place` or `&raw mut place`. 252 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 253 RawAddr(ExprRawAddr), 254 255 /// A referencing operation: `&a` or `&mut a`. 256 Reference(ExprReference), 257 258 /// An array literal constructed from one repeated element: `[0u8; N]`. 259 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 260 Repeat(ExprRepeat), 261 262 /// A `return`, with an optional value to be returned. 263 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 264 Return(ExprReturn), 265 266 /// A struct literal expression: `Point { x: 1, y: 1 }`. 267 /// 268 /// The `rest` provides the value of the remaining fields as in `S { a: 269 /// 1, b: 1, ..rest }`. 270 Struct(ExprStruct), 271 272 /// A try-expression: `expr?`. 273 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 274 Try(ExprTry), 275 276 /// A try block: `try { ... }`. 277 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 278 TryBlock(ExprTryBlock), 279 280 /// A tuple expression: `(a, b, c, d)`. 281 Tuple(ExprTuple), 282 283 /// A unary operation: `!x`, `*x`, `-x`. 284 Unary(ExprUnary), 285 286 /// An unsafe block: `unsafe { ... }`. 287 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 288 Unsafe(ExprUnsafe), 289 290 /// Tokens in expression position not interpreted by Syn. 291 /// 292 ///
293 /// 294 /// Important: see [Compatibility notes][crate#verbatim-variants]. 295 /// 296 ///
297 Verbatim(TokenStream), 298 299 /// A while loop: `while expr { ... }`. 300 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 301 While(ExprWhile), 302 303 /// A yield expression: `yield expr`. 304 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 305 Yield(ExprYield), 306 } 307 } 308 309 ast_struct! { 310 /// A slice literal expression: `[a, b, c, d]`. 311 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 312 pub struct ExprArray #full { 313 pub attrs: Vec, 314 pub bracket_token: token::Bracket, 315 pub elems: Punctuated, 316 } 317 } 318 319 ast_struct! { 320 /// An assignment expression: `a = compute()`. 321 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 322 pub struct ExprAssign #full { 323 pub attrs: Vec, 324 pub left: Box, 325 pub eq_token: Token![=], 326 pub right: Box, 327 } 328 } 329 330 ast_struct! { 331 /// An async block: `async { ... }`. 332 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 333 pub struct ExprAsync #full { 334 pub attrs: Vec, 335 pub async_token: Token![async], 336 pub capture: Option, 337 /// (Non-exhaustive) Additional optional information about a block. 338 pub modifiers: BlockModifiers, 339 pub block: Block, 340 } 341 } 342 343 ast_struct! { 344 /// An await expression: `fut.await`. 345 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 346 pub struct ExprAwait #full { 347 pub attrs: Vec, 348 pub base: Box, 349 pub dot_token: Token![.], 350 pub await_token: Token![await], 351 } 352 } 353 354 ast_struct! { 355 /// A binary operation: `a + b`, `a += b`. 356 #[cfg_attr(docsrs, doc(cfg(any(feature = "full", feature = "derive"))))] 357 pub struct ExprBinary { 358 pub attrs: Vec, 359 pub left: Box, 360 pub op: BinOp, 361 pub right: Box, 362 } 363 } 364 365 ast_struct! { 366 /// A braced block: `{ ... }`. 367 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 368 pub struct ExprBlock #full { 369 pub attrs: Vec, 370 pub label: Option