Skip to content

Commit bd880bc

Browse files
Add tests for module suggestions
1 parent e482529 commit bd880bc

File tree

4 files changed

+70
-15
lines changed

4 files changed

+70
-15
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ use symbol::{Symbol, keywords};
5858
use util::ThinVec;
5959

6060
use std::collections::HashSet;
61-
use std::{cmp, mem, slice};
62-
use std::path::{self, Path, PathBuf};
61+
use std::env;
62+
use std::mem;
63+
use std::path::{Path, PathBuf};
64+
use std::rc::Rc;
65+
use std::slice;
6366

6467
bitflags! {
6568
flags Restrictions: u8 {
@@ -5364,19 +5367,29 @@ impl<'a> Parser<'a> {
53645367
let mut err = self.diagnostic().struct_span_err(id_sp,
53655368
"cannot declare a new module at this location");
53665369
if id_sp != syntax_pos::DUMMY_SP {
5367-
let full_path = self.sess.codemap().span_to_filename(id_sp);
5368-
let path = Path::new(&full_path);
5369-
let filename = path.file_stem().unwrap();
5370-
let parent = path.parent().unwrap_or(Path::new(""))
5371-
.to_str().unwrap_or("").to_owned();
5372-
let path = format!("{}/{}",
5373-
if parent.len() == 0 { "." } else { &parent },
5374-
filename.to_str().unwrap_or(""));
5375-
err.span_note(id_sp,
5376-
&format!("maybe move this module `{0}` to its own directory \
5377-
via `{0}{1}mod.rs`",
5378-
path,
5379-
path::MAIN_SEPARATOR));
5370+
let mut src_path = PathBuf::from(self.sess.codemap().span_to_filename(id_sp));
5371+
if let Some(stem) = src_path.clone().file_stem() {
5372+
let mut dest_path = src_path.clone();
5373+
dest_path.set_file_name(stem);
5374+
dest_path.push("mod.rs");
5375+
if let Ok(cur_dir) = env::current_dir() {
5376+
let tmp = if let (Ok(src_path), Ok(dest_path)) =
5377+
(Path::new(&src_path).strip_prefix(&cur_dir),
5378+
Path::new(&dest_path).strip_prefix(&cur_dir)) {
5379+
Some((src_path.to_path_buf(), dest_path.to_path_buf()))
5380+
} else {
5381+
None
5382+
};
5383+
if let Some(tmp) = tmp {
5384+
src_path = tmp.0;
5385+
dest_path = tmp.1;
5386+
}
5387+
}
5388+
err.span_note(id_sp,
5389+
&format!("maybe move this module `{}` to its own \
5390+
directory via `{}`", src_path.to_string_lossy(),
5391+
dest_path.to_string_lossy()));
5392+
}
53805393
}
53815394
if paths.path_exists {
53825395
err.span_note(id_sp,
Lines changed: 11 additions & 0 deletions
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;
Lines changed: 11 additions & 0 deletions
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;
Lines changed: 20 additions & 0 deletions
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)