|
| 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. |
| 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 flag is needed for plugins to work: |
| 12 | +// compile-flags: -C prefer-dynamic |
| 13 | + |
| 14 | +#![feature(plugin_registrar, rustc_private)] |
| 15 | +#![crate_type = "dylib"] |
| 16 | +#![deny(region_hierarchy)] |
| 17 | + |
| 18 | +extern crate syntax; |
| 19 | +#[macro_use] |
| 20 | +extern crate rustc; |
| 21 | +extern crate rustc_plugin; |
| 22 | + |
| 23 | +use rustc::lint::{LateContext, LintPass, LateLintPass, LintArray, LintContext}; |
| 24 | +use rustc::hir; |
| 25 | +use rustc::hir::intravisit::FnKind; |
| 26 | +use rustc::middle::region::CodeExtent; |
| 27 | +use rustc::util::nodemap::FxHashMap; |
| 28 | + |
| 29 | +use syntax::ast::{self, NodeId}; |
| 30 | +use syntax::codemap::Span; |
| 31 | + |
| 32 | +declare_lint!(REGION_HIERARCHY, Warn, "warn about bogus region hierarchy"); |
| 33 | + |
| 34 | +struct Pass { |
| 35 | + map: FxHashMap<CodeExtent, NodeId> |
| 36 | +} |
| 37 | + |
| 38 | +impl LintPass for Pass { |
| 39 | + fn get_lints(&self) -> LintArray { lint_array!(REGION_HIERARCHY) } |
| 40 | +} |
| 41 | + |
| 42 | +impl LateLintPass for Pass { |
| 43 | + fn check_fn(&mut self, cx: &LateContext, |
| 44 | + fk: FnKind, _: &hir::FnDecl, expr: &hir::Expr, |
| 45 | + span: Span, node: ast::NodeId) |
| 46 | + { |
| 47 | + if let FnKind::Closure(..) = fk { return } |
| 48 | + |
| 49 | + let mut extent = cx.tcx.region_maps.node_extent(expr.id); |
| 50 | + while let Some(parent) = cx.tcx.region_maps.opt_encl_scope(extent) { |
| 51 | + extent = parent; |
| 52 | + } |
| 53 | + if let Some(other) = self.map.insert(extent, node) { |
| 54 | + cx.span_lint(REGION_HIERARCHY, span, &format!( |
| 55 | + "different fns {:?}, {:?} with the same root extent {:?}", |
| 56 | + cx.tcx.map.local_def_id(other), |
| 57 | + cx.tcx.map.local_def_id(node), |
| 58 | + extent)); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#[plugin_registrar] |
| 64 | +pub fn plugin_registrar(reg: &mut ::rustc_plugin::Registry) { |
| 65 | + reg.register_late_lint_pass(Box::new( |
| 66 | + Pass { map: FxHashMap() } |
| 67 | + )); |
| 68 | +} |
0 commit comments