1
Fork 0

Fix invalid module suggestion

This commit is contained in:
Guillaume Gomez 2016-12-08 16:12:38 -08:00
parent 846891aeff
commit e482529022

View file

@ -5363,26 +5363,33 @@ impl<'a> Parser<'a> {
} }
let mut err = self.diagnostic().struct_span_err(id_sp, let mut err = self.diagnostic().struct_span_err(id_sp,
"cannot declare a new module at this location"); "cannot declare a new module at this location");
let this_module = match self.directory.path.file_name() { if id_sp != syntax_pos::DUMMY_SP {
Some(file_name) => file_name.to_str().unwrap().to_owned(), let full_path = self.sess.codemap().span_to_filename(id_sp);
None => self.root_module_name.as_ref().unwrap().clone(), 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, err.span_note(id_sp,
&format!("maybe move this module `{0}` to its own directory \ &format!("maybe move this module `{0}` to its own directory \
via `{0}{1}mod.rs`", via `{0}{1}mod.rs`",
this_module, path,
path::MAIN_SEPARATOR)); path::MAIN_SEPARATOR));
}
if paths.path_exists { if paths.path_exists {
err.span_note(id_sp, err.span_note(id_sp,
&format!("... or maybe `use` the module `{}` instead \ &format!("... or maybe `use` the module `{}` instead \
of possibly redeclaring it", of possibly redeclaring it",
paths.name)); paths.name));
Err(err)
} else {
Err(err)
} }
Err(err)
} else { } else {
paths.result.map_err(|err| self.span_fatal_err(id_sp, err)) match paths.result {
Ok(succ) => Ok(succ),
Err(err) => Err(self.span_fatal_err(id_sp, &err.err_msg, &err.help_msg)),
}
} }
} }