about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJubilee <46493976+workingjubilee@users.noreply.github.com>2024-07-12 13:47:07 -0700
committerGitHub <noreply@github.com>2024-07-12 13:47:07 -0700
commitafb2fbf6926e64479f885295fa0a7da27f4040a2 (patch)
tree3e1fd37b8805a40277007ea20656e1594ce8496c
parenta6a7129827f942a58783feec4320807774dae6ec (diff)
parentfe5581dd31eb1b86c915e5c3eaae9ebfdea28037 (diff)
downloadrust-afb2fbf6926e64479f885295fa0a7da27f4040a2.tar.gz
rust-afb2fbf6926e64479f885295fa0a7da27f4040a2.zip
Rollup merge of #127310 - chenyukang:yukang-fix-suggest-import-ice, r=estebank
Fix import suggestion ice

Fixes #127302

#127302 only crash in edition 2015
#120074 can only reproduced in edition 2021
so I added revisions in test file.
-rw-r--r--compiler/rustc_resolve/src/diagnostics.rs10
-rw-r--r--tests/ui/imports/suggest-import-ice-issue-127302.edition2015.stderr41
-rw-r--r--tests/ui/imports/suggest-import-ice-issue-127302.edition2021.stderr41
-rw-r--r--tests/ui/imports/suggest-import-ice-issue-127302.rs12
-rw-r--r--tests/ui/imports/suggest-import-issue-120074.edition2015.stderr (renamed from tests/ui/imports/suggest-import-issue-120074.stderr)2
-rw-r--r--tests/ui/imports/suggest-import-issue-120074.edition2021.stderr23
-rw-r--r--tests/ui/imports/suggest-import-issue-120074.rs2
7 files changed, 125 insertions, 6 deletions
diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs
index 09221041031..e3917acce65 100644
--- a/compiler/rustc_resolve/src/diagnostics.rs
+++ b/compiler/rustc_resolve/src/diagnostics.rs
@@ -1992,12 +1992,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
             if let Some(candidate) = candidates.get(0) {
                 let path = {
                     // remove the possible common prefix of the path
-                    let start_index = (0..failed_segment_idx)
-                        .find(|&i| path[i].ident != candidate.path.segments[i].ident)
+                    let len = candidate.path.segments.len();
+                    let start_index = (0..=failed_segment_idx.min(len - 1))
+                        .find(|&i| path[i].ident.name != candidate.path.segments[i].ident.name)
                         .unwrap_or_default();
-                    let segments = (start_index..=failed_segment_idx)
-                        .map(|s| candidate.path.segments[s].clone())
-                        .collect();
+                    let segments =
+                        (start_index..len).map(|s| candidate.path.segments[s].clone()).collect();
                     Path { segments, span: Span::default(), tokens: None }
                 };
                 (
diff --git a/tests/ui/imports/suggest-import-ice-issue-127302.edition2015.stderr b/tests/ui/imports/suggest-import-ice-issue-127302.edition2015.stderr
new file mode 100644
index 00000000000..24574c8796b
--- /dev/null
+++ b/tests/ui/imports/suggest-import-ice-issue-127302.edition2015.stderr
@@ -0,0 +1,41 @@
+error[E0583]: file not found for module `config`
+  --> $DIR/suggest-import-ice-issue-127302.rs:3:1
+   |
+LL | mod config;
+   | ^^^^^^^^^^^
+   |
+   = help: to create the module `config`, create file "$DIR/config.rs" or "$DIR/config/mod.rs"
+   = note: if there is a `mod config` elsewhere in the crate already, import it with `use crate::...` instead
+
+error: format argument must be a string literal
+  --> $DIR/suggest-import-ice-issue-127302.rs:10:14
+   |
+LL |     println!(args.ctx.compiler.display());
+   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+help: you might be missing a string literal to format with
+   |
+LL |     println!("{}", args.ctx.compiler.display());
+   |              +++++
+
+error[E0425]: cannot find value `args` in this scope
+  --> $DIR/suggest-import-ice-issue-127302.rs:6:12
+   |
+LL |     match &args.cmd {
+   |            ^^^^ not found in this scope
+   |
+help: consider importing this function
+   |
+LL + use std::env::args;
+   |
+
+error[E0532]: expected unit struct, unit variant or constant, found module `crate::config`
+  --> $DIR/suggest-import-ice-issue-127302.rs:7:9
+   |
+LL |         crate::config => {}
+   |         ^^^^^^^^^^^^^ not a unit struct, unit variant or constant
+
+error: aborting due to 4 previous errors
+
+Some errors have detailed explanations: E0425, E0532, E0583.
+For more information about an error, try `rustc --explain E0425`.
diff --git a/tests/ui/imports/suggest-import-ice-issue-127302.edition2021.stderr b/tests/ui/imports/suggest-import-ice-issue-127302.edition2021.stderr
new file mode 100644
index 00000000000..24574c8796b
--- /dev/null
+++ b/tests/ui/imports/suggest-import-ice-issue-127302.edition2021.stderr
@@ -0,0 +1,41 @@
+error[E0583]: file not found for module `config`
+  --> $DIR/suggest-import-ice-issue-127302.rs:3:1
+   |
+LL | mod config;
+   | ^^^^^^^^^^^
+   |
+   = help: to create the module `config`, create file "$DIR/config.rs" or "$DIR/config/mod.rs"
+   = note: if there is a `mod config` elsewhere in the crate already, import it with `use crate::...` instead
+
+error: format argument must be a string literal
+  --> $DIR/suggest-import-ice-issue-127302.rs:10:14
+   |
+LL |     println!(args.ctx.compiler.display());
+   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+help: you might be missing a string literal to format with
+   |
+LL |     println!("{}", args.ctx.compiler.display());
+   |              +++++
+
+error[E0425]: cannot find value `args` in this scope
+  --> $DIR/suggest-import-ice-issue-127302.rs:6:12
+   |
+LL |     match &args.cmd {
+   |            ^^^^ not found in this scope
+   |
+help: consider importing this function
+   |
+LL + use std::env::args;
+   |
+
+error[E0532]: expected unit struct, unit variant or constant, found module `crate::config`
+  --> $DIR/suggest-import-ice-issue-127302.rs:7:9
+   |
+LL |         crate::config => {}
+   |         ^^^^^^^^^^^^^ not a unit struct, unit variant or constant
+
+error: aborting due to 4 previous errors
+
+Some errors have detailed explanations: E0425, E0532, E0583.
+For more information about an error, try `rustc --explain E0425`.
diff --git a/tests/ui/imports/suggest-import-ice-issue-127302.rs b/tests/ui/imports/suggest-import-ice-issue-127302.rs
new file mode 100644
index 00000000000..6b8d4c71831
--- /dev/null
+++ b/tests/ui/imports/suggest-import-ice-issue-127302.rs
@@ -0,0 +1,12 @@
+//@ revisions: edition2015 edition2021
+
+mod config; //~ ERROR file not found for module
+
+fn main() {
+    match &args.cmd { //~ ERROR cannot find value `args` in this scope
+        crate::config => {} //~ ERROR expected unit struct, unit variant or constant, found module `crate::config`
+    }
+
+    println!(args.ctx.compiler.display());
+    //~^ ERROR format argument must be a string literal
+}
diff --git a/tests/ui/imports/suggest-import-issue-120074.stderr b/tests/ui/imports/suggest-import-issue-120074.edition2015.stderr
index c1dff93bbdb..414eeee0fed 100644
--- a/tests/ui/imports/suggest-import-issue-120074.stderr
+++ b/tests/ui/imports/suggest-import-issue-120074.edition2015.stderr
@@ -1,5 +1,5 @@
 error[E0433]: failed to resolve: unresolved import
-  --> $DIR/suggest-import-issue-120074.rs:10:35
+  --> $DIR/suggest-import-issue-120074.rs:12:35
    |
 LL |     println!("Hello, {}!", crate::bar::do_the_thing);
    |                                   ^^^ unresolved import
diff --git a/tests/ui/imports/suggest-import-issue-120074.edition2021.stderr b/tests/ui/imports/suggest-import-issue-120074.edition2021.stderr
new file mode 100644
index 00000000000..414eeee0fed
--- /dev/null
+++ b/tests/ui/imports/suggest-import-issue-120074.edition2021.stderr
@@ -0,0 +1,23 @@
+error[E0433]: failed to resolve: unresolved import
+  --> $DIR/suggest-import-issue-120074.rs:12:35
+   |
+LL |     println!("Hello, {}!", crate::bar::do_the_thing);
+   |                                   ^^^ unresolved import
+   |
+help: a similar path exists
+   |
+LL |     println!("Hello, {}!", crate::foo::bar::do_the_thing);
+   |                                   ~~~~~~~~
+help: consider importing this module
+   |
+LL + use foo::bar;
+   |
+help: if you import `bar`, refer to it directly
+   |
+LL -     println!("Hello, {}!", crate::bar::do_the_thing);
+LL +     println!("Hello, {}!", bar::do_the_thing);
+   |
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0433`.
diff --git a/tests/ui/imports/suggest-import-issue-120074.rs b/tests/ui/imports/suggest-import-issue-120074.rs
index a798e9eeeb8..7b6b5c73103 100644
--- a/tests/ui/imports/suggest-import-issue-120074.rs
+++ b/tests/ui/imports/suggest-import-issue-120074.rs
@@ -1,3 +1,5 @@
+//@ revisions: edition2015 edition2021
+
 pub mod foo {
     pub mod bar {
         pub fn do_the_thing() -> usize {