Skip to content

Commit 9b3364f

Browse files
committed
extract error_cannot_declare_mod_here
1 parent f2cbe30 commit 9b3364f

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

src/librustc_parse/parser/module.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -142,41 +142,41 @@ impl<'a> Parser<'a> {
142142
}
143143
Err(err)
144144
}
145-
DirectoryOwnership::UnownedViaMod => {
146-
let mut err =
147-
self.struct_span_err(id_sp, "cannot declare a new module at this location");
148-
if !id_sp.is_dummy() {
149-
let src_path = self.sess.source_map().span_to_filename(id_sp);
150-
if let FileName::Real(src_path) = src_path {
151-
if let Some(stem) = src_path.file_stem() {
152-
let mut dest_path = src_path.clone();
153-
dest_path.set_file_name(stem);
154-
dest_path.push("mod.rs");
155-
err.span_note(
156-
id_sp,
157-
&format!(
158-
"maybe move this module `{}` to its own \
159-
directory via `{}`",
160-
src_path.display(),
161-
dest_path.display()
162-
),
163-
);
164-
}
165-
}
166-
}
167-
if paths.path_exists {
145+
DirectoryOwnership::UnownedViaMod => self.error_cannot_declare_mod_here(id_sp, paths),
146+
}
147+
}
148+
149+
fn error_cannot_declare_mod_here<T>(&self, id_sp: Span, paths: ModulePath) -> PResult<'a, T> {
150+
let mut err = self.struct_span_err(id_sp, "cannot declare a new module at this location");
151+
if !id_sp.is_dummy() {
152+
if let FileName::Real(src_path) = self.sess.source_map().span_to_filename(id_sp) {
153+
if let Some(stem) = src_path.file_stem() {
154+
let mut dest_path = src_path.clone();
155+
dest_path.set_file_name(stem);
156+
dest_path.push("mod.rs");
168157
err.span_note(
169158
id_sp,
170159
&format!(
171-
"... or maybe `use` the module `{}` instead \
172-
of possibly redeclaring it",
173-
paths.name
160+
"maybe move this module `{}` to its own \
161+
directory via `{}`",
162+
src_path.display(),
163+
dest_path.display()
174164
),
175165
);
176166
}
177-
Err(err)
178167
}
179168
}
169+
if paths.path_exists {
170+
err.span_note(
171+
id_sp,
172+
&format!(
173+
"... or maybe `use` the module `{}` instead \
174+
of possibly redeclaring it",
175+
paths.name
176+
),
177+
);
178+
}
179+
Err(err)
180180
}
181181

182182
/// Derive a submodule path from the first found `#[path = "path_string"]`.

0 commit comments

Comments
 (0)