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-2.0.119/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-2.0.119 / src / expr.rs` 4179 lines · 141993 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 #[cfg(feature = "full")] 24 use crate::ty::ReturnType; 25 use crate::ty::Type; 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 #[cfg_attr(docsrs, doc(cfg(any(feature = "full", feature = "derive"))))] 113 #[non_exhaustive] 114 pub enum Expr { 115 /// A slice literal expression: `[a, b, c, d]`. 116 Array(ExprArray), 117 118 /// An assignment expression: `a = compute()`. 119 Assign(ExprAssign), 120 121 /// An async block: `async { ... }`. 122 Async(ExprAsync), 123 124 /// An await expression: `fut.await`. 125 Await(ExprAwait), 126 127 /// A binary operation: `a + b`, `a += b`. 128 Binary(ExprBinary), 129 130 /// A blocked scope: `{ ... }`. 131 Block(ExprBlock), 132 133 /// A `break`, with an optional label to break and an optional 134 /// expression. 135 Break(ExprBreak), 136 137 /// A function call expression: `invoke(a, b)`. 138 Call(ExprCall), 139 140 /// A cast expression: `foo as f64`. 141 Cast(ExprCast), 142 143 /// A closure expression: `|a, b| a + b`. 144 Closure(ExprClosure), 145 146 /// A const block: `const { ... }`. 147 Const(ExprConst), 148 149 /// A `continue`, with an optional label. 150 Continue(ExprContinue), 151 152 /// Access of a named struct field (`obj.k`) or unnamed tuple struct 153 /// field (`obj.0`). 154 Field(ExprField), 155 156 /// A for loop: `for pat in expr { ... }`. 157 ForLoop(ExprForLoop), 158 159 /// An expression contained within invisible delimiters. 160 /// 161 /// This variant is important for faithfully representing the precedence 162 /// of expressions and is related to `None`-delimited spans in a 163 /// `TokenStream`. 164 Group(ExprGroup), 165 166 /// An `if` expression with an optional `else` block: `if expr { ... } 167 /// else { ... }`. 168 /// 169 /// The `else` branch expression may only be an `If` or `Block` 170 /// expression, not any of the other types of expression. 171 If(ExprIf), 172 173 /// A square bracketed indexing expression: `vector[2]`. 174 Index(ExprIndex), 175 176 /// The inferred value of a const generic argument, denoted `_`. 177 Infer(ExprInfer), 178 179 /// A `let` guard: `let Some(x) = opt`. 180 Let(ExprLet), 181 182 /// A literal in place of an expression: `1`, `"foo"`. 183 Lit(ExprLit), 184 185 /// Conditionless loop: `loop { ... }`. 186 Loop(ExprLoop), 187 188 /// A macro invocation expression: `format!("{}", q)`. 189 Macro(ExprMacro), 190 191 /// A `match` expression: `match n { Some(n) => {}, None => {} }`. 192 Match(ExprMatch), 193 194 /// A method call expression: `x.foo::(a, b)`. 195 MethodCall(ExprMethodCall), 196 197 /// A parenthesized expression: `(a + b)`. 198 Paren(ExprParen), 199 200 /// A path like `core::mem::replace` possibly containing generic 201 /// parameters and a qualified self-type. 202 /// 203 /// A plain identifier like `x` is a path of length 1. 204 Path(ExprPath), 205 206 /// A range expression: `1..2`, `1..`, `..2`, `1..=2`, `..=2`. 207 Range(ExprRange), 208 209 /// Address-of operation: `&raw const place` or `&raw mut place`. 210 RawAddr(ExprRawAddr), 211 212 /// A referencing operation: `&a` or `&mut a`. 213 Reference(ExprReference), 214 215 /// An array literal constructed from one repeated element: `[0u8; N]`. 216 Repeat(ExprRepeat), 217 218 /// A `return`, with an optional value to be returned. 219 Return(ExprReturn), 220 221 /// A struct literal expression: `Point { x: 1, y: 1 }`. 222 /// 223 /// The `rest` provides the value of the remaining fields as in `S { a: 224 /// 1, b: 1, ..rest }`. 225 Struct(ExprStruct), 226 227 /// A try-expression: `expr?`. 228 Try(ExprTry), 229 230 /// A try block: `try { ... }`. 231 TryBlock(ExprTryBlock), 232 233 /// A tuple expression: `(a, b, c, d)`. 234 Tuple(ExprTuple), 235 236 /// A unary operation: `!x`, `*x`, `-x`. 237 Unary(ExprUnary), 238 239 /// An unsafe block: `unsafe { ... }`. 240 Unsafe(ExprUnsafe), 241 242 /// Tokens in expression position not interpreted by Syn. 243 Verbatim(TokenStream), 244 245 /// A while loop: `while expr { ... }`. 246 While(ExprWhile), 247 248 /// A yield expression: `yield expr`. 249 Yield(ExprYield), 250 251 // For testing exhaustiveness in downstream code, use the following idiom: 252 // 253 // match expr { 254 // #![cfg_attr(test, deny(non_exhaustive_omitted_patterns))] 255 // 256 // Expr::Array(expr) => {...} 257 // Expr::Assign(expr) => {...} 258 // ... 259 // Expr::Yield(expr) => {...} 260 // 261 // _ => { /* some sane fallback */ } 262 // } 263 // 264 // This way we fail your tests but don't break your library when adding 265 // a variant. You will be notified by a test failure when a variant is 266 // added, so that you can add code to handle it, but your library will 267 // continue to compile and work for downstream users in the interim. 268 } 269 } 270 271 ast_struct! { 272 /// A slice literal expression: `[a, b, c, d]`. 273 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 274 pub struct ExprArray #full { 275 pub attrs: Vec, 276 pub bracket_token: token::Bracket, 277 pub elems: Punctuated, 278 } 279 } 280 281 ast_struct! { 282 /// An assignment expression: `a = compute()`. 283 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 284 pub struct ExprAssign #full { 285 pub attrs: Vec, 286 pub left: Box, 287 pub eq_token: Token![=], 288 pub right: Box, 289 } 290 } 291 292 ast_struct! { 293 /// An async block: `async { ... }`. 294 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 295 pub struct ExprAsync #full { 296 pub attrs: Vec, 297 pub async_token: Token![async], 298 pub capture: Option, 299 pub block: Block, 300 } 301 } 302 303 ast_struct! { 304 /// An await expression: `fut.await`. 305 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 306 pub struct ExprAwait #full { 307 pub attrs: Vec, 308 pub base: Box, 309 pub dot_token: Token![.], 310 pub await_token: Token![await], 311 } 312 } 313 314 ast_struct! { 315 /// A binary operation: `a + b`, `a += b`. 316 #[cfg_attr(docsrs, doc(cfg(any(feature = "full", feature = "derive"))))] 317 pub struct ExprBinary { 318 pub attrs: Vec, 319 pub left: Box, 320 pub op: BinOp, 321 pub right: Box, 322 } 323 } 324 325 ast_struct! { 326 /// A blocked scope: `{ ... }`. 327 #[cfg_attr(docsrs, doc(cfg(feature = "full")))] 328 pub struct ExprBlock #full { 329 pub attrs: Vec, 330 pub label: Option