3 //! The job of the categorization module is to analyze an expression to
4 //! determine what kind of memory is used in evaluating it (for example,
5 //! where dereferences occur and what kind of pointer is dereferenced;
6 //! whether the memory is mutable, etc.).
8 //! Categorization effectively transforms all of our expressions into
9 //! expressions of the following forms (the actual enum has many more
10 //! possibilities, naturally, but they are all variants of these base
13 //! E = rvalue // some computed rvalue
14 //! | x // address of a local variable or argument
15 //! | *E // deref of a ptr
16 //! | E.comp // access to an interior component
18 //! Imagine a routine ToAddr(Expr) that evaluates an expression and returns an
19 //! address where the result is to be found. If Expr is a place, then this
20 //! is the address of the place. If `Expr` is an rvalue, this is the address of
21 //! some temporary spot in memory where the result is stored.
23 //! Now, `cat_expr()` classifies the expression `Expr` and the address `A = ToAddr(Expr)`
26 //! - `cat`: what kind of expression was this? This is a subset of the
27 //! full expression forms which only includes those that we care about
28 //! for the purpose of the analysis.
29 //! - `mutbl`: mutability of the address `A`.
30 //! - `ty`: the type of data found at the address `A`.
32 //! The resulting categorization tree differs somewhat from the expressions
33 //! themselves. For example, auto-derefs are explicit. Also, an index a[b] is
34 //! decomposed into two operations: a dereference to reach the array data and
35 //! then an index to jump forward to the relevant item.
37 //! ## By-reference upvars
39 //! One part of the codegen which may be non-obvious is that we translate
40 //! closure upvars into the dereference of a borrowed pointer; this more closely
41 //! resembles the runtime codegen. So, for example, if we had:
45 //! let inc = || x += y;
47 //! Then when we categorize `x` (*within* the closure) we would yield a
48 //! result of `*x'`, effectively, where `x'` is a `Categorization::Upvar` reference
49 //! tied to `x`. The type of `x'` will be a borrowed pointer.
51 use rustc::ty::adjustment;
52 use rustc::ty::fold::TypeFoldable;
53 use rustc::ty::{self, Ty, TyCtxt};
55 use rustc_data_structures::fx::FxIndexMap;
57 use rustc_hir::def::{DefKind, Res};
58 use rustc_hir::def_id::DefId;
59 use rustc_hir::PatKind;
60 use rustc_infer::infer::InferCtxt;
63 #[derive(Clone, Debug)]
65 /// A temporary variable
67 /// A named `static` item
69 /// A named local variable
71 /// An upvar referenced by closure env
75 #[derive(Clone, Debug)]
76 pub enum Projection<'tcx> {
77 /// A dereference of a pointer, reference or `Box<T>` of the given type
79 /// An index or a field
83 /// A `Place` represents how a value is located in memory.
85 /// This is an HIR version of `mir::Place`
86 #[derive(Clone, Debug)]
87 pub struct Place<'tcx> {
88 /// `HirId` of the expression or pattern producing this value.
89 pub hir_id: hir::HirId,
90 /// The `Span` of the expression or pattern producing this value.
92 /// The type of the `Place`
94 /// The "outermost" place that holds this value.
96 /// How this place is derived from the base place.
97 pub projections: Vec<Projection<'tcx>>,
100 impl<'tcx> Place<'tcx> {
101 /// Returns an iterator of the types that have to be dereferenced to access
104 /// The types are in the reverse order that they are applied. So if
105 /// `x: &*const u32` and the `Place` is `**x`, then the types returned are
106 ///`*const u32` then `&*const u32`.
107 crate fn deref_tys(&self) -> impl Iterator<Item = Ty<'tcx>> + '_ {
108 self.projections.iter().rev().filter_map(|proj| {
109 if let Projection::Deref(deref_ty) = *proj { Some(deref_ty) } else { None }
114 crate trait HirNode {
115 fn hir_id(&self) -> hir::HirId;
116 fn span(&self) -> Span;
119 impl HirNode for hir::Expr<'_> {
120 fn hir_id(&self) -> hir::HirId {
123 fn span(&self) -> Span {
128 impl HirNode for hir::Pat<'_> {
129 fn hir_id(&self) -> hir::HirId {
132 fn span(&self) -> Span {
138 crate struct MemCategorizationContext<'a, 'tcx> {
139 crate tables: &'a ty::TypeckTables<'tcx>,
140 infcx: &'a InferCtxt<'a, 'tcx>,
141 param_env: ty::ParamEnv<'tcx>,
143 upvars: Option<&'tcx FxIndexMap<hir::HirId, hir::Upvar>>,
146 crate type McResult<T> = Result<T, ()>;
148 impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
149 /// Creates a `MemCategorizationContext`.
151 infcx: &'a InferCtxt<'a, 'tcx>,
152 param_env: ty::ParamEnv<'tcx>,
154 tables: &'a ty::TypeckTables<'tcx>,
155 ) -> MemCategorizationContext<'a, 'tcx> {
156 MemCategorizationContext {
161 upvars: infcx.tcx.upvars(body_owner),
165 crate fn tcx(&self) -> TyCtxt<'tcx> {
169 crate fn type_is_copy_modulo_regions(&self, ty: Ty<'tcx>, span: Span) -> bool {
170 self.infcx.type_is_copy_modulo_regions(self.param_env, ty, span)
173 fn resolve_vars_if_possible<T>(&self, value: &T) -> T
175 T: TypeFoldable<'tcx>,
177 self.infcx.resolve_vars_if_possible(value)
180 fn is_tainted_by_errors(&self) -> bool {
181 self.infcx.is_tainted_by_errors()
184 fn resolve_type_vars_or_error(
187 ty: Option<Ty<'tcx>>,
188 ) -> McResult<Ty<'tcx>> {
191 let ty = self.resolve_vars_if_possible(&ty);
192 if ty.references_error() || ty.is_ty_var() {
193 debug!("resolve_type_vars_or_error: error from {:?}", ty);
200 None if self.is_tainted_by_errors() => Err(()),
203 "no type for node {}: {} in mem_categorization",
205 self.tcx().hir().node_to_string(id)
211 crate fn node_ty(&self, hir_id: hir::HirId) -> McResult<Ty<'tcx>> {
212 self.resolve_type_vars_or_error(hir_id, self.tables.node_type_opt(hir_id))
215 fn expr_ty(&self, expr: &hir::Expr<'_>) -> McResult<Ty<'tcx>> {
216 self.resolve_type_vars_or_error(expr.hir_id, self.tables.expr_ty_opt(expr))
219 crate fn expr_ty_adjusted(&self, expr: &hir::Expr<'_>) -> McResult<Ty<'tcx>> {
220 self.resolve_type_vars_or_error(expr.hir_id, self.tables.expr_ty_adjusted_opt(expr))
223 /// Returns the type of value that this pattern matches against.
224 /// Some non-obvious cases:
226 /// - a `ref x` binding matches against a value of type `T` and gives
227 /// `x` the type `&T`; we return `T`.
228 /// - a pattern with implicit derefs (thanks to default binding
229 /// modes #42640) may look like `Some(x)` but in fact have
230 /// implicit deref patterns attached (e.g., it is really
231 /// `&Some(x)`). In that case, we return the "outermost" type
232 /// (e.g., `&Option<T>).
233 crate fn pat_ty_adjusted(&self, pat: &hir::Pat<'_>) -> McResult<Ty<'tcx>> {
234 // Check for implicit `&` types wrapping the pattern; note
235 // that these are never attached to binding patterns, so
236 // actually this is somewhat "disjoint" from the code below
237 // that aims to account for `ref x`.
238 if let Some(vec) = self.tables.pat_adjustments().get(pat.hir_id) {
239 if let Some(first_ty) = vec.first() {
240 debug!("pat_ty(pat={:?}) found adjusted ty `{:?}`", pat, first_ty);
245 self.pat_ty_unadjusted(pat)
248 /// Like `pat_ty`, but ignores implicit `&` patterns.
249 fn pat_ty_unadjusted(&self, pat: &hir::Pat<'_>) -> McResult<Ty<'tcx>> {
250 let base_ty = self.node_ty(pat.hir_id)?;
251 debug!("pat_ty(pat={:?}) base_ty={:?}", pat, base_ty);
253 // This code detects whether we are looking at a `ref x`,
254 // and if so, figures out what the type *being borrowed* is.
255 let ret_ty = match pat.kind {
256 PatKind::Binding(..) => {
258 *self.tables.pat_binding_modes().get(pat.hir_id).expect("missing binding mode");
260 if let ty::BindByReference(_) = bm {
261 // a bind-by-ref means that the base_ty will be the type of the ident itself,
262 // but what we want here is the type of the underlying value being borrowed.
263 // So peel off one-level, turning the &T into T.
264 match base_ty.builtin_deref(false) {
267 debug!("By-ref binding of non-derefable type {:?}", base_ty);
277 debug!("pat_ty(pat={:?}) ret_ty={:?}", pat, ret_ty);
282 crate fn cat_expr(&self, expr: &hir::Expr<'_>) -> McResult<Place<'tcx>> {
283 // This recursion helper avoids going through *too many*
284 // adjustments, since *only* non-overloaded deref recurses.
286 mc: &MemCategorizationContext<'a, 'tcx>,
287 expr: &hir::Expr<'_>,
288 adjustments: &[adjustment::Adjustment<'tcx>],
289 ) -> McResult<Place<'tcx>> {
290 match adjustments.split_last() {
291 None => mc.cat_expr_unadjusted(expr),
292 Some((adjustment, previous)) => {
293 mc.cat_expr_adjusted_with(expr, || helper(mc, expr, previous), adjustment)
298 helper(self, expr, self.tables.expr_adjustments(expr))
301 crate fn cat_expr_adjusted(
303 expr: &hir::Expr<'_>,
304 previous: Place<'tcx>,
305 adjustment: &adjustment::Adjustment<'tcx>,
306 ) -> McResult<Place<'tcx>> {
307 self.cat_expr_adjusted_with(expr, || Ok(previous), adjustment)
310 fn cat_expr_adjusted_with<F>(
312 expr: &hir::Expr<'_>,
314 adjustment: &adjustment::Adjustment<'tcx>,
315 ) -> McResult<Place<'tcx>>
317 F: FnOnce() -> McResult<Place<'tcx>>,
319 debug!("cat_expr_adjusted_with({:?}): {:?}", adjustment, expr);
320 let target = self.resolve_vars_if_possible(&adjustment.target);
321 match adjustment.kind {
322 adjustment::Adjust::Deref(overloaded) => {
323 // Equivalent to *expr or something similar.
324 let base = if let Some(deref) = overloaded {
327 .mk_ref(deref.region, ty::TypeAndMut { ty: target, mutbl: deref.mutbl });
328 self.cat_rvalue(expr.hir_id, expr.span, ref_ty)
332 self.cat_deref(expr, base)
335 adjustment::Adjust::NeverToAny
336 | adjustment::Adjust::Pointer(_)
337 | adjustment::Adjust::Borrow(_) => {
338 // Result is an rvalue.
339 Ok(self.cat_rvalue(expr.hir_id, expr.span, target))
344 crate fn cat_expr_unadjusted(&self, expr: &hir::Expr<'_>) -> McResult<Place<'tcx>> {
345 debug!("cat_expr: id={} expr={:?}", expr.hir_id, expr);
347 let expr_ty = self.expr_ty(expr)?;
349 hir::ExprKind::Unary(hir::UnOp::UnDeref, ref e_base) => {
350 if self.tables.is_method_call(expr) {
351 self.cat_overloaded_place(expr, e_base)
353 let base = self.cat_expr(&e_base)?;
354 self.cat_deref(expr, base)
358 hir::ExprKind::Field(ref base, _) => {
359 let base = self.cat_expr(&base)?;
360 debug!("cat_expr(cat_field): id={} expr={:?} base={:?}", expr.hir_id, expr, base);
361 Ok(self.cat_projection(expr, base, expr_ty))
364 hir::ExprKind::Index(ref base, _) => {
365 if self.tables.is_method_call(expr) {
366 // If this is an index implemented by a method call, then it
367 // will include an implicit deref of the result.
368 // The call to index() returns a `&T` value, which
369 // is an rvalue. That is what we will be
371 self.cat_overloaded_place(expr, base)
373 let base = self.cat_expr(&base)?;
374 Ok(self.cat_projection(expr, base, expr_ty))
378 hir::ExprKind::Path(ref qpath) => {
379 let res = self.tables.qpath_res(qpath, expr.hir_id);
380 self.cat_res(expr.hir_id, expr.span, expr_ty, res)
383 hir::ExprKind::Type(ref e, _) => self.cat_expr(&e),
385 hir::ExprKind::AddrOf(..)
386 | hir::ExprKind::Call(..)
387 | hir::ExprKind::Assign(..)
388 | hir::ExprKind::AssignOp(..)
389 | hir::ExprKind::Closure(..)
390 | hir::ExprKind::Ret(..)
391 | hir::ExprKind::Unary(..)
392 | hir::ExprKind::Yield(..)
393 | hir::ExprKind::MethodCall(..)
394 | hir::ExprKind::Cast(..)
395 | hir::ExprKind::DropTemps(..)
396 | hir::ExprKind::Array(..)
397 | hir::ExprKind::Tup(..)
398 | hir::ExprKind::Binary(..)
399 | hir::ExprKind::Block(..)
400 | hir::ExprKind::Loop(..)
401 | hir::ExprKind::Match(..)
402 | hir::ExprKind::Lit(..)
403 | hir::ExprKind::Break(..)
404 | hir::ExprKind::Continue(..)
405 | hir::ExprKind::Struct(..)
406 | hir::ExprKind::Repeat(..)
407 | hir::ExprKind::InlineAsm(..)
408 | hir::ExprKind::Box(..)
409 | hir::ExprKind::Err => Ok(self.cat_rvalue(expr.hir_id, expr.span, expr_ty)),
419 ) -> McResult<Place<'tcx>> {
420 debug!("cat_res: id={:?} expr={:?} def={:?}", hir_id, expr_ty, res);
423 Res::Def(DefKind::Ctor(..), _)
424 | Res::Def(DefKind::Const, _)
425 | Res::Def(DefKind::ConstParam, _)
426 | Res::Def(DefKind::AssocConst, _)
427 | Res::Def(DefKind::Fn, _)
428 | Res::Def(DefKind::Method, _)
429 | Res::SelfCtor(..) => Ok(self.cat_rvalue(hir_id, span, expr_ty)),
431 Res::Def(DefKind::Static, _) => Ok(Place {
435 base: PlaceBase::StaticItem,
436 projections: Vec::new(),
439 Res::Local(var_id) => {
440 if self.upvars.map_or(false, |upvars| upvars.contains_key(&var_id)) {
441 self.cat_upvar(hir_id, span, var_id)
447 base: PlaceBase::Local(var_id),
448 projections: Vec::new(),
453 def => span_bug!(span, "unexpected definition in memory categorization: {:?}", def),
457 /// Categorize an upvar.
459 /// Note: the actual upvar access contains invisible derefs of closure
460 /// environment and upvar reference as appropriate. Only regionck cares
461 /// about these dereferences, so we let it compute them as needed.
467 ) -> McResult<Place<'tcx>> {
468 let closure_expr_def_id = self.body_owner;
470 let upvar_id = ty::UpvarId {
471 var_path: ty::UpvarPath { hir_id: var_id },
472 closure_expr_id: closure_expr_def_id.to_local(),
474 let var_ty = self.node_ty(var_id)?;
480 base: PlaceBase::Upvar(upvar_id),
481 projections: Vec::new(),
484 debug!("cat_upvar ret={:?}", ret);
488 crate fn cat_rvalue(&self, hir_id: hir::HirId, span: Span, expr_ty: Ty<'tcx>) -> Place<'tcx> {
489 debug!("cat_rvalue hir_id={:?}, expr_ty={:?}, span={:?}", hir_id, expr_ty, span);
491 Place { hir_id, span, base: PlaceBase::Rvalue, projections: Vec::new(), ty: expr_ty };
492 debug!("cat_rvalue ret={:?}", ret);
496 crate fn cat_projection<N: HirNode>(
499 base_place: Place<'tcx>,
502 let mut projections = base_place.projections;
503 projections.push(Projection::Other);
505 hir_id: node.hir_id(),
508 base: base_place.base,
511 debug!("cat_field ret {:?}", ret);
515 fn cat_overloaded_place(
517 expr: &hir::Expr<'_>,
518 base: &hir::Expr<'_>,
519 ) -> McResult<Place<'tcx>> {
520 debug!("cat_overloaded_place(expr={:?}, base={:?})", expr, base);
522 // Reconstruct the output assuming it's a reference with the
523 // same region and mutability as the receiver. This holds for
524 // `Deref(Mut)::Deref(_mut)` and `Index(Mut)::index(_mut)`.
525 let place_ty = self.expr_ty(expr)?;
526 let base_ty = self.expr_ty_adjusted(base)?;
528 let (region, mutbl) = match base_ty.kind {
529 ty::Ref(region, _, mutbl) => (region, mutbl),
530 _ => span_bug!(expr.span, "cat_overloaded_place: base is not a reference"),
532 let ref_ty = self.tcx().mk_ref(region, ty::TypeAndMut { ty: place_ty, mutbl });
534 let base = self.cat_rvalue(expr.hir_id, expr.span, ref_ty);
535 self.cat_deref(expr, base)
538 fn cat_deref(&self, node: &impl HirNode, base_place: Place<'tcx>) -> McResult<Place<'tcx>> {
539 debug!("cat_deref: base_place={:?}", base_place);
541 let base_ty = base_place.ty;
542 let deref_ty = match base_ty.builtin_deref(true) {
545 debug!("explicit deref of non-derefable type: {:?}", base_ty);
549 let mut projections = base_place.projections;
550 projections.push(Projection::Deref(base_ty));
553 hir_id: node.hir_id(),
556 base: base_place.base,
559 debug!("cat_deref ret {:?}", ret);
563 crate fn cat_pattern<F>(
570 F: FnMut(&Place<'tcx>, &hir::Pat<'_>),
572 self.cat_pattern_(place, pat, &mut op)
575 // FIXME(#19596) This is a workaround, but there should be a better way to do this
578 mut place: Place<'tcx>,
583 F: FnMut(&Place<'tcx>, &hir::Pat<'_>),
585 // Here, `place` is the `Place` being matched and pat is the pattern it
586 // is being matched against.
588 // In general, the way that this works is that we walk down the pattern,
589 // constructing a `Place` that represents the path that will be taken
590 // to reach the value being matched.
592 debug!("cat_pattern(pat={:?}, place={:?})", pat, place);
594 // If (pattern) adjustments are active for this pattern, adjust the `Place` correspondingly.
595 // `Place`s are constructed differently from patterns. For example, in
599 // &&Some(x, ) => { ... },
604 // the pattern `&&Some(x,)` is represented as `Ref { Ref { TupleStruct }}`. To build the
605 // corresponding `Place` we start with the `Place` for `foo`, and then, by traversing the
606 // pattern, try to answer the question: given the address of `foo`, how is `x` reached?
608 // `&&Some(x,)` `place_foo`
609 // `&Some(x,)` `deref { place_foo}`
610 // `Some(x,)` `deref { deref { place_foo }}`
611 // (x,)` `field0 { deref { deref { place_foo }}}` <- resulting place
613 // The above example has no adjustments. If the code were instead the (after adjustments,
614 // equivalent) version
618 // Some(x, ) => { ... },
623 // Then we see that to get the same result, we must start with
624 // `deref { deref { place_foo }}` instead of `place_foo` since the pattern is now `Some(x,)`
625 // and not `&&Some(x,)`, even though its assigned type is that of `&&Some(x,)`.
626 for _ in 0..self.tables.pat_adjustments().get(pat.hir_id).map(|v| v.len()).unwrap_or(0) {
627 debug!("cat_pattern: applying adjustment to place={:?}", place);
628 place = self.cat_deref(pat, place)?;
630 let place = place; // lose mutability
631 debug!("cat_pattern: applied adjustment derefs to get place={:?}", place);
633 // Invoke the callback, but only now, after the `place` has adjusted.
635 // To see that this makes sense, consider `match &Some(3) { Some(x) => { ... }}`. In that
636 // case, the initial `place` will be that for `&Some(3)` and the pattern is `Some(x)`. We
637 // don't want to call `op` with these incompatible values. As written, what happens instead
638 // is that `op` is called with the adjusted place (that for `*&Some(3)`) and the pattern
639 // `Some(x)` (which matches). Recursing once more, `*&Some(3)` and the pattern `Some(x)`
640 // result in the place `Downcast<Some>(*&Some(3)).0` associated to `x` and invoke `op` with
641 // that (where the `ref` on `x` is implied).
645 PatKind::TupleStruct(_, ref subpats, _) | PatKind::Tuple(ref subpats, _) => {
646 // S(p1, ..., pN) or (p1, ..., pN)
647 for subpat in subpats.iter() {
648 let subpat_ty = self.pat_ty_adjusted(&subpat)?;
649 let sub_place = self.cat_projection(pat, place.clone(), subpat_ty);
650 self.cat_pattern_(sub_place, &subpat, op)?;
654 PatKind::Struct(_, field_pats, _) => {
655 // S { f1: p1, ..., fN: pN }
656 for fp in field_pats {
657 let field_ty = self.pat_ty_adjusted(&fp.pat)?;
658 let field_place = self.cat_projection(pat, place.clone(), field_ty);
659 self.cat_pattern_(field_place, &fp.pat, op)?;
663 PatKind::Or(pats) => {
665 self.cat_pattern_(place.clone(), &pat, op)?;
669 PatKind::Binding(.., Some(ref subpat)) => {
670 self.cat_pattern_(place, &subpat, op)?;
673 PatKind::Box(ref subpat) | PatKind::Ref(ref subpat, _) => {
674 // box p1, &p1, &mut p1. we can ignore the mutability of
675 // PatKind::Ref since that information is already contained
677 let subplace = self.cat_deref(pat, place)?;
678 self.cat_pattern_(subplace, &subpat, op)?;
681 PatKind::Slice(before, ref slice, after) => {
682 let element_ty = match place.ty.builtin_index() {
685 debug!("explicit index of non-indexable type {:?}", place);
689 let elt_place = self.cat_projection(pat, place.clone(), element_ty);
690 for before_pat in before {
691 self.cat_pattern_(elt_place.clone(), &before_pat, op)?;
693 if let Some(ref slice_pat) = *slice {
694 let slice_pat_ty = self.pat_ty_adjusted(&slice_pat)?;
695 let slice_place = self.cat_projection(pat, place, slice_pat_ty);
696 self.cat_pattern_(slice_place, &slice_pat, op)?;
698 for after_pat in after {
699 self.cat_pattern_(elt_place.clone(), &after_pat, op)?;
704 | PatKind::Binding(.., None)