Skip to content

Commit fa8c53b

Browse files
committed
Start warning cycle.
1 parent 808a7ca commit fa8c53b

File tree

8 files changed

+92
-13
lines changed

8 files changed

+92
-13
lines changed

src/librustc/lint/builtin.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@ declare_lint! {
204204
"detects extra requirements in impls that were erroneously allowed"
205205
}
206206

207+
declare_lint! {
208+
pub LEGACY_DIRECTORY_OWNERSHIP,
209+
Warn,
210+
"non-inline, non-`#[path]` modules (e.g. `mod foo;`) were erroneously allowed in some files \
211+
not named `mod.rs`"
212+
}
213+
207214
/// Does nothing as a lint pass, but registers some `Lint`s
208215
/// which are used by other parts of the compiler.
209216
#[derive(Copy, Clone)]
@@ -242,7 +249,8 @@ impl LintPass for HardwiredLints {
242249
LIFETIME_UNDERSCORE,
243250
SAFE_EXTERN_STATICS,
244251
PATTERNS_IN_FNS_WITHOUT_BODY,
245-
EXTRA_REQUIREMENT_IN_IMPL
252+
EXTRA_REQUIREMENT_IN_IMPL,
253+
LEGACY_DIRECTORY_OWNERSHIP
246254
)
247255
}
248256
}

src/librustc_lint/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
232232
id: LintId::of(EXTRA_REQUIREMENT_IN_IMPL),
233233
reference: "issue #37166 <https://github.com/rust-lang/rust/issues/37166>",
234234
},
235+
FutureIncompatibleInfo {
236+
id: LintId::of(LEGACY_DIRECTORY_OWNERSHIP),
237+
reference: "issue #37872 <https://github.com/rust-lang/rust/issues/37872>",
238+
},
235239
]);
236240

237241
// Register renamed and removed lints

src/librustc_passes/ast_validation.rs

