Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize layout of TypeVariants #50395

Merged
merged 3 commits into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/librustc/ich/impls_mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,10 +483,10 @@ for mir::AggregateKind<'gcx> {
def_id.hash_stable(hcx, hasher);
substs.hash_stable(hcx, hasher);
}
mir::AggregateKind::Generator(def_id, ref substs, ref interior) => {
mir::AggregateKind::Generator(def_id, ref substs, movability) => {
def_id.hash_stable(hcx, hasher);
substs.hash_stable(hcx, hasher);
interior.hash_stable(hcx, hasher);
movability.hash_stable(hcx, hasher);
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/librustc/ich/impls_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,7 @@ for ::middle::const_val::ErrKind<'gcx> {
}

impl_stable_hash_for!(struct ty::ClosureSubsts<'tcx> { substs });

impl_stable_hash_for!(struct ty::GeneratorInterior<'tcx> { witness, movable });
impl_stable_hash_for!(struct ty::GeneratorSubsts<'tcx> { substs });

impl_stable_hash_for!(struct ty::GenericPredicates<'tcx> {
parent,
Expand Down Expand Up @@ -889,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) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not also change TyRawPtr and maybe getting rid of TypeAndMut?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's additional work which does not further reduce the size of TypeVariants. It probably would be a bit cleaner though.

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 All @@ -908,10 +908,10 @@ for ty::TypeVariants<'gcx>
def_id.hash_stable(hcx, hasher);
closure_substs.hash_stable(hcx, hasher);
}
TyGenerator(def_id, closure_substs, interior) => {
TyGenerator(def_id, generator_substs, movability) => {
def_id.hash_stable(hcx, hasher);
closure_substs.hash_stable(hcx, hasher);
interior.hash_stable(hcx, hasher);
generator_substs.hash_stable(hcx, hasher);
movability.hash_stable(hcx, hasher);
}
TyGeneratorWitness(types) => {
types.hash_stable(hcx, hasher)
Expand Down Expand Up @@ -1315,11 +1315,11 @@ for traits::VtableGeneratorData<'gcx, N> where N: HashStable<StableHashingContex
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
let traits::VtableGeneratorData {
closure_def_id,
generator_def_id,
substs,
ref nested,
} = *self;
closure_def_id.hash_stable(hcx, hasher);
generator_def_id.hash_stable(hcx, hasher);
substs.hash_stable(hcx, hasher);
nested.hash_stable(hcx, hasher);
}
Expand Down
22 changes: 12 additions & 10 deletions src/librustc/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
19 changes: 7 additions & 12 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ use hir::def_id::DefId;
use mir::visit::MirVisitable;
use mir::interpret::{Value, PrimVal, EvalErrorKind};
use ty::subst::{Subst, Substs};
use ty::{self, AdtDef, CanonicalTy, ClosureSubsts, Region, Ty, TyCtxt, GeneratorInterior};
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 @@ -1641,7 +1640,7 @@ pub enum AggregateKind<'tcx> {
Adt(&'tcx AdtDef, usize, &'tcx Substs<'tcx>, Option<usize>),

Closure(DefId, ClosureSubsts<'tcx>),
Generator(DefId, ClosureSubsts<'tcx>, GeneratorInterior<'tcx>),
Generator(DefId, GeneratorSubsts<'tcx>, hir::GeneratorMovability),
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
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 Expand Up @@ -2375,10 +2373,8 @@ impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
AggregateKind::Adt(def, v, substs.fold_with(folder), n),
AggregateKind::Closure(id, substs) =>
AggregateKind::Closure(id, substs.fold_with(folder)),
AggregateKind::Generator(id, substs, interior) =>
AggregateKind::Generator(id,
substs.fold_with(folder),
interior.fold_with(folder)),
AggregateKind::Generator(id, substs, movablity) =>
AggregateKind::Generator(id, substs.fold_with(folder), movablity),
};
Aggregate(kind, fields.fold_with(folder))
}
Expand All @@ -2405,8 +2401,7 @@ impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
AggregateKind::Tuple => false,
AggregateKind::Adt(_, _, substs, _) => substs.visit_with(visitor),
AggregateKind::Closure(_, substs) => substs.visit_with(visitor),
AggregateKind::Generator(_, substs, interior) => substs.visit_with(visitor) ||
interior.visit_with(visitor),
AggregateKind::Generator(_, substs, _) => substs.visit_with(visitor),
}) || fields.visit_with(visitor)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/mir/tcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ impl<'tcx> Rvalue<'tcx> {
tcx.type_of(def.did).subst(tcx, substs)
}
AggregateKind::Closure(did, substs) => {
tcx.mk_closure_from_closure_substs(did, substs)
tcx.mk_closure(did, substs)
}
AggregateKind::Generator(did, substs, interior) => {
tcx.mk_generator(did, substs, interior)
AggregateKind::Generator(did, substs, movability) => {
tcx.mk_generator(did, substs, movability)
}
}
}
Expand Down
19 changes: 9 additions & 10 deletions src/librustc/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use hir::def_id::DefId;
use ty::subst::Substs;
use ty::{CanonicalTy, ClosureSubsts, Region, Ty, GeneratorInterior};
use ty::{CanonicalTy, ClosureSubsts, GeneratorSubsts, Region, Ty};
use mir::*;
use syntax_pos::Span;

Expand Down Expand Up @@ -243,10 +243,10 @@ macro_rules! make_mir_visitor {
self.super_closure_substs(substs);
}

fn visit_generator_interior(&mut self,
interior: & $($mutability)* GeneratorInterior<'tcx>,
fn visit_generator_substs(&mut self,
substs: & $($mutability)* GeneratorSubsts<'tcx>,
_: Location) {
self.super_generator_interior(interior);
self.super_generator_substs(substs);
}

fn visit_local_decl(&mut self,
Expand Down Expand Up @@ -595,11 +595,10 @@ macro_rules! make_mir_visitor {
self.visit_closure_substs(closure_substs, location);
}
AggregateKind::Generator(ref $($mutability)* def_id,
ref $($mutability)* closure_substs,
ref $($mutability)* interior) => {
ref $($mutability)* generator_substs,
_movability) => {
self.visit_def_id(def_id, location);
self.visit_closure_substs(closure_substs, location);
self.visit_generator_interior(interior, location);
self.visit_generator_substs(generator_substs, location);
}
}

Expand Down Expand Up @@ -786,8 +785,8 @@ macro_rules! make_mir_visitor {
fn super_substs(&mut self, _substs: & $($mutability)* &'tcx Substs<'tcx>) {
}

fn super_generator_interior(&mut self,
_interior: & $($mutability)* GeneratorInterior<'tcx>) {
fn super_generator_substs(&mut self,
_substs: & $($mutability)* GeneratorSubsts<'tcx>) {
}

fn super_closure_substs(&mut self,
Expand Down
4 changes: 1 addition & 3 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
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/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,8 @@ pub struct VtableImplData<'tcx, N> {

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable)]
pub struct VtableGeneratorData<'tcx, N> {
pub closure_def_id: DefId,
pub substs: ty::ClosureSubsts<'tcx>,
pub generator_def_id: DefId,
pub substs: ty::GeneratorSubsts<'tcx>,
/// Nested obligations. This can be non-empty if the generator
/// signature contains associated types.
pub nested: Vec<N>
Expand Down Expand Up @@ -989,7 +989,7 @@ impl<'tcx, N> Vtable<'tcx, N> {
nested: p.nested.into_iter().map(f).collect(),
}),
VtableGenerator(c) => VtableGenerator(VtableGeneratorData {
closure_def_id: c.closure_def_id,
generator_def_id: c.generator_def_id,
substs: c.substs,
nested: c.nested.into_iter().map(f).collect(),
}),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@ fn confirm_generator_candidate<'cx, 'gcx, 'tcx>(
vtable: VtableGeneratorData<'tcx, PredicateObligation<'tcx>>)
-> Progress<'tcx>
{
let gen_sig = vtable.substs.generator_poly_sig(vtable.closure_def_id, selcx.tcx());
let gen_sig = vtable.substs.poly_sig(vtable.generator_def_id, selcx.tcx());
let Normalized {
value: gen_sig,
obligations
Expand Down
Loading