Skip to content

Commit c7bcdeb

Browse files
Fix invalid module suggestion
1 parent b1e3176 commit c7bcdeb

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/libsyntax/parse/parser.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -5131,25 +5131,31 @@ impl<'a> Parser<'a> {
51315131
}
51325132
let mut err = self.diagnostic().struct_span_err(id_sp,
51335133
"cannot declare a new module at this location");
5134-
let this_module = match self.directory.path.file_name() {
5135-
Some(file_name) => file_name.to_str().unwrap().to_owned(),
5136-
None => self.root_module_name.as_ref().unwrap().clone(),
5137-
};
5138-
err.span_note(id_sp,
5139-
&format!("maybe move this module `{0}` to its own directory \
5140-
via `{0}/mod.rs`",
5141-
this_module));
5134+
if id_sp != syntax_pos::DUMMY_SP {
5135+
let full_path = self.sess.codemap().span_to_filename(id_sp);
5136+
let path = Path::new(&full_path);
5137+
let filename = path.file_stem().unwrap();
5138+
let parent = path.parent().unwrap_or(Path::new(""))
5139+
.to_str().unwrap_or("").to_owned();
5140+
let path = format!("{}/{}",
5141+
if parent.len() == 0 { "." } else { &parent },
5142+
filename.to_str().unwrap_or(""));
5143+
err.span_note(id_sp,
5144+
&format!("maybe move this module `{0}` to its own directory \
5145+
via `{0}/mod.rs`", path));
5146+
}
51425147
if paths.path_exists {
51435148
err.span_note(id_sp,
51445149
&format!("... or maybe `use` the module `{}` instead \
51455150
of possibly redeclaring it",
51465151
paths.name));
5147-
Err(err)
5148-
} else {
5149-
Err(err)
51505152
}
5153+
Err(err)
51515154
} else {
5152-
paths.result.map_err(|err| self.span_fatal_err(id_sp, err))
5155+
match paths.result {
5156+
Ok(succ) => Ok(succ),
5157+
Err(err) => Err(self.span_fatal_err(id_sp, &err.err_msg, &err.help_msg)),
5158+
}
51535159
}
51545160
}
51555161

0 commit comments

Comments
 (0)