1 //! This module describes hir-level representation of expressions.
3 //! This representation is:
5 //! 1. Identity-based. Each expression has an `id`, so we can distinguish
6 //! between different `1` in `1 + 1`.
7 //! 2. Independent of syntax. Though syntactic provenance information can be
8 //! attached separately via id-based side map.
9 //! 3. Unresolved. Paths are stored as sequences of names, and not as defs the
11 //! 4. Desugared. There's no `if let`.
13 //! See also a neighboring `body` module.
15 use arena::{Idx, RawId};
16 use hir_expand::name::Name;
17 use syntax::ast::RangeOp;
20 builtin_type::{BuiltinFloat, BuiltinInt},
21 path::{GenericArgs, Path},
22 type_ref::{Mutability, Rawness, TypeRef},
25 pub type ExprId = Idx<Expr>;
26 pub(crate) fn dummy_expr_id() -> ExprId {
27 ExprId::from_raw(RawId::from(!0))
30 pub type PatId = Idx<Pat>;
32 #[derive(Debug, Clone, Eq, PartialEq)]
36 pub type LabelId = Idx<Label>;
38 #[derive(Debug, Clone, Eq, PartialEq)]
44 Int(u64, Option<BuiltinInt>),
45 Float(u64, Option<BuiltinFloat>), // FIXME: f64 is not Eq
48 #[derive(Debug, Clone, Eq, PartialEq)]
50 /// This is produced if the syntax tree does not have a required expression piece.
56 else_branch: Option<ExprId>,
59 statements: Vec<Statement>,
61 label: Option<LabelId>,
65 label: Option<LabelId>,
70 label: Option<LabelId>,
76 label: Option<LabelId>,
86 generic_args: Option<GenericArgs>,
100 expr: Option<ExprId>,
104 fields: Vec<RecordLitField>,
105 spread: Option<ExprId>,
133 mutability: Mutability,
145 op: Option<BinaryOp>,
158 arg_types: Vec<Option<TypeRef>>,
159 ret_type: Option<TypeRef>,
172 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
177 Assignment { op: Option<ArithOp> },
180 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
186 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
188 Eq { negated: bool },
189 Ord { ordering: Ordering, strict: bool },
192 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
198 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
212 pub use syntax::ast::PrefixOp as UnaryOp;
213 #[derive(Debug, Clone, Eq, PartialEq)]
215 ElementList(Vec<ExprId>),
216 Repeat { initializer: ExprId, repeat: ExprId },
219 #[derive(Debug, Clone, Eq, PartialEq)]
220 pub struct MatchArm {
222 pub guard: Option<ExprId>,
226 #[derive(Debug, Clone, Eq, PartialEq)]
227 pub struct RecordLitField {
232 #[derive(Debug, Clone, Eq, PartialEq)]
234 Let { pat: PatId, type_ref: Option<TypeRef>, initializer: Option<ExprId> },
239 pub fn walk_child_exprs(&self, mut f: impl FnMut(ExprId)) {
243 Expr::If { condition, then_branch, else_branch } => {
246 if let Some(else_branch) = else_branch {
250 Expr::Block { statements, tail, .. } => {
251 for stmt in statements {
253 Statement::Let { initializer, .. } => {
254 if let Some(expr) = initializer {
258 Statement::Expr(e) => f(*e),
261 if let Some(expr) = tail {
265 Expr::TryBlock { body }
266 | Expr::Unsafe { body }
267 | Expr::Async { body }
268 | Expr::Const { body } => f(*body),
269 Expr::Loop { body, .. } => f(*body),
270 Expr::While { condition, body, .. } => {
274 Expr::For { iterable, body, .. } => {
278 Expr::Call { callee, args } => {
284 Expr::MethodCall { receiver, args, .. } => {
290 Expr::Match { expr, arms } => {
296 Expr::Continue { .. } => {}
297 Expr::Break { expr, .. } | Expr::Return { expr } => {
298 if let Some(expr) = expr {
302 Expr::RecordLit { fields, spread, .. } => {
303 for field in fields {
306 if let Some(expr) = spread {
310 Expr::Lambda { body, .. } => {
313 Expr::BinaryOp { lhs, rhs, .. } => {
317 Expr::Range { lhs, rhs, .. } => {
318 if let Some(lhs) = rhs {
321 if let Some(rhs) = lhs {
325 Expr::Index { base, index } => {
329 Expr::Field { expr, .. }
330 | Expr::Await { expr }
332 | Expr::Cast { expr, .. }
333 | Expr::Ref { expr, .. }
334 | Expr::UnaryOp { expr, .. }
335 | Expr::Box { expr } => {
338 Expr::Tuple { exprs } => {
343 Expr::Array(a) => match a {
344 Array::ElementList(exprs) => {
349 Array::Repeat { initializer, repeat } => {
354 Expr::Literal(_) => {}
359 /// Explicit binding annotations given in the HIR for a binding. Note
360 /// that this is not the final binding *mode* that we infer after type
362 #[derive(Clone, PartialEq, Eq, Debug, Copy)]
363 pub enum BindingAnnotation {
364 /// No binding annotation given: this means that the final binding mode
365 /// will depend on whether we have skipped through a `&` reference
366 /// when matching. For example, the `x` in `Some(x)` will have binding
367 /// mode `None`; if you do `let Some(x) = &Some(22)`, it will
368 /// ultimately be inferred to be by-reference.
371 /// Annotated with `mut x` -- could be either ref or not, similar to `None`.
374 /// Annotated as `ref`, like `ref x`
377 /// Annotated as `ref mut x`.
381 impl BindingAnnotation {
382 pub fn new(is_mutable: bool, is_ref: bool) -> Self {
383 match (is_mutable, is_ref) {
384 (true, true) => BindingAnnotation::RefMut,
385 (false, true) => BindingAnnotation::Ref,
386 (true, false) => BindingAnnotation::Mutable,
387 (false, false) => BindingAnnotation::Unannotated,
392 #[derive(Debug, Clone, Eq, PartialEq)]
393 pub struct RecordFieldPat {
398 /// Close relative to rustc's hir::PatKind
399 #[derive(Debug, Clone, Eq, PartialEq)]
403 Tuple { args: Vec<PatId>, ellipsis: Option<usize> },
405 Record { path: Option<Path>, args: Vec<RecordFieldPat>, ellipsis: bool },
406 Range { start: ExprId, end: ExprId },
407 Slice { prefix: Vec<PatId>, slice: Option<PatId>, suffix: Vec<PatId> },
410 Bind { mode: BindingAnnotation, name: Name, subpat: Option<PatId> },
411 TupleStruct { path: Option<Path>, args: Vec<PatId>, ellipsis: Option<usize> },
412 Ref { pat: PatId, mutability: Mutability },
413 Box { inner: PatId },
418 pub fn walk_child_pats(&self, mut f: impl FnMut(PatId)) {
423 | Pat::ConstBlock(..)
426 Pat::Bind { subpat, .. } => {
427 subpat.iter().copied().for_each(f);
429 Pat::Or(args) | Pat::Tuple { args, .. } | Pat::TupleStruct { args, .. } => {
430 args.iter().copied().for_each(f);
432 Pat::Ref { pat, .. } => f(*pat),
433 Pat::Slice { prefix, slice, suffix } => {
434 let total_iter = prefix.iter().chain(slice.iter()).chain(suffix.iter());
435 total_iter.copied().for_each(f);
437 Pat::Record { args, .. } => {
438 args.iter().map(|f| f.pat).for_each(f);
440 Pat::Box { inner } => f(*inner),