about summary refs log tree commit diff
path: root/src/libsyntax/parse
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2017-01-03 12:19:13 +0100
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2017-04-24 15:16:52 +0200
commitbd880bc6bf62a223664467d4df2b056a9122b7e7 (patch)
treec5726dc2eb311f9734968b62617c21bd577910df /src/libsyntax/parse
parente4825290223f39647bf2782b9d4ef5f00554c7ee (diff)
downloadrust-bd880bc6bf62a223664467d4df2b056a9122b7e7.tar.gz
rust-bd880bc6bf62a223664467d4df2b056a9122b7e7.zip
Add tests for module suggestions
Diffstat (limited to 'src/libsyntax/parse')
-rw-r--r--src/libsyntax/parse/parser.rs43
1 files changed, 28 insertions, 15 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index dd32b40554b..ac72d21ec42 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -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,