Rollup merge of #41501 - GuillaumeGomez:invalid_module_location, r=jseyfried
Invalid module location Fixes #38110. r? @jseyfried
This commit is contained in:
commit
ccc790f9e6
5 changed files with 72 additions and 13 deletions
|
@ -57,9 +57,11 @@ use tokenstream::{self, Delimited, ThinTokenStream, TokenTree, TokenStream};
|
|||
use symbol::{Symbol, keywords};
|
||||
use util::ThinVec;
|
||||
|
||||
use std::cmp;
|
||||
use std::collections::HashSet;
|
||||
use std::{cmp, mem, slice};
|
||||
use std::mem;
|
||||
use std::path::{self, Path, PathBuf};
|
||||
use std::slice;
|
||||
|
||||
bitflags! {
|
||||
flags Restrictions: u8 {
|
||||
|
@ -5363,24 +5365,25 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
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() {
|
||||
Some(file_name) => file_name.to_str().unwrap().to_owned(),
|
||||
None => self.root_module_name.as_ref().unwrap().clone(),
|
||||
};
|
||||
err.span_note(id_sp,
|
||||
&format!("maybe move this module `{0}` to its own directory \
|
||||
via `{0}{1}mod.rs`",
|
||||
this_module,
|
||||
path::MAIN_SEPARATOR));
|
||||
if id_sp != syntax_pos::DUMMY_SP {
|
||||
let src_path = PathBuf::from(self.sess.codemap().span_to_filename(id_sp));
|
||||
if let Some(stem) = src_path.file_stem() {
|
||||
let mut dest_path = src_path.clone();
|
||||
dest_path.set_file_name(stem);
|
||||
dest_path.push("mod.rs");
|
||||
err.span_note(id_sp,
|
||||
&format!("maybe move this module `{}` to its own \
|
||||
directory via `{}`", src_path.to_string_lossy(),
|
||||
dest_path.to_string_lossy()));
|
||||
}
|
||||
}
|
||||
if paths.path_exists {
|
||||
err.span_note(id_sp,
|
||||
&format!("... or maybe `use` the module `{}` instead \
|
||||
of possibly redeclaring it",
|
||||
paths.name));
|
||||
Err(err)
|
||||
} else {
|
||||
Err(err)
|
||||
}
|
||||
Err(err)
|
||||
} else {
|
||||
paths.result.map_err(|err| self.span_fatal_err(id_sp, err))
|
||||
}
|
||||
|
|
11
src/test/ui/invalid-module-declaration/auxiliary/foo/bar.rs
Normal file
11
src/test/ui/invalid-module-declaration/auxiliary/foo/bar.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
// Copyright 2017 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.
|
||||
|
||||
pub mod baz;
|
11
src/test/ui/invalid-module-declaration/auxiliary/foo/mod.rs
Normal file
11
src/test/ui/invalid-module-declaration/auxiliary/foo/mod.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
// Copyright 2017 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.
|
||||
|
||||
pub mod bar;
|
|
@ -0,0 +1,20 @@
|
|||
// 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-tidy-linelength
|
||||
|
||||
// error-pattern: cannot declare a new module at this location
|
||||
// error-pattern: maybe move this module
|
||||
|
||||
mod auxiliary {
|
||||
mod foo;
|
||||
}
|
||||
|
||||
fn main() {}
|
|
@ -0,0 +1,14 @@
|
|||
error: cannot declare a new module at this location
|
||||
--> $DIR/auxiliary/foo/bar.rs:11:9
|
||||
|
|
||||
11 | pub mod baz;
|
||||
| ^^^
|
||||
|
|
||||
note: maybe move this module `$DIR/auxiliary/foo/bar.rs` to its own directory via `$DIR/auxiliary/foo/bar/mod.rs`
|
||||
--> $DIR/auxiliary/foo/bar.rs:11:9
|
||||
|
|
||||
11 | pub mod baz;
|
||||
| ^^^
|
||||
|
||||
error: aborting due to previous error
|
||||
|
Loading…
Add table
Reference in a new issue