Skip to content

Commit

Permalink
move frozen to rustc_data_structures
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-i-m committed Mar 13, 2020
1 parent 508d4a2 commit da4e33a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 26 deletions.
57 changes: 57 additions & 0 deletions src/librustc_data_structures/frozen.rs
@@ -0,0 +1,57 @@
//! An immutable, owned value.
//!
//! The purpose of `Frozen` is to make a value immutable for the sake of defensive programming. For example,
//! suppose we have the following:
//!
//! ```rust
//! struct Bar { /* some data */ }
//!
//! struct Foo {
//! /// Some computed data that should never change after construction.
//! pub computed: Bar,
//!
//! /* some other fields */
//! }
//!
//! impl Bar {
//! /// Mutate the `Bar`.
//! pub fn mutate(&mut self) { }
//! }
//! ```
//!
//! Now suppose we want to pass around a mutable `Foo` instance but, we want to make sure that
//! `computed` does not change accidentally (e.g. somebody might accidentally call
//! `foo.computed.mutate()`). This is what `Frozen` is for. We can do the following:
//!
//! ```rust
//! use rustc_data_structures::frozen::Frozen;
//!
//! struct Foo {
//! /// Some computed data that should never change after construction.
//! pub computed: Frozen<Bar>,
//!
//! /* some other fields */
//! }
//! ```
//!
//! `Frozen` impls `Deref`, so we can ergonomically call methods on `Bar`, but it doesn't `impl
//! DerefMut`. Now calling `foo.compute.mutate()` will result in a compile-time error stating that
//! `mutate` requires a mutable reference but we don't have one.

/// An owned immutable value.
#[derive(Debug)]
pub struct Frozen<T>(T);

impl<T> Frozen<T> {
pub fn freeze(val: T) -> Self {
Frozen(val)
}
}

impl<T> std::ops::Deref for Frozen<T> {
type Target = T;

fn deref(&self) -> &T {
&self.0
}
}
1 change: 1 addition & 0 deletions src/librustc_data_structures/lib.rs
Expand Up @@ -94,6 +94,7 @@ pub mod profiling;
pub mod vec_linked_list;
pub mod work_queue;
pub use atomic_ref::AtomicRef;
pub mod frozen;

pub struct OnDrop<F: Fn()>(pub F);

Expand Down
28 changes: 5 additions & 23 deletions src/librustc_mir/borrow_check/mod.rs
Expand Up @@ -73,24 +73,6 @@ crate use place_ext::PlaceExt;
crate use places_conflict::{places_conflict, PlaceConflictBias};
crate use region_infer::RegionInferenceContext;

/// An owned immutable value.
#[derive(Debug)]
struct Frozen<T>(T);

impl<T> Frozen<T> {
pub fn freeze(val: T) -> Self {
Frozen(val)
}
}

impl<T> std::ops::Deref for Frozen<T> {
type Target = T;

fn deref(&self) -> &T {
&self.0
}
}

// FIXME(eddyb) perhaps move this somewhere more centrally.
#[derive(Debug)]
crate struct Upvar {
Expand Down Expand Up @@ -1595,11 +1577,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
mpi,
);
} // Only query longest prefix with a MovePath, not further
// ancestors; dataflow recurs on children when parents
// move (to support partial (re)inits).
//
// (I.e., querying parents breaks scenario 7; but may want
// to do such a query based on partial-init feature-gate.)
// ancestors; dataflow recurs on children when parents
// move (to support partial (re)inits).
//
// (I.e., querying parents breaks scenario 7; but may want
// to do such a query based on partial-init feature-gate.)
}

/// Subslices correspond to multiple move paths, so we iterate through the
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/region_infer/mod.rs
Expand Up @@ -7,6 +7,7 @@ use rustc::mir::{
};
use rustc::ty::{self, subst::SubstsRef, RegionVid, Ty, TyCtxt, TypeFoldable};
use rustc_data_structures::binary_search_util;
use rustc_data_structures::frozen::Frozen;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::graph::scc::Sccs;
use rustc_hir::def_id::DefId;
Expand All @@ -31,7 +32,6 @@ use crate::borrow_check::{
},
type_check::{free_region_relations::UniversalRegionRelations, Locations},
universal_regions::UniversalRegions,
Frozen,
};

mod dump_mir;
Expand Down
@@ -1,6 +1,7 @@
use rustc::mir::ConstraintCategory;
use rustc::ty::free_region_map::FreeRegionRelations;
use rustc::ty::{self, RegionVid, Ty, TyCtxt};
use rustc_data_structures::frozen::Frozen;
use rustc_data_structures::transitive_relation::TransitiveRelation;
use rustc_infer::infer::canonical::QueryRegionConstraints;
use rustc_infer::infer::region_constraints::GenericKind;
Expand All @@ -15,7 +16,6 @@ use crate::borrow_check::{
type_check::constraint_conversion,
type_check::{Locations, MirTypeckRegionConstraints},
universal_regions::UniversalRegions,
Frozen,
};

#[derive(Debug)]
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/type_check/mod.rs
Expand Up @@ -18,6 +18,7 @@ use rustc::ty::{
self, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, RegionVid, ToPolyTraitRef, Ty,
TyCtxt, UserType, UserTypeAnnotationIndex,
};
use rustc_data_structures::frozen::Frozen;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::struct_span_err;
use rustc_hir as hir;
Expand Down Expand Up @@ -55,7 +56,6 @@ use crate::borrow_check::{
renumber,
type_check::free_region_relations::{CreateResult, UniversalRegionRelations},
universal_regions::{DefiningTy, UniversalRegions},
Frozen,
};

macro_rules! span_mirbug {
Expand Down

0 comments on commit da4e33a

Please sign in to comment.