Skip to content

Commit 15c1da4

Browse files
committed
Convert from using named fields to always using indices
1 parent 1e30f3e commit 15c1da4

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

src/librustc/middle/ty/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,13 @@ impl<'tcx, 'container> VariantDefData<'tcx, 'container> {
17311731
self.fields.iter().find(|f| f.name == name)
17321732
}
17331733

1734+
#[inline]
1735+
pub fn index_of_field_named(&self,
1736+
name: ast::Name)
1737+
-> Option<usize> {
1738+
self.fields.iter().position(|f| f.name == name)
1739+
}
1740+
17341741
#[inline]
17351742
pub fn field_named(&self, name: ast::Name) -> &FieldDefData<'tcx, 'container> {
17361743
self.find_field_named(name).unwrap()

src/librustc_mir/build/expr/as_rvalue.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,19 @@ impl<'a,'tcx> Builder<'a,'tcx> {
149149
block.and(Rvalue::Aggregate(AggregateKind::Closure(closure_id, substs), upvars))
150150
}
151151
ExprKind::Adt { adt_def, variant_index, substs, fields, base } => { // see (*) above
152-
// first process the set of fields
152+
// first process the set of fields that were provided
153+
// (evaluating them in order given by user)
153154
let fields_map: FnvHashMap<_, _> =
154155
fields.into_iter()
155156
.map(|f| (f.name, unpack!(block = this.as_operand(block, f.expr))))
156157
.collect();
157158

158-
let field_names = this.hir.fields(adt_def, variant_index);
159-
159+
// if base expression is given, evaluate it now
160160
let base = base.map(|base| unpack!(block = this.as_lvalue(block, base)));
161161

162+
// get list of all fields that we will need
163+
let field_names = this.hir.all_fields(adt_def, variant_index);
164+
162165
// for the actual values we use, take either the
163166
// expr the user specified or, if they didn't
164167
// specify something for this field name, create a

src/librustc_mir/repr.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,19 @@ pub type LvalueProjection<'tcx> =
443443
pub type LvalueElem<'tcx> =
444444
ProjectionElem<'tcx,Operand<'tcx>>;
445445

446+
/// Index into the list of fields found in a `VariantDef`
446447
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
447-
pub enum Field {
448-
Named(Name),
449-
Indexed(usize),
448+
pub struct Field(u32);
449+
450+
impl Field {
451+
pub fn new(value: usize) -> Field {
452+
assert!(value < (u32::MAX) as usize);
453+
Field(value as u32)
454+
}
455+
456+
pub fn index(self) -> usize {
457+
self.0 as usize
458+
}
450459
}
451460

452461
impl<'tcx> Lvalue<'tcx> {
@@ -491,10 +500,8 @@ impl<'tcx> Debug for Lvalue<'tcx> {
491500
write!(fmt,"({:?} as {:?})", data.base, variant_index),
492501
ProjectionElem::Deref =>
493502
write!(fmt,"(*{:?})", data.base),
494-
ProjectionElem::Field(Field::Named(name)) =>
495-
write!(fmt,"{:?}.{:?}", data.base, name),
496-
ProjectionElem::Field(Field::Indexed(index)) =>
497-
write!(fmt,"{:?}.{:?}", data.base, index),
503+
ProjectionElem::Field(field) =>
504+
write!(fmt,"{:?}.{:?}", data.base, field.index()),
498505
ProjectionElem::Index(ref index) =>
499506
write!(fmt,"{:?}[{:?}]", data.base, index),
500507
ProjectionElem::ConstantIndex { offset, min_length, from_end: false } =>

0 commit comments

Comments
 (0)