Skip to content

Commit 9658645

Browse files
committed
Auto merge of #31534 - jseyfried:restrict_noninline_mod, r=nikomatsakis
This PR disallows non-inline modules without path annotations that are either in a block or in an inline module whose containing file is not a directory owner (fixes #29765). This is a [breaking-change]. r? @nikomatsakis
2 parents 18f8143 + d21e908 commit 9658645

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
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-7
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ bitflags! {
7272
flags Restrictions: u8 {
7373
const RESTRICTION_STMT_EXPR = 1 << 0,
7474
const RESTRICTION_NO_STRUCT_LITERAL = 1 << 1,
75+
const NO_NONINLINE_MOD = 1 << 2,
7576
}
7677
}
7778

@@ -3301,8 +3302,8 @@ impl<'a> Parser<'a> {
33013302
/// Evaluate the closure with restrictions in place.
33023303
///
33033304
/// After the closure is evaluated, restrictions are reset.
3304-
pub fn with_res<F>(&mut self, r: Restrictions, f: F) -> PResult<'a, P<Expr>>
3305-
where F: FnOnce(&mut Self) -> PResult<'a, P<Expr>>
3305+
pub fn with_res<F, T>(&mut self, r: Restrictions, f: F) -> T
3306+
where F: FnOnce(&mut Self) -> T
33063307
{
33073308
let old = self.restrictions;
33083309
self.restrictions = r;
@@ -3926,7 +3927,9 @@ impl<'a> Parser<'a> {
39263927
}
39273928
} else {
39283929
// FIXME: Bad copy of attrs
3929-
match try!(self.parse_item_(attrs.clone(), false, true)) {
3930+
let restrictions = self.restrictions | Restrictions::NO_NONINLINE_MOD;
3931+
match try!(self.with_res(restrictions,
3932+
|this| this.parse_item_(attrs.clone(), false, true))) {
39303933
Some(i) => {
39313934
let hi = i.span.hi;
39323935
let decl = P(spanned(lo, hi, DeclKind::Item(i)));
@@ -5257,11 +5260,8 @@ impl<'a> Parser<'a> {
52575260
self.push_mod_path(id, outer_attrs);
52585261
try!(self.expect(&token::OpenDelim(token::Brace)));
52595262
let mod_inner_lo = self.span.lo;
5260-
let old_owns_directory = self.owns_directory;
5261-
self.owns_directory = true;
52625263
let attrs = try!(self.parse_inner_attributes());
52635264
let m = try!(self.parse_mod_items(&token::CloseDelim(token::Brace), mod_inner_lo));
5264-
self.owns_directory = old_owns_directory;
52655265
self.pop_mod_path();
52665266
Ok((id, ItemKind::Mod(m), Some(attrs)))
52675267
}
@@ -5338,7 +5338,17 @@ impl<'a> Parser<'a> {
53385338

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

5341-
if !self.owns_directory {
5341+
if self.restrictions.contains(Restrictions::NO_NONINLINE_MOD) {
5342+
let msg =
5343+
"Cannot declare a non-inline module inside a block unless it has a path attribute";
5344+
let mut err = self.diagnostic().struct_span_err(id_sp, msg);
5345+
if paths.path_exists {
5346+
let msg = format!("Maybe `use` the module `{}` instead of redeclaring it",
5347+
paths.name);
5348+
err.span_note(id_sp, &msg);
5349+
}
5350+
return Err(err);
5351+
} else if !self.owns_directory {
53425352
let mut err = self.diagnostic().struct_span_err(id_sp,
53435353
"cannot declare a new module at this location");
53445354
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)