|
| 1 | +// Copyright 2016 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 | +// Sanity check AST before lowering it to HIR |
| 12 | +// |
| 13 | +// This pass is supposed to catch things that fit into AST data structures, |
| 14 | +// but not permitted by the language. It runs after expansion when AST is frozen, |
| 15 | +// so it can check for erroneous constructions produced by syntax extensions. |
| 16 | +// This pass is supposed to perform only simple checks not requiring name resolution |
| 17 | +// or type checking or some other kind of complex analysis. |
| 18 | + |
| 19 | +use rustc::lint; |
| 20 | +use rustc::session::Session; |
| 21 | +use syntax::ast::*; |
| 22 | +use syntax::codemap::Span; |
| 23 | +use syntax::errors; |
| 24 | +use syntax::parse::token::keywords; |
| 25 | +use syntax::visit::{self, Visitor}; |
| 26 | + |
| 27 | +struct SanityChecker<'a> { |
| 28 | + session: &'a Session, |
| 29 | +} |
| 30 | + |
| 31 | +impl<'a> SanityChecker<'a> { |
| 32 | + fn err_handler(&self) -> &errors::Handler { |
| 33 | + &self.session.parse_sess.span_diagnostic |
| 34 | + } |
| 35 | + |
| 36 | + fn check_label(&self, label: Ident, span: Span, id: NodeId) { |
| 37 | + if label.name == keywords::StaticLifetime.name() { |
| 38 | + self.err_handler().span_err(span, &format!("invalid label name `{}`", label.name)); |
| 39 | + } |
| 40 | + if label.name.as_str() == "'_" { |
| 41 | + self.session.add_lint( |
| 42 | + lint::builtin::LIFETIME_UNDERSCORE, id, span, |
| 43 | + format!("invalid label name `{}`", label.name) |
| 44 | + ); |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl<'a, 'v> Visitor<'v> for SanityChecker<'a> { |
| 50 | + fn visit_lifetime(&mut self, lt: &Lifetime) { |
| 51 | + if lt.name.as_str() == "'_" { |
| 52 | + self.session.add_lint( |
| 53 | + lint::builtin::LIFETIME_UNDERSCORE, lt.id, lt.span, |
| 54 | + format!("invalid lifetime name `{}`", lt.name) |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + visit::walk_lifetime(self, lt) |
| 59 | + } |
| 60 | + |
| 61 | + fn visit_expr(&mut self, expr: &Expr) { |
| 62 | + match expr.node { |
| 63 | + ExprKind::While(_, _, Some(ident)) | ExprKind::Loop(_, Some(ident)) | |
| 64 | + ExprKind::WhileLet(_, _, _, Some(ident)) | ExprKind::ForLoop(_, _, _, Some(ident)) => { |
| 65 | + self.check_label(ident, expr.span, expr.id); |
| 66 | + } |
| 67 | + ExprKind::Break(Some(ident)) | ExprKind::Again(Some(ident)) => { |
| 68 | + self.check_label(ident.node, ident.span, expr.id); |
| 69 | + } |
| 70 | + _ => {} |
| 71 | + } |
| 72 | + |
| 73 | + visit::walk_expr(self, expr) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +pub fn check_crate(session: &Session, krate: &Crate) { |
| 78 | + visit::walk_crate(&mut SanityChecker { session: session }, krate) |
| 79 | +} |
0 commit comments