Introduce semi-duplicate DefIds into DefLocal/DefUpvar to remove use

of xxx_local.
This commit is contained in:
Niko Matsakis 2015-09-09 14:23:43 -04:00
parent 3b1399df2d
commit a0dc2d9a29
16 changed files with 51 additions and 45 deletions

View File

@ -461,7 +461,11 @@ fn tr(&self, dcx: &DecodeContext) -> def::Def {
def::DefStatic(did, m) => { def::DefStatic(did.tr(dcx), m) }
def::DefConst(did) => { def::DefConst(did.tr(dcx)) }
def::DefAssociatedConst(did) => def::DefAssociatedConst(did.tr(dcx)),
def::DefLocal(nid) => { def::DefLocal(dcx.tr_id(nid)) }
def::DefLocal(_, nid) => {
let nid = dcx.tr_id(nid);
let did = dcx.tcx.map.local_def_id(nid);
def::DefLocal(did, nid)
}
def::DefVariant(e_did, v_did, is_s) => {
def::DefVariant(e_did.tr(dcx), v_did.tr(dcx), is_s)
},
@ -472,8 +476,11 @@ fn tr(&self, dcx: &DecodeContext) -> def::Def {
def::DefPrimTy(p) => def::DefPrimTy(p),
def::DefTyParam(s, index, def_id, n) => def::DefTyParam(s, index, def_id.tr(dcx), n),
def::DefUse(did) => def::DefUse(did.tr(dcx)),
def::DefUpvar(nid1, index, nid2) => {
def::DefUpvar(dcx.tr_id(nid1), index, dcx.tr_id(nid2))
def::DefUpvar(_, nid1, index, nid2) => {
let nid1 = dcx.tr_id(nid1);
let nid2 = dcx.tr_id(nid2);
let did1 = dcx.tcx.map.local_def_id(nid1);
def::DefUpvar(did1, nid1, index, nid2)
}
def::DefStruct(did) => def::DefStruct(did.tr(dcx)),
def::DefLabel(nid) => def::DefLabel(dcx.tr_id(nid))

View File

@ -659,7 +659,7 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
doesn't point to a constant");
}
}
Some(def::DefLocal(_)) if v.mode == Mode::ConstFn => {
Some(def::DefLocal(..)) if v.mode == Mode::ConstFn => {
// Sadly, we can't determine whether the types are zero-sized.
v.add_qualif(ConstQualif::NOT_CONST | ConstQualif::NON_ZERO_SIZED);
}

View File

@ -245,7 +245,7 @@ fn check_for_bindings_named_the_same_as_variants(cx: &MatchCheckCtxt, pat: &Pat)
let pat_ty = cx.tcx.pat_ty(p);
if let ty::TyEnum(edef, _) = pat_ty.sty {
let def = cx.tcx.def_map.borrow().get(&p.id).map(|d| d.full_def());
if let Some(DefLocal(_)) = def {
if let Some(DefLocal(..)) = def {
if edef.variants.iter().any(|variant|
variant.name == ident.node.name
&& variant.kind() == VariantKind::Unit

View File

@ -29,7 +29,8 @@ pub enum Def {
DefStatic(DefId, bool /* is_mutbl */),
DefConst(DefId),
DefAssociatedConst(DefId),
DefLocal(ast::NodeId),
DefLocal(DefId, // def id of variable
ast::NodeId), // node id of variable
DefVariant(DefId /* enum */, DefId /* variant */, bool /* is_structure */),
DefTy(DefId, bool /* is_enum */),
DefAssociatedTy(DefId /* trait */, DefId),
@ -37,7 +38,8 @@ pub enum Def {
DefPrimTy(hir::PrimTy),
DefTyParam(ParamSpace, u32, DefId, ast::Name),
DefUse(DefId),
DefUpvar(ast::NodeId, // id of closed over local
DefUpvar(DefId, // def id of closed over local
ast::NodeId, // node id of closed over local
usize, // index in the freevars list of the closure
ast::NodeId), // expr node that creates the closure
@ -115,8 +117,8 @@ pub struct Export {
impl Def {
pub fn var_id(&self) -> ast::NodeId {
match *self {
DefLocal(id) |
DefUpvar(id, _, _) => {
DefLocal(_, id) |
DefUpvar(_, id, _, _) => {
id
}
@ -135,15 +137,11 @@ pub fn def_id(&self) -> DefId {
DefFn(id, _) | DefMod(id) | DefForeignMod(id) | DefStatic(id, _) |
DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(_, id) |
DefTyParam(_, _, id, _) | DefUse(id) | DefStruct(id) | DefTrait(id) |
DefMethod(id) | DefConst(id) | DefAssociatedConst(id) => {
DefMethod(id) | DefConst(id) | DefAssociatedConst(id) |
DefLocal(id, _) | DefUpvar(id, _, _, _) => {
id
}
DefLocal(id) |
DefUpvar(id, _, _) => {
DefId::xxx_local(id) // TODO, clearly
}
DefLabel(..) |
DefPrimTy(..) |
DefSelfTy(..) => {

View File

@ -280,11 +280,9 @@ pub fn new(delegate: &'d mut Delegate<'tcx>,
typer: &'t infer::InferCtxt<'a, 'tcx>)
-> ExprUseVisitor<'d,'t,'a,'tcx> where 'tcx:'a
{
ExprUseVisitor {
typer: typer,
mc: mc::MemCategorizationContext::new(typer),
delegate: delegate,
}
let mc: mc::MemCategorizationContext<'t, 'a, 'tcx> =
mc::MemCategorizationContext::new(typer);
ExprUseVisitor { typer: typer, mc: mc, delegate: delegate }
}
pub fn walk_fn(&mut self,

View File

@ -465,7 +465,7 @@ fn visit_expr(ir: &mut IrMaps, expr: &Expr) {
let mut call_caps = Vec::new();
ir.tcx.with_freevars(expr.id, |freevars| {
for fv in freevars {
if let DefLocal(rv) = fv.def {
if let DefLocal(_, rv) = fv.def {
let fv_ln = ir.add_live_node(FreeVarNode(fv.span));
call_caps.push(CaptureInfo {ln: fv_ln,
var_nid: rv});
@ -1268,7 +1268,7 @@ fn write_lvalue(&mut self, expr: &Expr, succ: LiveNode, acc: u32)
fn access_path(&mut self, expr: &Expr, succ: LiveNode, acc: u32)
-> LiveNode {
match self.ir.tcx.def_map.borrow().get(&expr.id).unwrap().full_def() {
DefLocal(nid) => {
DefLocal(_, nid) => {
let ln = self.live_node(expr.id, expr.span);
if acc != 0 {
self.init_from_succ(ln, succ);
@ -1517,9 +1517,9 @@ fn check_ret(&self,
fn check_lvalue(&mut self, expr: &Expr) {
match expr.node {
hir::ExprPath(..) => {
if let DefLocal(nid) = self.ir.tcx.def_map.borrow().get(&expr.id)
.unwrap()
.full_def() {
if let DefLocal(_, nid) = self.ir.tcx.def_map.borrow().get(&expr.id)
.unwrap()
.full_def() {
// Assignment to an immutable variable or argument: only legal
// if there is no later assignment. If this local is actually
// mutable, then check for a reassignment to flag the mutability

View File

@ -575,7 +575,7 @@ pub fn cat_def(&self,
}))
}
def::DefUpvar(var_id, _, fn_node_id) => {
def::DefUpvar(_, var_id, _, fn_node_id) => {
let ty = try!(self.node_ty(fn_node_id));
match ty.sty {
ty::TyClosure(closure_id, _) => {
@ -600,7 +600,7 @@ pub fn cat_def(&self,
}
}
def::DefLocal(vid) => {
def::DefLocal(_, vid) => {
Ok(Rc::new(cmt_ {
id: id,
span: span,

View File

@ -274,7 +274,7 @@ fn check_lifetime_def(&mut self, cx: &LateContext, t: &hir::LifetimeDef) {
fn check_pat(&mut self, cx: &LateContext, p: &hir::Pat) {
if let &hir::PatIdent(_, ref path1, _) = &p.node {
let def = cx.tcx.def_map.borrow().get(&p.id).map(|d| d.full_def());
if let Some(def::DefLocal(_)) = def {
if let Some(def::DefLocal(..)) = def {
self.check_snake_case(cx, "variable", &path1.node.name.as_str(), Some(p.span));
}
}

View File

@ -248,7 +248,8 @@ fn check_pat(&mut self, cx: &LateContext, pat: &hir::Pat) {
return false;
}
let def = def_map.get(&fieldpat.node.pat.id).map(|d| d.full_def());
def == Some(def::DefLocal(fieldpat.node.pat.id))
let def_id = cx.tcx.map.local_def_id(fieldpat.node.pat.id);
def == Some(def::DefLocal(def_id, fieldpat.node.pat.id))
});
for fieldpat in field_pats {
if let hir::PatIdent(_, ident, None) = fieldpat.node.pat.node {

View File

@ -569,13 +569,13 @@ fn convert_var<'a,'tcx:'a>(cx: &mut Cx<'a,'tcx>,
let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
match def {
def::DefLocal(node_id) => {
def::DefLocal(_, node_id) => {
ExprKind::VarRef {
id: node_id,
}
}
def::DefUpvar(id_var, index, closure_expr_id) => {
def::DefUpvar(_, id_var, index, closure_expr_id) => {
debug!("convert_var(upvar({:?}, {:?}, {:?}))", id_var, index, closure_expr_id);
let var_ty = cx.tcx.node_id_to_type(id_var);

View File

@ -1982,7 +1982,7 @@ fn upvarify(&self,
self.session.span_bug(span,
&format!("unexpected {:?} in bindings", def))
}
DefLocal(node_id) => {
DefLocal(_, node_id) => {
for rib in ribs {
match rib.kind {
NormalRibKind => {
@ -1990,11 +1990,12 @@ fn upvarify(&self,
}
ClosureRibKind(function_id) => {
let prev_def = def;
let node_def_id = self.ast_map.local_def_id(node_id);
let mut seen = self.freevars_seen.borrow_mut();
let seen = seen.entry(function_id).or_insert_with(|| NodeMap());
if let Some(&index) = seen.get(&node_id) {
def = DefUpvar(node_id, index, function_id);
def = DefUpvar(node_def_id, node_id, index, function_id);
continue;
}
let mut freevars = self.freevars.borrow_mut();
@ -2003,7 +2004,7 @@ fn upvarify(&self,
let depth = vec.len();
vec.push(Freevar { def: prev_def, span: span });
def = DefUpvar(node_id, depth, function_id);
def = DefUpvar(node_def_id, node_id, depth, function_id);
seen.insert(node_id, depth);
}
ItemRibKind | MethodRibKind => {
@ -2817,7 +2818,8 @@ struct or enum variant",
debug!("(resolving pattern) binding `{}`",
renamed);
let def = DefLocal(pattern.id);
let def_id = self.ast_map.local_def_id(pattern.id);
let def = DefLocal(def_id, pattern.id);
// Record the definition so that later passes
// will be able to distinguish variants from

View File

@ -258,7 +258,7 @@ fn lookup_def_kind(&self, ref_id: NodeId, span: Span) -> Option<recorder::Row> {
def::DefStatic(_, _) |
def::DefConst(_) |
def::DefAssociatedConst(..) |
def::DefLocal(_) |
def::DefLocal(..) |
def::DefVariant(_, _, _) |
def::DefUpvar(..) => Some(recorder::VarRef),
@ -721,7 +721,7 @@ fn process_path(&mut self, id: NodeId, path: &ast::Path, ref_kind: Option<record
}
}
}
def::DefLocal(_) |
def::DefLocal(..) |
def::DefStatic(_,_) |
def::DefConst(..) |
def::DefAssociatedConst(..) |
@ -1170,7 +1170,7 @@ fn visit_arm(&mut self, arm: &ast::Arm) {
}
let def = def_map.get(&id).unwrap().full_def();
match def {
def::DefLocal(id) => {
def::DefLocal(_, id) => {
let value = if immut == ast::MutImmutable {
self.span.snippet(p.span).to_string()
} else {

View File

@ -1432,19 +1432,19 @@ pub fn trans_match<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
fn is_discr_reassigned(bcx: Block, discr: &hir::Expr, body: &hir::Expr) -> bool {
let (vid, field) = match discr.node {
hir::ExprPath(..) => match bcx.def(discr.id) {
def::DefLocal(vid) | def::DefUpvar(vid, _, _) => (vid, None),
def::DefLocal(_, vid) | def::DefUpvar(_, vid, _, _) => (vid, None),
_ => return false
},
hir::ExprField(ref base, field) => {
let vid = match bcx.tcx().def_map.borrow().get(&base.id).map(|d| d.full_def()) {
Some(def::DefLocal(vid)) | Some(def::DefUpvar(vid, _, _)) => vid,
Some(def::DefLocal(_, vid)) | Some(def::DefUpvar(_, vid, _, _)) => vid,
_ => return false
};
(vid, Some(mc::NamedField(field.node)))
},
hir::ExprTupField(ref base, field) => {
let vid = match bcx.tcx().def_map.borrow().get(&base.id).map(|d| d.full_def()) {
Some(def::DefLocal(vid)) | Some(def::DefUpvar(vid, _, _)) => vid,
Some(def::DefLocal(_, vid)) | Some(def::DefUpvar(_, vid, _, _)) => vid,
_ => return false
};
(vid, Some(mc::PositionalField(field.node)))

View File

@ -783,7 +783,7 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
hir::ExprPath(..) => {
let def = cx.tcx().def_map.borrow().get(&e.id).unwrap().full_def();
match def {
def::DefLocal(id) => {
def::DefLocal(_, id) => {
if let Some(val) = fn_args.and_then(|args| args.get(&id).cloned()) {
val
} else {

View File

@ -1358,7 +1358,7 @@ pub fn trans_local_var<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
let _icx = push_ctxt("trans_local_var");
match def {
def::DefUpvar(nid, _, _) => {
def::DefUpvar(_, nid, _, _) => {
// Can't move upvars, so this is never a ZeroMemLastUse.
let local_ty = node_id_type(bcx, nid);
let lval = Lvalue::new_with_hint("expr::trans_local_var (upvar)",
@ -1372,7 +1372,7 @@ pub fn trans_local_var<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
}
}
}
def::DefLocal(nid) => {
def::DefLocal(_, nid) => {
let datum = match bcx.fcx.lllocals.borrow().get(&nid) {
Some(&v) => v,
None => {

View File

@ -4300,7 +4300,7 @@ fn type_scheme_and_predicates_for_def<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
defn: def::Def)
-> (TypeScheme<'tcx>, GenericPredicates<'tcx>) {
match defn {
def::DefLocal(nid) | def::DefUpvar(nid, _, _) => {
def::DefLocal(_, nid) | def::DefUpvar(_, nid, _, _) => {
let typ = fcx.local_ty(sp, nid);
(ty::TypeScheme { generics: ty::Generics::empty(), ty: typ },
ty::GenericPredicates::empty())