Skip to content

Commit eef284b

Browse files
committed
Tweak ordering of suggestions
Modify logic to make it easier to follow and recover labels that would otherwise be lost.
1 parent 18f3be7 commit eef284b

13 files changed

+131
-87
lines changed

src/librustc_resolve/late/diagnostics.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,6 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
339339

340340
// Try Levenshtein algorithm.
341341
let typo_sugg = self.lookup_typo_candidate(path, ns, is_expected, span);
342-
let levenshtein_worked = self.r.add_typo_suggestion(&mut err, typo_sugg, ident_span);
343-
344342
// Try context-dependent help if relaxed lookup didn't work.
345343
if let Some(res) = res {
346344
if self.smart_resolve_context_dependent_help(
@@ -351,14 +349,18 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
351349
&path_str,
352350
&fallback_label,
353351
) {
352+
// We do this to avoid losing a secondary span when we override the main error span.
353+
self.r.add_typo_suggestion(&mut err, typo_sugg, ident_span);
354354
return (err, candidates);
355355
}
356356
}
357357

358-
// Fallback label.
359-
if !levenshtein_worked {
358+
if !self.type_ascription_suggestion(&mut err, base_span)
359+
&& !self.r.add_typo_suggestion(&mut err, typo_sugg, ident_span)
360+
{
361+
// Fallback label.
360362
err.span_label(base_span, fallback_label);
361-
self.type_ascription_suggestion(&mut err, base_span);
363+
362364
match self.diagnostic_metadata.current_let_binding {
363365
Some((pat_sp, Some(ty_sp), None)) if ty_sp.contains(base_span) && could_be_expr => {
364366
err.span_suggestion_short(
@@ -869,7 +871,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
869871
start.to(sm.next_point(start))
870872
}
871873

872-
fn type_ascription_suggestion(&self, err: &mut DiagnosticBuilder<'_>, base_span: Span) {
874+
fn type_ascription_suggestion(&self, err: &mut DiagnosticBuilder<'_>, base_span: Span) -> bool {
873875
let sm = self.r.session.source_map();
874876
let base_snippet = sm.span_to_snippet(base_span);
875877
if let Some(&sp) = self.diagnostic_metadata.current_type_ascription.last() {
@@ -939,9 +941,11 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
939941
"expecting a type here because of type ascription",
940942
);
941943
}
944+
return show_label;
942945
}
943946
}
944947
}
948+
false
945949
}
946950

947951
fn find_module(&mut self, def_id: DefId) -> Option<(Module<'a>, ImportSuggestion)> {

src/test/ui/empty/empty-struct-braces-expr.stderr

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ LL | let e1 = Empty1;
1212
LL | pub struct XEmpty2;
1313
| ------------------- similarly named unit struct `XEmpty2` defined here
1414
|
15-
help: a unit struct with a similar name exists
16-
|
17-
LL | let e1 = XEmpty2;
18-
| ^^^^^^^
1915
help: use struct literal syntax instead
2016
|
2117
LL | let e1 = Empty1 {};
2218
| ^^^^^^^^^
19+
help: a unit struct with a similar name exists
20+
|
21+
LL | let e1 = XEmpty2;
22+
| ^^^^^^^
2323

2424
error[E0423]: expected function, tuple struct or tuple variant, found struct `Empty1`
2525
--> $DIR/empty-struct-braces-expr.rs:16:14
@@ -29,15 +29,20 @@ LL | struct Empty1 {}
2929
...
3030
LL | let e1 = Empty1();
3131
| ^^^^^^^^
32+
|
33+
::: $DIR/auxiliary/empty-struct.rs:2:1
3234
|
33-
help: a unit struct with a similar name exists
35+
LL | pub struct XEmpty2;
36+
| ------------------- similarly named unit struct `XEmpty2` defined here
3437
|
35-
LL | let e1 = XEmpty2();
36-
| ^^^^^^^
3738
help: use struct literal syntax instead
3839
|
3940
LL | let e1 = Empty1 {};
4041
| ^^^^^^^^^
42+
help: a unit struct with a similar name exists
43+
|
44+
LL | let e1 = XEmpty2();
45+
| ^^^^^^^
4146

4247
error[E0423]: expected value, found struct variant `E::Empty3`
4348
--> $DIR/empty-struct-braces-expr.rs:18:14
@@ -68,29 +73,34 @@ LL | let xe1 = XEmpty1;
6873
LL | pub struct XEmpty2;
6974
| ------------------- similarly named unit struct `XEmpty2` defined here
7075
|
71-
help: a unit struct with a similar name exists
72-
|
73-
LL | let xe1 = XEmpty2;
74-
| ^^^^^^^
7576
help: use struct literal syntax instead
7677
|
7778
LL | let xe1 = XEmpty1 {};
7879
| ^^^^^^^^^^
80+
help: a unit struct with a similar name exists
81+
|
82+
LL | let xe1 = XEmpty2;
83+
| ^^^^^^^
7984

8085
error[E0423]: expected function, tuple struct or tuple variant, found struct `XEmpty1`
8186
--> $DIR/empty-struct-braces-expr.rs:23:15
8287
|
8388
LL | let xe1 = XEmpty1();
8489
| ^^^^^^^^^
90+
|
91+
::: $DIR/auxiliary/empty-struct.rs:2:1
8592
|
86-
help: a unit struct with a similar name exists
93+
LL | pub struct XEmpty2;
94+
| ------------------- similarly named unit struct `XEmpty2` defined here
8795
|
88-
LL | let xe1 = XEmpty2();
89-
| ^^^^^^^
9096
help: use struct literal syntax instead
9197
|
9298
LL | let xe1 = XEmpty1 {};
9399
| ^^^^^^^^^^
100+
help: a unit struct with a similar name exists
101+
|
102+
LL | let xe1 = XEmpty2();
103+
| ^^^^^^^
94104

95105
error[E0599]: no variant or associated item named `Empty3` found for enum `empty_struct::XE` in the current scope
96106
--> $DIR/empty-struct-braces-expr.rs:25:19

src/test/ui/empty/empty-struct-braces-pat-1.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ LL | XE::XEmpty3 => ()
1818
LL | XEmpty4,
1919
| ------- similarly named unit variant `XEmpty4` defined here
2020
|
21-
help: a unit variant with a similar name exists
22-
|
23-
LL | XE::XEmpty4 => ()
24-
| ^^^^^^^
2521
help: use struct pattern syntax instead
2622
|
2723
LL | XE::XEmpty3 { /* fields */ } => ()
2824
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25+
help: a unit variant with a similar name exists
26+
|
27+
LL | XE::XEmpty4 => ()
28+
| ^^^^^^^
2929

3030
error: aborting due to 2 previous errors
3131

src/test/ui/empty/empty-struct-braces-pat-2.stderr

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,40 @@ LL | struct Empty1 {}
66
...
77
LL | Empty1() => ()
88
| ^^^^^^^^
9+
|
10+
::: $DIR/auxiliary/empty-struct.rs:3:1
911
|
10-
help: a tuple struct with a similar name exists
12+
LL | pub struct XEmpty6();
13+
| --------------------- similarly named tuple struct `XEmpty6` defined here
1114
|
12-
LL | XEmpty6() => ()
13-
| ^^^^^^^
1415
help: use struct pattern syntax instead
1516
|
1617
LL | Empty1 {} => ()
1718
| ^^^^^^^^^
19+
help: a tuple struct with a similar name exists
20+
|
21+
LL | XEmpty6() => ()
22+
| ^^^^^^^
1823

1924
error[E0532]: expected tuple struct or tuple variant, found struct `XEmpty1`
2025
--> $DIR/empty-struct-braces-pat-2.rs:18:9
2126
|
2227
LL | XEmpty1() => ()
2328
| ^^^^^^^^^
29+
|
30+
::: $DIR/auxiliary/empty-struct.rs:3:1
2431
|
25-
help: a tuple struct with a similar name exists
32+
LL | pub struct XEmpty6();
33+
| --------------------- similarly named tuple struct `XEmpty6` defined here
2634
|
27-
LL | XEmpty6() => ()
28-
| ^^^^^^^
2935
help: use struct pattern syntax instead
3036
|
3137
LL | XEmpty1 {} => ()
3238
| ^^^^^^^^^^
39+
help: a tuple struct with a similar name exists
40+
|
41+
LL | XEmpty6() => ()
42+
| ^^^^^^^
3343

3444
error[E0532]: expected tuple struct or tuple variant, found struct `Empty1`
3545
--> $DIR/empty-struct-braces-pat-2.rs:21:9
@@ -39,30 +49,40 @@ LL | struct Empty1 {}
3949
...
4050
LL | Empty1(..) => ()
4151
| ^^^^^^^^^^
52+
|
53+
::: $DIR/auxiliary/empty-struct.rs:3:1
4254
|
43-
help: a tuple struct with a similar name exists
55+
LL | pub struct XEmpty6();
56+
| --------------------- similarly named tuple struct `XEmpty6` defined here
4457
|
45-
LL | XEmpty6(..) => ()
46-
| ^^^^^^^
4758
help: use struct pattern syntax instead
4859
|
4960
LL | Empty1 {} => ()
5061
| ^^^^^^^^^
62+
help: a tuple struct with a similar name exists
63+
|
64+
LL | XEmpty6(..) => ()
65+
| ^^^^^^^
5166

5267
error[E0532]: expected tuple struct or tuple variant, found struct `XEmpty1`
5368
--> $DIR/empty-struct-braces-pat-2.rs:24:9
5469
|
5570
LL | XEmpty1(..) => ()
5671
| ^^^^^^^^^^^
72+
|
73+
::: $DIR/auxiliary/empty-struct.rs:3:1
5774
|
58-
help: a tuple struct with a similar name exists
75+
LL | pub struct XEmpty6();
76+
| --------------------- similarly named tuple struct `XEmpty6` defined here
5977
|
60-
LL | XEmpty6(..) => ()
61-
| ^^^^^^^
6278
help: use struct pattern syntax instead
6379
|
6480
LL | XEmpty1 {} => ()
6581
| ^^^^^^^^^^
82+
help: a tuple struct with a similar name exists
83+
|
84+
LL | XEmpty6(..) => ()
85+
| ^^^^^^^
6686

6787
error: aborting due to 4 previous errors
6888

src/test/ui/empty/empty-struct-braces-pat-3.stderr

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,20 @@ error[E0532]: expected tuple struct or tuple variant, found struct variant `XE::
1212
|
1313
LL | XE::XEmpty3() => ()
1414
| ^^^^^^^^^^^^^
15+
|
16+
::: $DIR/auxiliary/empty-struct.rs:8:5
1517
|
16-
help: a tuple variant with a similar name exists
18+
LL | XEmpty5(),
19+
| --------- similarly named tuple variant `XEmpty5` defined here
1720
|
18-
LL | XE::XEmpty5() => ()
19-
| ^^^^^^^
2021
help: use struct pattern syntax instead
2122
|
2223
LL | XE::XEmpty3 { /* fields */ } => ()
2324
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25+
help: a tuple variant with a similar name exists
26+
|
27+
LL | XE::XEmpty5() => ()
28+
| ^^^^^^^
2429

2530
error[E0532]: expected tuple struct or tuple variant, found struct variant `E::Empty3`
2631
--> $DIR/empty-struct-braces-pat-3.rs:25:9
@@ -36,15 +41,20 @@ error[E0532]: expected tuple struct or tuple variant, found struct variant `XE::
3641
|
3742
LL | XE::XEmpty3(..) => ()
3843
| ^^^^^^^^^^^^^^^
44+
|
45+
::: $DIR/auxiliary/empty-struct.rs:8:5
3946
|
40-
help: a tuple variant with a similar name exists
47+
LL | XEmpty5(),
48+
| --------- similarly named tuple variant `XEmpty5` defined here
4149
|
42-
LL | XE::XEmpty5(..) => ()
43-
| ^^^^^^^
4450
help: use struct pattern syntax instead
4551
|
4652
LL | XE::XEmpty3 { /* fields */ } => ()
4753
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
54+
help: a tuple variant with a similar name exists
55+
|
56+
LL | XE::XEmpty5(..) => ()
57+
| ^^^^^^^
4858

4959
error: aborting due to 4 previous errors
5060

src/test/ui/error-codes/E0423.stderr

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,18 @@ LL | struct Foo { a: bool };
3434
LL |
3535
LL | let f = Foo();
3636
| ^^^^^
37+
...
38+
LL | fn foo() {
39+
| -------- similarly named function `foo` defined here
3740
|
38-
help: a function with a similar name exists
39-
|
40-
LL | let f = foo();
41-
| ^^^
4241
help: use struct literal syntax instead
4342
|
4443
LL | let f = Foo { a: val };
4544
| ^^^^^^^^^^^^^^
45+
help: a function with a similar name exists
46+
|
47+
LL | let f = foo();
48+
| ^^^
4649

4750
error[E0423]: expected value, found struct `T`
4851
--> $DIR/E0423.rs:14:8

src/test/ui/issues/issue-64792-bad-unicode-ctor.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ LL | struct X {}
55
| ----------- `X` defined here
66
LL |
77
LL | const Y: X = X("ö");
8-
| ^^^^^^
8+
| -------------^^^^^^- similarly named constant `Y` defined here
99
|
10-
help: a constant with a similar name exists
11-
|
12-
LL | const Y: X = Y("ö");
13-
| ^
1410
help: use struct literal syntax instead
1511
|
1612
LL | const Y: X = X {};
1713
| ^^^^
14+
help: a constant with a similar name exists
15+
|
16+
LL | const Y: X = Y("ö");
17+
| ^
1818

1919
error: aborting due to previous error
2020

src/test/ui/namespace/namespace-mix.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ LL | TV(),
5353
LL | check(m7::V);
5454
| ^^^^^
5555
|
56-
help: a tuple variant with a similar name exists
57-
|
58-
LL | check(m7::TV);
59-
| ^^
6056
help: use struct literal syntax instead
6157
|
6258
LL | check(m7::V {});
6359
| ^^^^^^^^
60+
help: a tuple variant with a similar name exists
61+
|
62+
LL | check(m7::TV);
63+
| ^^
6464
help: consider importing one of these items instead
6565
|
6666
LL | use m8::V;
@@ -79,14 +79,14 @@ LL | check(xm7::V);
7979
LL | TV(),
8080
| ---- similarly named tuple variant `TV` defined here
8181
|
82-
help: a tuple variant with a similar name exists
83-
|
84-
LL | check(xm7::TV);
85-
| ^^
8682
help: use struct literal syntax instead
8783
|
8884
LL | check(xm7::V { /* fields */ });
8985
| ^^^^^^^^^^^^^^^^^^^^^^^
86+
help: a tuple variant with a similar name exists
87+
|
88+
LL | check(xm7::TV);
89+
| ^^
9090
help: consider importing one of these items instead
9191
|
9292
LL | use m8::V;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ LL | struct Handle {}
77
LL | handle: Handle
88
| ^^^^^^
99
|
10-
help: a local variable with a similar name exists
11-
|
12-
LL | handle: handle
13-
| ^^^^^^
1410
help: use struct literal syntax instead
1511
|
1612
LL | handle: Handle {}
1713
| ^^^^^^^^^
14+
help: a local variable with a similar name exists
15+
|
16+
LL | handle: handle
17+
| ^^^^^^
1818

1919
error: aborting due to previous error
2020

0 commit comments

Comments
 (0)