]> git.proxmox.com Git - rustc.git/blob - src/librustc_mir/borrow_check/place_ext.rs
New upstream version 1.27.1+dfsg1
[rustc.git] / src / librustc_mir / borrow_check / place_ext.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use rustc::hir;
12 use rustc::mir::ProjectionElem;
13 use rustc::mir::{Local, Mir, Place};
14 use rustc::ty::{self, TyCtxt};
15
16 /// Extension methods for the `Place` type.
17 crate trait PlaceExt<'tcx> {
18 /// True if this is a deref of a raw pointer.
19 fn is_unsafe_place(&self, tcx: TyCtxt<'_, '_, 'tcx>, mir: &Mir<'tcx>) -> bool;
20
21 /// If this is a place like `x.f.g`, returns the local
22 /// `x`. Returns `None` if this is based in a static.
23 fn root_local(&self) -> Option<Local>;
24 }
25
26 impl<'tcx> PlaceExt<'tcx> for Place<'tcx> {
27 fn is_unsafe_place(&self, tcx: TyCtxt<'_, '_, 'tcx>, mir: &Mir<'tcx>) -> bool {
28 match self {
29 Place::Local(_) => false,
30 Place::Static(static_) => {
31 tcx.is_static(static_.def_id) == Some(hir::Mutability::MutMutable)
32 }
33 Place::Projection(proj) => match proj.elem {
34 ProjectionElem::Field(..)
35 | ProjectionElem::Downcast(..)
36 | ProjectionElem::Subslice { .. }
37 | ProjectionElem::ConstantIndex { .. }
38 | ProjectionElem::Index(_) => proj.base.is_unsafe_place(tcx, mir),
39 ProjectionElem::Deref => {
40 let ty = proj.base.ty(mir, tcx).to_ty(tcx);
41 match ty.sty {
42 ty::TyRawPtr(..) => true,
43 _ => proj.base.is_unsafe_place(tcx, mir),
44 }
45 }
46 },
47 }
48 }
49
50 fn root_local(&self) -> Option<Local> {
51 let mut p = self;
52 loop {
53 match p {
54 Place::Projection(pi) => p = &pi.base,
55 Place::Static(_) => return None,
56 Place::Local(l) => return Some(*l),
57 }
58 }
59 }
60 }