Skip to content

Commit

Permalink
Refactor UserTypeAnnotation.
Browse files Browse the repository at this point in the history
This commit refactors the `UserTypeAnnotation` type to be referred to by
an index within `UserTypeProjection`. `UserTypeAnnotation` is instead
kept in an `IndexVec` within the `Mir` struct.

Further, instead of `UserTypeAnnotation` containing canonicalized types,
it now contains normal types and the entire `UserTypeAnnotation` is
canonicalized. To support this, the type was moved from the `rustc::mir`
module to `rustc::ty` module.
  • Loading branch information
davidtwco committed Dec 30, 2018
1 parent 7155690 commit 24a7a01
Show file tree
Hide file tree
Showing 27 changed files with 476 additions and 355 deletions.
17 changes: 0 additions & 17 deletions src/librustc/ich/impls_mir.rs
Expand Up @@ -494,22 +494,5 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for mir::ClosureOutlivesSubj

impl_stable_hash_for!(struct mir::interpret::GlobalId<'tcx> { instance, promoted });

impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for mir::UserTypeAnnotation<'gcx> {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
mem::discriminant(self).hash_stable(hcx, hasher);
match *self {
mir::UserTypeAnnotation::Ty(ref ty) => {
ty.hash_stable(hcx, hasher);
}
mir::UserTypeAnnotation::TypeOf(ref def_id, ref substs) => {
def_id.hash_stable(hcx, hasher);
substs.hash_stable(hcx, hasher);
}
}
}
}

impl_stable_hash_for!(struct mir::UserTypeProjection<'tcx> { base, projs });
impl_stable_hash_for!(struct mir::UserTypeProjections<'tcx> { contents });
26 changes: 26 additions & 0 deletions src/librustc/ich/impls_ty.rs
Expand Up @@ -1251,3 +1251,29 @@ impl_stable_hash_for!(
goal,
}
);

impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for ty::UserTypeAnnotation<'gcx> {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
mem::discriminant(self).hash_stable(hcx, hasher);
match *self {
ty::UserTypeAnnotation::Ty(ref ty) => {
ty.hash_stable(hcx, hasher);
}
ty::UserTypeAnnotation::TypeOf(ref def_id, ref substs) => {
def_id.hash_stable(hcx, hasher);
substs.hash_stable(hcx, hasher);
}
}
}
}

impl<'a> HashStable<StableHashingContext<'a>> for ty::UserTypeAnnotationIndex {
#[inline]
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
self.index().hash_stable(hcx, hasher);
}
}
50 changes: 17 additions & 33 deletions src/librustc/mir/mod.rs
Expand Up @@ -27,9 +27,12 @@ use syntax::ast::{self, Name};
use syntax::symbol::InternedString;
use syntax_pos::{Span, DUMMY_SP};
use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
use ty::subst::{CanonicalUserSubsts, Subst, Substs};
use ty::{self, AdtDef, CanonicalTy, ClosureSubsts, GeneratorSubsts, Region, Ty, TyCtxt};
use ty::subst::{Subst, Substs};
use ty::layout::VariantIdx;
use ty::{
self, AdtDef, CanonicalUserTypeAnnotations, ClosureSubsts, GeneratorSubsts, Region, Ty, TyCtxt,
UserTypeAnnotationIndex, UserTypeAnnotation,
};
use util::ppaux;

pub use mir::interpret::AssertMessage;
Expand Down Expand Up @@ -121,6 +124,9 @@ pub struct Mir<'tcx> {
/// variables and temporaries.
pub local_decls: LocalDecls<'tcx>,

/// User type annotations
pub user_type_annotations: CanonicalUserTypeAnnotations<'tcx>,

