|
| 1 | +//! Finds if an expression is an immutable context or a mutable context, which is used in selecting |
| 2 | +//! between `Deref` and `DerefMut` or `Index` and `IndexMut` or similar. |
| 3 | +
|
| 4 | +use chalk_ir::Mutability; |
| 5 | +use hir_def::{ |
| 6 | + expr::{Array, BindingAnnotation, Expr, ExprId, PatId, Statement, UnaryOp}, |
| 7 | + lang_item::LangItem, |
| 8 | +}; |
| 9 | +use hir_expand::name; |
| 10 | + |
| 11 | +use crate::{lower::lower_to_chalk_mutability, Adjust, AutoBorrow, OverloadedDeref}; |
| 12 | + |
| 13 | +use super::InferenceContext; |
| 14 | + |
| 15 | +impl<'a> InferenceContext<'a> { |
| 16 | + pub(crate) fn infer_mut_body(&mut self) { |
| 17 | + self.infer_mut_expr(self.body.body_expr, Mutability::Not); |
| 18 | + } |
| 19 | + |
| 20 | + fn infer_mut_expr(&mut self, tgt_expr: ExprId, mut mutability: Mutability) { |
| 21 | + let mut v = vec![]; |
| 22 | + let adjustments = self.result.expr_adjustments.get_mut(&tgt_expr).unwrap_or(&mut v); |
| 23 | + for adj in adjustments.iter_mut().rev() { |
| 24 | + match &mut adj.kind { |
| 25 | + Adjust::NeverToAny | Adjust::Deref(None) | Adjust::Pointer(_) => (), |
| 26 | + Adjust::Deref(Some(d)) => *d = OverloadedDeref(Some(mutability)), |
| 27 | + Adjust::Borrow(b) => match b { |
| 28 | + AutoBorrow::Ref(m) | AutoBorrow::RawPtr(m) => mutability = *m, |
| 29 | + }, |
| 30 | + } |
| 31 | + } |
| 32 | + self.infer_mut_expr_without_adjust(tgt_expr, mutability); |
| 33 | + } |
| 34 | + |
| 35 | + fn infer_mut_expr_without_adjust(&mut self, tgt_expr: ExprId, mutability: Mutability) { |
| 36 | + match &self.body[tgt_expr] { |
| 37 | + Expr::Missing => (), |
| 38 | + &Expr::If { condition, then_branch, else_branch } => { |
| 39 | + self.infer_mut_expr(condition, Mutability::Not); |
| 40 | + self.infer_mut_expr(then_branch, Mutability::Not); |
| 41 | + if let Some(else_branch) = else_branch { |
| 42 | + self.infer_mut_expr(else_branch, Mutability::Not); |
| 43 | + } |
| 44 | + } |
| 45 | + Expr::Let { pat, expr } => self.infer_mut_expr(*expr, self.pat_bound_mutability(*pat)), |
| 46 | + Expr::Block { id: _, statements, tail, label: _ } |
| 47 | + | Expr::TryBlock { id: _, statements, tail } |
| 48 | + | Expr::Async { id: _, statements, tail } |
| 49 | + | Expr::Const { id: _, statements, tail } |
| 50 | + | Expr::Unsafe { id: _, statements, tail } => { |
| 51 | + for st in statements.iter() { |
| 52 | + match st { |
| 53 | + Statement::Let { pat, type_ref: _, initializer, else_branch } => { |
| 54 | + if let Some(i) = initializer { |
| 55 | + self.infer_mut_expr(*i, self.pat_bound_mutability(*pat)); |
| 56 | + } |
| 57 | + if let Some(e) = else_branch { |
| 58 | + self.infer_mut_expr(*e, Mutability::Not); |
| 59 | + } |
| 60 | + } |
| 61 | + Statement::Expr { expr, has_semi: _ } => { |
| 62 | + self.infer_mut_expr(*expr, Mutability::Not); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + if let Some(tail) = tail { |
| 67 | + self.infer_mut_expr(*tail, Mutability::Not); |
| 68 | + } |
| 69 | + } |
| 70 | + &Expr::For { iterable: c, pat: _, body, label: _ } |
| 71 | + | &Expr::While { condition: c, body, label: _ } => { |
| 72 | + self.infer_mut_expr(c, Mutability::Not); |
| 73 | + self.infer_mut_expr(body, Mutability::Not); |
| 74 | + } |
| 75 | + Expr::MethodCall { receiver: x, method_name: _, args, generic_args: _ } |
| 76 | + | Expr::Call { callee: x, args, is_assignee_expr: _ } => { |
| 77 | + self.infer_mut_not_expr_iter(args.iter().copied().chain(Some(*x))); |
| 78 | + } |
| 79 | + Expr::Match { expr, arms } => { |
| 80 | + let m = self.pat_iter_bound_mutability(arms.iter().map(|x| x.pat)); |
| 81 | + self.infer_mut_expr(*expr, m); |
| 82 | + for arm in arms.iter() { |
| 83 | + self.infer_mut_expr(arm.expr, Mutability::Not); |
| 84 | + } |
| 85 | + } |
| 86 | + Expr::Yield { expr } |
| 87 | + | Expr::Yeet { expr } |
| 88 | + | Expr::Return { expr } |
| 89 | + | Expr::Break { expr, label: _ } => { |
| 90 | + if let &Some(expr) = expr { |
| 91 | + self.infer_mut_expr(expr, Mutability::Not); |
| 92 | + } |
| 93 | + } |
| 94 | + Expr::RecordLit { path: _, fields, spread, ellipsis: _, is_assignee_expr: _ } => { |
| 95 | + self.infer_mut_not_expr_iter(fields.iter().map(|x| x.expr).chain(*spread)) |
| 96 | + } |
| 97 | + &Expr::Index { base, index } => { |
| 98 | + self.infer_mut_expr(base, mutability); |
| 99 | + self.infer_mut_expr(index, Mutability::Not); |
| 100 | + } |
| 101 | + Expr::UnaryOp { expr, op: UnaryOp::Deref } => { |
| 102 | + if let Some((f, _)) = self.result.method_resolutions.get_mut(&tgt_expr) { |
| 103 | + if mutability == Mutability::Mut { |
| 104 | + if let Some(deref_trait) = self |
| 105 | + .db |
| 106 | + .lang_item(self.table.trait_env.krate, LangItem::DerefMut) |
| 107 | + .and_then(|l| l.as_trait()) |
| 108 | + { |
| 109 | + if let Some(deref_fn) = |
| 110 | + self.db.trait_data(deref_trait).method_by_name(&name![deref_mut]) |
| 111 | + { |
| 112 | + *f = deref_fn; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + self.infer_mut_expr(*expr, mutability); |
| 118 | + } |
| 119 | + Expr::Field { expr, name: _ } => { |
| 120 | + self.infer_mut_expr(*expr, mutability); |
| 121 | + } |
| 122 | + Expr::UnaryOp { expr, op: _ } |
| 123 | + | Expr::Range { lhs: Some(expr), rhs: None, range_type: _ } |
| 124 | + | Expr::Range { rhs: Some(expr), lhs: None, range_type: _ } |
| 125 | + | Expr::Await { expr } |
| 126 | + | Expr::Box { expr } |
| 127 | + | Expr::Loop { body: expr, label: _ } |
| 128 | + | Expr::Cast { expr, type_ref: _ } => { |
| 129 | + self.infer_mut_expr(*expr, Mutability::Not); |
| 130 | + } |
| 131 | + Expr::Ref { expr, rawness: _, mutability } => { |
| 132 | + let mutability = lower_to_chalk_mutability(*mutability); |
| 133 | + self.infer_mut_expr(*expr, mutability); |
| 134 | + } |
| 135 | + Expr::Array(Array::Repeat { initializer: lhs, repeat: rhs }) |
| 136 | + | Expr::BinaryOp { lhs, rhs, op: _ } |
| 137 | + | Expr::Range { lhs: Some(lhs), rhs: Some(rhs), range_type: _ } => { |
| 138 | + self.infer_mut_expr(*lhs, Mutability::Not); |
| 139 | + self.infer_mut_expr(*rhs, Mutability::Not); |
| 140 | + } |
| 141 | + // not implemented |
| 142 | + Expr::Closure { .. } => (), |
| 143 | + Expr::Tuple { exprs, is_assignee_expr: _ } |
| 144 | + | Expr::Array(Array::ElementList { elements: exprs, is_assignee_expr: _ }) => { |
| 145 | + self.infer_mut_not_expr_iter(exprs.iter().copied()); |
| 146 | + } |
| 147 | + // These don't need any action, as they don't have sub expressions |
| 148 | + Expr::Range { lhs: None, rhs: None, range_type: _ } |
| 149 | + | Expr::Literal(_) |
| 150 | + | Expr::Path(_) |
| 151 | + | Expr::Continue { .. } |
| 152 | + | Expr::Underscore => (), |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + fn infer_mut_not_expr_iter(&mut self, exprs: impl Iterator<Item = ExprId>) { |
| 157 | + for expr in exprs { |
| 158 | + self.infer_mut_expr(expr, Mutability::Not); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + fn pat_iter_bound_mutability(&self, mut pat: impl Iterator<Item = PatId>) -> Mutability { |
| 163 | + if pat.any(|p| self.pat_bound_mutability(p) == Mutability::Mut) { |
| 164 | + Mutability::Mut |
| 165 | + } else { |
| 166 | + Mutability::Not |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + /// Checks if the pat contains a `ref mut` binding. Such paths makes the context of bounded expressions |
| 171 | + /// mutable. For example in `let (ref mut x0, ref x1) = *x;` we need to use `DerefMut` for `*x` but in |
| 172 | + /// `let (ref x0, ref x1) = *x;` we should use `Deref`. |
| 173 | + fn pat_bound_mutability(&self, pat: PatId) -> Mutability { |
| 174 | + let mut r = Mutability::Not; |
| 175 | + self.body.walk_bindings_in_pat(pat, |b| { |
| 176 | + if self.body.bindings[b].mode == BindingAnnotation::RefMut { |
| 177 | + r = Mutability::Mut; |
| 178 | + } |
| 179 | + }); |
| 180 | + r |
| 181 | + } |
| 182 | +} |
0 commit comments