Skip to content

Commit 5f9eb73

Browse files
author
Ariel Ben-Yehuda
authored
Rollup merge of rust-lang#41501 - GuillaumeGomez:invalid_module_location, r=jseyfried
Invalid module location Fixes rust-lang#38110. r? @jseyfried
2 parents 7a910ad + 3f97b2a commit 5f9eb73

File tree

5 files changed

+72
-13
lines changed

5 files changed

+72
-13
lines changed

src/libsyntax/parse/parser.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ use tokenstream::{self, Delimited, ThinTokenStream, TokenTree, TokenStream};
5757
use symbol::{Symbol, keywords};
5858
use util::ThinVec;
5959

60+
use std::cmp;
6061
use std::collections::HashSet;
61-
use std::{cmp, mem, slice};
62+
use std::mem;
6263
use std::path::{self, Path, PathBuf};
64+
use std::slice;
6365

6466
bitflags! {
6567
flags Restrictions: u8 {
@@ -5363,24 +5365,25 @@ impl<'a> Parser<'a> {
53635365
}
53645366
let mut err = self.diagnostic().struct_span_err(id_sp,
53655367
"cannot declare a new module at this location");
5366-
let this_module = match self.directory.path.file_name() {
5367-
Some(file_name) => file_name.to_str().unwrap().to_owned(),
5368-
None => self.root_module_name.as_ref().unwrap().clone(),
5369-
};
5370-
err.span_note(id_sp,
5371-
&format!("maybe move this module `{0}` to its own directory \
5372-
via `{0}{1}mod.rs`",
5373-
this_module,
5374-
path::MAIN_SEPARATOR));
5368+
if id_sp != syntax_pos::DUMMY_SP {
5369+
let src_path = PathBuf::from(self.sess.codemap().span_to_filename(id_sp));
5370+
if let Some(stem) = src_path.file_stem() {
5371+
let mut dest_path = src_path.clone();
5372+
dest_path.set_file_name(stem);
5373+
dest_path.push("mod.rs");
5374+
err.span_note(id_sp,
5375+
&format!("maybe move this module `{}` to its own \
5376+
directory via `{}`", src_path.to_string_lossy(),
5377+
dest_path.to_string_lossy()));
5378+
}
5379+
}
53755380
if paths.path_exists {
53765381
err.span_note(id_sp,
53775382
&format!("... or maybe `use` the module `{}` instead \
53785383
of possibly redeclaring it",
53795384
paths.name));
5380-
Err(err)
5381-
} else {
5382-
Err(err)
53835385
}
5386+
Err(err)
53845387
} else {
53855388
paths.result.map_err(|err| self.span_fatal_err(id_sp, err))
53865389
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2017 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+
pub mod baz;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2017 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+
pub mod bar;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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-tidy-linelength
12+
13+
// error-pattern: cannot declare a new module at this location
14+
// error-pattern: maybe move this module
15+
16+
mod auxiliary {
17+
mod foo;
18+
}
19+
20+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: cannot declare a new module at this location
2+
--> $DIR/auxiliary/foo/bar.rs:11:9
3+
|
4+
11 | pub mod baz;
5+
| ^^^
6+
|
7+
note: maybe move this module `$DIR/auxiliary/foo/bar.rs` to its own directory via `$DIR/auxiliary/foo/bar/mod.rs`
8+
--> $DIR/auxiliary/foo/bar.rs:11:9
9+
|
10+
11 | pub mod baz;
11+
| ^^^
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)