Navigation Menu

Skip to content

Commit

Permalink
Start warning cycle.
Browse files Browse the repository at this point in the history
  • Loading branch information
jseyfried committed Nov 22, 2016
1 parent 808a7ca commit fa8c53b
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 13 deletions.
10 changes: 9 additions & 1 deletion src/librustc/lint/builtin.rs
Expand Up @@ -204,6 +204,13 @@ declare_lint! {
"detects extra requirements in impls that were erroneously allowed"
}

declare_lint! {
pub LEGACY_DIRECTORY_OWNERSHIP,
Warn,
"non-inline, non-`#[path]` modules (e.g. `mod foo;`) were erroneously allowed in some files \
not named `mod.rs`"
}

/// Does nothing as a lint pass, but registers some `Lint`s
/// which are used by other parts of the compiler.
#[derive(Copy, Clone)]
Expand Down Expand Up @@ -242,7 +249,8 @@ impl LintPass for HardwiredLints {
LIFETIME_UNDERSCORE,
SAFE_EXTERN_STATICS,
PATTERNS_IN_FNS_WITHOUT_BODY,
EXTRA_REQUIREMENT_IN_IMPL
EXTRA_REQUIREMENT_IN_IMPL,
LEGACY_DIRECTORY_OWNERSHIP
)
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/librustc_lint/lib.rs
Expand Up @@ -232,6 +232,10 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
id: LintId::of(EXTRA_REQUIREMENT_IN_IMPL),
reference: "issue #37166 <https://github.com/rust-lang/rust/issues/37166>",
},
FutureIncompatibleInfo {
id: LintId::of(LEGACY_DIRECTORY_OWNERSHIP),
reference: "issue #37872 <https://github.com/rust-lang/rust/issues/37872>",
},
]);

