1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// This compiler pass detects static items that refer to themselves
// recursively.

use session::Session;
use middle::def::{DefStatic, DefConst, DefMap};

use syntax::ast;
use syntax::{ast_util, ast_map};
use syntax::visit::Visitor;
use syntax::visit;

struct CheckCrateVisitor<'a, 'ast: 'a> {
    sess: &'a Session,
    def_map: &'a DefMap,
    ast_map: &'a ast_map::Map<'ast>
}

impl<'v, 'a, 'ast> Visitor<'v> for CheckCrateVisitor<'a, 'ast> {
    fn visit_item(&mut self, i: &ast::Item) {
        check_item(self, i);
    }
}

pub fn check_crate<'ast>(sess: &Session,
                         krate: &ast::Crate,
                         def_map: &DefMap,
                         ast_map: &ast_map::Map<'ast>) {
    let mut visitor = CheckCrateVisitor {
        sess: sess,
        def_map: def_map,
        ast_map: ast_map
    };
    visit::walk_crate(&mut visitor, krate);
    sess.abort_if_errors();
}

fn check_item(v: &mut CheckCrateVisitor, it: &ast::Item) {
    match it.node {
        ast::ItemStatic(_, _, ref ex) |
        ast::ItemConst(_, ref ex) => {
            check_item_recursion(v.sess, v.ast_map, v.def_map, it);
            visit::walk_expr(v, &**ex)
        },
        _ => visit::walk_item(v, it)
    }
}

struct CheckItemRecursionVisitor<'a, 'ast: 'a> {
    root_it: &'a ast::Item,
    sess: &'a Session,
    ast_map: &'a ast_map::Map<'ast>,
    def_map: &'a DefMap,
    idstack: Vec<ast::NodeId>
}

// Make sure a const item doesn't recursively refer to itself
// FIXME: Should use the dependency graph when it's available (#1356)
pub fn check_item_recursion<'a>(sess: &'a Session,
                                ast_map: &'a ast_map::Map,
                                def_map: &'a DefMap,
                                it: &'a ast::Item) {

    let mut visitor = CheckItemRecursionVisitor {
        root_it: it,
        sess: sess,
        ast_map: ast_map,
        def_map: def_map,
        idstack: Vec::new()
    };
    visitor.visit_item(it);
}

impl<'a, 'ast, 'v> Visitor<'v> for CheckItemRecursionVisitor<'a, 'ast> {
    fn visit_item(&mut self, it: &ast::Item) {
        if self.idstack.iter().any(|x| x == &(it.id)) {
            span_err!(self.sess, self.root_it.span, E0265, "recursive constant");
            return;
        }
        self.idstack.push(it.id);
        visit::walk_item(self, it);
        self.idstack.pop();
    }

    fn visit_expr(&mut self, e: &ast::Expr) {
        match e.node {
            ast::ExprPath(..) => {
                match self.def_map.borrow().get(&e.id).map(|d| d.base_def) {
                    Some(DefStatic(def_id, _)) |
                    Some(DefConst(def_id)) if
                            ast_util::is_local(def_id) => {
                        match self.ast_map.get(def_id.node) {
                          ast_map::NodeItem(item) =>
                            self.visit_item(item),
                          ast_map::NodeForeignItem(_) => {},
                          _ => {
                            span_err!(self.sess, e.span, E0266,
                              "expected item, found {}",
                                      self.ast_map.node_to_string(def_id.node));
                            return;
                          },
                        }
                    }
                    _ => ()
                }
            },
            _ => ()
        }
        visit::walk_expr(self, e);
    }
}