summaryrefslogtreecommitdiffstats
path: root/src/librustc_mir/borrow_check/mod.rs
diff options
context:
space:
mode:
authorKeith Yeung <kungfukeith11@gmail.com>2018-03-20 12:58:38 -0700
committerKeith Yeung <kungfukeith11@gmail.com>2018-04-28 01:55:25 -0700
commit180e2426c90cae9362bcc68576293da7a5bdd22a (patch)
tree2c534b6c59331d7002862dbab1fddedb1850cf3c /src/librustc_mir/borrow_check/mod.rs
parentDetermine unused mutable variables based on the 'root' place (diff)
downloadgrust-180e2426c90cae9362bcc68576293da7a5bdd22a.tar.gz
grust-180e2426c90cae9362bcc68576293da7a5bdd22a.tar.bz2
grust-180e2426c90cae9362bcc68576293da7a5bdd22a.tar.xz
Return LocalMutationIsAllowed in is_mutable
Diffstat (limited to 'src/librustc_mir/borrow_check/mod.rs')
-rw-r--r--src/librustc_mir/borrow_check/mod.rs63
1 files changed, 32 insertions, 31 deletions
diff --git a/src/librustc_mir/borrow_check/mod.rs b/src/librustc_mir/borrow_check/mod.rs
index 2498162d10..9da5d1dcdc 100644
--- a/src/librustc_mir/borrow_check/mod.rs
+++ b/src/librustc_mir/borrow_check/mod.rs
@@ -1366,7 +1366,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
1366 ) { 1366 ) {
1367 debug!("check_if_reassignment_to_immutable_state({:?})", place); 1367 debug!("check_if_reassignment_to_immutable_state({:?})", place);
1368 // determine if this path has a non-mut owner (and thus needs checking). 1368 // determine if this path has a non-mut owner (and thus needs checking).
1369 if let Ok(_) = self.is_mutable(place, LocalMutationIsAllowed::No) { 1369 if let Ok(..) = self.is_mutable(place, LocalMutationIsAllowed::No) {
1370 return; 1370 return;
1371 } 1371 }
1372 debug!( 1372 debug!(
@@ -1681,24 +1681,23 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
1681 Reservation(WriteKind::MutableBorrow(BorrowKind::Mut { .. })) 1681 Reservation(WriteKind::MutableBorrow(BorrowKind::Mut { .. }))
1682 | Write(WriteKind::MutableBorrow(BorrowKind::Mut { .. })) => { 1682 | Write(WriteKind::MutableBorrow(BorrowKind::Mut { .. })) => {
1683 match self.is_mutable(place, is_local_mutation_allowed) { 1683 match self.is_mutable(place, is_local_mutation_allowed) {
1684 Ok(Place::Local(local)) 1684 Ok((Place::Local(local), mut_allowed)) => {
1685 if is_local_mutation_allowed != LocalMutationIsAllowed::Yes => 1685 if mut_allowed != LocalMutationIsAllowed::Yes {
1686 { 1686 // If the local may be initialized, and it is now currently being
1687 // If the local may be initialized, and it is now currently being 1687 // mutated, then it is justified to be annotated with the `mut`
1688 // mutated, then it is justified to be annotated with the `mut` keyword, 1688 // keyword, since the mutation may be a possible reassignment.
1689 // since the mutation may be a possible reassignment. 1689 let mpi = self.move_data.rev_lookup.find_local(*local);
1690 let mpi = self.move_data.rev_lookup.find_local(*local); 1690 if flow_state.inits.contains(&mpi) {
1691 if flow_state.inits.contains(&mpi) { 1691 self.used_mut.insert(*local);
1692 self.used_mut.insert(*local); 1692 }
1693 } 1693 }
1694 } 1694 }
1695 Ok(Place::Projection(ref proj)) => { 1695 Ok((Place::Projection(ref proj), _mut_allowed)) => {
1696 if let Some(field) = self.is_upvar_field_projection(&proj.base) { 1696 if let Some(field) = self.is_upvar_field_projection(&proj.base) {
1697 self.used_mut_upvars.push(field); 1697 self.used_mut_upvars.push(field);
1698 } 1698 }
1699 } 1699 }
1700 Ok(Place::Local(_)) | 1700 Ok((Place::Static(..), _mut_allowed)) => {}
1701 Ok(Place::Static(..)) => {}
1702 Err(place_err) => { 1701 Err(place_err) => {
1703 error_reported = true; 1702 error_reported = true;
1704 let item_msg = self.get_default_err_msg(place); 1703 let item_msg = self.get_default_err_msg(place);
@@ -1719,24 +1718,23 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
1719 } 1718 }
1720 Reservation(WriteKind::Mutate) | Write(WriteKind::Mutate) => { 1719 Reservation(WriteKind::Mutate) | Write(WriteKind::Mutate) => {
1721 match self.is_mutable(place, is_local_mutation_allowed) { 1720 match self.is_mutable(place, is_local_mutation_allowed) {
1722 Ok(Place::Local(local)) 1721 Ok((Place::Local(local), mut_allowed)) => {
1723 if is_local_mutation_allowed != LocalMutationIsAllowed::Yes => 1722 if mut_allowed != LocalMutationIsAllowed::Yes {
1724 { 1723 // If the local may be initialized, and it is now currently being
1725 // If the local may be initialized, and it is now currently being 1724 // mutated, then it is justified to be annotated with the `mut`
1726 // mutated, then it is justified to be annotated with the `mut` keyword, 1725 // keyword, since the mutation may be a possible reassignment.
1727 // since the mutation may be a possible reassignment. 1726 let mpi = self.move_data.rev_lookup.find_local(*local);
1728 let mpi = self.move_data.rev_lookup.find_local(*local); 1727 if flow_state.inits.contains(&mpi) {
1729 if flow_state.inits.contains(&mpi) { 1728 self.used_mut.insert(*local);
1730 self.used_mut.insert(*local); 1729 }
1731 } 1730 }
1732 } 1731 }
1733 Ok(Place::Projection(ref proj)) => { 1732 Ok((Place::Projection(ref proj), _mut_allowed)) => {
1734 if let Some(field) = self.is_upvar_field_projection(&proj.base) { 1733 if let Some(field) = self.is_upvar_field_projection(&proj.base) {
1735 self.used_mut_upvars.push(field); 1734 self.used_mut_upvars.push(field);
1736 } 1735 }
1737 } 1736 }
1738 Ok(Place::Local(_)) | 1737 Ok((Place::Static(..), _mut_allowed)) => {}
1739 Ok(Place::Static(..)) => {}
1740 Err(place_err) => { 1738 Err(place_err) => {
1741 error_reported = true; 1739 error_reported = true;
1742 1740
@@ -1835,25 +1833,28 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
1835 &self, 1833 &self,
1836 place: &'d Place<'tcx>, 1834 place: &'d Place<'tcx>,
1837 is_local_mutation_allowed: LocalMutationIsAllowed, 1835 is_local_mutation_allowed: LocalMutationIsAllowed,
1838 ) -> Result<&'d Place<'tcx>, &'d Place<'tcx>> { 1836 ) -> Result<(&'d Place<'tcx>, LocalMutationIsAllowed), &'d Place<'tcx>> {
1839 match *place { 1837 match *place {
1840 Place::Local(local) => { 1838 Place::Local(local) => {
1841 let local = &self.mir.local_decls[local]; 1839 let local = &self.mir.local_decls[local];
1842 match local.mutability { 1840 match local.mutability {
1843 Mutability::Not => match is_local_mutation_allowed { 1841 Mutability::Not => match is_local_mutation_allowed {
1844 LocalMutationIsAllowed::Yes | LocalMutationIsAllowed::ExceptUpvars => { 1842 LocalMutationIsAllowed::Yes => {
1845 Ok(place) 1843 Ok((place, LocalMutationIsAllowed::Yes))
1844 }
1845 LocalMutationIsAllowed::ExceptUpvars => {
1846 Ok((place, LocalMutationIsAllowed::ExceptUpvars))
1846 } 1847 }
1847 LocalMutationIsAllowed::No => Err(place), 1848 LocalMutationIsAllowed::No => Err(place),
1848 }, 1849 },
1849 Mutability::Mut => Ok(place), 1850 Mutability::Mut => Ok((place, is_local_mutation_allowed)),
1850 } 1851 }
1851 } 1852 }
1852 Place::Static(ref static_) => 1853 Place::Static(ref static_) =>
1853 if self.tcx.is_static(static_.def_id) != Some(hir::Mutability::MutMutable) { 1854 if self.tcx.is_static(static_.def_id) != Some(hir::Mutability::MutMutable) {
1854 Err(place) 1855 Err(place)
1855 } else { 1856 } else {
1856 Ok(place) 1857 Ok((place, is_local_mutation_allowed))
1857 }, 1858 },
1858 Place::Projection(ref proj) => { 1859 Place::Projection(ref proj) => {
1859 match proj.elem { 1860 match proj.elem {
@@ -1891,7 +1892,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
1891 hir::MutImmutable => return Err(place), 1892 hir::MutImmutable => return Err(place),
1892 // `*mut` raw pointers are always mutable, regardless of context 1893 // `*mut` raw pointers are always mutable, regardless of context
1893 // The users have to check by themselve. 1894 // The users have to check by themselve.
1894 hir::MutMutable => return Ok(place), 1895 hir::MutMutable => return Ok((place, is_local_mutation_allowed)),
1895 } 1896 }
1896 } 1897 }
1897 // `Box<T>` owns its content, so mutable if its location is mutable 1898 // `Box<T>` owns its content, so mutable if its location is mutable