// Register renamed and removed lints
Expand Down
7 changes: 7 additions & 0 deletions src/librustc_passes/ast_validation.rs
Expand Up @@ -207,6 +207,13 @@ impl<'a> Visitor for AstValidator<'a> {
ItemKind::Mod(_) => {
// Ensure that `path` attributes on modules are recorded as used (c.f. #35584).
attr::first_attr_value_str_by_name(&item.attrs, "path");
if let Some(attr) =
item.attrs.iter().find(|attr| attr.name() == "warn_directory_ownership") {
let lint = lint::builtin::LEGACY_DIRECTORY_OWNERSHIP;
let msg = "cannot declare a new module at this location";
self.session.add_lint(lint, item.id, item.span, msg.to_string());
attr::mark_used(attr);
}
}
ItemKind::Union(ref vdata, _) => {
if !vdata.is_struct() {
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ext/expand.rs
Expand Up @@ -790,7 +790,7 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
PathBuf::from(self.cx.parse_sess.codemap().span_to_filename(inner));
let directory_ownership = match path.file_name().unwrap().to_str() {
Some("mod.rs") => DirectoryOwnership::Owned,
_ => DirectoryOwnership::UnownedViaMod,
_ => DirectoryOwnership::UnownedViaMod(false),
};
path.pop();
module.directory = path;
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/parse/mod.rs
Expand Up @@ -86,7 +86,7 @@ pub struct Directory {
pub enum DirectoryOwnership {
Owned,
UnownedViaBlock,
UnownedViaMod,
UnownedViaMod(bool /* legacy warnings? */),
}

// a bunch of utility functions of the form parse_<thing>_from_<source>
Expand Down
46 changes: 36 additions & 10 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -38,7 +38,7 @@ use ast::{Ty, TyKind, TypeBinding, TyParam, TyParamBounds};
use ast::{ViewPath, ViewPathGlob, ViewPathList, ViewPathSimple};
use ast::{Visibility, WhereClause};
use ast::{BinOpKind, UnOp};
use ast;
use {ast, attr};
use codemap::{self, CodeMap, Spanned, spanned, respan};
use syntax_pos::{self, Span, BytePos, mk_sp};
use errors::{self, DiagnosticBuilder};
Expand Down Expand Up @@ -243,6 +243,7 @@ pub struct ModulePath {
pub struct ModulePathSuccess {
pub path: PathBuf,
pub directory_ownership: DirectoryOwnership,
warn: bool,
}

pub struct ModulePathError {
Expand Down Expand Up @@ -5268,10 +5269,25 @@ impl<'a> Parser<'a> {
self.bump();
if in_cfg {
// This mod is in an external file. Let's go get it!
let ModulePathSuccess { path, directory_ownership } =
let ModulePathSuccess { path, directory_ownership, warn } =
self.submod_path(id, &outer_attrs, id_span)?;
let (module, attrs) =
let (module, mut attrs) =
self.eval_src_mod(path, directory_ownership, id.to_string(), id_span)?;
if warn {
let attr = ast::Attribute {
id: attr::mk_attr_id(),
style: ast::AttrStyle::Outer,
value: ast::MetaItem {
name: Symbol::intern("warn_directory_ownership"),
node: ast::MetaItemKind::Word,
span: syntax_pos::DUMMY_SP,
},
is_sugared_doc: false,
span: syntax_pos::DUMMY_SP,
};
attr::mark_known(&attr);
attrs.push(attr);
}
Ok((id, module, Some(attrs)))
} else {
let placeholder = ast::Mod { inner: syntax_pos::DUMMY_SP, items: Vec::new() };
Expand All @@ -5290,7 +5306,7 @@ impl<'a> Parser<'a> {
}

fn push_directory(&mut self, id: Ident, attrs: &[Attribute]) {
if let Some(path) = ::attr::first_attr_value_str_by_name(attrs, "path") {
if let Some(path) = attr::first_attr_value_str_by_name(attrs, "path") {
self.directory.path.push(&*path.as_str());
self.directory.ownership = DirectoryOwnership::Owned;
} else {
Expand All @@ -5299,7 +5315,7 @@ impl<'a> Parser<'a> {
}

pub fn submod_path_from_attr(attrs: &[ast::Attribute], dir_path: &Path) -> Option<PathBuf> {
::attr::first_attr_value_str_by_name(attrs, "path").map(|d| dir_path.join(&*d.as_str()))
attr::first_attr_value_str_by_name(attrs, "path").map(|d| dir_path.join(&*d.as_str()))
}

/// Returns either a path to a module, or .
Expand All @@ -5316,11 +5332,13 @@ impl<'a> Parser<'a> {
let result = match (default_exists, secondary_exists) {
(true, false) => Ok(ModulePathSuccess {
path: default_path,
directory_ownership: DirectoryOwnership::UnownedViaMod,
directory_ownership: DirectoryOwnership::UnownedViaMod(false),
warn: false,
}),
(false, true) => Ok(ModulePathSuccess {
path: secondary_path,
directory_ownership: DirectoryOwnership::Owned,
warn: false,
}),
(false, false) => Err(ModulePathError {
err_msg: format!("file not found for module `{}`", mod_name),
Expand Down Expand Up @@ -5353,9 +5371,10 @@ impl<'a> Parser<'a> {
return Ok(ModulePathSuccess {
directory_ownership: match path.file_name().and_then(|s| s.to_str()) {
Some("mod.rs") => DirectoryOwnership::Owned,
_ => DirectoryOwnership::UnownedViaMod,
_ => DirectoryOwnership::UnownedViaMod(true),
},
path: path,
warn: false,
});
}

Expand All @@ -5371,7 +5390,12 @@ impl<'a> Parser<'a> {
err.span_note(id_sp, &msg);
}
return Err(err);
} else if let DirectoryOwnership::UnownedViaMod = self.directory.ownership {
} else if let DirectoryOwnership::UnownedViaMod(warn) = self.directory.ownership {
if warn {
if let Ok(result) = paths.result {
return Ok(ModulePathSuccess { warn: true, ..result });
}
}
let mut err = self.diagnostic().struct_span_err(id_sp,
"cannot declare a new module at this location");
let this_module = match self.directory.path.file_name() {
Expand All @@ -5387,8 +5411,10 @@ impl<'a> Parser<'a> {
&format!("... or maybe `use` the module `{}` instead \
of possibly redeclaring it",
paths.name));
}
return Err(err);
return Err(err);
} else {
return Err(err);
};
}

match paths.result {
Expand Down
21 changes: 21 additions & 0 deletions src/test/compile-fail/directory_ownership/backcompat-warnings.rs
@@ -0,0 +1,21 @@
// 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.

// error-pattern: cannot declare a new module at this location
// error-pattern: will become a hard error
// error-pattern: compilation successful

#![feature(rustc_attrs)]

#[path="mod_file_not_owning_aux3.rs"]
mod foo;

#[rustc_error]
fn main() {}
@@ -0,0 +1,13 @@
// 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.

// ignore-test this is not a test

mod mod_file_not_owning_aux2;

0 comments on commit fa8c53b

Please sign in to comment.