Skip to content

Commit

Permalink
Uplift movability and mutability, the simple way
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Oct 19, 2023
1 parent c104861 commit e8e9f6a
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 88 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3419,6 +3419,7 @@ dependencies = [
"rustc_macros",
"rustc_serialize",
"rustc_span",
"rustc_type_ir",
"smallvec",
"thin-vec",
"tracing",
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ rustc_lexer = { path = "../rustc_lexer" }
rustc_macros = { path = "../rustc_macros" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_span = { path = "../rustc_span" }
# depends on Mutability and Movability, which could be uplifted into a common crate.
rustc_type_ir = { path = "../rustc_type_ir" }
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
thin-vec = "0.2.12"
tracing = "0.1"
63 changes: 1 addition & 62 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_span::source_map::{respan, Spanned};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{ErrorGuaranteed, Span, DUMMY_SP};
pub use rustc_type_ir::{Movability, Mutability};
use std::fmt;
use std::mem;
use thin_vec::{thin_vec, ThinVec};
Expand Down Expand Up @@ -800,57 +801,6 @@ pub enum PatKind {
MacCall(P<MacCall>),
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)]
#[derive(HashStable_Generic, Encodable, Decodable)]
pub enum Mutability {
// N.B. Order is deliberate, so that Not < Mut
Not,
Mut,
}

impl Mutability {
pub fn invert(self) -> Self {
match self {
Mutability::Mut => Mutability::Not,
Mutability::Not => Mutability::Mut,
}
}

/// Returns `""` (empty string) or `"mut "` depending on the mutability.
pub fn prefix_str(self) -> &'static str {
match self {
Mutability::Mut => "mut ",
Mutability::Not => "",
}
}

/// Returns `"&"` or `"&mut "` depending on the mutability.
pub fn ref_prefix_str(self) -> &'static str {
match self {
Mutability::Not => "&",
Mutability::Mut => "&mut ",
}
}

/// Returns `""` (empty string) or `"mutably "` depending on the mutability.
pub fn mutably_str(self) -> &'static str {
match self {
Mutability::Not => "",
Mutability::Mut => "mutably ",
}
}

/// Return `true` if self is mutable
pub fn is_mut(self) -> bool {
matches!(self, Self::Mut)
}

/// Return `true` if self is **not** mutable
pub fn is_not(self) -> bool {
matches!(self, Self::Not)
}
}

/// The kind of borrow in an `AddrOf` expression,
/// e.g., `&place` or `&raw const place`.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
Expand Down Expand Up @@ -1579,17 +1529,6 @@ pub enum CaptureBy {
Ref,
}

/// The movability of a generator / closure literal:
/// whether a generator contains self-references, causing it to be `!Unpin`.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable, Debug, Copy)]
#[derive(HashStable_Generic)]
pub enum Movability {
/// May contain self-references, `!Unpin`.
Static,
/// Must not contain self-references, `Unpin`.
Movable,
}

/// Closure lifetime binder, `for<'a, 'b>` in `for<'a, 'b> |_: &'a (), _: &'b ()|`.
#[derive(Clone, Encodable, Decodable, Debug)]
pub enum ClosureBinder {
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
/// Requirements for a `StableHashingContext` to be used in this crate.
/// This is a hack to allow using the `HashStable_Generic` derive macro
/// instead of implementing everything in `rustc_middle`.
pub trait HashStableContext: rustc_span::HashStableContext {
pub trait HashStableContext:
rustc_type_ir::HashStableContext + rustc_span::HashStableContext
{
fn hash_attr(&mut self, _: &ast::Attribute, hasher: &mut StableHasher);
}

Expand Down
8 changes: 1 addition & 7 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
type Predicate = Predicate<'tcx>;
type PredicateKind = ty::PredicateKind<'tcx>;
type TypeAndMut = TypeAndMut<'tcx>;
type Mutability = hir::Mutability;
type Movability = hir::Movability;
type Ty = Ty<'tcx>;
type Tys = &'tcx List<Ty<'tcx>>;
type AliasTy = ty::AliasTy<'tcx>;
Expand Down Expand Up @@ -118,13 +116,9 @@ impl<'tcx> Interner for TyCtxt<'tcx> {

fn ty_and_mut_to_parts(
TypeAndMut { ty, mutbl }: TypeAndMut<'tcx>,
) -> (Self::Ty, Self::Mutability) {
) -> (Self::Ty, ty::Mutability) {
(ty, mutbl)
}

fn mutability_is_mut(mutbl: Self::Mutability) -> bool {
mutbl.is_mut()
}
}

type InternedSet<'tcx, T> = ShardedHashMap<InternedInSet<'tcx, T>, ()>;
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_type_ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ pub trait Interner: Sized {
type PredicateKind: Clone + Debug + Hash + PartialEq + Eq;

type TypeAndMut: Clone + Debug + Hash + Ord;
type Mutability: Clone + Debug + Hash + Ord;
type Movability: Clone + Debug + Hash + Ord;

// Kinds of tys
type Ty: Clone + DebugWithInfcx<Self> + Hash + Ord;
Expand Down Expand Up @@ -95,8 +93,7 @@ pub trait Interner: Sized {
type InferRegion: Clone + DebugWithInfcx<Self> + Hash + Ord;
type PlaceholderRegion: Clone + Debug + Hash + Ord;

fn ty_and_mut_to_parts(ty_and_mut: Self::TypeAndMut) -> (Self::Ty, Self::Mutability);
fn mutability_is_mut(mutbl: Self::Mutability) -> bool;
fn ty_and_mut_to_parts(ty_and_mut: Self::TypeAndMut) -> (Self::Ty, Mutability);
}

/// Imagine you have a function `F: FnOnce(&[T]) -> R`, plus an iterator `iter`
Expand Down
84 changes: 70 additions & 14 deletions compiler/rustc_type_ir/src/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,68 @@ use self::TyKind::*;
use rustc_data_structures::stable_hasher::HashStable;
use rustc_serialize::{Decodable, Decoder, Encodable};

/// The movability of a generator / closure literal:
/// whether a generator contains self-references, causing it to be `!Unpin`.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable, Debug, Copy)]
#[derive(HashStable_Generic)]
pub enum Movability {
/// May contain self-references, `!Unpin`.
Static,
/// Must not contain self-references, `Unpin`.
Movable,
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)]
#[derive(HashStable_Generic, Encodable, Decodable)]
pub enum Mutability {
// N.B. Order is deliberate, so that Not < Mut
Not,
Mut,
}

impl Mutability {
pub fn invert(self) -> Self {
match self {
Mutability::Mut => Mutability::Not,
Mutability::Not => Mutability::Mut,
}
}

/// Returns `""` (empty string) or `"mut "` depending on the mutability.
pub fn prefix_str(self) -> &'static str {
match self {
Mutability::Mut => "mut ",
Mutability::Not => "",
}
}

/// Returns `"&"` or `"&mut "` depending on the mutability.
pub fn ref_prefix_str(self) -> &'static str {
match self {
Mutability::Not => "&",
Mutability::Mut => "&mut ",
}
}

/// Returns `""` (empty string) or `"mutably "` depending on the mutability.
pub fn mutably_str(self) -> &'static str {
match self {
Mutability::Not => "",
Mutability::Mut => "mutably ",
}
}

