Skip to content

Commit 4000a49

Browse files
committed
Deny imports after non-item statements.
1 parent 7daebb5 commit 4000a49

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

src/librustc/diagnostics.rs

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ register_diagnostics! {
5252
E0140,
5353
E0152,
5454
E0153,
55+
E0154,
5556
E0157,
5657
E0158,
5758
E0161,

src/librustc_resolve/lib.rs

+20
Original file line numberDiff line numberDiff line change
@@ -3503,6 +3503,26 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
35033503
}
35043504
}
35053505

3506+
// Check for imports appearing after non-item statements.
3507+
let mut found_non_item = false;
3508+
for statement in block.stmts.iter() {
3509+
if let ast::StmtDecl(ref declaration, _) = statement.node {
3510+
if let ast::DeclItem(ref i) = declaration.node {
3511+
match i.node {
3512+
ItemExternCrate(_) | ItemUse(_) if found_non_item => {
3513+
span_err!(self.session, i.span, E0154,
3514+
"imports are not allowed after non-item statements");
3515+
}
3516+
_ => {}
3517+
}
3518+
} else {
3519+
found_non_item = true
3520+
}
3521+
} else {
3522+
found_non_item = true;
3523+
}
3524+
}
3525+
35063526
// Descend into the block.
35073527
visit::walk_block(self, block);
35083528

src/test/compile-fail/blind-item-block-middle.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ fn main() {
1414
let bar = 5;
1515
//~^ ERROR declaration of `bar` shadows an enum variant or unit-like struct in scope
1616
use foo::bar;
17+
//~^ ERROR imports are not allowed after non-item statements
1718
}

src/test/run-pass/blind-item-local-shadow.rs renamed to src/test/compile-fail/blind-item-local-shadow.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
// except according to those terms.
1010

1111
mod bar {
12-
pub fn foo() -> uint { 42 }
12+
pub fn foo() -> bool { true }
1313
}
1414

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

0 commit comments

Comments
 (0)