Skip to content

Commit e482529

Browse files
Fix invalid module suggestion
1 parent 846891a commit e482529

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5363,26 +5363,33 @@ impl<'a> Parser<'a> {
53635363
}
53645364
let mut err = self.diagnostic().struct_span_err(id_sp,
53655365
"cannot declare a new module at this location");
5366-
let this_module = match self.directory.path.file_name() {
5367-
Some(file_name) => file_name.to_str().unwrap().to_owned(),
5368-
None => self.root_module_name.as_ref().unwrap().clone(),
5369-
};
5370-
err.span_note(id_sp,
5371-
&format!("maybe move this module `{0}` to its own directory \
5372-
via `{0}{1}mod.rs`",
5373-
this_module,
5374-
path::MAIN_SEPARATOR));
5366+
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));
5380+
}
53755381
if paths.path_exists {
53765382
err.span_note(id_sp,
53775383
&format!("... or maybe `use` the module `{}` instead \
53785384
of possibly redeclaring it",
53795385
paths.name));
5380-
Err(err)
5381-
} else {
5382-
Err(err)
53835386
}
5387+
Err(err)
53845388
} else {
5385-
paths.result.map_err(|err| self.span_fatal_err(id_sp, err))
5389+
match paths.result {
5390+
Ok(succ) => Ok(succ),
5391+
Err(err) => Err(self.span_fatal_err(id_sp, &err.err_msg, &err.help_msg)),
5392+
}
53865393
}
53875394
}
53885395

0 commit comments

Comments
 (0)