/// Number of arguments this function takes.
///
/// Starting at local 1, `arg_count` locals will be provided by the caller
Expand Down Expand Up @@ -161,7 +167,8 @@ impl<'tcx> Mir<'tcx> {
source_scope_local_data: ClearCrossCrate<IndexVec<SourceScope, SourceScopeLocalData>>,
promoted: IndexVec<Promoted, Mir<'tcx>>,
yield_ty: Option<Ty<'tcx>>,
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
local_decls: LocalDecls<'tcx>,
user_type_annotations: CanonicalUserTypeAnnotations<'tcx>,
arg_count: usize,
upvar_decls: Vec<UpvarDecl>,
span: Span,
Expand All @@ -185,6 +192,7 @@ impl<'tcx> Mir<'tcx> {
generator_drop: None,
generator_layout: None,
local_decls,
user_type_annotations,
arg_count,
upvar_decls,
spread_arg: None,
Expand Down Expand Up @@ -418,6 +426,7 @@ impl_stable_hash_for!(struct Mir<'tcx> {
generator_drop,
generator_layout,
local_decls,
user_type_annotations,
arg_count,
upvar_decls,
spread_arg,
Expand Down Expand Up @@ -2232,7 +2241,7 @@ pub enum AggregateKind<'tcx> {
&'tcx AdtDef,
VariantIdx,
&'tcx Substs<'tcx>,
Option<UserTypeAnnotation<'tcx>>,
Option<UserTypeAnnotationIndex>,
Option<usize>,
),

Expand Down Expand Up @@ -2446,38 +2455,11 @@ pub struct Constant<'tcx> {
/// indicate that `Vec<_>` was explicitly specified.
///
/// Needed for NLL to impose user-given type constraints.
pub user_ty: Option<UserTypeAnnotation<'tcx>>,
pub user_ty: Option<UserTypeAnnotationIndex>,

pub literal: &'tcx ty::Const<'tcx>,
}

/// A user-given type annotation attached to a constant. These arise
/// from constants that are named via paths, like `Foo::<A>::new` and
/// so forth.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
pub enum UserTypeAnnotation<'tcx> {
Ty(CanonicalTy<'tcx>),

/// The canonical type is the result of `type_of(def_id)` with the
/// given substitutions applied.
TypeOf(DefId, CanonicalUserSubsts<'tcx>),
}

EnumTypeFoldableImpl! {
impl<'tcx> TypeFoldable<'tcx> for UserTypeAnnotation<'tcx> {
(UserTypeAnnotation::Ty)(ty),
(UserTypeAnnotation::TypeOf)(def, substs),
}
}

EnumLiftImpl! {
impl<'a, 'tcx> Lift<'tcx> for UserTypeAnnotation<'a> {
type Lifted = UserTypeAnnotation<'tcx>;
(UserTypeAnnotation::Ty)(ty),
(UserTypeAnnotation::TypeOf)(def, substs),
}
}

/// A collection of projections into user types.
///
/// They are projections because a binding can occur a part of a
Expand Down Expand Up @@ -2556,7 +2538,7 @@ impl<'tcx> UserTypeProjections<'tcx> {
/// determined by finding the type of the `.0` field from `T`.
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
pub struct UserTypeProjection<'tcx> {
pub base: UserTypeAnnotation<'tcx>,
pub base: UserTypeAnnotationIndex,
pub projs: Vec<ProjectionElem<'tcx, (), ()>>,
}

Expand Down Expand Up @@ -2970,6 +2952,7 @@ CloneTypeFoldableAndLiftImpls! {
SourceScope,
SourceScopeData,
SourceScopeLocalData,
UserTypeAnnotationIndex,
}

BraceStructTypeFoldableImpl! {
Expand All @@ -2983,6 +2966,7 @@ BraceStructTypeFoldableImpl! {
generator_drop,
generator_layout,
local_decls,
user_type_annotations,
arg_count,
upvar_decls,
spread_arg,
Expand Down
24 changes: 15 additions & 9 deletions src/librustc/mir/visit.rs
@@ -1,4 +1,5 @@
use hir::def_id::DefId;
use infer::canonical::Canonical;
use ty::subst::Substs;
use ty::{ClosureSubsts, GeneratorSubsts, Region, Ty};
use mir::*;
Expand Down Expand Up @@ -219,9 +220,10 @@ macro_rules! make_mir_visitor {

fn visit_user_type_annotation(
&mut self,
ty: & $($mutability)* UserTypeAnnotation<'tcx>,
index: UserTypeAnnotationIndex,
ty: & $($mutability)* Canonical<'tcx, UserTypeAnnotation<'tcx>>,
) {
self.super_user_type_annotation(ty);
self.super_user_type_annotation(index, ty);
}

fn visit_region(&mut self,
Expand Down Expand Up @@ -307,6 +309,14 @@ macro_rules! make_mir_visitor {
self.visit_local_decl(local, & $($mutability)* mir.local_decls[local]);
}

for index in mir.user_type_annotations.indices() {
let (span, annotation) = & $($mutability)* mir.user_type_annotations[index];
self.visit_user_type_annotation(
index, annotation
);
self.visit_span(span);
}

self.visit_span(&$($mutability)* mir.span);
}

Expand Down Expand Up @@ -865,18 +875,14 @@ macro_rules! make_mir_visitor {

fn super_user_type_projection(
&mut self,
ty: & $($mutability)* UserTypeProjection<'tcx>,
_ty: & $($mutability)* UserTypeProjection<'tcx>,
) {
let UserTypeProjection {
ref $($mutability)* base,
projs: _, // Note: Does not visit projection elems!
} = *ty;
self.visit_user_type_annotation(base);
}

fn super_user_type_annotation(
&mut self,
_ty: & $($mutability)* UserTypeAnnotation<'tcx>,
_index: UserTypeAnnotationIndex,
_ty: & $($mutability)* Canonical<'tcx, UserTypeAnnotation<'tcx>>,
) {
}

Expand Down

0 comments on commit 24a7a01

Please sign in to comment.