Skip to content

Commit b4d3ed6

Browse files
committed
Auto merge of #39291 - Freyskeyd:check_context_E0423, r=petrochenkov
Checker:: Execute levenshtein before other context checking As explain [here]() i think it's better to check for a miss typing before checking context dependent help. ```rust struct Handle {} struct Something { handle: Handle } fn main() { let handle: Handle = Handle {}; let s: Something = Something { // Checker detect an error and propose a solution with `Handle { /* ... */ }` // but it's a miss typing of `handle` handle: Handle }; } ``` Ping: @nagisa for #39226 Signed-off-by: Freyskeyd <[email protected]>
2 parents e326e86 + 0d7e6cf commit b4d3ed6

7 files changed

+65
-14
lines changed

src/librustc_resolve/lib.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -2312,6 +2312,14 @@ impl<'a> Resolver<'a> {
23122312
}
23132313
}
23142314

2315+
let mut levenshtein_worked = false;
2316+
2317+
// Try Levenshtein.
2318+
if let Some(candidate) = this.lookup_typo_candidate(path, ns, is_expected) {
2319+
err.span_label(ident_span, &format!("did you mean `{}`?", candidate));
2320+
levenshtein_worked = true;
2321+
}
2322+
23152323
// Try context dependent help if relaxed lookup didn't work.
23162324
if let Some(def) = def {
23172325
match (def, source) {
@@ -2354,14 +2362,10 @@ impl<'a> Resolver<'a> {
23542362
}
23552363
}
23562364

2357-
// Try Levenshtein if nothing else worked.
2358-
if let Some(candidate) = this.lookup_typo_candidate(path, ns, is_expected) {
2359-
err.span_label(ident_span, &format!("did you mean `{}`?", candidate));
2360-
return err;
2361-
}
2362-
23632365
// Fallback label.
2364-
err.span_label(base_span, &fallback_label);
2366+
if !levenshtein_worked {
2367+
err.span_label(base_span, &fallback_label);
2368+
}
23652369
err
23662370
};
23672371
let report_errors = |this: &mut Self, def: Option<Def>| {

src/test/ui/resolve/issue-39226.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
struct Handle {}
11+
12+
struct Something {
13+
handle: Handle
14+
}
15+
16+
fn main() {
17+
let handle: Handle = Handle {};
18+
19+
let s: Something = Something {
20+
handle: Handle
21+
//~^ ERROR cannot find value `Handle` in this scope
22+
//~| NOTE did you mean `handle`?
23+
};
24+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0423]: expected value, found struct `Handle`
2+
--> $DIR/issue-39226.rs:20:17
3+
|
4+
20 | handle: Handle
5+
| ^^^^^^
6+
| |
7+
| did you mean `handle`?
8+
| did you mean `Handle { /* fields */ }`?
9+
10+
error: aborting due to previous error
11+

src/test/ui/resolve/issue-5035.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ error[E0404]: expected trait, found type alias `K`
88
--> $DIR/issue-5035.rs:13:6
99
|
1010
13 | impl K for isize {} //~ ERROR expected trait, found type alias `K`
11-
| ^ type aliases cannot be used for traits
11+
| ^
12+
| |
13+
| type aliases cannot be used for traits
14+
| did you mean `I`?
1215

1316
error: cannot continue compilation due to previous error
1417

src/test/ui/resolve/privacy-struct-ctor.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ error[E0423]: expected value, found struct `Z`
55
| ^
66
| |
77
| did you mean `Z { /* fields */ }`?
8+
| did you mean `S`?
89
| constructor is not visible here due to private fields
910
|
1011
= help: possible better candidate is found in another module, you can import it into scope:

src/test/ui/resolve/suggest-path-instead-of-mod-dot-item.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ error[E0423]: expected value, found module `a::b`
2626
--> $DIR/suggest-path-instead-of-mod-dot-item.rs:45:5
2727
|
2828
45 | a::b.J
29-
| ^^^^--
30-
| |
29+
| ^^^---
30+
| | |
31+
| | did you mean `I`?
3132
| did you mean `a::b::J`?
3233

3334
error[E0423]: expected value, found module `a`
@@ -50,8 +51,9 @@ error[E0423]: expected value, found module `a::b`
5051
--> $DIR/suggest-path-instead-of-mod-dot-item.rs:61:5
5152
|
5253
61 | a::b.f()
53-
| ^^^^----
54-
| |
54+
| ^^^-----
55+
| | |
56+
| | did you mean `I`?
5557
| did you mean `a::b::f(...)`?
5658

5759
error[E0423]: expected value, found module `a::b`

src/test/ui/resolve/tuple-struct-alias.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@ error[E0423]: expected function, found type alias `A`
1414
--> $DIR/tuple-struct-alias.rs:24:13
1515
|
1616
24 | let s = A(0, 1);
17-
| ^ did you mean `A { /* fields */ }`?
17+
| ^
18+
| |
19+
| did you mean `S`?
20+
| did you mean `A { /* fields */ }`?
1821

1922
error[E0532]: expected tuple struct/variant, found type alias `A`
2023
--> $DIR/tuple-struct-alias.rs:26:9
2124
|
2225
26 | A(..) => {}
23-
| ^ did you mean `A { /* fields */ }`?
26+
| ^
27+
| |
28+
| did you mean `S`?
29+
| did you mean `A { /* fields */ }`?
2430

2531
error: aborting due to 4 previous errors
2632

0 commit comments

Comments
 (0)