Skip to content

Commit 470c2ca

Browse files
authored
Unrolled build for #142730
Rollup merge of #142730 - bend-n:suggest_declaring_modules_when_file_found_but_module_not_defined, r=petrochenkov suggest declaring modules when file found but module not defined suggests declaring modules when a module is found but not defined, i.e ``` ├── main.rs: `use thing::thang;` └── thing.rs: `struct thang` ``` or ``` ├── main.rs: `use thing::thang;` └── thing └── mod.rs: `struct thang` ``` which currently is just ```rust error[E0432]: unresolved import `yeah` --> src/main.rs:1:1 | 1 | use thing::thang; | ^^^^^ use of unresolved module or unlinked crate `thing` | ``` but now would have this nice help: ```text = help: you may have forgotten to declare the module `thing`. use `mod thing` in this file to declare this module. ```
2 parents bdaba05 + 57cb419 commit 470c2ca

File tree

10 files changed

+119
-1
lines changed

10 files changed

+119
-1
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,6 +2421,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24212421
} else {
24222422
let suggestion = if suggestion.is_some() {
24232423
suggestion
2424+
} else if let Some(m) = self.undeclared_module_exists(ident) {
2425+
self.undeclared_module_suggest_declare(ident, m)
24242426
} else if was_invoked_from_cargo() {
24252427
Some((
24262428
vec![],
@@ -2442,6 +2444,55 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24422444
}
24432445
}
24442446

2447+
fn undeclared_module_suggest_declare(
2448+
&mut self,
2449+
ident: Ident,
2450+
path: std::path::PathBuf,
2451+
) -> Option<(Vec<(Span, String)>, String, Applicability)> {
2452+
Some((
2453+
vec![(self.current_crate_outer_attr_insert_span, format!("mod {ident};\n"))],
2454+
format!(
2455+
"to make use of source file {}, use `mod {ident}` \
2456+
in this file to declare the module",
2457+
path.display()
2458+
),
2459+
Applicability::MaybeIncorrect,
2460+
))
2461+
}
2462+
2463+
fn undeclared_module_exists(&mut self, ident: Ident) -> Option<std::path::PathBuf> {
2464+
let map = self.tcx.sess.source_map();
2465+
2466+
let src = map.span_to_filename(ident.span).into_local_path()?;
2467+
let i = ident.as_str();
2468+
// FIXME: add case where non parent using undeclared module (hard?)
2469+
let dir = src.parent()?;
2470+
let src = src.file_stem()?.to_str()?;
2471+
for file in [
2472+
// …/x.rs
2473+
dir.join(i).with_extension("rs"),
2474+
// …/x/mod.rs
2475+
dir.join(i).join("mod.rs"),
2476+
] {
2477+
if file.exists() {
2478+
return Some(file);
2479+
}
2480+
}
2481+
if !matches!(src, "main" | "lib" | "mod") {
2482+
for file in [
2483+
// …/x/y.rs
2484+
dir.join(src).join(i).with_extension("rs"),
2485+
// …/x/y/mod.rs
2486+
dir.join(src).join(i).join("mod.rs"),
2487+
] {
2488+
if file.exists() {
2489+
return Some(file);
2490+
}
2491+
}
2492+
}
2493+
None
2494+
}
2495+
24452496
/// Adds suggestions for a path that cannot be resolved.
24462497
#[instrument(level = "debug", skip(self, parent_scope))]
24472498
pub(crate) fn make_path_suggestion(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//@ ignore-auxiliary
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//@ ignore-auxiliary
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//@ ignore-auxiliary
2+
3+
use submodule3::ferris; // these modules are unresolved.
4+
use submodule4::error;

tests/ui/modules/module_suggestion_when_module_not_found/success/compiletest-ignore-dir

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//@ edition:2024
2+
use submodule::cat; //~ ERROR unresolved import `submodule`
3+
use submodule2::help; //~ ERROR unresolved import `submodule2`
4+
mod success;
5+
fn main() {}
6+
//~? ERROR unresolved import `submodule3`
7+
//~? ERROR unresolved import `submodule4`
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
error[E0432]: unresolved import `submodule`
2+
--> $DIR/suggestion.rs:2:5
3+
|
4+
LL | use submodule::cat;
5+
| ^^^^^^^^^ use of unresolved module or unlinked crate `submodule`
6+
|
7+
help: to make use of source file $DIR/submodule/mod.rs, use `mod submodule` in this file to declare the module
8+
|
9+
LL + mod submodule;
10+
|
11+
12+
error[E0432]: unresolved import `submodule2`
13+
--> $DIR/suggestion.rs:3:5
14+
|
15+
LL | use submodule2::help;
16+
| ^^^^^^^^^^ use of unresolved module or unlinked crate `submodule2`
17+
|
18+
help: to make use of source file $DIR/submodule2.rs, use `mod submodule2` in this file to declare the module
19+
|
20+
LL + mod submodule2;
21+
|
22+
23+
error[E0432]: unresolved import `submodule3`
24+
--> $DIR/success.rs:3:5
25+
|
26+
LL | use submodule3::ferris; // these modules are unresolved.
27+
| ^^^^^^^^^^ use of unresolved module or unlinked crate `submodule3`
28+
|
29+
help: to make use of source file $DIR/success/submodule3/mod.rs, use `mod submodule3` in this file to declare the module
30+
--> $DIR/suggestion.rs:2:1
31+
|
32+
LL + mod submodule3;
33+
|
34+
35+
error[E0432]: unresolved import `submodule4`
36+
--> $DIR/success.rs:4:5
37+
|
38+
LL | use submodule4::error;
39+
| ^^^^^^^^^^ use of unresolved module or unlinked crate `submodule4`
40+
|
41+
help: to make use of source file $DIR/success/submodule4.rs, use `mod submodule4` in this file to declare the module
42+
--> $DIR/suggestion.rs:2:1
43+
|
44+
LL + mod submodule4;
45+
|
46+
47+
error: aborting due to 4 previous errors
48+
49+
For more information about this error, try `rustc --explain E0432`.

tests/ui/modules_and_files_visibility/mod_file_disambig.stderr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ error[E0433]: failed to resolve: use of unresolved module or unlinked crate `mod
1212
LL | assert_eq!(mod_file_aux::bar(), 10);
1313
| ^^^^^^^^^^^^ use of unresolved module or unlinked crate `mod_file_aux`
1414
|
15-
= help: you might be missing a crate named `mod_file_aux`
15+
help: to make use of source file $DIR/mod_file_aux.rs, use `mod mod_file_aux` in this file to declare the module
16+
|
17+
LL + mod mod_file_aux;
18+
|
1619

1720
error: aborting due to 2 previous errors
1821

0 commit comments

Comments
 (0)