Add tests for module suggestions

This commit is contained in:
Guillaume Gomez 2017-01-03 12:19:13 +01:00
parent e482529022
commit bd880bc6bf
4 changed files with 70 additions and 15 deletions

View File

@ -58,8 +58,11 @@ use symbol::{Symbol, keywords};
use util::ThinVec;
use std::collections::HashSet;
use std::{cmp, mem, slice};
use std::path::{self, Path, PathBuf};
use std::env;
use std::mem;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::slice;
bitflags! {
flags Restrictions: u8 {
@ -5364,19 +5367,29 @@ impl<'a> Parser<'a> {
let mut err = self.diagnostic().struct_span_err(id_sp,
"cannot declare a new module at this location");
if id_sp != syntax_pos::DUMMY_SP {
let full_path = self.sess.codemap().span_to_filename(id_sp);
let path = Path::new(&full_path);
let filename = path.file_stem().unwrap();
let parent = path.parent().unwrap_or(Path::new(""))
.to_str().unwrap_or("").to_owned();
let path = format!("{}/{}",
if parent.len() == 0 { "." } else { &parent },
filename.to_str().unwrap_or(""));
err.span_note(id_sp,
&format!("maybe move this module `{0}` to its own directory \
via `{0}{1}mod.rs`",
path,
path::MAIN_SEPARATOR));
let mut src_path = PathBuf::from(self.sess.codemap().span_to_filename(id_sp));
if let Some(stem) = src_path.clone().file_stem() {
let mut dest_path = src_path.clone();
dest_path.set_file_name(stem);
dest_path.push("mod.rs");
if let Ok(cur_dir) = env::current_dir() {
let tmp = if let (Ok(src_path), Ok(dest_path)) =
(Path::new(&src_path).strip_prefix(&cur_dir),
Path::new(&dest_path).strip_prefix(&cur_dir)) {
Some((src_path.to_path_buf(), dest_path.to_path_buf()))
} else {
None
};
if let Some(tmp) = tmp {
src_path = tmp.0;
dest_path = tmp.1;
}
}
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,

View 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;

View 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;

View File

@ -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 `src/test/compile-fail/auxiliary/foo/bar.rs` to its own directory via `src/test/compile-fail/auxiliary/foo/bar/mod.rs`
mod auxiliary {
mod foo;
}
fn main() {}