Skip to content

Commit fe5581d

Browse files
committed
Fix import suggestion ice
1 parent f6fa358 commit fe5581d

7 files changed

+125
-6
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1989,12 +1989,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
19891989
if let Some(candidate) = candidates.get(0) {
19901990
let path = {
19911991
// remove the possible common prefix of the path
1992-
let start_index = (0..failed_segment_idx)
1993-
.find(|&i| path[i].ident != candidate.path.segments[i].ident)
1992+
let len = candidate.path.segments.len();
1993+
let start_index = (0..=failed_segment_idx.min(len - 1))
1994+
.find(|&i| path[i].ident.name != candidate.path.segments[i].ident.name)
19941995
.unwrap_or_default();
1995-
let segments = (start_index..=failed_segment_idx)
1996-
.map(|s| candidate.path.segments[s].clone())
1997-
.collect();
1996+
let segments =
1997+
(start_index..len).map(|s| candidate.path.segments[s].clone()).collect();
19981998
Path { segments, span: Span::default(), tokens: None }
19991999
};
20002000
(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
error[E0583]: file not found for module `config`
2+
--> $DIR/suggest-import-ice-issue-127302.rs:3:1
3+
|
4+
LL | mod config;
5+
| ^^^^^^^^^^^
6+
|
7+
= help: to create the module `config`, create file "$DIR/config.rs" or "$DIR/config/mod.rs"
8+
= note: if there is a `mod config` elsewhere in the crate already, import it with `use crate::...` instead
9+
10+
error: format argument must be a string literal
11+
--> $DIR/suggest-import-ice-issue-127302.rs:10:14
12+
|
13+
LL | println!(args.ctx.compiler.display());
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
|
16+
help: you might be missing a string literal to format with
17+
|
18+
LL | println!("{}", args.ctx.compiler.display());
19+
| +++++
20+
21+
error[E0425]: cannot find value `args` in this scope
22+
--> $DIR/suggest-import-ice-issue-127302.rs:6:12
23+
|
24+
LL | match &args.cmd {
25+
| ^^^^ not found in this scope
26+
|
27+
help: consider importing this function
28+
|
29+
LL + use std::env::args;
30+
|
31+
32+
error[E0532]: expected unit struct, unit variant or constant, found module `crate::config`
33+
--> $DIR/suggest-import-ice-issue-127302.rs:7:9
34+
|
35+
LL | crate::config => {}
36+
| ^^^^^^^^^^^^^ not a unit struct, unit variant or constant
37+
38+
error: aborting due to 4 previous errors
39+
40+
Some errors have detailed explanations: E0425, E0532, E0583.
41+
For more information about an error, try `rustc --explain E0425`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
error[E0583]: file not found for module `config`
2+
--> $DIR/suggest-import-ice-issue-127302.rs:3:1
3+
|
4+
LL | mod config;
5+
| ^^^^^^^^^^^
6+
|
7+
= help: to create the module `config`, create file "$DIR/config.rs" or "$DIR/config/mod.rs"
8+
= note: if there is a `mod config` elsewhere in the crate already, import it with `use crate::...` instead
9+
10+
error: format argument must be a string literal
11+
--> $DIR/suggest-import-ice-issue-127302.rs:10:14
12+
|
13+
LL | println!(args.ctx.compiler.display());
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
|
16+
help: you might be missing a string literal to format with
17+
|
18+
LL | println!("{}", args.ctx.compiler.display());
19+
| +++++
20+
21+
error[E0425]: cannot find value `args` in this scope
22+
--> $DIR/suggest-import-ice-issue-127302.rs:6:12
23+
|
24+
LL | match &args.cmd {
25+
| ^^^^ not found in this scope
26+
|
27+
help: consider importing this function
28+
|
29+
LL + use std::env::args;
30+
|
31+
32+
error[E0532]: expected unit struct, unit variant or constant, found module `crate::config`
33+
--> $DIR/suggest-import-ice-issue-127302.rs:7:9
34+
|
35+
LL | crate::config => {}
36+
| ^^^^^^^^^^^^^ not a unit struct, unit variant or constant
37+
38+
error: aborting due to 4 previous errors
39+
40+
Some errors have detailed explanations: E0425, E0532, E0583.
41+
For more information about an error, try `rustc --explain E0425`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ revisions: edition2015 edition2021
2+
3+
mod config; //~ ERROR file not found for module
4+
5+
fn main() {
6+
match &args.cmd { //~ ERROR cannot find value `args` in this scope
7+
crate::config => {} //~ ERROR expected unit struct, unit variant or constant, found module `crate::config`
8+
}
9+
10+
println!(args.ctx.compiler.display());
11+
//~^ ERROR format argument must be a string literal
12+
}

tests/ui/imports/suggest-import-issue-120074.stderr renamed to tests/ui/imports/suggest-import-issue-120074.edition2015.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0433]: failed to resolve: unresolved import
2-
--> $DIR/suggest-import-issue-120074.rs:10:35
2+
--> $DIR/suggest-import-issue-120074.rs:12:35
33
|
44
LL | println!("Hello, {}!", crate::bar::do_the_thing);
55
| ^^^ unresolved import
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0433]: failed to resolve: unresolved import
2+
--> $DIR/suggest-import-issue-120074.rs:12:35
3+
|
4+
LL | println!("Hello, {}!", crate::bar::do_the_thing);
5+
| ^^^ unresolved import
6+
|
7+
help: a similar path exists
8+
|
9+
LL | println!("Hello, {}!", crate::foo::bar::do_the_thing);
10+
| ~~~~~~~~
11+
help: consider importing this module
12+
|
13+
LL + use foo::bar;
14+
|
15+
help: if you import `bar`, refer to it directly
16+
|
17+
LL - println!("Hello, {}!", crate::bar::do_the_thing);
18+
LL + println!("Hello, {}!", bar::do_the_thing);
19+
|
20+
21+
error: aborting due to 1 previous error
22+
23+
For more information about this error, try `rustc --explain E0433`.

tests/ui/imports/suggest-import-issue-120074.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ revisions: edition2015 edition2021
2+
13
pub mod foo {
24
pub mod bar {
35
pub fn do_the_thing() -> usize {

0 commit comments

Comments
 (0)