Skip to content

Commit

Permalink
sanity -> validation
Browse files Browse the repository at this point in the history
Add missing check_path for paths in import prefixes
  • Loading branch information
petrochenkov committed May 24, 2016
1 parent 676a719 commit 91d8a4f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use rustc_privacy;
use rustc_plugin::registry::Registry;
use rustc_plugin as plugin;
use rustc::hir::lowering::lower_crate;
use rustc_passes::{ast_sanity, no_asm, loops, consts, rvalues, static_recursion};
use rustc_passes::{ast_validation, no_asm, loops, consts, rvalues, static_recursion};
use rustc_const_eval::check_match;
use super::Compilation;

Expand Down Expand Up @@ -167,8 +167,8 @@ pub fn compile_input(sess: &Session,
|| lint::check_ast_crate(sess, &expanded_crate));

time(sess.time_passes(),
"AST sanity checking",
|| ast_sanity::check_crate(sess, &expanded_crate));
"AST validation",
|| ast_validation::check_crate(sess, &expanded_crate));

let (analysis, resolutions, mut hir_forest) = {
lower_and_resolve(sess, &id, &mut defs, &expanded_crate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Sanity check AST before lowering it to HIR
// Validate AST before lowering it to HIR
//
// This pass is supposed to catch things that fit into AST data structures,
// but not permitted by the language. It runs after expansion when AST is frozen,
Expand All @@ -24,11 +24,11 @@ use syntax::errors;
use syntax::parse::token::{self, keywords};
use syntax::visit::{self, Visitor};

struct SanityChecker<'a> {
struct AstValidator<'a> {
session: &'a Session,
}

impl<'a> SanityChecker<'a> {
impl<'a> AstValidator<'a> {
fn err_handler(&self) -> &errors::Handler {
&self.session.parse_sess.span_diagnostic
}
Expand All @@ -55,9 +55,21 @@ impl<'a> SanityChecker<'a> {
err.emit();
}
}

fn check_path(&self, path: &Path, id: NodeId) {
if path.global && path.segments.len() > 0 {
let ident = path.segments[0].identifier;
if token::Ident(ident).is_path_segment_keyword() {
self.session.add_lint(
lint::builtin::SUPER_OR_SELF_IN_GLOBAL_PATH, id, path.span,
format!("global paths cannot start with `{}`", ident)
);
}
}
}
}

impl<'a, 'v> Visitor<'v> for SanityChecker<'a> {
impl<'a, 'v> Visitor<'v> for AstValidator<'a> {
fn visit_lifetime(&mut self, lt: &Lifetime) {
if lt.name.as_str() == "'_" {
self.session.add_lint(
Expand Down Expand Up @@ -85,19 +97,17 @@ impl<'a, 'v> Visitor<'v> for SanityChecker<'a> {
}

fn visit_path(&mut self, path: &Path, id: NodeId) {
if path.global && path.segments.len() > 0 {
let ident = path.segments[0].identifier;
if token::Ident(ident).is_path_segment_keyword() {
self.session.add_lint(
lint::builtin::SUPER_OR_SELF_IN_GLOBAL_PATH, id, path.span,
format!("global paths cannot start with `{}`", ident)
);
}
}
self.check_path(path, id);

visit::walk_path(self, path)
}

fn visit_path_list_item(&mut self, prefix: &Path, item: &PathListItem) {
self.check_path(prefix, item.node.id());

visit::walk_path_list_item(self, prefix, item)
}

fn visit_item(&mut self, item: &Item) {
match item.node {
ItemKind::Use(ref view_path) => {
Expand Down Expand Up @@ -169,5 +179,5 @@ impl<'a, 'v> Visitor<'v> for SanityChecker<'a> {
}

pub fn check_crate(session: &Session, krate: &Crate) {
visit::walk_crate(&mut SanityChecker { session: session }, krate)
visit::walk_crate(&mut AstValidator { session: session }, krate)
}
2 changes: 1 addition & 1 deletion src/librustc_passes/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extern crate rustc_const_math;

pub mod diagnostics;

pub mod ast_sanity;
pub mod ast_validation;
pub mod consts;
pub mod loops;
pub mod no_asm;
Expand Down
9 changes: 9 additions & 0 deletions src/test/compile-fail/use-super-global-path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@
// except according to those terms.

#![feature(rustc_attrs)]
#![allow(unused_imports, dead_code)]

struct S;
struct Z;

mod foo {
use ::super::{S, Z}; //~ WARN global paths cannot start with `super`
//~^ WARN global paths cannot start with `super`
//~^^ WARN this was previously accepted by the compiler but is being phased out
//~^^^ WARN this was previously accepted by the compiler but is being phased out

pub fn g() {
use ::super::main; //~ WARN global paths cannot start with `super`
//~^ WARN this was previously accepted by the compiler but is being phased out
Expand Down

0 comments on commit 91d8a4f

Please sign in to comment.