Skip to content

Commit

Permalink
store the normalized types of field accesses
Browse files Browse the repository at this point in the history
Fixes #31504
  • Loading branch information
Ariel Ben-Yehuda authored and arielb1 committed Feb 19, 2016
1 parent 350b50d commit 3c6f410
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 56 deletions.
7 changes: 7 additions & 0 deletions src/librustc/middle/ty/context.rs
Expand Up @@ -131,6 +131,12 @@ pub struct Tables<'tcx> {
/// equivalents. This table is not used in trans (since regions
/// are erased there) and hence is not serialized to metadata.
pub liberated_fn_sigs: NodeMap<ty::FnSig<'tcx>>,

/// For each FRU expression, record the normalized types of the fields
/// of the struct - this is needed because it is non-trivial to
/// normalize while preserving regions. This table is used only in
/// MIR construction and hence is not serialized to metadata.
pub fru_field_types: NodeMap<Vec<Ty<'tcx>>>
}

impl<'tcx> Tables<'tcx> {
Expand All @@ -144,6 +150,7 @@ impl<'tcx> Tables<'tcx> {
closure_tys: DefIdMap(),
closure_kinds: DefIdMap(),
liberated_fn_sigs: NodeMap(),
fru_field_types: NodeMap()
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/librustc/mir/repr.rs
Expand Up @@ -502,7 +502,7 @@ pub struct Projection<'tcx, B, V> {
#[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
pub enum ProjectionElem<'tcx, V> {
Deref,
Field(Field),
Field(Field, Ty<'tcx>),
Index(V),

/// These indices are generated by slice patterns. Easiest to explain
Expand Down Expand Up @@ -553,8 +553,8 @@ impl Field {
}

impl<'tcx> Lvalue<'tcx> {
pub fn field(self, f: Field) -> Lvalue<'tcx> {
self.elem(ProjectionElem::Field(f))
pub fn field(self, f: Field, ty: Ty<'tcx>) -> Lvalue<'tcx> {
self.elem(ProjectionElem::Field(f, ty))
}

pub fn deref(self) -> Lvalue<'tcx> {
Expand Down Expand Up @@ -594,8 +594,8 @@ impl<'tcx> Debug for Lvalue<'tcx> {
write!(fmt, "({:?} as {})", data.base, adt_def.variants[index].name),
ProjectionElem::Deref =>
write!(fmt, "(*{:?})", data.base),
ProjectionElem::Field(field) =>
write!(fmt, "{:?}.{:?}", data.base, field.index()),
ProjectionElem::Field(field, ty) =>
write!(fmt, "({:?}.{:?}: {:?})", data.base, field.index(), ty),
ProjectionElem::Index(ref index) =>
write!(fmt, "{:?}[{:?}]", data.base, index),
ProjectionElem::ConstantIndex { offset, min_length, from_end: false } =>
Expand Down
18 changes: 1 addition & 17 deletions src/librustc/mir/tcx.rs
Expand Up @@ -73,23 +73,7 @@ impl<'tcx> LvalueTy<'tcx> {
tcx.sess.bug(&format!("cannot downcast non-enum type: `{:?}`", self))
}
},
ProjectionElem::Field(field) => {
let field_ty = match self {
LvalueTy::Ty { ty } => match ty.sty {
ty::TyStruct(adt_def, substs) =>
adt_def.struct_variant().fields[field.index()].ty(tcx, substs),
ty::TyTuple(ref tys) =>
tys[field.index()],
ty::TyClosure(_, ref closure_substs) =>
closure_substs.upvar_tys[field.index()],
_ =>
tcx.sess.bug(&format!("cannot get field of type: `{:?}`", ty)),
},
LvalueTy::Downcast { adt_def, substs, variant_index } =>
adt_def.variants[variant_index].fields[field.index()].ty(tcx, substs),
};
LvalueTy::Ty { ty: field_ty }
}
ProjectionElem::Field(_, fty) => LvalueTy::Ty { ty: fty }
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/build/expr/as_lvalue.rs
Expand Up @@ -41,7 +41,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
}
ExprKind::Field { lhs, name } => {
let lvalue = unpack!(block = this.as_lvalue(block, lhs));
let lvalue = lvalue.field(name);
let lvalue = lvalue.field(name, expr.ty);
block.and(lvalue)
}
ExprKind::Deref { arg } => {
Expand Down
31 changes: 14 additions & 17 deletions src/librustc_mir/build/expr/as_rvalue.rs
Expand Up @@ -139,33 +139,30 @@ impl<'a,'tcx> Builder<'a,'tcx> {
.collect();
block.and(Rvalue::Aggregate(AggregateKind::Closure(closure_id, substs), upvars))
}
ExprKind::Adt { adt_def, variant_index, substs, fields, base } => { // see (*) above
ExprKind::Adt {
adt_def, variant_index, substs, fields, base
} => { // see (*) above
// first process the set of fields that were provided
// (evaluating them in order given by user)
let fields_map: FnvHashMap<_, _> =
fields.into_iter()
.map(|f| (f.name, unpack!(block = this.as_operand(block, f.expr))))
.collect();

// if base expression is given, evaluate it now
let base = base.map(|base| unpack!(block = this.as_lvalue(block, base)));

// get list of all fields that we will need
let field_names = this.hir.all_fields(adt_def, variant_index);

// for the actual values we use, take either the
// expr the user specified or, if they didn't
// specify something for this field name, create a
// path relative to the base (which must have been
// supplied, or the IR is internally
// inconsistent).
let fields: Vec<_> =
let fields = if let Some(FruInfo { base, field_types }) = base {
let base = unpack!(block = this.as_lvalue(block, base));
field_names.into_iter()
.map(|n| match fields_map.get(&n) {
Some(v) => v.clone(),
None => Operand::Consume(base.clone().unwrap().field(n)),
})
.collect();
.zip(field_types.into_iter())
.map(|(n, ty)| match fields_map.get(&n) {
Some(v) => v.clone(),
None => Operand::Consume(base.clone().field(n, ty))
})
.collect()
} else {
field_names.iter().map(|n| fields_map[n].clone()).collect()
};

block.and(Rvalue::Aggregate(AggregateKind::Adt(adt_def, variant_index, substs),
fields))
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_mir/build/matches/test.rs
Expand Up @@ -404,7 +404,8 @@ impl<'a,'tcx> Builder<'a,'tcx> {
subpatterns.iter()
.map(|subpattern| {
// e.g., `(x as Variant).0`
let lvalue = downcast_lvalue.clone().field(subpattern.field);
let lvalue = downcast_lvalue.clone().field(subpattern.field,
subpattern.field_ty());
// e.g., `(x as Variant).0 @ P1`
MatchPair::new(lvalue, &subpattern.pattern)
});
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_mir/build/matches/util.rs
Expand Up @@ -21,7 +21,8 @@ impl<'a,'tcx> Builder<'a,'tcx> {
-> Vec<MatchPair<'pat, 'tcx>> {
subpatterns.iter()
.map(|fieldpat| {
let lvalue = lvalue.clone().field(fieldpat.field);
let lvalue = lvalue.clone().field(fieldpat.field,
fieldpat.field_ty());
MatchPair::new(lvalue, &fieldpat.pattern)
})
.collect()
Expand Down
23 changes: 19 additions & 4 deletions src/librustc_mir/hair/cx/expr.rs
Expand Up @@ -248,21 +248,31 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
variant_index: 0,
substs: substs,
fields: field_refs,
base: base.to_ref(),
base: base.as_ref().map(|base| {
FruInfo {
base: base.to_ref(),
field_types: cx.tcx.tables
.borrow()
.fru_field_types[&self.id]
.clone()
}
})
}
}
ty::TyEnum(adt, substs) => {
match cx.tcx.def_map.borrow()[&self.id].full_def() {
Def::Variant(enum_id, variant_id) => {
debug_assert!(adt.did == enum_id);
assert!(base.is_none());

let index = adt.variant_index_with_id(variant_id);
let field_refs = field_refs(&adt.variants[index], fields);
ExprKind::Adt {
adt_def: adt,
variant_index: index,
substs: substs,
fields: field_refs,
base: base.to_ref(),
base: None
}
}
ref def => {
Expand Down Expand Up @@ -810,11 +820,16 @@ fn convert_var<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
};
match upvar_capture {
ty::UpvarCapture::ByValue => field_kind,
ty::UpvarCapture::ByRef(_) => {
ty::UpvarCapture::ByRef(borrow) => {
ExprKind::Deref {
arg: Expr {
temp_lifetime: temp_lifetime,
ty: var_ty,
ty: cx.tcx.mk_ref(
cx.tcx.mk_region(borrow.region),
ty::TypeAndMut {
ty: var_ty,
mutbl: borrow.kind.to_mutbl_lossy()
}),
span: expr.span,
kind: field_kind,
}.to_ref()
Expand Down
17 changes: 17 additions & 0 deletions src/librustc_mir/hair/cx/pattern.rs
Expand Up @@ -314,3 +314,20 @@ impl<'patcx, 'cx, 'tcx> PatCx<'patcx, 'cx, 'tcx> {
}
}
}

impl<'tcx> FieldPattern<'tcx> {
pub fn field_ty(&self) -> Ty<'tcx> {
debug!("field_ty({:?},ty={:?})", self, self.pattern.ty);
let r = match *self.pattern.kind {
PatternKind::Binding { mode: BindingMode::ByRef(..), ..} => {
match self.pattern.ty.sty {
ty::TyRef(_, mt) => mt.ty,
_ => unreachable!()
}
}
_ => self.pattern.ty
};
debug!("field_ty -> {:?}", r);
r
}
}
8 changes: 7 additions & 1 deletion src/librustc_mir/hair/mod.rs
Expand Up @@ -229,7 +229,7 @@ pub enum ExprKind<'tcx> {
variant_index: usize,
substs: &'tcx Substs<'tcx>,
fields: Vec<FieldExprRef<'tcx>>,
base: Option<ExprRef<'tcx>>,
base: Option<FruInfo<'tcx>>
},
Closure {
closure_id: DefId,
Expand All @@ -256,6 +256,12 @@ pub struct FieldExprRef<'tcx> {
pub expr: ExprRef<'tcx>,
}

#[derive(Clone, Debug)]
pub struct FruInfo<'tcx> {
pub base: ExprRef<'tcx>,
pub field_types: Vec<Ty<'tcx>>
}

#[derive(Clone, Debug)]
pub struct Arm<'tcx> {
pub patterns: Vec<Pattern<'tcx>>,
Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/transform/type_check.rs
Expand Up @@ -47,6 +47,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'tcx> {

fn visit_lvalue(&mut self, lvalue: &Lvalue<'tcx>, context: visit::LvalueContext) {
self.super_lvalue(lvalue, context);
debug!("visiting lvalue {:?}", lvalue);
let lv_ty = self.mir.lvalue_ty(self.tcx(), lvalue).to_ty(self.tcx());
self.sanitize_type(lvalue, lv_ty);
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/trans/mir/lvalue.rs
Expand Up @@ -126,7 +126,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
}
})
}
mir::ProjectionElem::Field(ref field) => {
mir::ProjectionElem::Field(ref field, _) => {
let base_ty = tr_base.ty.to_ty(tcx);
let base_repr = adt::represent_type(ccx, base_ty);
let discr = match tr_base.ty {
Expand Down
23 changes: 17 additions & 6 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -3179,8 +3179,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
check_struct_fields_on_error(fcx, expr.id, fields, base_expr);
return;
}
let (adt, variant) = match fcx.def_struct_variant(def, path.span) {
Some((adt, variant)) => (adt, variant),
let variant = match fcx.def_struct_variant(def, path.span) {
Some((_, variant)) => variant,
None => {
span_err!(fcx.tcx().sess, path.span, E0071,
"`{}` does not name a structure",
Expand All @@ -3195,12 +3195,23 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,

check_expr_struct_fields(fcx, expr_ty, expr.span, variant, fields,
base_expr.is_none());

if let &Some(ref base_expr) = base_expr {
check_expr_has_type(fcx, base_expr, expr_ty);
if adt.adt_kind() == ty::AdtKind::Enum {
span_err!(tcx.sess, base_expr.span, E0436,
"functional record update syntax requires a struct");
match expr_ty.sty {
ty::TyStruct(adt, substs) => {
fcx.inh.tables.borrow_mut().fru_field_types.insert(
expr.id,
adt.struct_variant().fields.iter().map(|f| {
fcx.normalize_associated_types_in(
expr.span, &f.ty(tcx, substs)
)
}).collect()
);
}
_ => {
span_err!(tcx.sess, base_expr.span, E0436,
"functional record update syntax requires a struct");
}
}
}
}
Expand Down
17 changes: 15 additions & 2 deletions src/librustc_typeck/check/writeback.rs
Expand Up @@ -43,6 +43,7 @@ pub fn resolve_type_vars_in_expr(fcx: &FnCtxt, e: &hir::Expr) {
wbcx.visit_upvar_borrow_map();
wbcx.visit_closures();
wbcx.visit_liberated_fn_sigs();
wbcx.visit_fru_field_types();
}

pub fn resolve_type_vars_in_fn(fcx: &FnCtxt,
Expand All @@ -64,6 +65,7 @@ pub fn resolve_type_vars_in_fn(fcx: &FnCtxt,
wbcx.visit_upvar_borrow_map();
wbcx.visit_closures();
wbcx.visit_liberated_fn_sigs();
wbcx.visit_fru_field_types();
}

///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -371,6 +373,13 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
}
}

fn visit_fru_field_types(&self) {
for (&node_id, ftys) in self.fcx.inh.tables.borrow().fru_field_types.iter() {
let ftys = self.resolve(ftys, ResolvingFieldTypes(node_id));
self.tcx().tables.borrow_mut().fru_field_types.insert(node_id, ftys);
}
}

fn resolve<T:TypeFoldable<'tcx>>(&self, t: &T, reason: ResolveReason) -> T {
t.fold_with(&mut Resolver::new(self.fcx, reason))
}
Expand All @@ -387,6 +396,7 @@ enum ResolveReason {
ResolvingUpvar(ty::UpvarId),
ResolvingClosure(DefId),
ResolvingFnSig(ast::NodeId),
ResolvingFieldTypes(ast::NodeId)
}

impl ResolveReason {
Expand All @@ -401,6 +411,9 @@ impl ResolveReason {
ResolvingFnSig(id) => {
tcx.map.span(id)
}
ResolvingFieldTypes(id) => {
tcx.map.span(id)
}
ResolvingClosure(did) => {
if let Some(node_id) = tcx.map.as_local_node_id(did) {
tcx.expr_span(node_id)
Expand Down Expand Up @@ -478,14 +491,14 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> {
"cannot determine a type for this closure")
}

ResolvingFnSig(id) => {
ResolvingFnSig(id) | ResolvingFieldTypes(id) => {
// any failures here should also fail when
// resolving the patterns, closure types, or
// something else.
let span = self.reason.span(self.tcx);
self.tcx.sess.delay_span_bug(
span,
&format!("cannot resolve some aspect of fn sig for {:?}", id));
&format!("cannot resolve some aspect of data for {:?}", id));
}
}
}
Expand Down

0 comments on commit 3c6f410

Please sign in to comment.