Skip to content

Commit 001496c

Browse files
committed
Shrink LocalDecl by 16 bytes.
By boxing `user_ty`.
1 parent 27ae2f0 commit 001496c

File tree

6 files changed

+51
-41
lines changed

6 files changed

+51
-41
lines changed

src/librustc_middle/mir/mod.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ pub struct LocalDecl<'tcx> {
732732
/// borrow checker needs this information since it can affect
733733
/// region inference.
734734
// FIXME(matthewjasper) Don't store in this in `Body`
735-
pub user_ty: UserTypeProjections,
735+
pub user_ty: Option<Box<UserTypeProjections>>,
736736

737737
/// The *syntactic* (i.e., not visibility) source scope the local is defined
738738
/// in. If the local was defined in a let-statement, this
@@ -818,7 +818,7 @@ pub struct LocalDecl<'tcx> {
818818

819819
// `LocalDecl` is used a lot. Make sure it doesn't unintentionally get bigger.
820820
#[cfg(target_arch = "x86_64")]
821-
static_assert_size!(LocalDecl<'_>, 72);
821+
static_assert_size!(LocalDecl<'_>, 56);
822822

823823
/// Extra information about a some locals that's used for diagnostics. (Not
824824
/// used for non-StaticRef temporaries, the return place, or anonymous function
@@ -937,7 +937,7 @@ impl<'tcx> LocalDecl<'tcx> {
937937
internal: false,
938938
is_block_tail: None,
939939
ty,
940-
user_ty: UserTypeProjections::none(),
940+
user_ty: None,
941941
source_info,
942942
}
943943
}
@@ -2451,14 +2451,18 @@ impl Constant<'tcx> {
24512451
/// &'static str`.
24522452
#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
24532453
pub struct UserTypeProjections {
2454-
pub(crate) contents: Vec<(UserTypeProjection, Span)>,
2454+
pub contents: Vec<(UserTypeProjection, Span)>,
24552455
}
24562456

24572457
impl<'tcx> UserTypeProjections {
24582458
pub fn none() -> Self {
24592459
UserTypeProjections { contents: vec![] }
24602460
}
24612461

2462+
pub fn is_empty(&self) -> bool {
2463+
self.contents.is_empty()
2464+
}
2465+
24622466
pub fn from_projections(projs: impl Iterator<Item = (UserTypeProjection, Span)>) -> Self {
24632467
UserTypeProjections { contents: projs.collect() }
24642468
}

src/librustc_middle/mir/visit.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -715,8 +715,10 @@ macro_rules! make_mir_visitor {
715715
local,
716716
source_info: *source_info,
717717
});
718-
for (user_ty, _) in & $($mutability)? user_ty.contents {
719-
self.visit_user_type_projection(user_ty);
718+
if let Some(user_ty) = user_ty {
719+
for (user_ty, _) in & $($mutability)? user_ty.contents {
720+
self.visit_user_type_projection(user_ty);
721+
}
720722
}
721723
self.visit_source_info(source_info);
722724
}

src/librustc_mir/borrow_check/diagnostics/move_errors.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -482,10 +482,9 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
482482
let mut suggestions: Vec<(Span, &str, String)> = Vec::new();
483483
for local in binds_to {
484484
let bind_to = &self.body.local_decls[*local];
485-
if let Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
486-
pat_span,
487-
..
488-
})))) = bind_to.local_info
485+
if let Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
486+
VarBindingForm { pat_span, .. },
487+
)))) = bind_to.local_info
489488
{
490489
if let Ok(pat_snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(pat_span)
491490
{

src/librustc_mir/borrow_check/type_check/mod.rs

+30-27
Original file line numberDiff line numberDiff line change
@@ -405,35 +405,38 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
405405
self.super_local_decl(local, local_decl);
406406
self.sanitize_type(local_decl, local_decl.ty);
407407

408-
for (user_ty, span) in local_decl.user_ty.projections_and_spans() {
409-
let ty = if !local_decl.is_nonref_binding() {
410-
// If we have a binding of the form `let ref x: T = ..` then remove the outermost
411-
// reference so we can check the type annotation for the remaining type.
412-
if let ty::Ref(_, rty, _) = local_decl.ty.kind {
413-
rty
408+
if let Some(user_ty) = &local_decl.user_ty {
409+
for (user_ty, span) in user_ty.projections_and_spans() {
410+
let ty = if !local_decl.is_nonref_binding() {
411+
// If we have a binding of the form `let ref x: T = ..`
412+
// then remove the outermost reference so we can check the
413+
// type annotation for the remaining type.
414+
if let ty::Ref(_, rty, _) = local_decl.ty.kind {
415+
rty
416+
} else {
417+
bug!("{:?} with ref binding has wrong type {}", local, local_decl.ty);
418+
}
414419
} else {
415-
bug!("{:?} with ref binding has wrong type {}", local, local_decl.ty);
416-
}
417-
} else {
418-
local_decl.ty
419-
};
420+
local_decl.ty
421+
};
420422

421-
if let Err(terr) = self.cx.relate_type_and_user_type(
422-
ty,
423-
ty::Variance::Invariant,
424-
user_ty,
425-
Locations::All(*span),
426-
ConstraintCategory::TypeAnnotation,
427-
) {
428-
span_mirbug!(
429-
self,
430-
local,
431-
"bad user type on variable {:?}: {:?} != {:?} ({:?})",
432-
local,
433-
local_decl.ty,
434-
local_decl.user_ty,
435-
terr,
436-
);
423+
if let Err(terr) = self.cx.relate_type_and_user_type(
424+
ty,
425+
ty::Variance::Invariant,
426+
user_ty,
427+
Locations::All(*span),
428+
ConstraintCategory::TypeAnnotation,
429+
) {
430+
span_mirbug!(
431+
self,
432+
local,
433+
"bad user type on variable {:?}: {:?} != {:?} ({:?})",
434+
local,
435+
local_decl.ty,
436+
local_decl.user_ty,
437+
terr,
438+
);
439+
}
437440
}
438441
}
439442
}

src/librustc_mir/util/pretty.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,10 @@ fn write_scope_tree(
472472

473473
let mut indented_decl =
474474
format!("{0:1$}let {2}{3:?}: {4:?}", INDENT, indent, mut_str, local, local_decl.ty);
475-
for user_ty in local_decl.user_ty.projections() {
476-
write!(indented_decl, " as {:?}", user_ty).unwrap();
475+
if let Some(user_ty) = &local_decl.user_ty {
476+
for user_ty in user_ty.projections() {
477+
write!(indented_decl, " as {:?}", user_ty).unwrap();
478+
}
477479
}
478480
indented_decl.push_str(";");
479481

src/librustc_mir_build/build/matches/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1949,7 +1949,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
19491949
let local = LocalDecl::<'tcx> {
19501950
mutability,
19511951
ty: var_ty,
1952-
user_ty,
1952+
user_ty: if user_ty.is_empty() { None } else { Some(box user_ty) },
19531953
source_info,
19541954
internal: false,
19551955
is_block_tail: None,
@@ -1976,7 +1976,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
19761976
// immutable to avoid the unused mut lint.
19771977
mutability: Mutability::Not,
19781978
ty: tcx.mk_imm_ref(tcx.lifetimes.re_erased, var_ty),
1979-
user_ty: UserTypeProjections::none(),
1979+
user_ty: None,
19801980
source_info,
19811981
internal: false,
19821982
is_block_tail: None,

0 commit comments

Comments
 (0)