提交 ed871cb3 编写于 作者: M Matthew Jasper

Use a struct for user type annotations

上级 5ca6bd50
......@@ -1240,6 +1240,12 @@ impl<'tcx, G> for struct traits::InEnvironment<'tcx, G> {
}
);
impl_stable_hash_for!(
struct ty::CanonicalUserTypeAnnotation<'tcx> {
user_ty, span
}
);
impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for ty::UserType<'gcx> {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
......
......@@ -31,7 +31,7 @@
use ty::layout::VariantIdx;
use ty::{
self, AdtDef, CanonicalUserTypeAnnotations, ClosureSubsts, GeneratorSubsts, Region, Ty, TyCtxt,
UserTypeAnnotationIndex, UserType,
UserTypeAnnotationIndex,
};
use util::ppaux;
......
use hir::def_id::DefId;
use infer::canonical::Canonical;
use ty::subst::Substs;
use ty::{ClosureSubsts, GeneratorSubsts, Region, Ty};
use ty::{CanonicalUserTypeAnnotation, ClosureSubsts, GeneratorSubsts, Region, Ty};
use mir::*;
use syntax_pos::Span;
......@@ -221,7 +220,7 @@ fn visit_user_type_projection(
fn visit_user_type_annotation(
&mut self,
index: UserTypeAnnotationIndex,
ty: & $($mutability)* Canonical<'tcx, UserType<'tcx>>,
ty: & $($mutability)* CanonicalUserTypeAnnotation<'tcx>,
) {
self.super_user_type_annotation(index, ty);
}
......@@ -309,12 +308,15 @@ fn super_mir(&mut self,
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];
macro_rules! type_annotations {
(mut) => (mir.user_type_annotations.iter_enumerated_mut());
() => (mir.user_type_annotations.iter_enumerated());
};
for (index, annotation) in type_annotations!($($mutability)*) {
self.visit_user_type_annotation(
index, annotation
);
self.visit_span(span);
}
self.visit_span(&$($mutability)* mir.span);
......@@ -882,8 +884,9 @@ fn super_user_type_projection(
fn super_user_type_annotation(
&mut self,
_index: UserTypeAnnotationIndex,
_ty: & $($mutability)* Canonical<'tcx, UserType<'tcx>>,
ty: & $($mutability)* CanonicalUserTypeAnnotation<'tcx>,
) {
self.visit_span(& $($mutability)* ty.span);
}
fn super_ty(&mut self, _ty: & $($mutability)* Ty<'tcx>) {
......
......@@ -807,7 +807,27 @@ pub struct UserTypeAnnotationIndex {
/// Mapping of type annotation indices to canonical user type annotations.
pub type CanonicalUserTypeAnnotations<'tcx> =
IndexVec<UserTypeAnnotationIndex, (Span, CanonicalUserType<'tcx>)>;
IndexVec<UserTypeAnnotationIndex, CanonicalUserTypeAnnotation<'tcx>>;
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
pub struct CanonicalUserTypeAnnotation<'tcx> {
pub user_ty: CanonicalUserType<'tcx>,
pub span: Span,
}
BraceStructTypeFoldableImpl! {
impl<'tcx> TypeFoldable<'tcx> for CanonicalUserTypeAnnotation<'tcx> {
user_ty, span
}
}
BraceStructLiftImpl! {
impl<'a, 'tcx> Lift<'tcx> for CanonicalUserTypeAnnotation<'a> {
type Lifted = CanonicalUserTypeAnnotation<'tcx>;
user_ty, span
}
}
/// Canonicalized user type annotation.
pub type CanonicalUserType<'gcx> = Canonical<'gcx, UserType<'gcx>>;
......
......@@ -74,7 +74,7 @@
pub use self::context::{Lift, TypeckTables, CtxtInterners};
pub use self::context::{
UserTypeAnnotationIndex, UserType, CanonicalUserType,
CanonicalUserTypeAnnotations,
CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations,
};
pub use self::instance::{Instance, InstanceDef};
......
use rustc::infer::canonical::Canonical;
use rustc::ty::subst::Substs;
use rustc::ty::{
self, ClosureSubsts, GeneratorSubsts, Ty, TypeFoldable, UserType,
UserTypeAnnotationIndex,
self, ClosureSubsts, GeneratorSubsts, Ty, TypeFoldable,
UserTypeAnnotationIndex, CanonicalUserTypeAnnotation
};
use rustc::mir::{Location, Mir};
use rustc::mir::visit::{MutVisitor, TyContext};
......@@ -62,7 +61,7 @@ fn visit_ty(&mut self, ty: &mut Ty<'tcx>, ty_context: TyContext) {
fn visit_user_type_annotation(
&mut self,
_index: UserTypeAnnotationIndex,
_ty: &mut Canonical<'tcx, UserType<'tcx>>,
_ty: &mut CanonicalUserTypeAnnotation,
) {
// User type annotations represent the types that the user
// wrote in the progarm. We don't want to erase the regions
......
......@@ -38,7 +38,7 @@
use rustc::ty::subst::{Subst, Substs, UnpackedKind};
use rustc::ty::{
self, RegionVid, ToPolyTraitRef, Ty, TyCtxt, TyKind, UserType,
UserTypeAnnotationIndex,
CanonicalUserTypeAnnotation, UserTypeAnnotationIndex,
};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
......@@ -920,13 +920,14 @@ fn instantiate_user_type_annotations(&mut self) {
self.mir.user_type_annotations
);
for annotation_index in self.mir.user_type_annotations.indices() {
let (span, canonical_annotation) = &self.mir.user_type_annotations[annotation_index];
let CanonicalUserTypeAnnotation { span, ref user_ty } =
self.mir.user_type_annotations[annotation_index];
let (mut annotation, _) = self.infcx.instantiate_canonical_with_fresh_inference_vars(
*span, &canonical_annotation
span, user_ty
);
match annotation {
UserType::Ty(ref mut ty) =>
*ty = self.normalize(ty, Locations::All(*span)),
*ty = self.normalize(ty, Locations::All(span)),
_ => {},
}
self.instantiated_type_annotations.insert(annotation_index, annotation);
......
......@@ -3,6 +3,7 @@
use build::Builder;
use hair::*;
use rustc::mir::*;
use rustc::ty::CanonicalUserTypeAnnotation;
impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
/// Compile `expr`, yielding a compile-time constant. Assumes that
......@@ -31,7 +32,10 @@ fn expr_as_constant(&mut self, expr: Expr<'tcx>) -> Constant<'tcx> {
} => this.as_constant(value),
ExprKind::Literal { literal, user_ty } => {
let user_ty = user_ty.map(|ty| {
this.canonical_user_type_annotations.push((span, ty))
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span,
user_ty: ty,
})
});
Constant {
span,
......
......@@ -6,7 +6,7 @@
use hair::*;
use rustc::mir::interpret::EvalErrorKind::BoundsCheck;
use rustc::mir::*;
use rustc::ty::Variance;
use rustc::ty::{CanonicalUserTypeAnnotation, Variance};
use rustc_data_structures::indexed_vec::Idx;
......@@ -134,7 +134,7 @@ fn expr_as_place(
let place = unpack!(block = this.as_place(block, source));
if let Some(user_ty) = user_ty {
let annotation_index = this.canonical_user_type_annotations.push(
(source_info.span, user_ty)
CanonicalUserTypeAnnotation { span: source_info.span, user_ty }
);
this.cfg.push(
block,
......@@ -157,7 +157,7 @@ fn expr_as_place(
);
if let Some(user_ty) = user_ty {
let annotation_index = this.canonical_user_type_annotations.push(
(source_info.span, user_ty)
CanonicalUserTypeAnnotation { span: source_info.span, user_ty }
);
this.cfg.push(
block,
......
......@@ -9,7 +9,7 @@
use rustc::middle::region;
use rustc::mir::interpret::EvalErrorKind;
use rustc::mir::*;
use rustc::ty::{self, Ty, UpvarSubsts};
use rustc::ty::{self, CanonicalUserTypeAnnotation, Ty, UpvarSubsts};
use syntax_pos::Span;
impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
......@@ -332,7 +332,10 @@ fn expr_as_rvalue(
};
let user_ty = user_ty.map(|ty| {
this.canonical_user_type_annotations.push((expr_span, ty))
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span: source_info.span,
user_ty: ty,
})
});
let adt = box AggregateKind::Adt(
adt_def,
......
......@@ -9,7 +9,7 @@
use build::{GuardFrame, GuardFrameLocal, LocalsForNode};
use hair::*;
use rustc::mir::*;
use rustc::ty::{self, Ty};
use rustc::ty::{self, CanonicalUserTypeAnnotation, Ty};
use rustc::ty::layout::VariantIdx;
use rustc_data_structures::bit_set::BitSet;
use rustc_data_structures::fx::FxHashMap;
......@@ -570,7 +570,10 @@ pub(super) fn visit_bindings(
//
// Note that the variance doesn't apply here, as we are tracking the effect
// of `user_ty` on any bindings contained with subpattern.
let annotation = (user_ty_span, user_ty.base);
let annotation = CanonicalUserTypeAnnotation {
span: user_ty_span,
user_ty: user_ty.base,
};
let projection = UserTypeProjection {
base: self.canonical_user_type_annotations.push(annotation),
projs: user_ty.projs.clone(),
......
......@@ -14,8 +14,8 @@
use rustc::mir::{fmt_const_val, Field, BorrowKind, Mutability};
use rustc::mir::{ProjectionElem, UserTypeProjection};
use rustc::mir::interpret::{Scalar, GlobalId, ConstValue, sign_extend};
use rustc::ty::{self, Region, TyCtxt, AdtDef, Ty, Lift};
use rustc::ty::{CanonicalUserType, CanonicalUserTypeAnnotations, UserType};
use rustc::ty::{self, Region, TyCtxt, AdtDef, Ty, Lift, UserType};
use rustc::ty::{CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations};
use rustc::ty::subst::{Substs, Kind};
use rustc::ty::layout::VariantIdx;
use rustc::hir::{self, PatKind, RangeEnd};
......@@ -78,7 +78,7 @@ pub(crate) fn user_ty(
span: Span,
) -> UserTypeProjection<'tcx> {
UserTypeProjection {
base: annotations.push((span, self.base)),
base: annotations.push(CanonicalUserTypeAnnotation{ span, user_ty: self.base }),
projs: self.projs
}
}
......
......@@ -632,8 +632,8 @@ fn write_user_type_annotations(mir: &Mir, w: &mut dyn Write) -> io::Result<()> {
if !mir.user_type_annotations.is_empty() {
writeln!(w, "| User Type Annotations")?;
}
for (index, (span, annotation)) in mir.user_type_annotations.iter_enumerated() {
writeln!(w, "| {:?}: {:?} at {:?}", index.index(), annotation, span)?;
for (index, annotation) in mir.user_type_annotations.iter_enumerated() {
writeln!(w, "| {:?}: {:?} at {:?}", index.index(), annotation.user_ty, annotation.span)?;
}
if !mir.user_type_annotations.is_empty() {
writeln!(w, "|")?;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册