Skip to content

Commit aa811d7

Browse files
committed
rustc: remove source field path from Layout::StructWrappedNullablePointer.
1 parent bc8e1f7 commit aa811d7

File tree

2 files changed

+55
-67
lines changed

2 files changed

+55
-67
lines changed

src/librustc/ty/layout.rs

Lines changed: 21 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -619,10 +619,6 @@ impl Primitive {
619619
}
620620
}
621621

622-
/// Path through fields of nested structures.
623-
// FIXME(eddyb) use small vector optimization for the common case.
624-
pub type FieldPath = Vec<u32>;
625-
626622
/// A structure, a product type in ADT terms.
627623
#[derive(PartialEq, Eq, Hash, Debug)]
628624
pub struct Struct {
@@ -848,20 +844,19 @@ impl<'a, 'tcx> Struct {
848844
fn non_zero_field_in_type(tcx: TyCtxt<'a, 'tcx, 'tcx>,
849845
param_env: ty::ParamEnv<'tcx>,
850846
ty: Ty<'tcx>)
851-
-> Result<Option<(Size, Primitive, FieldPath)>, LayoutError<'tcx>> {
847+
-> Result<Option<(Size, Primitive)>, LayoutError<'tcx>> {
852848
let layout = ty.layout(tcx, param_env)?;
853849
match (layout, &ty.sty) {
854850
(&Scalar { non_zero: true, value, .. }, _) => {
855-
Ok(Some((Size::from_bytes(0), value, vec![])))
851+
Ok(Some((Size::from_bytes(0), value)))
856852
}
857853
(&CEnum { non_zero: true, discr, .. }, _) => {
858-
Ok(Some((Size::from_bytes(0), Int(discr), vec![])))
854+
Ok(Some((Size::from_bytes(0), Int(discr))))
859855
}
860856

861857
(&FatPointer { non_zero: true, .. }, _) => {
862858
Ok(Some((layout.field_offset(tcx, FAT_PTR_ADDR, None),
863-
Pointer,
864-
vec![FAT_PTR_ADDR as u32])))
859+
Pointer)))
865860
}
866861

867862
// Is this the NonZero lang item wrapping a pointer or integer type?
@@ -873,15 +868,12 @@ impl<'a, 'tcx> Struct {
873868
// FIXME(eddyb) also allow floating-point types here.
874869
Scalar { value: value @ Int(_), non_zero: false } |
875870
Scalar { value: value @ Pointer, non_zero: false } => {
876-
Ok(Some((layout.field_offset(tcx, 0, None),
877-
value,
878-
vec![0])))
871+
Ok(Some((layout.field_offset(tcx, 0, None), value)))
879872
}
880873
FatPointer { non_zero: false, .. } => {
881874
Ok(Some((layout.field_offset(tcx, 0, None) +
882875
field.field_offset(tcx, FAT_PTR_ADDR, None),
883-
Pointer,
884-
vec![FAT_PTR_ADDR as u32, 0])))
876+
Pointer)))
885877
}
886878
_ => Ok(None)
887879
}
@@ -890,31 +882,22 @@ impl<'a, 'tcx> Struct {
890882
// Perhaps one of the fields of this struct is non-zero
891883
// let's recurse and find out
892884
(&Univariant { ref variant, .. }, &ty::TyAdt(def, substs)) if def.is_struct() => {
893-
Struct::non_zero_field(
885+
variant.non_zero_field(
894886
tcx,
895887
param_env,
896888
def.struct_variant().fields.iter().map(|field| {
897889
field.ty(tcx, substs)
898-
}),
899-
&variant.offsets)
890+
}))
900891
}
901892

