Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libsyntax: Restrict where non-inline modules can appear (fixes #29765) #31534

Merged
merged 2 commits into from
Feb 16, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/librustc_back/target/mod.rs
Original file line number Diff line number Diff line change
@@ -65,12 +65,13 @@ mod windows_msvc_base;

macro_rules! supported_targets {
( $(($triple:expr, $module:ident)),+ ) => (
$(mod $module;)*

/// List of supported targets
pub const TARGETS: &'static [&'static str] = &[$($triple),*];

// this would use a match if stringify! were allowed in pattern position
fn load_specific(target: &str) -> Option<Target> {
$(mod $module;)*
let target = target.replace("-", "_");
if false { }
$(
24 changes: 17 additions & 7 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
@@ -73,6 +73,7 @@ bitflags! {
flags Restrictions: u8 {
const RESTRICTION_STMT_EXPR = 1 << 0,
const RESTRICTION_NO_STRUCT_LITERAL = 1 << 1,
const NO_NONINLINE_MOD = 1 << 2,
}
}

@@ -3208,8 +3209,8 @@ impl<'a> Parser<'a> {
/// Evaluate the closure with restrictions in place.
///
/// After the closure is evaluated, restrictions are reset.
pub fn with_res<F>(&mut self, r: Restrictions, f: F) -> PResult<'a, P<Expr>>
where F: FnOnce(&mut Self) -> PResult<'a, P<Expr>>
pub fn with_res<F, T>(&mut self, r: Restrictions, f: F) -> T
where F: FnOnce(&mut Self) -> T
{
let old = self.restrictions;
self.restrictions = r;
@@ -3767,7 +3768,9 @@ impl<'a> Parser<'a> {
}
} else {
// FIXME: Bad copy of attrs
match try!(self.parse_item_(attrs.clone(), false, true)) {
let restrictions = self.restrictions | Restrictions::NO_NONINLINE_MOD;
match try!(self.with_res(restrictions,
|this| this.parse_item_(attrs.clone(), false, true))) {
Some(i) => {
let hi = i.span.hi;
let decl = P(spanned(lo, hi, DeclKind::Item(i)));
@@ -5096,11 +5099,8 @@ impl<'a> Parser<'a> {
self.push_mod_path(id, outer_attrs);
try!(self.expect(&token::OpenDelim(token::Brace)));
let mod_inner_lo = self.span.lo;
let old_owns_directory = self.owns_directory;
self.owns_directory = true;
let attrs = try!(self.parse_inner_attributes());
let m = try!(self.parse_mod_items(&token::CloseDelim(token::Brace), mod_inner_lo));
self.owns_directory = old_owns_directory;
self.pop_mod_path();
Ok((id, ItemKind::Mod(m), Some(attrs)))
}
@@ -5177,7 +5177,17 @@ impl<'a> Parser<'a> {

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

if !self.owns_directory {
if self.restrictions.contains(Restrictions::NO_NONINLINE_MOD) {
let msg =
"Cannot declare a non-inline module inside a block unless it has a path attribute";
let mut err = self.diagnostic().struct_span_err(id_sp, msg);
if paths.path_exists {
let msg = format!("Maybe `use` the module `{}` instead of redeclaring it",
paths.name);
err.span_note(id_sp, &msg);
}
return Err(err);
} else if !self.owns_directory {
let mut err = self.diagnostic().struct_span_err(id_sp,
"cannot declare a new module at this location");
let this_module = match self.mod_path_stack.last() {
15 changes: 15 additions & 0 deletions src/test/compile-fail/non-inline-mod-restriction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Test that non-inline modules are not allowed inside blocks.

fn main() {
mod foo; //~ ERROR Cannot declare a non-inline module inside a block
}