+7
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ impl<'a> Visitor for AstValidator<'a> {
207207
ItemKind::Mod(_) => {
208208
// Ensure that `path` attributes on modules are recorded as used (c.f. #35584).
209209
attr::first_attr_value_str_by_name(&item.attrs, "path");
210+
if let Some(attr) =
211+
item.attrs.iter().find(|attr| attr.name() == "warn_directory_ownership") {
212+
let lint = lint::builtin::LEGACY_DIRECTORY_OWNERSHIP;
213+
let msg = "cannot declare a new module at this location";
214+
self.session.add_lint(lint, item.id, item.span, msg.to_string());
215+
attr::mark_used(attr);
216+
}
210217
}
211218
ItemKind::Union(ref vdata, _) => {
212219
if !vdata.is_struct() {

src/libsyntax/ext/expand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
790790
PathBuf::from(self.cx.parse_sess.codemap().span_to_filename(inner));
791791
let directory_ownership = match path.file_name().unwrap().to_str() {
792792
Some("mod.rs") => DirectoryOwnership::Owned,
793-
_ => DirectoryOwnership::UnownedViaMod,
793+
_ => DirectoryOwnership::UnownedViaMod(false),
794794
};
795795
path.pop();
796796
module.directory = path;

src/libsyntax/parse/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub struct Directory {
8686
pub enum DirectoryOwnership {
8787
Owned,
8888
UnownedViaBlock,
89-
UnownedViaMod,
89+
UnownedViaMod(bool /* legacy warnings? */),
9090
}
9191

9292
// a bunch of utility functions of the form parse_<thing>_from_<source>

src/libsyntax/parse/parser.rs

+36-10
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use ast::{Ty, TyKind, TypeBinding, TyParam, TyParamBounds};
3838
use ast::{ViewPath, ViewPathGlob, ViewPathList, ViewPathSimple};
3939
use ast::{Visibility, WhereClause};
4040
use ast::{BinOpKind, UnOp};
41-
use ast;
41+
use {ast, attr};
4242
use codemap::{self, CodeMap, Spanned, spanned, respan};
4343
use syntax_pos::{self, Span, BytePos, mk_sp};
4444
use errors::{self, DiagnosticBuilder};
@@ -243,6 +243,7 @@ pub struct ModulePath {
243243
pub struct ModulePathSuccess {
244244
pub path: PathBuf,
245245
pub directory_ownership: DirectoryOwnership,
246+
warn: bool,
246247
}
247248

248249
pub struct ModulePathError {
@@ -5268,10 +5269,25 @@ impl<'a> Parser<'a> {
52685269
self.bump();
52695270
if in_cfg {
52705271
// This mod is in an external file. Let's go get it!
5271-
let ModulePathSuccess { path, directory_ownership } =
5272+
let ModulePathSuccess { path, directory_ownership, warn } =
52725273
self.submod_path(id, &outer_attrs, id_span)?;
5273-
let (module, attrs) =
5274+
let (module, mut attrs) =
52745275
self.eval_src_mod(path, directory_ownership, id.to_string(), id_span)?;
5276+
if warn {
5277+
let attr = ast::Attribute {
5278+
id: attr::mk_attr_id(),
5279+
style: ast::AttrStyle::Outer,
5280+
value: ast::MetaItem {
5281+
name: Symbol::intern("warn_directory_ownership"),
5282+
node: ast::MetaItemKind::Word,
5283+
span: syntax_pos::DUMMY_SP,
5284+
},
5285+
is_sugared_doc: false,
5286+
span: syntax_pos::DUMMY_SP,
5287+
};
5288+
attr::mark_known(&attr);
5289+
attrs.push(attr);
5290+
}
52755291
Ok((id, module, Some(attrs)))
52765292
} else {
52775293
let placeholder = ast::Mod { inner: syntax_pos::DUMMY_SP, items: Vec::new() };
@@ -5290,7 +5306,7 @@ impl<'a> Parser<'a> {
52905306
}
52915307

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

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

53055321
/// Returns either a path to a module, or .
@@ -5316,11 +5332,13 @@ impl<'a> Parser<'a> {
53165332
let result = match (default_exists, secondary_exists) {
53175333
(true, false) => Ok(ModulePathSuccess {
53185334
path: default_path,
5319-
directory_ownership: DirectoryOwnership::UnownedViaMod,
5335+
directory_ownership: DirectoryOwnership::UnownedViaMod(false),
5336+
warn: false,
53205337
}),
53215338
(false, true) => Ok(ModulePathSuccess {
53225339
path: secondary_path,
53235340
directory_ownership: DirectoryOwnership::Owned,
5341+
warn: false,
53245342
}),
53255343
(false, false) => Err(ModulePathError {
53265344
err_msg: format!("file not found for module `{}`", mod_name),
@@ -5353,9 +5371,10 @@ impl<'a> Parser<'a> {
53535371
return Ok(ModulePathSuccess {
53545372
directory_ownership: match path.file_name().and_then(|s| s.to_str()) {
53555373
Some("mod.rs") => DirectoryOwnership::Owned,
5356-
_ => DirectoryOwnership::UnownedViaMod,
5374+
_ => DirectoryOwnership::UnownedViaMod(true),
53575375
},
53585376
path: path,
5377+
warn: false,
53595378
});
53605379
}
53615380

@@ -5371,7 +5390,12 @@ impl<'a> Parser<'a> {
53715390
err.span_note(id_sp, &msg);
53725391
}
53735392
return Err(err);
5374-
} else if let DirectoryOwnership::UnownedViaMod = self.directory.ownership {
5393+
} else if let DirectoryOwnership::UnownedViaMod(warn) = self.directory.ownership {
5394+
if warn {
5395+
if let Ok(result) = paths.result {
5396+
return Ok(ModulePathSuccess { warn: true, ..result });
5397+
}
5398+
}
53755399
let mut err = self.diagnostic().struct_span_err(id_sp,
53765400
"cannot declare a new module at this location");
53775401
let this_module = match self.directory.path.file_name() {
@@ -5387,8 +5411,10 @@ impl<'a> Parser<'a> {
53875411
&format!("... or maybe `use` the module `{}` instead \
53885412
of possibly redeclaring it",
53895413
paths.name));
5390-
}
5391-
return Err(err);
5414+
return Err(err);
5415+
} else {
5416+
return Err(err);
5417+
};
53925418
}
53935419

53945420
match paths.result {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
// error-pattern: cannot declare a new module at this location
12+
// error-pattern: will become a hard error
13+
// error-pattern: compilation successful
14+
15+
#![feature(rustc_attrs)]
16+
17+
#[path="mod_file_not_owning_aux3.rs"]
18+
mod foo;
19+
20+
#[rustc_error]
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
// ignore-test this is not a test
12+
13+
mod mod_file_not_owning_aux2;

0 commit comments

Comments
 (0)