From 2d17a33878f1af0aa500a4e1ff6aa5c8689ab249 Mon Sep 17 00:00:00 2001 From: Eduard Burtescu Date: Tue, 13 Jan 2015 11:39:05 +0200 Subject: Deny imports after non-item statements. --- src/librustc/diagnostics.rs | 1 + src/librustc_resolve/lib.rs | 20 ++++++++++++++++++++ src/test/compile-fail/blind-item-block-middle.rs | 1 + src/test/compile-fail/blind-item-local-shadow.rs | 20 ++++++++++++++++++++ src/test/run-pass/blind-item-local-shadow.rs | 19 ------------------- 5 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 src/test/compile-fail/blind-item-local-shadow.rs delete mode 100644 src/test/run-pass/blind-item-local-shadow.rs diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index 1ea79bdf606..2602f38b3ad 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -52,6 +52,7 @@ register_diagnostics! { E0140, E0152, E0153, + E0154, E0157, E0158, E0161, diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index e4453e8fc59..b70980cf1ec 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -3509,6 +3509,26 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { } } + // Check for imports appearing after non-item statements. + let mut found_non_item = false; + for statement in block.stmts.iter() { + if let ast::StmtDecl(ref declaration, _) = statement.node { + if let ast::DeclItem(ref i) = declaration.node { + match i.node { + ItemExternCrate(_) | ItemUse(_) if found_non_item => { + span_err!(self.session, i.span, E0154, + "imports are not allowed after non-item statements"); + } + _ => {} + } + } else { + found_non_item = true + } + } else { + found_non_item = true; + } + } + // Descend into the block. visit::walk_block(self, block); diff --git a/src/test/compile-fail/blind-item-block-middle.rs b/src/test/compile-fail/blind-item-block-middle.rs index 24a1e4e24d8..fbb0730f014 100644 --- a/src/test/compile-fail/blind-item-block-middle.rs +++ b/src/test/compile-fail/blind-item-block-middle.rs @@ -14,4 +14,5 @@ fn main() { let bar = 5; //~^ ERROR declaration of `bar` shadows an enum variant or unit-like struct in scope use foo::bar; + //~^ ERROR imports are not allowed after non-item statements } diff --git a/src/test/compile-fail/blind-item-local-shadow.rs b/src/test/compile-fail/blind-item-local-shadow.rs new file mode 100644 index 00000000000..a28f5f6e557 --- /dev/null +++ b/src/test/compile-fail/blind-item-local-shadow.rs @@ -0,0 +1,20 @@ +// Copyright 2015 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +mod bar { + pub fn foo() -> bool { true } +} + +fn main() { + let foo = |&:| false; + use bar::foo; + //~^ ERROR imports are not allowed after non-item statements + assert_eq!(foo(), false); +} diff --git a/src/test/run-pass/blind-item-local-shadow.rs b/src/test/run-pass/blind-item-local-shadow.rs deleted file mode 100644 index d019215ca04..00000000000 --- a/src/test/run-pass/blind-item-local-shadow.rs +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2015 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -mod bar { - pub fn foo() -> uint { 42 } -} - -fn main() { - let foo = |&:| 5u; - use bar::foo; - assert_eq!(foo(), 5u); -} -- cgit 1.4.1-3-g733a5