mir-borrowck: Use Field instead of usize for field index in user description functions
This commit is contained in:
parent
e5d291afbf
commit
2285e35919
1 changed files with 18 additions and 19 deletions
|
@ -15,7 +15,7 @@ use rustc::hir::def_id::{DefId};
|
||||||
use rustc::infer::{InferCtxt};
|
use rustc::infer::{InferCtxt};
|
||||||
use rustc::ty::{self, TyCtxt, ParamEnv};
|
use rustc::ty::{self, TyCtxt, ParamEnv};
|
||||||
use rustc::ty::maps::Providers;
|
use rustc::ty::maps::Providers;
|
||||||
use rustc::mir::{AssertMessage, BasicBlock, BorrowKind, Location, Lvalue, Local};
|
use rustc::mir::{AssertMessage, BasicBlock, BorrowKind, Field, Location, Lvalue, Local};
|
||||||
use rustc::mir::{Mir, Mutability, Operand, Projection, ProjectionElem, Rvalue};
|
use rustc::mir::{Mir, Mutability, Operand, Projection, ProjectionElem, Rvalue};
|
||||||
use rustc::mir::{Statement, StatementKind, Terminator, TerminatorKind};
|
use rustc::mir::{Statement, StatementKind, Terminator, TerminatorKind};
|
||||||
use transform::nll;
|
use transform::nll;
|
||||||
|
@ -1611,7 +1611,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
|
||||||
let is_projection_from_ty_closure = proj.base.ty(self.mir, self.tcx)
|
let is_projection_from_ty_closure = proj.base.ty(self.mir, self.tcx)
|
||||||
.to_ty(self.tcx).is_closure();
|
.to_ty(self.tcx).is_closure();
|
||||||
|
|
||||||
let field_name = self.describe_field(&proj.base, field.index());
|
let field_name = self.describe_field(&proj.base, field);
|
||||||
if is_projection_from_ty_closure {
|
if is_projection_from_ty_closure {
|
||||||
buf.push_str(&format!("{}", field_name));
|
buf.push_str(&format!("{}", field_name));
|
||||||
} else {
|
} else {
|
||||||
|
@ -1650,58 +1650,57 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME Instead of passing usize, Field should be passed
|
// End-user visible description of the `field`nth field of `base`
|
||||||
// End-user visible description of the `field_index`nth field of `base`
|
fn describe_field(&self, base: &Lvalue, field: Field) -> String {
|
||||||
fn describe_field(&self, base: &Lvalue, field_index: usize) -> String {
|
|
||||||
match *base {
|
match *base {
|
||||||
Lvalue::Local(local) => {
|
Lvalue::Local(local) => {
|
||||||
let local = &self.mir.local_decls[local];
|
let local = &self.mir.local_decls[local];
|
||||||
self.describe_field_from_ty(&local.ty, field_index)
|
self.describe_field_from_ty(&local.ty, field)
|
||||||
},
|
},
|
||||||
Lvalue::Static(ref static_) => {
|
Lvalue::Static(ref static_) => {
|
||||||
self.describe_field_from_ty(&static_.ty, field_index)
|
self.describe_field_from_ty(&static_.ty, field)
|
||||||
},
|
},
|
||||||
Lvalue::Projection(ref proj) => {
|
Lvalue::Projection(ref proj) => {
|
||||||
match proj.elem {
|
match proj.elem {
|
||||||
ProjectionElem::Deref =>
|
ProjectionElem::Deref =>
|
||||||
self.describe_field(&proj.base, field_index),
|
self.describe_field(&proj.base, field),
|
||||||
ProjectionElem::Downcast(def, variant_index) =>
|
ProjectionElem::Downcast(def, variant_index) =>
|
||||||
format!("{}", def.variants[variant_index].fields[field_index].name),
|
format!("{}", def.variants[variant_index].fields[field.index()].name),
|
||||||
ProjectionElem::Field(_, field_type) =>
|
ProjectionElem::Field(_, field_type) =>
|
||||||
self.describe_field_from_ty(&field_type, field_index),
|
self.describe_field_from_ty(&field_type, field),
|
||||||
ProjectionElem::Index(..)
|
ProjectionElem::Index(..)
|
||||||
| ProjectionElem::ConstantIndex { .. }
|
| ProjectionElem::ConstantIndex { .. }
|
||||||
| ProjectionElem::Subslice { .. } =>
|
| ProjectionElem::Subslice { .. } =>
|
||||||
format!("{}", self.describe_field(&proj.base, field_index)),
|
format!("{}", self.describe_field(&proj.base, field)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// End-user visible description of the `field_index`nth field of `ty`
|
// End-user visible description of the `field_index`nth field of `ty`
|
||||||
fn describe_field_from_ty(&self, ty: &ty::Ty, field_index: usize) -> String {
|
fn describe_field_from_ty(&self, ty: &ty::Ty, field: Field) -> String {
|
||||||
if ty.is_box() {
|
if ty.is_box() {
|
||||||
// If the type is a box, the field is described from the boxed type
|
// If the type is a box, the field is described from the boxed type
|
||||||
self.describe_field_from_ty(&ty.boxed_ty(), field_index)
|
self.describe_field_from_ty(&ty.boxed_ty(), field)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
match ty.sty {
|
match ty.sty {
|
||||||
ty::TyAdt(def, _) => {
|
ty::TyAdt(def, _) => {
|
||||||
if def.is_enum() {
|
if def.is_enum() {
|
||||||
format!("{}", field_index)
|
format!("{}", field.index())
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
format!("{}", def.struct_variant().fields[field_index].name)
|
format!("{}", def.struct_variant().fields[field.index()].name)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ty::TyTuple(_, _) => {
|
ty::TyTuple(_, _) => {
|
||||||
format!("{}", field_index)
|
format!("{}", field.index())
|
||||||
},
|
},
|
||||||
ty::TyRef(_, tnm) | ty::TyRawPtr(tnm) => {
|
ty::TyRef(_, tnm) | ty::TyRawPtr(tnm) => {
|
||||||
self.describe_field_from_ty(&tnm.ty, field_index)
|
self.describe_field_from_ty(&tnm.ty, field)
|
||||||
},
|
},
|
||||||
ty::TyArray(ty, _) | ty::TySlice(ty) => {
|
ty::TyArray(ty, _) | ty::TySlice(ty) => {
|
||||||
self.describe_field_from_ty(&ty, field_index)
|
self.describe_field_from_ty(&ty, field)
|
||||||
},
|
},
|
||||||
ty::TyClosure(closure_def_id, _) => {
|
ty::TyClosure(closure_def_id, _) => {
|
||||||
// Convert the def-id into a node-id. node-ids are only valid for
|
// Convert the def-id into a node-id. node-ids are only valid for
|
||||||
|
@ -1709,7 +1708,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
|
||||||
// the closure comes from another crate. But in that case we wouldn't
|
// the closure comes from another crate. But in that case we wouldn't
|
||||||
// be borrowck'ing it, so we can just unwrap:
|
// be borrowck'ing it, so we can just unwrap:
|
||||||
let node_id = self.tcx.hir.as_local_node_id(closure_def_id).unwrap();
|
let node_id = self.tcx.hir.as_local_node_id(closure_def_id).unwrap();
|
||||||
let freevar = self.tcx.with_freevars(node_id, |fv| fv[field_index]);
|
let freevar = self.tcx.with_freevars(node_id, |fv| fv[field.index()]);
|
||||||
|
|
||||||
self.tcx.hir.name(freevar.var_id()).to_string()
|
self.tcx.hir.name(freevar.var_id()).to_string()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue