1
Fork 0

extract error_cannot_declare_mod_here

This commit is contained in:
Mazdak Farrokhzad 2020-03-07 19:11:47 +01:00
parent bc75cba23f
commit 2899a58cab

View file

@ -142,41 +142,41 @@ impl<'a> Parser<'a> {
} }
Err(err) Err(err)
} }
DirectoryOwnership::UnownedViaMod => { DirectoryOwnership::UnownedViaMod => self.error_cannot_declare_mod_here(id_sp, paths),
let mut err = }
self.struct_span_err(id_sp, "cannot declare a new module at this location"); }
if !id_sp.is_dummy() {
let src_path = self.sess.source_map().span_to_filename(id_sp); fn error_cannot_declare_mod_here<T>(&self, id_sp: Span, paths: ModulePath) -> PResult<'a, T> {
if let FileName::Real(src_path) = src_path { let mut err = self.struct_span_err(id_sp, "cannot declare a new module at this location");
if let Some(stem) = src_path.file_stem() { if !id_sp.is_dummy() {
let mut dest_path = src_path.clone(); if let FileName::Real(src_path) = self.sess.source_map().span_to_filename(id_sp) {
dest_path.set_file_name(stem); if let Some(stem) = src_path.file_stem() {
dest_path.push("mod.rs"); let mut dest_path = src_path.clone();
err.span_note( dest_path.set_file_name(stem);
id_sp, dest_path.push("mod.rs");
&format!(
"maybe move this module `{}` to its own \
directory via `{}`",
src_path.display(),
dest_path.display()
),
);
}
}
}
if paths.path_exists {
err.span_note( err.span_note(
id_sp, id_sp,
&format!( &format!(
"... or maybe `use` the module `{}` instead \ "maybe move this module `{}` to its own \
of possibly redeclaring it", directory via `{}`",
paths.name src_path.display(),
dest_path.display()
), ),
); );
} }
Err(err)
} }
} }
if paths.path_exists {
err.span_note(
id_sp,
&format!(
"... or maybe `use` the module `{}` instead \
of possibly redeclaring it",
paths.name
),
);
}
Err(err)
} }
/// Derive a submodule path from the first found `#[path = "path_string"]`. /// Derive a submodule path from the first found `#[path = "path_string"]`.