902893
// Perhaps one of the upvars of this closure is non-zero
903894
(&Univariant { ref variant, .. }, &ty::TyClosure(def, substs)) => {
904895
let upvar_tys = substs.upvar_tys(def, tcx);
905-
Struct::non_zero_field(
906-
tcx,
907-
param_env,
908-
upvar_tys,
909-
&variant.offsets)
896+
variant.non_zero_field(tcx, param_env, upvar_tys)
910897
}
911898
// Can we use one of the fields in this tuple?
912899
(&Univariant { ref variant, .. }, &ty::TyTuple(tys, _)) => {
913-
Struct::non_zero_field(
914-
tcx,
915-
param_env,
916-
tys.iter().cloned(),
917-
&variant.offsets)
900+
variant.non_zero_field(tcx, param_env, tys.iter().cloned())
918901
}
919902

920903
// Is this a fixed-size array of something non-zero
@@ -927,11 +910,7 @@ impl<'a, 'tcx> Struct {
927910
}
928911
}
929912
if count.val.to_const_int().unwrap().to_u64().unwrap() != 0 {
930-
Struct::non_zero_field(
931-
tcx,
932-
param_env,
933-
Some(ety).into_iter(),
934-
&[Size::from_bytes(0)])
913+
Struct::non_zero_field_in_type(tcx, param_env, ety)
935914
} else {
936915
Ok(None)
937916
}
@@ -953,17 +932,15 @@ impl<'a, 'tcx> Struct {
953932
/// Find the offset of a non-zero leaf field, starting from
954933
/// the given set of fields and recursing through aggregates.
955934
/// Returns Some((offset, primitive, source_path)) on success.
956-
fn non_zero_field<I>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
935+
fn non_zero_field<I>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
957936
param_env: ty::ParamEnv<'tcx>,
958-
fields: I,
959-
offsets: &[Size])
960-
-> Result<Option<(Size, Primitive, FieldPath)>, LayoutError<'tcx>>
937+
fields: I)
938+
-> Result<Option<(Size, Primitive)>, LayoutError<'tcx>>
961939
where I: Iterator<Item=Ty<'tcx>> {
962-
for (i, ty) in fields.enumerate() {
940+
for (ty, &field_offset) in fields.zip(&self.offsets) {
963941
let r = Struct::non_zero_field_in_type(tcx, param_env, ty)?;
964-
if let Some((offset, primitive, mut source_path)) = r {
965-
source_path.push(i as u32);
966-
return Ok(Some((offsets[i] + offset, primitive, source_path)));
942+
if let Some((offset, primitive)) = r {
943+
return Ok(Some((field_offset + offset, primitive)));
967944
}
968945
}
969946
Ok(None)
@@ -1152,8 +1129,6 @@ pub enum Layout {
11521129
nonnull: Struct,
11531130
discr: Primitive,
11541131
discr_offset: Size,
1155-
/// Like discr_offset, but the source field path. For debuginfo.
1156-
discrfield_source: FieldPath
11571132
}
11581133
}
11591134

@@ -1452,11 +1427,9 @@ impl<'a, 'tcx> Layout {
14521427
.collect::<Result<Vec<_>, _>>()?,
14531428
&def.repr, StructKind::AlwaysSizedUnivariant, ty)?;
14541429

1455-
let field = Struct::non_zero_field(tcx,
1456-
param_env,
1457-
variants[discr].iter().cloned(),
1458-
&st.offsets)?;
1459-
let (offset, primitive, mut path_source) = if let Some(f) = field { f }
1430+
let field = st.non_zero_field(tcx, param_env,
1431+
variants[discr].iter().cloned())?;
1432+
let (offset, primitive) = if let Some(f) = field { f }
14601433
else { continue };
14611434

14621435
// FIXME(eddyb) should take advantage of a newtype.
@@ -1468,15 +1441,11 @@ impl<'a, 'tcx> Layout {
14681441
});
14691442
}
14701443