/// Return `true` if self is mutable
pub fn is_mut(self) -> bool {
matches!(self, Self::Mut)
}

/// Return `true` if self is **not** mutable
pub fn is_not(self) -> bool {
matches!(self, Self::Not)
}
}

/// Specifies how a trait object is represented.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[derive(Encodable, Decodable, HashStable_Generic)]
Expand Down Expand Up @@ -98,7 +160,7 @@ pub enum TyKind<I: Interner> {

/// A reference; a pointer with an associated lifetime. Written as
/// `&'a mut T` or `&'a T`.
Ref(I::Region, I::Ty, I::Mutability),
Ref(I::Region, I::Ty, Mutability),

/// The anonymous type of a function declaration/definition. Each
/// function has a unique type.
Expand Down Expand Up @@ -141,7 +203,7 @@ pub enum TyKind<I: Interner> {
///
/// For more info about generator args, visit the documentation for
/// `GeneratorArgs`.
Generator(I::DefId, I::GenericArgs, I::Movability),
Generator(I::DefId, I::GenericArgs, Movability),

/// A type representing the types stored inside a generator.
/// This should only appear as part of the `GeneratorArgs`.
Expand Down Expand Up @@ -506,15 +568,15 @@ impl<I: Interner> DebugWithInfcx<I> for TyKind<I> {
Slice(t) => write!(f, "[{:?}]", &this.wrap(t)),
RawPtr(p) => {
let (ty, mutbl) = I::ty_and_mut_to_parts(p.clone());
match I::mutability_is_mut(mutbl) {
true => write!(f, "*mut "),
false => write!(f, "*const "),
match mutbl {
Mutability::Mut => write!(f, "*mut "),
Mutability::Not => write!(f, "*const "),
}?;
write!(f, "{:?}", &this.wrap(ty))
}
Ref(r, t, m) => match I::mutability_is_mut(m.clone()) {
true => write!(f, "&{:?} mut {:?}", &this.wrap(r), &this.wrap(t)),
false => write!(f, "&{:?} {:?}", &this.wrap(r), &this.wrap(t)),
Ref(r, t, m) => match m {
Mutability::Mut => write!(f, "&{:?} mut {:?}", &this.wrap(r), &this.wrap(t)),
Mutability::Not => write!(f, "&{:?} {:?}", &this.wrap(r), &this.wrap(t)),
},
FnDef(d, s) => f.debug_tuple_field2_finish("FnDef", d, &this.wrap(s)),
FnPtr(s) => write!(f, "{:?}", &this.wrap(s)),
Expand Down Expand Up @@ -573,8 +635,6 @@ where
I::Const: Encodable<E>,
I::Region: Encodable<E>,
I::TypeAndMut: Encodable<E>,
I::Mutability: Encodable<E>,
I::Movability: Encodable<E>,
I::PolyFnSig: Encodable<E>,
I::BoundExistentialPredicates: Encodable<E>,
I::Tys: Encodable<E>,
Expand Down Expand Up @@ -687,8 +747,6 @@ where
I::Const: Decodable<D>,
I::Region: Decodable<D>,
I::TypeAndMut: Decodable<D>,
I::Mutability: Decodable<D>,
I::Movability: Decodable<D>,
I::PolyFnSig: Decodable<D>,
I::BoundExistentialPredicates: Decodable<D>,
I::Tys: Decodable<D>,
Expand Down Expand Up @@ -753,8 +811,6 @@ where
I::PolyFnSig: HashStable<CTX>,
I::BoundExistentialPredicates: HashStable<CTX>,
I::Region: HashStable<CTX>,
I::Movability: HashStable<CTX>,
I::Mutability: HashStable<CTX>,
I::Tys: HashStable<CTX>,
I::AliasTy: HashStable<CTX>,
I::BoundTy: HashStable<CTX>,
Expand Down

0 comments on commit e8e9f6a

Please sign in to comment.