Skip to content

Commit c9d9c24

Browse files
committed
Insert fields from TypeAndMut into TyRef to allow layout optimization
1 parent 710b4ad commit c9d9c24

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+242
-241
lines changed

src/librustc/ich/impls_ty.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -888,9 +888,10 @@ for ty::TypeVariants<'gcx>
888888
TyRawPtr(pointee_ty) => {
889889
pointee_ty.hash_stable(hcx, hasher);
890890
}
891-
TyRef(region, pointee_ty) => {
891+
TyRef(region, pointee_ty, mutbl) => {
892892
region.hash_stable(hcx, hasher);
893893
pointee_ty.hash_stable(hcx, hasher);
894+
mutbl.hash_stable(hcx, hasher);
894895
}
895896
TyFnDef(def_id, substs) => {
896897
def_id.hash_stable(hcx, hasher);

src/librustc/infer/error_reporting/mod.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -665,21 +665,22 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
665665

666666
fn push_ty_ref<'tcx>(
667667
r: &ty::Region<'tcx>,
668-
tnm: &ty::TypeAndMut<'tcx>,
668+
ty: Ty<'tcx>,
669+
mutbl: hir::Mutability,
669670
s: &mut DiagnosticStyledString,
670671
) {
671672
let r = &format!("{}", r);
672673
s.push_highlighted(format!(
673674
"&{}{}{}",
674675
r,
675676
if r == "" { "" } else { " " },
676-
if tnm.mutbl == hir::MutMutable {
677+
if mutbl == hir::MutMutable {
677678
"mut "
678679
} else {
679680
""
680681
}
681682
));
682-
s.push_normal(format!("{}", tnm.ty));
683+
s.push_normal(format!("{}", ty));
683684
}
684685

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

805806
// When finding T != &T, highlight only the borrow
806-
(&ty::TyRef(r1, ref tnm1), _) if equals(&tnm1.ty, &t2) => {
807+
(&ty::TyRef(r1, ref_ty1, mutbl1), _) if equals(&ref_ty1, &t2) => {
807808
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
808-
push_ty_ref(&r1, tnm1, &mut values.0);
809+
push_ty_ref(&r1, ref_ty1, mutbl1, &mut values.0);
809810
values.1.push_normal(format!("{}", t2));
810811
values
811812
}
812-
(_, &ty::TyRef(r2, ref tnm2)) if equals(&t1, &tnm2.ty) => {
813+
(_, &ty::TyRef(r2, ref_ty2, mutbl2)) if equals(&t1, &ref_ty2) => {
813814
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
814815
values.0.push_normal(format!("{}", t1));
815-
push_ty_ref(&r2, tnm2, &mut values.1);
816+
push_ty_ref(&r2, ref_ty2, mutbl2, &mut values.1);
816817
values
817818
}
818819

819820
// When encountering &T != &mut T, highlight only the borrow
820-
(&ty::TyRef(r1, ref tnm1), &ty::TyRef(r2, ref tnm2)) if equals(&tnm1.ty, &tnm2.ty) => {
821+
(&ty::TyRef(r1, ref_ty1, mutbl1),
822+
&ty::TyRef(r2, ref_ty2, mutbl2)) if equals(&ref_ty1, &ref_ty2) => {
821823
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
822-
push_ty_ref(&r1, tnm1, &mut values.0);
823-
push_ty_ref(&r2, tnm2, &mut values.1);
824+
push_ty_ref(&r1, ref_ty1, mutbl1, &mut values.0);
825+
push_ty_ref(&r2, ref_ty2, mutbl2, &mut values.1);
824826
values
825827
}
826828

src/librustc/middle/expr_use_visitor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
456456
// make sure that the thing we are pointing out stays valid
457457
// for the lifetime `scope_r` of the resulting ptr:
458458
let expr_ty = return_if_err!(self.mc.expr_ty(expr));
459-
if let ty::TyRef(r, _) = expr_ty.sty {
459+
if let ty::TyRef(r, _, _) = expr_ty.sty {
460460
let bk = ty::BorrowKind::from_mutbl(m);
461461
self.borrow_expr(&base, r, bk, AddrOf);
462462
}
@@ -859,7 +859,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
859859
// It is also a borrow or copy/move of the value being matched.
860860
match bm {
861861
ty::BindByReference(m) => {
862-
if let ty::TyRef(r, _) = pat_ty.sty {
862+
if let ty::TyRef(r, _, _) = pat_ty.sty {
863863
let bk = ty::BorrowKind::from_mutbl(m);
864864
delegate.borrow(pat.id, pat.span, &cmt_pat, r, bk, RefBinding);
865865
}

src/librustc/middle/mem_categorization.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
10121012
let base_ty = self.expr_ty_adjusted(base)?;
10131013

10141014
let (region, mutbl) = match base_ty.sty {
1015-
ty::TyRef(region, mt) => (region, mt.mutbl),
1015+
ty::TyRef(region, _, mutbl) => (region, mutbl),
10161016
_ => {
10171017
span_bug!(expr.span, "cat_overloaded_place: base is not a reference")
10181018
}
@@ -1046,8 +1046,8 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
10461046
let ptr = match base_cmt.ty.sty {
10471047
ty::TyAdt(def, ..) if def.is_box() => Unique,
10481048
ty::TyRawPtr(ref mt) => UnsafePtr(mt.mutbl),
1049-
ty::TyRef(r, mt) => {
1050-
let bk = ty::BorrowKind::from_mutbl(mt.mutbl);
1049+
ty::TyRef(r, _, mutbl) => {
1050+
let bk = ty::BorrowKind::from_mutbl(mutbl);
10511051
if implicit { Implicit(bk, r) } else { BorrowedPtr(bk, r) }
10521052
}
10531053
ref ty => bug!("unexpected type in cat_deref: {:?}", ty)

src/librustc/mir/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use mir::interpret::{Value, PrimVal, EvalErrorKind};
2929
use ty::subst::{Subst, Substs};
3030
use ty::{self, AdtDef, CanonicalTy, ClosureSubsts, GeneratorSubsts, Region, Ty, TyCtxt};
3131
use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
32-
use ty::TypeAndMut;
3332
use util::ppaux;
3433
use std::slice;
3534
use hir::{self, InlineAsm};
@@ -1905,9 +1904,8 @@ pub fn print_miri_value<W: Write>(value: Value, ty: Ty, f: &mut W) -> fmt::Resul
19051904
write!(f, "{:?}", ::std::char::from_u32(n as u32).unwrap()),
19061905
(Value::ByVal(PrimVal::Undef), &TyFnDef(did, _)) =>
19071906
write!(f, "{}", item_path_str(did)),
1908-
(Value::ByValPair(PrimVal::Ptr(ptr), PrimVal::Bytes(len)), &TyRef(_, TypeAndMut {
1909-
ty: &ty::TyS { sty: TyStr, .. }, ..
1910-
})) => {
1907+
(Value::ByValPair(PrimVal::Ptr(ptr), PrimVal::Bytes(len)),
1908+
&TyRef(_, &ty::TyS { sty: TyStr, .. }, _)) => {
19111909
ty::tls::with(|tcx| {
19121910
let alloc = tcx
19131911
.interpret_interner

src/librustc/traits/error_reporting.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -878,9 +878,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
878878
let mut trait_type = trait_ref.self_ty();
879879

880880
for refs_remaining in 0..refs_number {
881-
if let ty::TypeVariants::TyRef(_, ty::TypeAndMut{ ty: t_type, mutbl: _ }) =
882-
trait_type.sty {
883-
881+
if let ty::TypeVariants::TyRef(_, t_type, _) = trait_type.sty {
884882
trait_type = t_type;
885883

886884
let substs = self.tcx.mk_substs_trait(trait_type, &[]);

src/librustc/traits/select.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2167,14 +2167,14 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
21672167

21682168
ty::TyUint(_) | ty::TyInt(_) | ty::TyBool | ty::TyFloat(_) |
21692169
ty::TyChar | ty::TyRawPtr(..) | ty::TyNever |
2170-
ty::TyRef(_, ty::TypeAndMut { ty: _, mutbl: hir::MutImmutable }) => {
2170+
ty::TyRef(_, _, hir::MutImmutable) => {
21712171
// Implementations provided in libcore
21722172
None
21732173
}
21742174

21752175
ty::TyDynamic(..) | ty::TyStr | ty::TySlice(..) |
21762176
ty::TyGenerator(..) | ty::TyGeneratorWitness(..) | ty::TyForeign(..) |
2177-
ty::TyRef(_, ty::TypeAndMut { ty: _, mutbl: hir::MutMutable }) => {
2177+
ty::TyRef(_, _, hir::MutMutable) => {
21782178
Never
21792179
}
21802180

@@ -2263,7 +2263,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
22632263
}
22642264

22652265
ty::TyRawPtr(ty::TypeAndMut { ty: element_ty, ..}) |
2266-
ty::TyRef(_, ty::TypeAndMut { ty: element_ty, ..}) => {
2266+
ty::TyRef(_, element_ty, _) => {
22672267
vec![element_ty]
22682268
},
22692269

src/librustc/ty/cast.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ pub enum CastTy<'tcx> {
3636
/// Function Pointers
3737
FnPtr,
3838
/// Raw pointers
39-
Ptr(&'tcx ty::TypeAndMut<'tcx>),
39+
Ptr(ty::TypeAndMut<'tcx>),
4040
/// References
41-
RPtr(&'tcx ty::TypeAndMut<'tcx>),
41+
RPtr(ty::TypeAndMut<'tcx>),
4242
}
4343

4444
/// Cast Kind. See RFC 401 (or librustc_typeck/check/cast.rs)
@@ -69,8 +69,8 @@ impl<'tcx> CastTy<'tcx> {
6969
ty::TyFloat(_) => Some(CastTy::Float),
7070
ty::TyAdt(d,_) if d.is_enum() && d.is_payloadfree() =>
7171
Some(CastTy::Int(IntTy::CEnum)),
72-
ty::TyRawPtr(ref mt) => Some(CastTy::Ptr(mt)),
73-
ty::TyRef(_, ref mt) => Some(CastTy::RPtr(mt)),
72+
ty::TyRawPtr(mt) => Some(CastTy::Ptr(mt)),
73+
ty::TyRef(_, ty, mutbl) => Some(CastTy::RPtr(ty::TypeAndMut { ty, mutbl })),
7474
ty::TyFnPtr(..) => Some(CastTy::FnPtr),
7575
_ => None,
7676
}

src/librustc/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2351,7 +2351,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
23512351
}
23522352

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

23572357
pub fn mk_mut_ref(self, r: Region<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {

src/librustc/ty/error.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -189,20 +189,17 @@ impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> {
189189
}
190190
ty::TySlice(_) => "slice".to_string(),
191191
ty::TyRawPtr(_) => "*-ptr".to_string(),
192-
ty::TyRef(region, tymut) => {
192+
ty::TyRef(region, ty, mutbl) => {
193+
let tymut = ty::TypeAndMut { ty, mutbl };
193194
let tymut_string = tymut.to_string();
194195
if tymut_string == "_" || //unknown type name,
195196
tymut_string.len() > 10 || //name longer than saying "reference",
196197
region.to_string() != "" //... or a complex type
197198
{
198-
match tymut {
199-
ty::TypeAndMut{mutbl, ..} => {
200-
format!("{}reference", match mutbl {
201-
hir::Mutability::MutMutable => "mutable ",
202-
_ => ""
203-
})
204-
}
205-
}
199+
format!("{}reference", match mutbl {
200+
hir::Mutability::MutMutable => "mutable ",
201+
_ => ""
202+
})
206203
} else {
207204
format!("&{}", tymut_string)
208205
}

src/librustc/ty/fast_reject.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ pub fn simplify_type<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
8080
ty::TyDynamic(ref trait_info, ..) => {
8181
trait_info.principal().map(|p| TraitSimplifiedType(p.def_id()))
8282
}
83-
ty::TyRef(_, mt) => {
83+
ty::TyRef(_, ty, _) => {
8484
// since we introduce auto-refs during method lookup, we
8585
// just treat &T and T as equivalent from the point of
8686
// view of possibly unifying
87-
simplify_type(tcx, mt.ty, can_simplify_params)
87+
simplify_type(tcx, ty, can_simplify_params)
8888
}
8989
ty::TyFnDef(def_id, _) |
9090
ty::TyClosure(def_id, _) => {

src/librustc/ty/flags.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ impl FlagComputation {
173173
self.add_ty(m.ty);
174174
}
175175

176-
&ty::TyRef(r, ref m) => {
176+
&ty::TyRef(r, ty, _) => {
177177
self.add_region(r);
178-
self.add_ty(m.ty);
178+
self.add_ty(ty);
179179
}
180180

181181
&ty::TyTuple(ref ts) => {

src/librustc/ty/inhabitedness/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
269269
_ => DefIdForest::empty()
270270
}
271271
}
272-
TyRef(_, ref tm) => {
273-
tm.ty.uninhabited_from(visited, tcx)
272+
TyRef(_, ty, _) => {
273+
ty.uninhabited_from(visited, tcx)
274274
}
275275

276276
_ => DefIdForest::empty(),

src/librustc/ty/item_path.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,9 @@ pub fn characteristic_def_id_of_type(ty: Ty) -> Option<DefId> {
360360
ty::TyArray(subty, _) |
361361
ty::TySlice(subty) => characteristic_def_id_of_type(subty),
362362

363-
ty::TyRawPtr(mt) |
364-
ty::TyRef(_, mt) => characteristic_def_id_of_type(mt.ty),
363+
ty::TyRawPtr(mt) => characteristic_def_id_of_type(mt.ty),
364+
365+
ty::TyRef(_, ty, _) => characteristic_def_id_of_type(ty),
365366

366367
ty::TyTuple(ref tys) => tys.iter()
367368
.filter_map(|ty| characteristic_def_id_of_type(ty))

src/librustc/ty/layout.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
501501
}
502502

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

12641264
match ty.sty {
1265-
ty::TyRef(_, ty::TypeAndMut { ty: pointee, .. }) |
1265+
ty::TyRef(_, pointee, _) |
12661266
ty::TyRawPtr(ty::TypeAndMut { ty: pointee, .. }) => {
12671267
let non_zero = !ty.is_unsafe_ptr();
12681268
let tail = tcx.struct_tail(pointee);
@@ -1560,7 +1560,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx>
15601560
}
15611561

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

src/librustc/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ impl<'tcx> TyS<'tcx> {
514514
TypeVariants::TyInfer(InferTy::FloatVar(_)) |
515515
TypeVariants::TyInfer(InferTy::FreshIntTy(_)) |
516516
TypeVariants::TyInfer(InferTy::FreshFloatTy(_)) => true,
517-
TypeVariants::TyRef(_, x) => x.ty.is_primitive_ty(),
517+
TypeVariants::TyRef(_, x, _) => x.is_primitive_ty(),
518518
_ => false,
519519
}
520520
}

src/librustc/ty/relate.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,12 @@ pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
454454
Ok(tcx.mk_ptr(mt))
455455
}
456456

457-
(&ty::TyRef(a_r, ref a_mt), &ty::TyRef(b_r, ref b_mt)) =>
457+
(&ty::TyRef(a_r, a_ty, a_mutbl), &ty::TyRef(b_r, b_ty, b_mutbl)) =>
458458
{
459459
let r = relation.relate_with_variance(ty::Contravariant, &a_r, &b_r)?;
460-
let mt = relation.relate(a_mt, b_mt)?;
460+
let a_mt = ty::TypeAndMut { ty: a_ty, mutbl: a_mutbl };
461+
let b_mt = ty::TypeAndMut { ty: b_ty, mutbl: b_mutbl };
462+
let mt = relation.relate(&a_mt, &b_mt)?;
461463
Ok(tcx.mk_ref(r, mt))
462464
}
463465

src/librustc/ty/structural_impls.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -864,8 +864,8 @@ impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
864864
ty::TyFnDef(def_id, substs.fold_with(folder))
865865
}
866866
ty::TyFnPtr(f) => ty::TyFnPtr(f.fold_with(folder)),
867-
ty::TyRef(ref r, tm) => {
868-
ty::TyRef(r.fold_with(folder), tm.fold_with(folder))
867+
ty::TyRef(ref r, ty, mutbl) => {
868+
ty::TyRef(r.fold_with(folder), ty.fold_with(folder), mutbl)
869869
}
870870
ty::TyGenerator(did, substs, movability) => {
871871
ty::TyGenerator(
@@ -904,7 +904,7 @@ impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
904904
ty::TyTuple(ts) => ts.visit_with(visitor),
905905
ty::TyFnDef(_, substs) => substs.visit_with(visitor),
906906
ty::TyFnPtr(ref f) => f.visit_with(visitor),
907-
ty::TyRef(r, ref tm) => r.visit_with(visitor) || tm.visit_with(visitor),
907+
ty::TyRef(r, ty, _) => r.visit_with(visitor) || ty.visit_with(visitor),
908908
ty::TyGenerator(_did, ref substs, _) => {
909909
substs.visit_with(visitor)
910910
}

src/librustc/ty/sty.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub enum TypeVariants<'tcx> {
121121

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

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

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

14421442
pub fn is_mutable_pointer(&self) -> bool {
14431443
match self.sty {
1444-
TyRawPtr(tnm) | TyRef(_, tnm) => if let hir::Mutability::MutMutable = tnm.mutbl {
1445-
true
1446-
} else {
1447-
false
1448-
},
1444+
TyRawPtr(TypeAndMut { mutbl: hir::Mutability::MutMutable, .. }) |
1445+
TyRef(_, _, hir::Mutability::MutMutable) => true,
14491446
_ => false
14501447
}
14511448
}
@@ -1598,7 +1595,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
15981595
mutbl: hir::MutImmutable,
15991596
})
16001597
},
1601-
TyRef(_, mt) => Some(mt),
1598+
TyRef(_, ty, mutbl) => Some(TypeAndMut { ty, mutbl }),
16021599
TyRawPtr(mt) if explicit => Some(mt),
16031600
_ => None,
16041601
}
@@ -1652,7 +1649,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
16521649
/// ignores late-bound regions binders.
16531650
pub fn regions(&self) -> Vec<ty::Region<'tcx>> {
16541651
match self.sty {
1655-
TyRef(region, _) => {
1652+
TyRef(region, _, _) => {
16561653
vec![region]
16571654
}
16581655
TyDynamic(ref obj, region) => {

0 commit comments

Comments
 (0)