1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 * A classic liveness analysis based on dataflow over the AST. Computes,
13 * for each local variable in a function, whether that variable is live
14 * at a given point. Program execution points are identified by their
19 * The basic model is that each local variable is assigned an index. We
20 * represent sets of local variables using a vector indexed by this
21 * index. The value in the vector is either 0, indicating the variable
22 * is dead, or the id of an expression that uses the variable.
24 * We conceptually walk over the AST in reverse execution order. If we
25 * find a use of a variable, we add it to the set of live variables. If
26 * we find an assignment to a variable, we remove it from the set of live
27 * variables. When we have to merge two flows, we take the union of
28 * those two flows---if the variable is live on both paths, we simply
29 * pick one id. In the event of loops, we continue doing this until a
30 * fixed point is reached.
32 * ## Checking initialization
34 * At the function entry point, all variables must be dead. If this is
35 * not the case, we can report an error using the id found in the set of
36 * live variables, which identifies a use of the variable which is not
37 * dominated by an assignment.
41 * After each explicit move, the variable must be dead.
43 * ## Computing last uses
45 * Any use of the variable where the variable is dead afterwards is a
48 * # Implementation details
50 * The actual implementation contains two (nested) walks over the AST.
51 * The outer walk has the job of building up the ir_maps instance for the
52 * enclosing function. On the way down the tree, it identifies those AST
53 * nodes and variable IDs that will be needed for the liveness analysis
54 * and assigns them contiguous IDs. The liveness id for an AST node is
55 * called a `live_node` (it's a newtype'd uint) and the id for a variable
56 * is called a `variable` (another newtype'd uint).
58 * On the way back up the tree, as we are about to exit from a function
59 * declaration we allocate a `liveness` instance. Now that we know
60 * precisely how many nodes and variables we need, we can allocate all
61 * the various arrays that we will need to precisely the right size. We then
62 * perform the actual propagation on the `liveness` instance.
64 * This propagation is encoded in the various `propagate_through_*()`
65 * methods. It effectively does a reverse walk of the AST; whenever we
66 * reach a loop node, we iterate until a fixed point is reached.
68 * ## The `Users` struct
70 * At each live node `N`, we track three pieces of information for each
71 * variable `V` (these are encapsulated in the `Users` struct):
73 * - `reader`: the `LiveNode` ID of some node which will read the value
74 * that `V` holds on entry to `N`. Formally: a node `M` such
75 * that there exists a path `P` from `N` to `M` where `P` does not
76 * write `V`. If the `reader` is `invalid_node()`, then the current
77 * value will never be read (the variable is dead, essentially).
79 * - `writer`: the `LiveNode` ID of some node which will write the
80 * variable `V` and which is reachable from `N`. Formally: a node `M`
81 * such that there exists a path `P` from `N` to `M` and `M` writes
82 * `V`. If the `writer` is `invalid_node()`, then there is no writer
83 * of `V` that follows `N`.
85 * - `used`: a boolean value indicating whether `V` is *used*. We
86 * distinguish a *read* from a *use* in that a *use* is some read that
87 * is not just used to generate a new value. For example, `x += 1` is
88 * a read but not a use. This is used to generate better warnings.
90 * ## Special Variables
92 * We generate various special variables for various, well, special purposes.
93 * These are described in the `specials` struct:
95 * - `exit_ln`: a live node that is generated to represent every 'exit' from
96 * the function, whether it be by explicit return, fail, or other means.
98 * - `fallthrough_ln`: a live node that represents a fallthrough
100 * - `no_ret_var`: a synthetic variable that is only 'read' from, the
101 * fallthrough node. This allows us to detect functions where we fail
102 * to return explicitly.
106 use middle::freevars;
107 use middle::lint::{UnusedVariable, DeadAssignment};
108 use middle::mem_categorization::Typer;
109 use middle::pat_util;
111 use util::nodemap::NodeMap;
113 use std::mem::transmute;
120 use syntax::codemap::{BytePos, original_sp, Span};
121 use syntax::parse::token::special_idents;
122 use syntax::parse::token;
123 use syntax::print::pprust::{expr_to_str, block_to_str};
124 use syntax::{visit, ast_util};
125 use syntax::visit::{Visitor, FnKind};
127 #[deriving(PartialEq)]
128 struct Variable(uint);
129 #[deriving(PartialEq)]
130 struct LiveNode(uint);
133 fn get(&self) -> uint { let Variable(v) = *self; v }
137 fn get(&self) -> uint { let LiveNode(v) = *self; v }
140 impl Clone for LiveNode {
141 fn clone(&self) -> LiveNode {
146 #[deriving(PartialEq)]
154 fn live_node_kind_to_str(lnk: LiveNodeKind, cx: &ty::ctxt) -> String {
155 let cm = cx.sess.codemap();
158 format!("Free var node [{}]", cm.span_to_str(s))
161 format!("Expr node [{}]", cm.span_to_str(s))
164 format!("Var def node [{}]", cm.span_to_str(s))
166 ExitNode => "Exit node".to_string(),
170 impl<'a> Visitor<()> for IrMaps<'a> {
171 fn visit_fn(&mut self, fk: &FnKind, fd: &FnDecl, b: &Block, s: Span, n: NodeId, _: ()) {
172 visit_fn(self, fk, fd, b, s, n);
174 fn visit_local(&mut self, l: &Local, _: ()) { visit_local(self, l); }
175 fn visit_expr(&mut self, ex: &Expr, _: ()) { visit_expr(self, ex); }
176 fn visit_arm(&mut self, a: &Arm, _: ()) { visit_arm(self, a); }
179 pub fn check_crate(tcx: &ty::ctxt,
181 visit::walk_crate(&mut IrMaps::new(tcx), krate, ());
182 tcx.sess.abort_if_errors();
185 impl fmt::Show for LiveNode {
186 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
187 write!(f, "ln({})", self.get())
191 impl fmt::Show for Variable {
192 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
193 write!(f, "v({})", self.get())
197 // ______________________________________________________________________
200 // This is the first pass and the one that drives the main
201 // computation. It walks up and down the IR once. On the way down,
202 // we count for each function the number of variables as well as
203 // liveness nodes. A liveness node is basically an expression or
204 // capture clause that does something of interest: either it has
205 // interesting control flow or it uses/defines a local variable.
207 // On the way back up, at each function node we create liveness sets
208 // (we now know precisely how big to make our various vectors and so
209 // forth) and then do the data-flow propagation to compute the set
210 // of live variables at each program point.
212 // Finally, we run back over the IR one last time and, using the
213 // computed liveness, check various safety conditions. For example,
214 // there must be no live nodes at the definition site for a variable
215 // unless it has an initializer. Similarly, each non-mutable local
216 // variable must not be assigned if there is some successor
217 // assignment. And so forth.
220 fn is_valid(&self) -> bool {
221 self.get() != uint::MAX
225 fn invalid_node() -> LiveNode { LiveNode(uint::MAX) }
234 FromMatch(BindingMode),
235 FromLetWithInitializer,
255 num_live_nodes: uint,
257 live_node_map: NodeMap<LiveNode>,
258 variable_map: NodeMap<Variable>,
259 capture_info_map: NodeMap<Rc<Vec<CaptureInfo>>>,
260 var_kinds: Vec<VarKind>,
261 lnks: Vec<LiveNodeKind>,
264 impl<'a> IrMaps<'a> {
265 fn new(tcx: &'a ty::ctxt) -> IrMaps<'a> {
270 live_node_map: NodeMap::new(),
271 variable_map: NodeMap::new(),
272 capture_info_map: NodeMap::new(),
273 var_kinds: Vec::new(),
278 fn add_live_node(&mut self, lnk: LiveNodeKind) -> LiveNode {
279 let ln = LiveNode(self.num_live_nodes);
281 self.num_live_nodes += 1;
283 debug!("{} is of kind {}", ln.to_str(),
284 live_node_kind_to_str(lnk, self.tcx));
289 fn add_live_node_for_node(&mut self, node_id: NodeId, lnk: LiveNodeKind) {
290 let ln = self.add_live_node(lnk);
291 self.live_node_map.insert(node_id, ln);
293 debug!("{} is node {}", ln.to_str(), node_id);
296 fn add_variable(&mut self, vk: VarKind) -> Variable {
297 let v = Variable(self.num_vars);
298 self.var_kinds.push(vk);
302 Local(LocalInfo { id: node_id, .. }) | Arg(node_id, _) => {
303 self.variable_map.insert(node_id, v);
308 debug!("{} is {:?}", v.to_str(), vk);
313 fn variable(&self, node_id: NodeId, span: Span) -> Variable {
314 match self.variable_map.find(&node_id) {
319 .span_bug(span, format!("no variable registered for id {}",
320 node_id).as_slice());
325 fn variable_name(&self, var: Variable) -> String {
326 match self.var_kinds.get(var.get()) {
327 &Local(LocalInfo { ident: nm, .. }) | &Arg(_, nm) => {
328 token::get_ident(nm).get().to_str()
330 &ImplicitRet => "<implicit-ret>".to_string()
334 fn set_captures(&mut self, node_id: NodeId, cs: Vec<CaptureInfo>) {
335 self.capture_info_map.insert(node_id, Rc::new(cs));
338 fn lnk(&self, ln: LiveNode) -> LiveNodeKind {
339 *self.lnks.get(ln.get())
343 impl<'a> Visitor<()> for Liveness<'a> {
344 fn visit_fn(&mut self, fk: &FnKind, fd: &FnDecl, b: &Block, s: Span, n: NodeId, _: ()) {
345 check_fn(self, fk, fd, b, s, n);
347 fn visit_local(&mut self, l: &Local, _: ()) {
348 check_local(self, l);
350 fn visit_expr(&mut self, ex: &Expr, _: ()) {
351 check_expr(self, ex);
353 fn visit_arm(&mut self, a: &Arm, _: ()) {
358 fn visit_fn(ir: &mut IrMaps,
364 debug!("visit_fn: id={}", id);
365 let _i = ::util::common::indenter();
367 // swap in a new set of IR maps for this function body:
368 let mut fn_maps = IrMaps::new(ir.tcx);
371 debug!("creating fn_maps: {}", transmute::<&IrMaps, *IrMaps>(&fn_maps));
374 for arg in decl.inputs.iter() {
375 pat_util::pat_bindings(&ir.tcx.def_map,
377 |_bm, arg_id, _x, path| {
378 debug!("adding argument {}", arg_id);
379 let ident = ast_util::path_to_ident(path);
380 fn_maps.add_variable(Arg(arg_id, ident));
384 // gather up the various local variables, significant expressions,
386 visit::walk_fn(&mut fn_maps, fk, decl, body, sp, ());
388 // Special nodes and variables:
389 // - exit_ln represents the end of the fn, either by return or fail
390 // - implicit_ret_var is a pseudo-variable that represents
391 // an implicit return
392 let specials = Specials {
393 exit_ln: fn_maps.add_live_node(ExitNode),
394 fallthrough_ln: fn_maps.add_live_node(ExitNode),
395 no_ret_var: fn_maps.add_variable(ImplicitRet)
399 let mut lsets = Liveness::new(&mut fn_maps, specials);
400 let entry_ln = lsets.compute(decl, body);
402 // check for various error conditions
403 lsets.visit_block(body, ());
404 lsets.check_ret(id, sp, fk, entry_ln, body);
405 lsets.warn_about_unused_args(decl, entry_ln);
408 fn visit_local(ir: &mut IrMaps, local: &Local) {
409 pat_util::pat_bindings(&ir.tcx.def_map, local.pat, |bm, p_id, sp, path| {
410 debug!("adding local variable {}", p_id);
411 let name = ast_util::path_to_ident(path);
412 ir.add_live_node_for_node(p_id, VarDefNode(sp));
413 let kind = match local.init {
414 Some(_) => FromLetWithInitializer,
415 None => FromLetNoInitializer
417 let mutbl = match bm {
418 BindByValue(MutMutable) => true,
421 ir.add_variable(Local(LocalInfo {
428 visit::walk_local(ir, local, ());
431 fn visit_arm(ir: &mut IrMaps, arm: &Arm) {
432 for pat in arm.pats.iter() {
433 pat_util::pat_bindings(&ir.tcx.def_map, *pat, |bm, p_id, sp, path| {
434 debug!("adding local variable {} from match with bm {:?}",
436 let name = ast_util::path_to_ident(path);
437 let mutbl = match bm {
438 BindByValue(MutMutable) => true,
441 ir.add_live_node_for_node(p_id, VarDefNode(sp));
442 ir.add_variable(Local(LocalInfo {
450 visit::walk_arm(ir, arm, ());
453 fn moved_variable_node_id_from_def(def: Def) -> Option<NodeId> {
457 DefLocal(nid, _) => Some(nid),
463 fn visit_expr(ir: &mut IrMaps, expr: &Expr) {
465 // live nodes required for uses or definitions of variables:
467 let def = ir.tcx.def_map.borrow().get_copy(&expr.id);
468 debug!("expr {}: path that leads to {:?}", expr.id, def);
469 if moved_variable_node_id_from_def(def).is_some() {
470 ir.add_live_node_for_node(expr.id, ExprNode(expr.span));
472 visit::walk_expr(ir, expr, ());
474 ExprFnBlock(..) | ExprProc(..) => {
475 // Interesting control flow (for loops can contain labeled
476 // breaks or continues)
477 ir.add_live_node_for_node(expr.id, ExprNode(expr.span));
479 // Make a live_node for each captured variable, with the span
480 // being the location that the variable is used. This results
481 // in better error messages than just pointing at the closure
482 // construction site.
483 let mut call_caps = Vec::new();
484 let fv_mode = freevars::get_capture_mode(ir.tcx, expr.id);
485 freevars::with_freevars(ir.tcx, expr.id, |freevars| {
486 for fv in freevars.iter() {
487 match moved_variable_node_id_from_def(fv.def) {
489 let fv_ln = ir.add_live_node(FreeVarNode(fv.span));
490 let fv_id = fv.def.def_id().node;
491 let fv_ty = ty::node_id_to_type(ir.tcx, fv_id);
492 let is_move = match fv_mode {
493 // var must be dead afterwards
494 freevars::CaptureByValue => {
495 ty::type_moves_by_default(ir.tcx, fv_ty)
498 // var can still be used
499 freevars::CaptureByRef => {
503 call_caps.push(CaptureInfo {ln: fv_ln,
511 ir.set_captures(expr.id, call_caps);
513 visit::walk_expr(ir, expr, ());
516 // live nodes required for interesting control flow:
517 ExprIf(..) | ExprMatch(..) | ExprWhile(..) | ExprLoop(..) => {
518 ir.add_live_node_for_node(expr.id, ExprNode(expr.span));
519 visit::walk_expr(ir, expr, ());
521 ExprForLoop(..) => fail!("non-desugared expr_for_loop"),
522 ExprBinary(op, _, _) if ast_util::lazy_binop(op) => {
523 ir.add_live_node_for_node(expr.id, ExprNode(expr.span));
524 visit::walk_expr(ir, expr, ());
527 // otherwise, live nodes are not required:
528 ExprIndex(..) | ExprField(..) | ExprVstore(..) | ExprVec(..) |
529 ExprCall(..) | ExprMethodCall(..) | ExprTup(..) |
530 ExprBinary(..) | ExprAddrOf(..) |
531 ExprCast(..) | ExprUnary(..) | ExprBreak(_) |
532 ExprAgain(_) | ExprLit(_) | ExprRet(..) | ExprBlock(..) |
533 ExprAssign(..) | ExprAssignOp(..) | ExprMac(..) |
534 ExprStruct(..) | ExprRepeat(..) | ExprParen(..) |
535 ExprInlineAsm(..) | ExprBox(..) => {
536 visit::walk_expr(ir, expr, ());
541 // ______________________________________________________________________
542 // Computing liveness sets
544 // Actually we compute just a bit more than just liveness, but we use
545 // the same basic propagation framework in all cases.
554 fn invalid_users() -> Users {
556 reader: invalid_node(),
557 writer: invalid_node(),
564 fallthrough_ln: LiveNode,
568 static ACC_READ: uint = 1u;
569 static ACC_WRITE: uint = 2u;
570 static ACC_USE: uint = 4u;
572 struct Liveness<'a> {
573 ir: &'a mut IrMaps<'a>,
575 successors: Vec<LiveNode>,
577 // The list of node IDs for the nested loop scopes
579 loop_scope: Vec<NodeId>,
580 // mappings from loop node ID to LiveNode
581 // ("break" label should map to loop node ID,
582 // it probably doesn't now)
583 break_ln: NodeMap<LiveNode>,
584 cont_ln: NodeMap<LiveNode>
587 impl<'a> Liveness<'a> {
588 fn new(ir: &'a mut IrMaps<'a>, specials: Specials) -> Liveness<'a> {
592 successors: Vec::from_elem(ir.num_live_nodes, invalid_node()),
593 users: Vec::from_elem(ir.num_live_nodes * ir.num_vars, invalid_users()),
594 loop_scope: Vec::new(),
595 break_ln: NodeMap::new(),
596 cont_ln: NodeMap::new(),
600 fn live_node(&self, node_id: NodeId, span: Span) -> LiveNode {
601 match self.ir.live_node_map.find(&node_id) {
604 // This must be a mismatch between the ir_map construction
605 // above and the propagation code below; the two sets of
606 // code have to agree about which AST nodes are worth
607 // creating liveness nodes for.
608 self.ir.tcx.sess.span_bug(
610 format!("no live node registered for node {}",
611 node_id).as_slice());
616 fn variable(&self, node_id: NodeId, span: Span) -> Variable {
617 self.ir.variable(node_id, span)
620 fn pat_bindings(&mut self,
622 f: |&mut Liveness<'a>, LiveNode, Variable, Span, NodeId|) {
623 pat_util::pat_bindings(&self.ir.tcx.def_map, pat, |_bm, p_id, sp, _n| {
624 let ln = self.live_node(p_id, sp);
625 let var = self.variable(p_id, sp);
626 f(self, ln, var, sp, p_id);
630 fn arm_pats_bindings(&mut self,
632 f: |&mut Liveness<'a>, LiveNode, Variable, Span, NodeId|) {
633 // only consider the first pattern; any later patterns must have
634 // the same bindings, and we also consider the first pattern to be
635 // the "authoritative" set of ids
636 if !pats.is_empty() {
637 self.pat_bindings(pats[0], f)
641 fn define_bindings_in_pat(&mut self, pat: @Pat, succ: LiveNode)
643 self.define_bindings_in_arm_pats([pat], succ)
646 fn define_bindings_in_arm_pats(&mut self, pats: &[@Pat], succ: LiveNode)
649 self.arm_pats_bindings(pats, |this, ln, var, _sp, _id| {
650 this.init_from_succ(ln, succ);
651 this.define(ln, var);
657 fn idx(&self, ln: LiveNode, var: Variable) -> uint {
658 ln.get() * self.ir.num_vars + var.get()
661 fn live_on_entry(&self, ln: LiveNode, var: Variable)
662 -> Option<LiveNodeKind> {
663 assert!(ln.is_valid());
664 let reader = self.users.get(self.idx(ln, var)).reader;
665 if reader.is_valid() {Some(self.ir.lnk(reader))} else {None}
669 Is this variable live on entry to any of its successor nodes?
671 fn live_on_exit(&self, ln: LiveNode, var: Variable)
672 -> Option<LiveNodeKind> {
673 let successor = *self.successors.get(ln.get());
674 self.live_on_entry(successor, var)
677 fn used_on_entry(&self, ln: LiveNode, var: Variable) -> bool {
678 assert!(ln.is_valid());
679 self.users.get(self.idx(ln, var)).used
682 fn assigned_on_entry(&self, ln: LiveNode, var: Variable)
683 -> Option<LiveNodeKind> {
684 assert!(ln.is_valid());
685 let writer = self.users.get(self.idx(ln, var)).writer;
686 if writer.is_valid() {Some(self.ir.lnk(writer))} else {None}
689 fn assigned_on_exit(&self, ln: LiveNode, var: Variable)
690 -> Option<LiveNodeKind> {
691 let successor = *self.successors.get(ln.get());
692 self.assigned_on_entry(successor, var)
695 fn indices2(&mut self,
698 op: |&mut Liveness<'a>, uint, uint|) {
699 let node_base_idx = self.idx(ln, Variable(0u));
700 let succ_base_idx = self.idx(succ_ln, Variable(0u));
701 for var_idx in range(0u, self.ir.num_vars) {
702 op(self, node_base_idx + var_idx, succ_base_idx + var_idx);
709 test: |uint| -> LiveNode) -> io::IoResult<()> {
710 let node_base_idx = self.idx(ln, Variable(0));
711 for var_idx in range(0u, self.ir.num_vars) {
712 let idx = node_base_idx + var_idx;
713 if test(idx).is_valid() {
714 try!(write!(wr, " {}", Variable(var_idx).to_str()));
720 fn find_loop_scope(&self,
721 opt_label: Option<Ident>,
727 // Refers to a labeled loop. Use the results of resolve
729 match self.ir.tcx.def_map.borrow().find(&id) {
730 Some(&DefLabel(loop_id)) => loop_id,
731 _ => self.ir.tcx.sess.span_bug(sp, "label on break/loop \
732 doesn't refer to a loop")
736 // Vanilla 'break' or 'loop', so use the enclosing
738 if self.loop_scope.len() == 0 {
739 self.ir.tcx.sess.span_bug(sp, "break outside loop");
741 // FIXME(#5275): this shouldn't have to be a method...
742 self.last_loop_scope()
748 fn last_loop_scope(&self) -> NodeId {
749 *self.loop_scope.last().unwrap()
752 #[allow(unused_must_use)]
753 fn ln_str(&self, ln: LiveNode) -> String {
754 let mut wr = io::MemWriter::new();
756 let wr = &mut wr as &mut io::Writer;
757 write!(wr, "[ln({}) of kind {:?} reads", ln.get(), self.ir.lnk(ln));
758 self.write_vars(wr, ln, |idx| self.users.get(idx).reader);
759 write!(wr, " writes");
760 self.write_vars(wr, ln, |idx| self.users.get(idx).writer);
761 write!(wr, " precedes {}]", self.successors.get(ln.get()).to_str());
763 str::from_utf8(wr.unwrap().as_slice()).unwrap().to_string()
766 fn init_empty(&mut self, ln: LiveNode, succ_ln: LiveNode) {
767 *self.successors.get_mut(ln.get()) = succ_ln;
769 // It is not necessary to initialize the
770 // values to empty because this is the value
771 // they have when they are created, and the sets
772 // only grow during iterations.
774 // self.indices(ln) { |idx|
775 // self.users[idx] = invalid_users();
779 fn init_from_succ(&mut self, ln: LiveNode, succ_ln: LiveNode) {
780 // more efficient version of init_empty() / merge_from_succ()
781 *self.successors.get_mut(ln.get()) = succ_ln;
783 self.indices2(ln, succ_ln, |this, idx, succ_idx| {
784 *this.users.get_mut(idx) = *this.users.get(succ_idx)
786 debug!("init_from_succ(ln={}, succ={})",
787 self.ln_str(ln), self.ln_str(succ_ln));
790 fn merge_from_succ(&mut self,
795 if ln == succ_ln { return false; }
797 let mut changed = false;
798 self.indices2(ln, succ_ln, |this, idx, succ_idx| {
799 changed |= copy_if_invalid(this.users.get(succ_idx).reader,
800 &mut this.users.get_mut(idx).reader);
801 changed |= copy_if_invalid(this.users.get(succ_idx).writer,
802 &mut this.users.get_mut(idx).writer);
803 if this.users.get(succ_idx).used && !this.users.get(idx).used {
804 this.users.get_mut(idx).used = true;
809 debug!("merge_from_succ(ln={}, succ={}, first_merge={}, changed={})",
810 ln.to_str(), self.ln_str(succ_ln), first_merge, changed);
813 fn copy_if_invalid(src: LiveNode, dst: &mut LiveNode) -> bool {
814 if src.is_valid() && !dst.is_valid() {
823 // Indicates that a local variable was *defined*; we know that no
824 // uses of the variable can precede the definition (resolve checks
825 // this) so we just clear out all the data.
826 fn define(&mut self, writer: LiveNode, var: Variable) {
827 let idx = self.idx(writer, var);
828 self.users.get_mut(idx).reader = invalid_node();
829 self.users.get_mut(idx).writer = invalid_node();
831 debug!("{} defines {} (idx={}): {}", writer.to_str(), var.to_str(),
832 idx, self.ln_str(writer));
835 // Either read, write, or both depending on the acc bitset
836 fn acc(&mut self, ln: LiveNode, var: Variable, acc: uint) {
837 debug!("{} accesses[{:x}] {}: {}",
838 ln.to_str(), acc, var.to_str(), self.ln_str(ln));
840 let idx = self.idx(ln, var);
841 let user = self.users.get_mut(idx);
843 if (acc & ACC_WRITE) != 0 {
844 user.reader = invalid_node();
848 // Important: if we both read/write, must do read second
849 // or else the write will override.
850 if (acc & ACC_READ) != 0 {
854 if (acc & ACC_USE) != 0 {
859 // _______________________________________________________________________
861 fn compute(&mut self, decl: &FnDecl, body: &Block) -> LiveNode {
862 // if there is a `break` or `again` at the top level, then it's
863 // effectively a return---this only occurs in `for` loops,
864 // where the body is really a closure.
866 debug!("compute: using id for block, {}", block_to_str(body));
868 let entry_ln: LiveNode =
869 self.with_loop_nodes(body.id, self.s.exit_ln, self.s.exit_ln,
870 |this| this.propagate_through_fn_block(decl, body));
872 // hack to skip the loop unless debug! is enabled:
873 debug!("^^ liveness computation results for body {} (entry={})",
875 for ln_idx in range(0u, self.ir.num_live_nodes) {
876 debug!("{}", self.ln_str(LiveNode(ln_idx)));
885 fn propagate_through_fn_block(&mut self, _: &FnDecl, blk: &Block)
887 // the fallthrough exit is only for those cases where we do not
888 // explicitly return:
889 self.init_from_succ(self.s.fallthrough_ln, self.s.exit_ln);
890 if blk.expr.is_none() {
891 self.acc(self.s.fallthrough_ln, self.s.no_ret_var, ACC_READ)
894 self.propagate_through_block(blk, self.s.fallthrough_ln)
897 fn propagate_through_block(&mut self, blk: &Block, succ: LiveNode)
899 let succ = self.propagate_through_opt_expr(blk.expr, succ);
900 blk.stmts.iter().rev().fold(succ, |succ, stmt| {
901 self.propagate_through_stmt(*stmt, succ)
905 fn propagate_through_stmt(&mut self, stmt: &Stmt, succ: LiveNode)
908 StmtDecl(decl, _) => {
909 self.propagate_through_decl(decl, succ)
912 StmtExpr(expr, _) | StmtSemi(expr, _) => {
913 self.propagate_through_expr(expr, succ)
917 self.ir.tcx.sess.span_bug(stmt.span, "unexpanded macro");
922 fn propagate_through_decl(&mut self, decl: &Decl, succ: LiveNode)
925 DeclLocal(ref local) => {
926 self.propagate_through_local(*local, succ)
932 fn propagate_through_local(&mut self, local: &Local, succ: LiveNode)
934 // Note: we mark the variable as defined regardless of whether
935 // there is an initializer. Initially I had thought to only mark
936 // the live variable as defined if it was initialized, and then we
937 // could check for uninit variables just by scanning what is live
938 // at the start of the function. But that doesn't work so well for
939 // immutable variables defined in a loop:
940 // loop { let x; x = 5; }
941 // because the "assignment" loops back around and generates an error.
943 // So now we just check that variables defined w/o an
944 // initializer are not live at the point of their
945 // initialization, which is mildly more complex than checking
946 // once at the func header but otherwise equivalent.
948 let succ = self.propagate_through_opt_expr(local.init, succ);
949 self.define_bindings_in_pat(local.pat, succ)
952 fn propagate_through_exprs(&mut self, exprs: &[@Expr], succ: LiveNode)
954 exprs.iter().rev().fold(succ, |succ, expr| {
955 self.propagate_through_expr(*expr, succ)
959 fn propagate_through_opt_expr(&mut self,
960 opt_expr: Option<@Expr>,
963 opt_expr.iter().fold(succ, |succ, expr| {
964 self.propagate_through_expr(*expr, succ)
968 fn propagate_through_expr(&mut self, expr: &Expr, succ: LiveNode)
970 debug!("propagate_through_expr: {}", expr_to_str(expr));
973 // Interesting cases with control flow or which gen/kill
976 self.access_path(expr, succ, ACC_READ | ACC_USE)
979 ExprField(e, _, _) => {
980 self.propagate_through_expr(e, succ)
983 ExprFnBlock(_, blk) | ExprProc(_, blk) => {
984 debug!("{} is an ExprFnBlock or ExprProc", expr_to_str(expr));
987 The next-node for a break is the successor of the entire
988 loop. The next-node for a continue is the top of this loop.
990 let node = self.live_node(expr.id, expr.span);
991 self.with_loop_nodes(blk.id, succ, node, |this| {
993 // the construction of a closure itself is not important,
994 // but we have to consider the closed over variables.
995 let caps = match this.ir.capture_info_map.find(&expr.id) {
996 Some(caps) => caps.clone(),
998 this.ir.tcx.sess.span_bug(expr.span, "no registered caps");
1001 caps.iter().rev().fold(succ, |succ, cap| {
1002 this.init_from_succ(cap.ln, succ);
1003 let var = this.variable(cap.var_nid, expr.span);
1004 this.acc(cap.ln, var, ACC_READ | ACC_USE);
1010 ExprIf(cond, then, els) => {
1024 let else_ln = self.propagate_through_opt_expr(els, succ);
1025 let then_ln = self.propagate_through_block(then, succ);
1026 let ln = self.live_node(expr.id, expr.span);
1027 self.init_from_succ(ln, else_ln);
1028 self.merge_from_succ(ln, then_ln, false);
1029 self.propagate_through_expr(cond, ln)
1032 ExprWhile(cond, blk) => {
1033 self.propagate_through_loop(expr, Some(cond), blk, succ)
1036 ExprForLoop(..) => fail!("non-desugared expr_for_loop"),
1038 // Note that labels have been resolved, so we don't need to look
1039 // at the label ident
1040 ExprLoop(blk, _) => {
1041 self.propagate_through_loop(expr, None, blk, succ)
1044 ExprMatch(e, ref arms) => {
1059 let ln = self.live_node(expr.id, expr.span);
1060 self.init_empty(ln, succ);
1061 let mut first_merge = true;
1062 for arm in arms.iter() {
1064 self.propagate_through_expr(arm.body, succ);
1066 self.propagate_through_opt_expr(arm.guard, body_succ);
1068 self.define_bindings_in_arm_pats(arm.pats.as_slice(),
1070 self.merge_from_succ(ln, arm_succ, first_merge);
1071 first_merge = false;
1073 self.propagate_through_expr(e, ln)
1077 // ignore succ and subst exit_ln:
1078 self.propagate_through_opt_expr(o_e, self.s.exit_ln)
1081 ExprBreak(opt_label) => {
1082 // Find which label this break jumps to
1083 let sc = self.find_loop_scope(opt_label, expr.id, expr.span);
1085 // Now that we know the label we're going to,
1086 // look it up in the break loop nodes table
1088 match self.break_ln.find(&sc) {
1090 None => self.ir.tcx.sess.span_bug(expr.span,
1091 "break to unknown label")
1095 ExprAgain(opt_label) => {
1096 // Find which label this expr continues to
1097 let sc = self.find_loop_scope(opt_label, expr.id, expr.span);
1099 // Now that we know the label we're going to,
1100 // look it up in the continue loop nodes table
1102 match self.cont_ln.find(&sc) {
1104 None => self.ir.tcx.sess.span_bug(expr.span,
1105 "loop to unknown label")
1109 ExprAssign(l, r) => {
1110 // see comment on lvalues in
1111 // propagate_through_lvalue_components()
1112 let succ = self.write_lvalue(l, succ, ACC_WRITE);
1113 let succ = self.propagate_through_lvalue_components(l, succ);
1114 self.propagate_through_expr(r, succ)
1117 ExprAssignOp(_, l, r) => {
1118 // see comment on lvalues in
1119 // propagate_through_lvalue_components()
1120 let succ = self.write_lvalue(l, succ, ACC_WRITE|ACC_READ);
1121 let succ = self.propagate_through_expr(r, succ);
1122 self.propagate_through_lvalue_components(l, succ)
1125 // Uninteresting cases: just propagate in rev exec order
1127 ExprVstore(expr, _) => {
1128 self.propagate_through_expr(expr, succ)
1131 ExprVec(ref exprs) => {
1132 self.propagate_through_exprs(exprs.as_slice(), succ)
1135 ExprRepeat(element, count) => {
1136 let succ = self.propagate_through_expr(count, succ);
1137 self.propagate_through_expr(element, succ)
1140 ExprStruct(_, ref fields, with_expr) => {
1141 let succ = self.propagate_through_opt_expr(with_expr, succ);
1142 fields.iter().rev().fold(succ, |succ, field| {
1143 self.propagate_through_expr(field.expr, succ)
1147 ExprCall(f, ref args) => {
1148 // calling a fn with bot return type means that the fn
1149 // will fail, and hence the successors can be ignored
1150 let is_bot = !self.ir.tcx.is_method_call(expr.id) && {
1151 let t_ret = ty::ty_fn_ret(ty::expr_ty(self.ir.tcx, f));
1152 ty::type_is_bot(t_ret)
1154 let succ = if is_bot {
1159 let succ = self.propagate_through_exprs(args.as_slice(), succ);
1160 self.propagate_through_expr(f, succ)
1163 ExprMethodCall(_, _, ref args) => {
1164 // calling a method with bot return type means that the method
1165 // will fail, and hence the successors can be ignored
1166 let t_ret = ty::node_id_to_type(self.ir.tcx, expr.id);
1167 let succ = if ty::type_is_bot(t_ret) {self.s.exit_ln}
1169 self.propagate_through_exprs(args.as_slice(), succ)
1172 ExprTup(ref exprs) => {
1173 self.propagate_through_exprs(exprs.as_slice(), succ)
1176 ExprBinary(op, l, r) if ast_util::lazy_binop(op) => {
1177 let r_succ = self.propagate_through_expr(r, succ);
1179 let ln = self.live_node(expr.id, expr.span);
1180 self.init_from_succ(ln, succ);
1181 self.merge_from_succ(ln, r_succ, false);
1183 self.propagate_through_expr(l, ln)
1187 ExprBinary(_, l, r) |
1189 self.propagate_through_exprs([l, r], succ)
1196 self.propagate_through_expr(e, succ)
1199 ExprInlineAsm(ref ia) => {
1200 let succ = ia.outputs.iter().rev().fold(succ, |succ, &(_, expr)| {
1201 // see comment on lvalues in
1202 // propagate_through_lvalue_components()
1203 let succ = self.write_lvalue(expr, succ, ACC_WRITE);
1204 self.propagate_through_lvalue_components(expr, succ)
1206 // Inputs are executed first. Propagate last because of rev order
1207 ia.inputs.iter().rev().fold(succ, |succ, &(_, expr)| {
1208 self.propagate_through_expr(expr, succ)
1217 self.propagate_through_block(blk, succ)
1221 self.ir.tcx.sess.span_bug(expr.span, "unexpanded macro");
1226 fn propagate_through_lvalue_components(&mut self,
1232 // In general, the full flow graph structure for an
1233 // assignment/move/etc can be handled in one of two ways,
1234 // depending on whether what is being assigned is a "tracked
1235 // value" or not. A tracked value is basically a local
1236 // variable or argument.
1238 // The two kinds of graphs are:
1240 // Tracked lvalue Untracked lvalue
1241 // ----------------------++-----------------------
1245 // (rvalue) || (rvalue)
1248 // (write of lvalue) || (lvalue components)
1253 // ----------------------++-----------------------
1255 // I will cover the two cases in turn:
1257 // # Tracked lvalues
1259 // A tracked lvalue is a local variable/argument `x`. In
1260 // these cases, the link_node where the write occurs is linked
1261 // to node id of `x`. The `write_lvalue()` routine generates
1262 // the contents of this node. There are no subcomponents to
1265 // # Non-tracked lvalues
1267 // These are lvalues like `x[5]` or `x.f`. In that case, we
1268 // basically ignore the value which is written to but generate
1269 // reads for the components---`x` in these two examples. The
1270 // components reads are generated by
1271 // `propagate_through_lvalue_components()` (this fn).
1273 // # Illegal lvalues
1275 // It is still possible to observe assignments to non-lvalues;
1276 // these errors are detected in the later pass borrowck. We
1277 // just ignore such cases and treat them as reads.
1280 ExprPath(_) => succ,
1281 ExprField(e, _, _) => self.propagate_through_expr(e, succ),
1282 _ => self.propagate_through_expr(expr, succ)
1286 // see comment on propagate_through_lvalue()
1287 fn write_lvalue(&mut self, expr: &Expr, succ: LiveNode, acc: uint)
1290 ExprPath(_) => self.access_path(expr, succ, acc),
1292 // We do not track other lvalues, so just propagate through
1293 // to their subcomponents. Also, it may happen that
1294 // non-lvalues occur here, because those are detected in the
1295 // later pass borrowck.
1300 fn access_path(&mut self, expr: &Expr, succ: LiveNode, acc: uint)
1302 let def = self.ir.tcx.def_map.borrow().get_copy(&expr.id);
1303 match moved_variable_node_id_from_def(def) {
1305 let ln = self.live_node(expr.id, expr.span);
1307 self.init_from_succ(ln, succ);
1308 let var = self.variable(nid, expr.span);
1309 self.acc(ln, var, acc);
1317 fn propagate_through_loop(&mut self,
1319 cond: Option<@Expr>,
1326 We model control flow like this:
1344 let mut first_merge = true;
1345 let ln = self.live_node(expr.id, expr.span);
1346 self.init_empty(ln, succ);
1348 // if there is a condition, then it's possible we bypass
1349 // the body altogether. otherwise, the only way is via a
1350 // break in the loop body.
1351 self.merge_from_succ(ln, succ, first_merge);
1352 first_merge = false;
1354 debug!("propagate_through_loop: using id for loop body {} {}",
1355 expr.id, block_to_str(body));
1357 let cond_ln = self.propagate_through_opt_expr(cond, ln);
1358 let body_ln = self.with_loop_nodes(expr.id, succ, ln, |this| {
1359 this.propagate_through_block(body, cond_ln)
1362 // repeat until fixed point is reached:
1363 while self.merge_from_succ(ln, body_ln, first_merge) {
1364 first_merge = false;
1365 assert!(cond_ln == self.propagate_through_opt_expr(cond,
1367 assert!(body_ln == self.with_loop_nodes(expr.id, succ, ln,
1368 |this| this.propagate_through_block(body, cond_ln)));
1374 fn with_loop_nodes<R>(&mut self,
1375 loop_node_id: NodeId,
1378 f: |&mut Liveness<'a>| -> R)
1380 debug!("with_loop_nodes: {} {}", loop_node_id, break_ln.get());
1381 self.loop_scope.push(loop_node_id);
1382 self.break_ln.insert(loop_node_id, break_ln);
1383 self.cont_ln.insert(loop_node_id, cont_ln);
1385 self.loop_scope.pop();
1390 // _______________________________________________________________________
1391 // Checking for error conditions
1393 fn check_local(this: &mut Liveness, local: &Local) {
1396 this.warn_about_unused_or_dead_vars_in_pat(local.pat);
1399 this.pat_bindings(local.pat, |this, ln, var, sp, id| {
1400 this.warn_about_unused(sp, id, ln, var);
1405 visit::walk_local(this, local, ());
1408 fn check_arm(this: &mut Liveness, arm: &Arm) {
1409 this.arm_pats_bindings(arm.pats.as_slice(), |this, ln, var, sp, id| {
1410 this.warn_about_unused(sp, id, ln, var);
1412 visit::walk_arm(this, arm, ());
1415 fn check_expr(this: &mut Liveness, expr: &Expr) {
1417 ExprAssign(l, r) => {
1418 this.check_lvalue(l);
1419 this.visit_expr(r, ());
1421 visit::walk_expr(this, expr, ());
1424 ExprAssignOp(_, l, _) => {
1425 this.check_lvalue(l);
1427 visit::walk_expr(this, expr, ());
1430 ExprInlineAsm(ref ia) => {
1431 for &(_, input) in ia.inputs.iter() {
1432 this.visit_expr(input, ());
1435 // Output operands must be lvalues
1436 for &(_, out) in ia.outputs.iter() {
1437 this.check_lvalue(out);
1438 this.visit_expr(out, ());
1441 visit::walk_expr(this, expr, ());
1444 // no correctness conditions related to liveness
1445 ExprCall(..) | ExprMethodCall(..) | ExprIf(..) | ExprMatch(..) |
1446 ExprWhile(..) | ExprLoop(..) | ExprIndex(..) | ExprField(..) |
1447 ExprVstore(..) | ExprVec(..) | ExprTup(..) |
1449 ExprCast(..) | ExprUnary(..) | ExprRet(..) | ExprBreak(..) |
1450 ExprAgain(..) | ExprLit(_) | ExprBlock(..) |
1451 ExprMac(..) | ExprAddrOf(..) | ExprStruct(..) | ExprRepeat(..) |
1452 ExprParen(..) | ExprFnBlock(..) | ExprProc(..) | ExprPath(..) |
1454 visit::walk_expr(this, expr, ());
1456 ExprForLoop(..) => fail!("non-desugared expr_for_loop")
1460 fn check_fn(_v: &Liveness,
1466 // do not check contents of nested fns
1469 impl<'a> Liveness<'a> {
1476 if self.live_on_entry(entry_ln, self.s.no_ret_var).is_some() {
1477 // if no_ret_var is live, then we fall off the end of the
1478 // function without any kind of return expression:
1480 let t_ret = ty::ty_fn_ret(ty::node_id_to_type(self.ir.tcx, id));
1481 if ty::type_is_nil(t_ret) {
1482 // for nil return types, it is ok to not return a value expl.
1483 } else if ty::type_is_bot(t_ret) {
1484 // for bot return types, not ok. Function should fail.
1485 self.ir.tcx.sess.span_err(
1486 sp, "some control paths may return");
1488 let ends_with_stmt = match body.expr {
1489 None if body.stmts.len() > 0 =>
1490 match body.stmts.last().unwrap().node {
1492 let t_stmt = ty::expr_ty(self.ir.tcx, e);
1493 ty::get(t_stmt).sty == ty::get(t_ret).sty
1500 let last_stmt = body.stmts.last().unwrap();
1501 let original_span = original_sp(last_stmt.span, sp);
1502 let span_semicolon = Span {
1503 lo: original_span.hi - BytePos(1),
1504 hi: original_span.hi,
1505 expn_info: original_span.expn_info
1507 self.ir.tcx.sess.span_note(
1508 span_semicolon, "consider removing this semicolon:");
1510 self.ir.tcx.sess.span_err(
1511 sp, "not all control paths return a value");
1516 fn check_lvalue(&mut self, expr: &Expr) {
1519 match self.ir.tcx.def_map.borrow().get_copy(&expr.id) {
1520 DefLocal(nid, _) => {
1521 // Assignment to an immutable variable or argument: only legal
1522 // if there is no later assignment. If this local is actually
1523 // mutable, then check for a reassignment to flag the mutability
1525 let ln = self.live_node(expr.id, expr.span);
1526 let var = self.variable(nid, expr.span);
1527 self.warn_about_dead_assign(expr.span, expr.id, ln, var);
1530 match moved_variable_node_id_from_def(def) {
1532 let ln = self.live_node(expr.id, expr.span);
1533 let var = self.variable(nid, expr.span);
1534 self.warn_about_dead_assign(expr.span, expr.id, ln, var);
1543 // For other kinds of lvalues, no checks are required,
1544 // and any embedded expressions are actually rvalues
1545 visit::walk_expr(self, expr, ());
1550 fn should_warn(&self, var: Variable) -> Option<String> {
1551 let name = self.ir.variable_name(var);
1552 if name.len() == 0 || name.as_slice()[0] == ('_' as u8) {
1559 fn warn_about_unused_args(&self, decl: &FnDecl, entry_ln: LiveNode) {
1560 for arg in decl.inputs.iter() {
1561 pat_util::pat_bindings(&self.ir.tcx.def_map,
1563 |_bm, p_id, sp, path| {
1564 let var = self.variable(p_id, sp);
1565 // Ignore unused self.
1566 let ident = ast_util::path_to_ident(path);
1567 if ident.name != special_idents::self_.name {
1568 self.warn_about_unused(sp, p_id, entry_ln, var);
1574 fn warn_about_unused_or_dead_vars_in_pat(&mut self, pat: &Pat) {
1575 self.pat_bindings(pat, |this, ln, var, sp, id| {
1576 if !this.warn_about_unused(sp, id, ln, var) {
1577 this.warn_about_dead_assign(sp, id, ln, var);
1582 fn warn_about_unused(&self,
1588 if !self.used_on_entry(ln, var) {
1589 let r = self.should_warn(var);
1590 for name in r.iter() {
1592 // annoying: for parameters in funcs like `fn(x: int)
1593 // {ret}`, there is only one node, so asking about
1594 // assigned_on_exit() is not meaningful.
1595 let is_assigned = if ln == self.s.exit_ln {
1598 self.assigned_on_exit(ln, var).is_some()
1602 self.ir.tcx.sess.add_lint(UnusedVariable, id, sp,
1603 format!("variable `{}` is assigned to, but never used",
1606 self.ir.tcx.sess.add_lint(UnusedVariable, id, sp,
1607 format!("unused variable: `{}`", *name));
1616 fn warn_about_dead_assign(&self,
1621 if self.live_on_exit(ln, var).is_none() {
1622 let r = self.should_warn(var);
1623 for name in r.iter() {
1624 self.ir.tcx.sess.add_lint(DeadAssignment, id, sp,
1625 format!("value assigned to `{}` is never read", *name));