1471-
// We have to fix the source path here.
1472-
path_source.reverse();
1473-
14741444
return success(StructWrappedNullablePointer {
14751445
nndiscr: discr as u64,
14761446
nonnull: st,
14771447
discr: primitive,
14781448
discr_offset: offset,
1479-
discrfield_source: path_source
14801449
});
14811450
}
14821451
}
@@ -1875,8 +1844,7 @@ impl<'a, 'tcx> Layout {
18751844
Layout::StructWrappedNullablePointer { nonnull: ref variant_layout,
18761845
nndiscr,
18771846
discr: _,
1878-
discr_offset: _,
1879-
discrfield_source: _ } => {
1847+
discr_offset: _ } => {
18801848
debug!("print-type-size t: `{:?}` adt struct-wrapped nullable nndiscr {} is {:?}",
18811849
ty, nndiscr, variant_layout);
18821850
let variant_def = &adt_def.variants[nndiscr as usize];
@@ -2418,13 +2386,11 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for Layout
24182386
ref nonnull,
24192387
ref discr,
24202388
discr_offset,
2421-
ref discrfield_source
24222389
} => {
24232390
nndiscr.hash_stable(hcx, hasher);
24242391
nonnull.hash_stable(hcx, hasher);
24252392
discr.hash_stable(hcx, hasher);
24262393
discr_offset.hash_stable(hcx, hasher);
2427-
discrfield_source.hash_stable(hcx, hasher);
24282394
}
24292395
}
24302396
}

src/librustc_trans/debuginfo/metadata.rs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use rustc::util::common::path2cstr;
3939

4040
use libc::{c_uint, c_longlong};
4141
use std::ffi::CString;
42+
use std::fmt::Write;
4243
use std::ptr;
4344
use std::path::Path;
4445
use syntax::ast;
@@ -1286,9 +1287,12 @@ impl<'tcx> EnumMemberDescriptionFactory<'tcx> {
12861287
}
12871288
]
12881289
},
1289-
layout::StructWrappedNullablePointer { nonnull: ref struct_def,
1290-
nndiscr,
1291-
ref discrfield_source, ..} => {
1290+
layout::StructWrappedNullablePointer {
1291+
nonnull: ref struct_def,
1292+
nndiscr,
1293+
discr,
1294+
discr_offset
1295+
} => {
12921296
// Create a description of the non-null variant
12931297
let (variant_type_metadata, member_description_factory) =
12941298
describe_enum_variant(cx,
@@ -1309,19 +1313,37 @@ impl<'tcx> EnumMemberDescriptionFactory<'tcx> {
13091313

13101314
// Encode the information about the null variant in the union
13111315
// member's name.
1312-
let null_variant_index = (1 - nndiscr) as usize;
1313-
let null_variant_name = adt.variants[null_variant_index].name;
1314-
let discrfield_source = discrfield_source.iter()
1315-
.map(|x| x.to_string())
1316-
.collect::<Vec<_>>().join("$");
1317-
let union_member_name = format!("RUST$ENCODED$ENUM${}${}",
1318-
discrfield_source,
1319-
null_variant_name);
1316+
let mut name = String::from("RUST$ENCODED$ENUM$");
1317+
// HACK(eddyb) the debuggers should just handle offset+size
1318+
// of discriminant instead of us having to recover its path.
1319+
fn compute_field_path<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
1320+
name: &mut String,
1321+
layout: TyLayout<'tcx>,
1322+
offset: Size,
1323+
size: Size) {
1324+
for i in 0..layout.field_count() {
1325+
let field_offset = layout.field_offset(ccx, i);
1326+
if field_offset > offset {
1327+
continue;
1328+
}
1329+
let inner_offset = offset - field_offset;
1330+
let field = layout.field(ccx, i);
1331+
if inner_offset + size <= field.size(ccx) {
1332+
write!(name, "{}$", i).unwrap();
1333+
compute_field_path(ccx, name, field, inner_offset, size);
1334+
}
1335+
}
1336+
}
1337+
compute_field_path(cx, &mut name,
1338+
self.type_rep,
1339+
discr_offset,
1340+
discr.size(cx));
1341+
name.push_str(&adt.variants[(1 - nndiscr) as usize].name.as_str());
13201342

13211343
// Create the (singleton) list of descriptions of union members.
13221344
vec![
13231345
MemberDescription {
1324-
name: union_member_name,
1346+
name,
13251347
type_metadata: variant_type_metadata,
13261348
offset: Size::from_bytes(0),
13271349
size: struct_def.stride(),

0 commit comments

Comments
 (0)