Skip to content

Commit 970154e

Browse files
Add tests for module suggestions
1 parent c7bcdeb commit 970154e

File tree

4 files changed

+66
-11
lines changed

4 files changed

+66
-11
lines changed

src/libsyntax/parse/parser.rs

+24-11
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ use symbol::{Symbol, keywords};
5757
use util::ThinVec;
5858

5959
use std::collections::HashSet;
60+
use std::env;
6061
use std::mem;
6162
use std::path::{Path, PathBuf};
6263
use std::rc::Rc;
@@ -5132,17 +5133,29 @@ impl<'a> Parser<'a> {
51325133
let mut err = self.diagnostic().struct_span_err(id_sp,
51335134
"cannot declare a new module at this location");
51345135
if id_sp != syntax_pos::DUMMY_SP {
5135-
let full_path = self.sess.codemap().span_to_filename(id_sp);
5136-
let path = Path::new(&full_path);
5137-
let filename = path.file_stem().unwrap();
5138-
let parent = path.parent().unwrap_or(Path::new(""))
5139-
.to_str().unwrap_or("").to_owned();
5140-
let path = format!("{}/{}",
5141-
if parent.len() == 0 { "." } else { &parent },
5142-
filename.to_str().unwrap_or(""));
5143-
err.span_note(id_sp,
5144-
&format!("maybe move this module `{0}` to its own directory \
5145-
via `{0}/mod.rs`", path));
5136+
let mut src_path = PathBuf::from(self.sess.codemap().span_to_filename(id_sp));
5137+
if let Some(stem) = src_path.clone().file_stem() {
5138+
let mut dest_path = src_path.clone();
5139+
dest_path.set_file_name(stem);
5140+
dest_path.push("mod.rs");
5141+
if let Ok(cur_dir) = env::current_dir() {
5142+
let tmp = if let (Ok(src_path), Ok(dest_path)) =
5143+
(Path::new(&src_path).strip_prefix(&cur_dir),
5144+
Path::new(&dest_path).strip_prefix(&cur_dir)) {
5145+
Some((src_path.to_path_buf(), dest_path.to_path_buf()))
5146+
} else {
5147+
None
5148+
};
5149+
if let Some(tmp) = tmp {
5150+
src_path = tmp.0;
5151+
dest_path = tmp.1;
5152+
}
5153+
}
5154+
err.span_note(id_sp,
5155+
&format!("maybe move this module `{}` to its own \
5156+
directory via `{}`", src_path.to_string_lossy(),
5157+
dest_path.to_string_lossy()));
5158+
}
51465159
}
51475160
if paths.path_exists {
51485161
err.span_note(id_sp,
+11
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;
+11
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 `src/test/compile-fail/auxiliary/foo/bar.rs` to its own directory via `src/test/compile-fail/auxiliary/foo/bar/mod.rs`
15+
16+
mod auxiliary {
17+
mod foo;
18+
}
19+
20+
fn main() {}

0 commit comments

Comments
 (0)