Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6ccd15d

Browse files
committedJan 18, 2015
Deny imports after non-item statements.
1 parent ea525ac commit 6ccd15d

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed
 

‎src/librustc/diagnostics.rs

Lines changed: 1 addition & 0 deletions
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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3505,6 +3505,26 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
35053505
}
35063506
}
35073507

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

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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 4 additions & 3 deletions
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)
Please sign in to comment.