Skip to content

Commit

Permalink
Deny imports after non-item statements.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Jan 14, 2015
1 parent 4bec184 commit 9574724
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/librustc/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ register_diagnostics! {
E0140,
E0152,
E0153,
E0154,
E0157,
E0158,
E0161,
Expand Down
20 changes: 20 additions & 0 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3503,6 +3503,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);

Expand Down
1 change: 1 addition & 0 deletions src/test/compile-fail/blind-item-block-middle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
// except according to those terms.

mod bar {
pub fn foo() -> uint { 42 }
pub fn foo() -> bool { true }
}

fn main() {
let foo = |&:| 5u;
let foo = |&:| false;
use bar::foo;
assert_eq!(foo(), 5u);
//~^ ERROR imports are not allowed after non-item statements
assert_eq!(foo(), false);
}

0 comments on commit 9574724

Please sign in to comment.