Skip to content

Commit e2aaf38

Browse files
committed
functionalize
1 parent 397c51b commit e2aaf38

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,36 +2425,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24252425
} else {
24262426
let suggestion = if suggestion.is_some() {
24272427
suggestion
2428-
} else if let map = self.tcx.sess.source_map()
2429-
&& let Some(src) = map.span_to_filename(ident.span).into_local_path()
2430-
&& let i = ident.as_str()
2431-
// FIXME: add case where non parent using undeclared module (hard?)
2432-
&& let Some(dir) = src.parent()
2433-
&& let Some(src) = src.with_extension("").file_name().and_then(|x| x.to_str())
2434-
&& let Some(m) = [
2435-
// …/x.rs
2436-
dir.join(i).with_extension("rs"),
2437-
// …/x/mod.rs
2438-
dir.join(i).join("mod.rs"),
2439-
]
2440-
.iter()
2441-
.chain(
2442-
// in …/x.rs
2443-
matches!(src, "main" | "lib" | "mod")
2444-
.not()
2445-
.then(|| {
2446-
[
2447-
// …/x/y.rs
2448-
dir.join(src).join(i).with_extension("rs"),
2449-
// …/x/y/mod.rs
2450-
dir.join(src).join(i).join("mod.rs"),
2451-
]
2452-
})
2453-
.iter()
2454-
.flatten(),
2455-
)
2456-
.find(|x| x.exists())
2457-
{
2428+
} else if let Some(m) = self.undeclared_module_exists(ident) {
2429+
let map = self.tcx.sess.source_map();
24582430
let sp = map
24592431
.span_extend_to_prev_char(
24602432
map.span_extend_to_prev_char(ident.span, '\n', true),
@@ -2492,6 +2464,40 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24922464
}
24932465
}
24942466

2467+
fn undeclared_module_exists(&mut self, ident: Ident) -> Option<std::path::PathBuf> {
2468+
let map = self.tcx.sess.source_map();
2469+
2470+
let src = map.span_to_filename(ident.span).into_local_path()?;
2471+
let i = ident.as_str();
2472+
// FIXME: add case where non parent using undeclared module (hard?)
2473+
let dir = src.parent()?;
2474+
let src = src.file_stem()?.to_str()?;
2475+
[
2476+
// …/x.rs
2477+
dir.join(i).with_extension("rs"),
2478+
// …/x/mod.rs
2479+
dir.join(i).join("mod.rs"),
2480+
]
2481+
.iter()
2482+
.chain(
2483+
// in …/x.rs
2484+
matches!(src, "main" | "lib" | "mod")
2485+
.not()
2486+
.then(|| {
2487+
[
2488+
// …/x/y.rs
2489+
dir.join(src).join(i).with_extension("rs"),
2490+
// …/x/y/mod.rs
2491+
dir.join(src).join(i).join("mod.rs"),
2492+
]
2493+
})
2494+
.iter()
2495+
.flatten(),
2496+
)
2497+
.find(|x| x.exists())
2498+
.cloned()
2499+
}
2500+
24952501
/// Adds suggestions for a path that cannot be resolved.
24962502
#[instrument(level = "debug", skip(self, parent_scope))]
24972503
pub(crate) fn make_path_suggestion(

0 commit comments

Comments
 (0)