Skip to content

Commit d21e908

Browse files
committed
Disallow non-inline modules without path annotations inside blocks and fix fallout
1 parent dbc2466 commit d21e908

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

src/librustc_back/target/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,13 @@ mod windows_msvc_base;
6565

6666
macro_rules! supported_targets {
6767
( $(($triple:expr, $module:ident)),+ ) => (
68+
$(mod $module;)*
69+
6870
/// List of supported targets
6971
pub const TARGETS: &'static [&'static str] = &[$($triple),*];
7072

7173
// this would use a match if stringify! were allowed in pattern position
7274
fn load_specific(target: &str) -> Option<Target> {
73-
$(mod $module;)*
7475
let target = target.replace("-", "_");
7576
if false { }
7677
$(

src/libsyntax/parse/parser.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ bitflags! {
7373
flags Restrictions: u8 {
7474
const RESTRICTION_STMT_EXPR = 1 << 0,
7575
const RESTRICTION_NO_STRUCT_LITERAL = 1 << 1,
76+
const NO_NONINLINE_MOD = 1 << 2,
7677
}
7778
}
7879

@@ -3208,8 +3209,8 @@ impl<'a> Parser<'a> {
32083209
/// Evaluate the closure with restrictions in place.
32093210
///
32103211
/// After the closure is evaluated, restrictions are reset.
3211-
pub fn with_res<F>(&mut self, r: Restrictions, f: F) -> PResult<'a, P<Expr>>
3212-
where F: FnOnce(&mut Self) -> PResult<'a, P<Expr>>
3212+
pub fn with_res<F, T>(&mut self, r: Restrictions, f: F) -> T
3213+
where F: FnOnce(&mut Self) -> T
32133214
{
32143215
let old = self.restrictions;
32153216
self.restrictions = r;
@@ -3767,7 +3768,9 @@ impl<'a> Parser<'a> {
37673768
}
37683769
} else {
37693770
// FIXME: Bad copy of attrs
3770-
match try!(self.parse_item_(attrs.clone(), false, true)) {
3771+
let restrictions = self.restrictions | Restrictions::NO_NONINLINE_MOD;
3772+
match try!(self.with_res(restrictions,
3773+
|this| this.parse_item_(attrs.clone(), false, true))) {
37713774
Some(i) => {
37723775
let hi = i.span.hi;
37733776
let decl = P(spanned(lo, hi, DeclKind::Item(i)));
@@ -5174,7 +5177,17 @@ impl<'a> Parser<'a> {
51745177

51755178
let paths = Parser::default_submod_path(id, &dir_path, self.sess.codemap());
51765179

5177-
if !self.owns_directory {
5180+
if self.restrictions.contains(Restrictions::NO_NONINLINE_MOD) {
5181+
let msg =
5182+
"Cannot declare a non-inline module inside a block unless it has a path attribute";
5183+
let mut err = self.diagnostic().struct_span_err(id_sp, msg);
5184+
if paths.path_exists {
5185+
let msg = format!("Maybe `use` the module `{}` instead of redeclaring it",
5186+
paths.name);
5187+
err.span_note(id_sp, &msg);
5188+
}
5189+
return Err(err);
5190+
} else if !self.owns_directory {
51785191
let mut err = self.diagnostic().struct_span_err(id_sp,
51795192
"cannot declare a new module at this location");
51805193
let this_module = match self.mod_path_stack.last() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test that non-inline modules are not allowed inside blocks.
12+
13+
fn main() {
14+
mod foo; //~ ERROR Cannot declare a non-inline module inside a block
15+
}

0 commit comments

Comments
 (0)