]>
git.proxmox.com Git - rustc.git/blob - 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.
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.
12 use rustc
::mir
::ProjectionElem
;
13 use rustc
::mir
::{Local, Mir, Place}
;
14 use rustc
::ty
::{self, TyCtxt}
;
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
;
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
>;
26 impl<'tcx
> PlaceExt
<'tcx
> for Place
<'tcx
> {
27 fn is_unsafe_place(&self, tcx
: TyCtxt
<'_
, '_
, 'tcx
>, mir
: &Mir
<'tcx
>) -> bool
{
29 Place
::Local(_
) => false,
30 Place
::Static(static_
) => {
31 tcx
.is_static(static_
.def_id
) == Some(hir
::Mutability
::MutMutable
)
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
);
42 ty
::TyRawPtr(..) => true,
43 _
=> proj
.base
.is_unsafe_place(tcx
, mir
),
50 fn root_local(&self) -> Option
<Local
> {
54 Place
::Projection(pi
) => p
= &pi
.base
,
55 Place
::Static(_
) => return None
,
56 Place
::Local(l
) => return Some(*l
),