Skip to content

Commit

Permalink
Insert fields from TypeAndMut into TyRef to allow layout optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed May 8, 2018
1 parent 710b4ad commit c9d9c24
Show file tree
Hide file tree
Showing 60 changed files with 242 additions and 241 deletions.
3 changes: 2 additions & 1 deletion src/librustc/ich/impls_ty.rs
Expand Up @@ -888,9 +888,10 @@ for ty::TypeVariants<'gcx>
TyRawPtr(pointee_ty) => {
pointee_ty.hash_stable(hcx, hasher);
}
TyRef(region, pointee_ty) => {
TyRef(region, pointee_ty, mutbl) => {
region.hash_stable(hcx, hasher);
pointee_ty.hash_stable(hcx, hasher);
mutbl.hash_stable(hcx, hasher);
}
TyFnDef(def_id, substs) => {
def_id.hash_stable(hcx, hasher);
Expand Down
22 changes: 12 additions & 10 deletions src/librustc/infer/error_reporting/mod.rs
Expand Up @@ -665,21 +665,22 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {

fn push_ty_ref<'tcx>(
r: &ty::Region<'tcx>,
tnm: &ty::TypeAndMut<'tcx>,
ty: Ty<'tcx>,
mutbl: hir::Mutability,
s: &mut DiagnosticStyledString,
) {
let r = &format!("{}", r);
s.push_highlighted(format!(
"&{}{}{}",
r,
if r == "" { "" } else { " " },
if tnm.mutbl == hir::MutMutable {
if mutbl == hir::MutMutable {
"mut "
} else {
""
}
));
s.push_normal(format!("{}", tnm.ty));
s.push_normal(format!("{}", ty));
}

match (&t1.sty, &t2.sty) {
Expand Down Expand Up @@ -803,24 +804,25 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
}

// When finding T != &T, highlight only the borrow
(&ty::TyRef(r1, ref tnm1), _) if equals(&tnm1.ty, &t2) => {
(&ty::TyRef(r1, ref_ty1, mutbl1), _) if equals(&ref_ty1, &t2) => {
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
push_ty_ref(&r1, tnm1, &mut values.0);
push_ty_ref(&r1, ref_ty1, mutbl1, &mut values.0);
values.1.push_normal(format!("{}", t2));
values
}
(_, &ty::TyRef(r2, ref tnm2)) if equals(&t1, &tnm2.ty) => {
(_, &ty::TyRef(r2, ref_ty2, mutbl2)) if equals(&t1, &ref_ty2) => {
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
values.0.push_normal(format!("{}", t1));
push_ty_ref(&r2, tnm2, &mut values.1);
push_ty_ref(&r2, ref_ty2, mutbl2, &mut values.1);
values
}

// When encountering &T != &mut T, highlight only the borrow
(&ty::TyRef(r1, ref tnm1), &ty::TyRef(r2, ref tnm2)) if equals(&tnm1.ty, &tnm2.ty) => {
(&ty::TyRef(r1, ref_ty1, mutbl1),
&ty::TyRef(r2, ref_ty2, mutbl2)) if equals(&ref_ty1, &ref_ty2) => {
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
push_ty_ref(&r1, tnm1, &mut values.0);
push_ty_ref(&r2, tnm2, &mut values.1);
push_ty_ref(&r1, ref_ty1, mutbl1, &mut values.0);
push_ty_ref(&r2, ref_ty2, mutbl2, &mut values.1);
values
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/expr_use_visitor.rs
Expand Up @@ -456,7 +456,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
// make sure that the thing we are pointing out stays valid
// for the lifetime `scope_r` of the resulting ptr:
let expr_ty = return_if_err!(self.mc.expr_ty(expr));
if let ty::TyRef(r, _) = expr_ty.sty {
if let ty::TyRef(r, _, _) = expr_ty.sty {
let bk = ty::BorrowKind::from_mutbl(m);
self.borrow_expr(&base, r, bk, AddrOf);
}
Expand Down Expand Up @@ -859,7 +859,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
// It is also a borrow or copy/move of the value being matched.
match bm {
ty::BindByReference(m) => {
if let ty::TyRef(r, _) = pat_ty.sty {
if let ty::TyRef(r, _, _) = pat_ty.sty {
let bk = ty::BorrowKind::from_mutbl(m);
delegate.borrow(pat.id, pat.span, &cmt_pat, r, bk, RefBinding);
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/mem_categorization.rs
Expand Up @@ -1012,7 +1012,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
let base_ty = self.expr_ty_adjusted(base)?;

let (region, mutbl) = match base_ty.sty {
ty::TyRef(region, mt) => (region, mt.mutbl),
ty::TyRef(region, _, mutbl) => (region, mutbl),
_ => {
span_bug!(expr.span, "cat_overloaded_place: base is not a reference")
}
Expand Down Expand Up @@ -1046,8 +1046,8 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
let ptr = match base_cmt.ty.sty {
ty::TyAdt(def, ..) if def.is_box() => Unique,
ty::TyRawPtr(ref mt) => UnsafePtr(mt.mutbl),
ty::TyRef(r, mt) => {
let bk = ty::BorrowKind::from_mutbl(mt.mutbl);
ty::TyRef(r, _, mutbl) => {
let bk = ty::BorrowKind::from_mutbl(mutbl);
if implicit { Implicit(bk, r) } else { BorrowedPtr(bk, r) }
}
ref ty => bug!("unexpected type in cat_deref: {:?}", ty)
Expand Down
6 changes: 2 additions & 4 deletions src/librustc/mir/mod.rs
Expand Up @@ -29,7 +29,6 @@ use mir::interpret::{Value, PrimVal, EvalErrorKind};
use ty::subst::{Subst, Substs};
use ty::{self, AdtDef, CanonicalTy, ClosureSubsts, GeneratorSubsts, Region, Ty, TyCtxt};
use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
use ty::TypeAndMut;
use util::ppaux;
use std::slice;
use hir::{self, InlineAsm};
Expand Down Expand Up @@ -1905,9 +1904,8 @@ pub fn print_miri_value<W: Write>(value: Value, ty: Ty, f: &mut W) -> fmt::Resul
write!(f, "{:?}", ::std::char::from_u32(n as u32).unwrap()),
(Value::ByVal(PrimVal::Undef), &TyFnDef(did, _)) =>
write!(f, "{}", item_path_str(did)),
(Value::ByValPair(PrimVal::Ptr(ptr), PrimVal::Bytes(len)), &TyRef(_, TypeAndMut {
ty: &ty::TyS { sty: TyStr, .. }, ..
})) => {
(Value::ByValPair(PrimVal::Ptr(ptr), PrimVal::Bytes(len)),
&TyRef(_, &ty::TyS { sty: TyStr, .. }, _)) => {
ty::tls::with(|tcx| {
let alloc = tcx
.interpret_interner
Expand Down
4 changes: 1 addition & 3 deletions src/librustc/traits/error_reporting.rs
Expand Up @@ -878,9 +878,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
let mut trait_type = trait_ref.self_ty();

for refs_remaining in 0..refs_number {
if let ty::TypeVariants::TyRef(_, ty::TypeAndMut{ ty: t_type, mutbl: _ }) =
trait_type.sty {

if let ty::TypeVariants::TyRef(_, t_type, _) = trait_type.sty {
trait_type = t_type;

let substs = self.tcx.mk_substs_trait(trait_type, &[]);
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/traits/select.rs
Expand Up @@ -2167,14 +2167,14 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {

ty::TyUint(_) | ty::TyInt(_) | ty::TyBool | ty::TyFloat(_) |
ty::TyChar | ty::TyRawPtr(..) | ty::TyNever |
ty::TyRef(_, ty::TypeAndMut { ty: _, mutbl: hir::MutImmutable }) => {
ty::TyRef(_, _, hir::MutImmutable) => {
// Implementations provided in libcore
None
}

ty::TyDynamic(..) | ty::TyStr | ty::TySlice(..) |
ty::TyGenerator(..) | ty::TyGeneratorWitness(..) | ty::TyForeign(..) |
ty::TyRef(_, ty::TypeAndMut { ty: _, mutbl: hir::MutMutable }) => {
ty::TyRef(_, _, hir::MutMutable) => {
Never
}

Expand Down Expand Up @@ -2263,7 +2263,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
}

ty::TyRawPtr(ty::TypeAndMut { ty: element_ty, ..}) |
ty::TyRef(_, ty::TypeAndMut { ty: element_ty, ..}) => {
ty::TyRef(_, element_ty, _) => {
vec![element_ty]
},

Expand Down
8 changes: 4 additions & 4 deletions src/librustc/ty/cast.rs
Expand Up @@ -36,9 +36,9 @@ pub enum CastTy<'tcx> {
/// Function Pointers
FnPtr,
/// Raw pointers
Ptr(&'tcx ty::TypeAndMut<'tcx>),
Ptr(ty::TypeAndMut<'tcx>),
/// References
RPtr(&'tcx ty::TypeAndMut<'tcx>),
RPtr(ty::TypeAndMut<'tcx>),
}

/// Cast Kind. See RFC 401 (or librustc_typeck/check/cast.rs)
Expand Down Expand Up @@ -69,8 +69,8 @@ impl<'tcx> CastTy<'tcx> {
ty::TyFloat(_) => Some(CastTy::Float),
ty::TyAdt(d,_) if d.is_enum() && d.is_payloadfree() =>
Some(CastTy::Int(IntTy::CEnum)),
ty::TyRawPtr(ref mt) => Some(CastTy::Ptr(mt)),
ty::TyRef(_, ref mt) => Some(CastTy::RPtr(mt)),
ty::TyRawPtr(mt) => Some(CastTy::Ptr(mt)),
ty::TyRef(_, ty, mutbl) => Some(CastTy::RPtr(ty::TypeAndMut { ty, mutbl })),
ty::TyFnPtr(..) => Some(CastTy::FnPtr),
_ => None,
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/context.rs
Expand Up @@ -2351,7 +2351,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}

pub fn mk_ref(self, r: Region<'tcx>, tm: TypeAndMut<'tcx>) -> Ty<'tcx> {
self.mk_ty(TyRef(r, tm))
self.mk_ty(TyRef(r, tm.ty, tm.mutbl))
}

pub fn mk_mut_ref(self, r: Region<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
Expand Down
15 changes: 6 additions & 9 deletions src/librustc/ty/error.rs
Expand Up @@ -189,20 +189,17 @@ impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> {
}
ty::TySlice(_) => "slice".to_string(),
ty::TyRawPtr(_) => "*-ptr".to_string(),
ty::TyRef(region, tymut) => {
ty::TyRef(region, ty, mutbl) => {
let tymut = ty::TypeAndMut { ty, mutbl };
let tymut_string = tymut.to_string();
if tymut_string == "_" || //unknown type name,
tymut_string.len() > 10 || //name longer than saying "reference",
region.to_string() != "" //... or a complex type
{
match tymut {
ty::TypeAndMut{mutbl, ..} => {
format!("{}reference", match mutbl {
hir::Mutability::MutMutable => "mutable ",
_ => ""
})
}
}
format!("{}reference", match mutbl {
hir::Mutability::MutMutable => "mutable ",
_ => ""
})
} else {
format!("&{}", tymut_string)
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/fast_reject.rs
Expand Up @@ -80,11 +80,11 @@ pub fn simplify_type<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
ty::TyDynamic(ref trait_info, ..) => {
trait_info.principal().map(|p| TraitSimplifiedType(p.def_id()))
}
ty::TyRef(_, mt) => {
ty::TyRef(_, ty, _) => {
// since we introduce auto-refs during method lookup, we
// just treat &T and T as equivalent from the point of
// view of possibly unifying
simplify_type(tcx, mt.ty, can_simplify_params)
simplify_type(tcx, ty, can_simplify_params)
}
ty::TyFnDef(def_id, _) |
ty::TyClosure(def_id, _) => {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/flags.rs
Expand Up @@ -173,9 +173,9 @@ impl FlagComputation {
self.add_ty(m.ty);
}

&ty::TyRef(r, ref m) => {
&ty::TyRef(r, ty, _) => {
self.add_region(r);
self.add_ty(m.ty);
self.add_ty(ty);
}

&ty::TyTuple(ref ts) => {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/inhabitedness/mod.rs
Expand Up @@ -269,8 +269,8 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
_ => DefIdForest::empty()
}
}
TyRef(_, ref tm) => {
tm.ty.uninhabited_from(visited, tcx)
TyRef(_, ty, _) => {
ty.uninhabited_from(visited, tcx)
}

_ => DefIdForest::empty(),
Expand Down
5 changes: 3 additions & 2 deletions src/librustc/ty/item_path.rs
Expand Up @@ -360,8 +360,9 @@ pub fn characteristic_def_id_of_type(ty: Ty) -> Option<DefId> {
ty::TyArray(subty, _) |
ty::TySlice(subty) => characteristic_def_id_of_type(subty),

ty::TyRawPtr(mt) |
ty::TyRef(_, mt) => characteristic_def_id_of_type(mt.ty),
ty::TyRawPtr(mt) => characteristic_def_id_of_type(mt.ty),

ty::TyRef(_, ty, _) => characteristic_def_id_of_type(ty),

ty::TyTuple(ref tys) => tys.iter()
.filter_map(|ty| characteristic_def_id_of_type(ty))
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/ty/layout.rs
Expand Up @@ -501,7 +501,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
}

// Potentially-fat pointers.
ty::TyRef(_, ty::TypeAndMut { ty: pointee, .. }) |
ty::TyRef(_, pointee, _) |
ty::TyRawPtr(ty::TypeAndMut { ty: pointee, .. }) => {
let mut data_ptr = scalar_unit(Pointer);
if !ty.is_unsafe_ptr() {
Expand Down Expand Up @@ -1262,7 +1262,7 @@ impl<'a, 'tcx> SizeSkeleton<'tcx> {
};

match ty.sty {
ty::TyRef(_, ty::TypeAndMut { ty: pointee, .. }) |
ty::TyRef(_, pointee, _) |
ty::TyRawPtr(ty::TypeAndMut { ty: pointee, .. }) => {
let non_zero = !ty.is_unsafe_ptr();
let tail = tcx.struct_tail(pointee);
Expand Down Expand Up @@ -1560,7 +1560,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx>
}

// Potentially-fat pointers.
ty::TyRef(_, ty::TypeAndMut { ty: pointee, .. }) |
ty::TyRef(_, pointee, _) |
ty::TyRawPtr(ty::TypeAndMut { ty: pointee, .. }) => {
assert!(i < 2);

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/mod.rs
Expand Up @@ -514,7 +514,7 @@ impl<'tcx> TyS<'tcx> {
TypeVariants::TyInfer(InferTy::FloatVar(_)) |
TypeVariants::TyInfer(InferTy::FreshIntTy(_)) |
TypeVariants::TyInfer(InferTy::FreshFloatTy(_)) => true,
TypeVariants::TyRef(_, x) => x.ty.is_primitive_ty(),
TypeVariants::TyRef(_, x, _) => x.is_primitive_ty(),
_ => false,
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/librustc/ty/relate.rs
Expand Up @@ -454,10 +454,12 @@ pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
Ok(tcx.mk_ptr(mt))
}

(&ty::TyRef(a_r, ref a_mt), &ty::TyRef(b_r, ref b_mt)) =>
(&ty::TyRef(a_r, a_ty, a_mutbl), &ty::TyRef(b_r, b_ty, b_mutbl)) =>
{
let r = relation.relate_with_variance(ty::Contravariant, &a_r, &b_r)?;
let mt = relation.relate(a_mt, b_mt)?;
let a_mt = ty::TypeAndMut { ty: a_ty, mutbl: a_mutbl };
let b_mt = ty::TypeAndMut { ty: b_ty, mutbl: b_mutbl };
let mt = relation.relate(&a_mt, &b_mt)?;
Ok(tcx.mk_ref(r, mt))
}

Expand Down
6 changes: 3 additions & 3 deletions src/librustc/ty/structural_impls.rs
Expand Up @@ -864,8 +864,8 @@ impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
ty::TyFnDef(def_id, substs.fold_with(folder))
}
ty::TyFnPtr(f) => ty::TyFnPtr(f.fold_with(folder)),
ty::TyRef(ref r, tm) => {
ty::TyRef(r.fold_with(folder), tm.fold_with(folder))
ty::TyRef(ref r, ty, mutbl) => {
ty::TyRef(r.fold_with(folder), ty.fold_with(folder), mutbl)
}
ty::TyGenerator(did, substs, movability) => {
ty::TyGenerator(
Expand Down Expand Up @@ -904,7 +904,7 @@ impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
ty::TyTuple(ts) => ts.visit_with(visitor),
ty::TyFnDef(_, substs) => substs.visit_with(visitor),
ty::TyFnPtr(ref f) => f.visit_with(visitor),
ty::TyRef(r, ref tm) => r.visit_with(visitor) || tm.visit_with(visitor),
ty::TyRef(r, ty, _) => r.visit_with(visitor) || ty.visit_with(visitor),
ty::TyGenerator(_did, ref substs, _) => {
substs.visit_with(visitor)
}
Expand Down
15 changes: 6 additions & 9 deletions src/librustc/ty/sty.rs
Expand Up @@ -121,7 +121,7 @@ pub enum TypeVariants<'tcx> {

/// A reference; a pointer with an associated lifetime. Written as
/// `&'a mut T` or `&'a T`.
TyRef(Region<'tcx>, TypeAndMut<'tcx>),
TyRef(Region<'tcx>, Ty<'tcx>, hir::Mutability),

/// The anonymous type of a function declaration/definition. Each
/// function has a unique type.
Expand Down Expand Up @@ -1392,7 +1392,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {

pub fn is_slice(&self) -> bool {
match self.sty {
TyRawPtr(mt) | TyRef(_, mt) => match mt.ty.sty {
TyRawPtr(TypeAndMut { ty, .. }) | TyRef(_, ty, _) => match ty.sty {
TySlice(_) | TyStr => true,
_ => false,
},
Expand Down Expand Up @@ -1441,11 +1441,8 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {

pub fn is_mutable_pointer(&self) -> bool {
match self.sty {
TyRawPtr(tnm) | TyRef(_, tnm) => if let hir::Mutability::MutMutable = tnm.mutbl {
true
} else {
false
},
TyRawPtr(TypeAndMut { mutbl: hir::Mutability::MutMutable, .. }) |
TyRef(_, _, hir::Mutability::MutMutable) => true,
_ => false
}
}
Expand Down Expand Up @@ -1598,7 +1595,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
mutbl: hir::MutImmutable,
})
},
TyRef(_, mt) => Some(mt),
TyRef(_, ty, mutbl) => Some(TypeAndMut { ty, mutbl }),
TyRawPtr(mt) if explicit => Some(mt),
_ => None,
}
Expand Down Expand Up @@ -1652,7 +1649,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
/// ignores late-bound regions binders.
pub fn regions(&self) -> Vec<ty::Region<'tcx>> {
match self.sty {
TyRef(region, _) => {
TyRef(region, _, _) => {
vec![region]
}
TyDynamic(ref obj, region) => {
Expand Down

0 comments on commit c9d9c24

Please sign in to comment.