|
| 1 | +// Copyright 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. |
| 4 | +// |
| 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. |
| 10 | + |
| 11 | +// This compiler pass detects static items that refer to themselves |
| 12 | +// recursively. |
| 13 | + |
| 14 | +use driver::session::Session; |
| 15 | +use middle::resolve; |
| 16 | +use middle::def::DefStatic; |
| 17 | + |
| 18 | +use syntax::ast::{Crate, Expr, ExprPath, Item, ItemStatic, NodeId}; |
| 19 | +use syntax::{ast_util, ast_map}; |
| 20 | +use syntax::visit::Visitor; |
| 21 | +use syntax::visit; |
| 22 | + |
| 23 | +struct CheckCrateVisitor<'a, 'ast: 'a> { |
| 24 | + sess: &'a Session, |
| 25 | + def_map: &'a resolve::DefMap, |
| 26 | + ast_map: &'a ast_map::Map<'ast> |
| 27 | +} |
| 28 | + |
| 29 | +impl<'v, 'a, 'ast> Visitor<'v> for CheckCrateVisitor<'a, 'ast> { |
| 30 | + fn visit_item(&mut self, i: &Item) { |
| 31 | + check_item(self, i); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +pub fn check_crate<'ast>(sess: &Session, |
| 36 | + krate: &Crate, |
| 37 | + def_map: &resolve::DefMap, |
| 38 | + ast_map: &ast_map::Map<'ast>) { |
| 39 | + let mut visitor = CheckCrateVisitor { |
| 40 | + sess: sess, |
| 41 | + def_map: def_map, |
| 42 | + ast_map: ast_map |
| 43 | + }; |
| 44 | + visit::walk_crate(&mut visitor, krate); |
| 45 | + sess.abort_if_errors(); |
| 46 | +} |
| 47 | + |
| 48 | +fn check_item(v: &mut CheckCrateVisitor, it: &Item) { |
| 49 | + match it.node { |
| 50 | + ItemStatic(_, _, ref ex) => { |
| 51 | + check_item_recursion(v.sess, v.ast_map, v.def_map, it); |
| 52 | + visit::walk_expr(v, &**ex) |
| 53 | + }, |
| 54 | + _ => visit::walk_item(v, it) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +struct CheckItemRecursionVisitor<'a, 'ast: 'a> { |
| 59 | + root_it: &'a Item, |
| 60 | + sess: &'a Session, |
| 61 | + ast_map: &'a ast_map::Map<'ast>, |
| 62 | + def_map: &'a resolve::DefMap, |
| 63 | + idstack: Vec<NodeId> |
| 64 | +} |
| 65 | + |
| 66 | +// Make sure a const item doesn't recursively refer to itself |
| 67 | +// FIXME: Should use the dependency graph when it's available (#1356) |
| 68 | +pub fn check_item_recursion<'a>(sess: &'a Session, |
| 69 | + ast_map: &'a ast_map::Map, |
| 70 | + def_map: &'a resolve::DefMap, |
| 71 | + it: &'a Item) { |
| 72 | + |
| 73 | + let mut visitor = CheckItemRecursionVisitor { |
| 74 | + root_it: it, |
| 75 | + sess: sess, |
| 76 | + ast_map: ast_map, |
| 77 | + def_map: def_map, |
| 78 | + idstack: Vec::new() |
| 79 | + }; |
| 80 | + visitor.visit_item(it); |
| 81 | +} |
| 82 | + |
| 83 | +impl<'a, 'ast, 'v> Visitor<'v> for CheckItemRecursionVisitor<'a, 'ast> { |
| 84 | + fn visit_item(&mut self, it: &Item) { |
| 85 | + if self.idstack.iter().any(|x| x == &(it.id)) { |
| 86 | + self.sess.span_err(self.root_it.span, "recursive constant"); |
| 87 | + return; |
| 88 | + } |
| 89 | + self.idstack.push(it.id); |
| 90 | + visit::walk_item(self, it); |
| 91 | + self.idstack.pop(); |
| 92 | + } |
| 93 | + |
| 94 | + fn visit_expr(&mut self, e: &Expr) { |
| 95 | + match e.node { |
| 96 | + ExprPath(..) => { |
| 97 | + match self.def_map.borrow().find(&e.id) { |
| 98 | + Some(&DefStatic(def_id, _)) if |
| 99 | + ast_util::is_local(def_id) => { |
| 100 | + self.visit_item(&*self.ast_map.expect_item(def_id.node)); |
| 101 | + } |
| 102 | + _ => () |
| 103 | + } |
| 104 | + }, |
| 105 | + _ => () |
| 106 | + } |
| 107 | + visit::walk_expr(self, e); |
| 108 | + } |
| 109 | +} |
0 commit comments