Skip to content

Commit 867554a

Browse files
Fix suggesting turbofish with lifetime arguments
1 parent 256721e commit 867554a

File tree

8 files changed

+115
-61
lines changed

8 files changed

+115
-61
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+23-16
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use std::mem::take;
2727
use tracing::{debug, trace};
2828

2929
const TURBOFISH_SUGGESTION_STR: &str =
30-
"use `::<...>` instead of `<...>` to specify type or const arguments";
30+
"use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments";
3131

3232
/// Creates a placeholder argument.
3333
pub(super) fn dummy_arg(ident: Ident) -> Param {
@@ -731,21 +731,28 @@ impl<'a> Parser<'a> {
731731
match x {
732732
Ok((_, _, false)) => {
733733
if self.eat(&token::Gt) {
734-
match self.parse_expr() {
735-
Ok(_) => {
736-
e.span_suggestion_verbose(
737-
binop.span.shrink_to_lo(),
738-
TURBOFISH_SUGGESTION_STR,
739-
"::".to_string(),
740-
Applicability::MaybeIncorrect,
741-
);
742-
e.emit();
743-
*expr =
744-
self.mk_expr_err(expr.span.to(self.prev_token.span));
745-
return Ok(());
746-
}
747-
Err(mut err) => {
748-
err.cancel();
734+
let turbo_err = e.span_suggestion_verbose(
735+
binop.span.shrink_to_lo(),
736+
TURBOFISH_SUGGESTION_STR,
737+
"::".to_string(),
738+
Applicability::MaybeIncorrect,
739+
);
740+
if self.check(&TokenKind::Semi) {
741+
turbo_err.emit();
742+
*expr = self.mk_expr_err(expr.span);
743+
return Ok(());
744+
} else {
745+
match self.parse_expr() {
746+
Ok(_) => {
747+
turbo_err.emit();
748+
*expr = self
749+
.mk_expr_err(expr.span.to(self.prev_token.span));
750+
return Ok(());
751+
}
752+
Err(mut err) => {
753+
turbo_err.cancel();
754+
err.cancel();
755+
}
749756
}
750757
}
751758
}

compiler/rustc_parse/src/parser/expr.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ impl<'a> Parser<'a> {
14431443
&mut self,
14441444
label: Label,
14451445
attrs: AttrVec,
1446-
consume_colon: bool,
1446+
mut consume_colon: bool,
14471447
) -> PResult<'a, P<Expr>> {
14481448
let lo = label.ident.span;
14491449
let label = Some(label);
@@ -1456,6 +1456,12 @@ impl<'a> Parser<'a> {
14561456
self.parse_loop_expr(label, lo, attrs)
14571457
} else if self.check(&token::OpenDelim(token::Brace)) || self.token.is_whole_block() {
14581458
self.parse_block_expr(label, lo, BlockCheckMode::Default, attrs)
1459+
} else if !ate_colon && (self.check(&TokenKind::Comma) || self.check(&TokenKind::Gt)) {
1460+
// We're probably inside of a `Path<'a>` that needs a turbofish, so suppress the
1461+
// "must be followed by a colon" error.
1462+
self.diagnostic().delay_span_bug(lo, "this label wasn't parsed correctly");
1463+
consume_colon = false;
1464+
Ok(self.mk_expr_err(lo))
14591465
} else {
14601466
let msg = "expected `while`, `for`, `loop` or `{` after a label";
14611467
self.struct_span_err(self.token.span, msg).span_label(self.token.span, msg).emit();

src/test/ui/const-generics/min_const_generics/const-expression-suggest-missing-braces-without-turbofish.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: comparison operators cannot be chained
44
LL | foo<BAR + 3>();
55
| ^ ^
66
|
7-
help: use `::<...>` instead of `<...>` to specify type or const arguments
7+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
88
|
99
LL | foo::<BAR + 3>();
1010
| ++
@@ -15,7 +15,7 @@ error: comparison operators cannot be chained
1515
LL | foo<BAR + BAR>();
1616
| ^ ^
1717
|
18-
help: use `::<...>` instead of `<...>` to specify type or const arguments
18+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
1919
|
2020
LL | foo::<BAR + BAR>();
2121
| ++
@@ -26,7 +26,7 @@ error: comparison operators cannot be chained
2626
LL | foo<3 + 3>();
2727
| ^ ^
2828
|
29-
help: use `::<...>` instead of `<...>` to specify type or const arguments
29+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
3030
|
3131
LL | foo::<3 + 3>();
3232
| ++
@@ -37,7 +37,7 @@ error: comparison operators cannot be chained
3737
LL | foo<BAR - 3>();
3838
| ^ ^
3939
|
40-
help: use `::<...>` instead of `<...>` to specify type or const arguments
40+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
4141
|
4242
LL | foo::<BAR - 3>();
4343
| ++
@@ -48,7 +48,7 @@ error: comparison operators cannot be chained
4848
LL | foo<BAR - BAR>();
4949
| ^ ^
5050
|
51-
help: use `::<...>` instead of `<...>` to specify type or const arguments
51+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
5252
|
5353
LL | foo::<BAR - BAR>();
5454
| ++
@@ -59,7 +59,7 @@ error: comparison operators cannot be chained
5959
LL | foo<100 - BAR>();
6060
| ^ ^
6161
|
62-
help: use `::<...>` instead of `<...>` to specify type or const arguments
62+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
6363
|
6464
LL | foo::<100 - BAR>();
6565
| ++
@@ -70,7 +70,7 @@ error: comparison operators cannot be chained
7070
LL | foo<bar<i32>()>();
7171
| ^ ^
7272
|
73-
help: use `::<...>` instead of `<...>` to specify type or const arguments
73+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
7474
|
7575
LL | foo::<bar<i32>()>();
7676
| ++
@@ -87,7 +87,7 @@ error: comparison operators cannot be chained
8787
LL | foo<bar::<i32>()>();
8888
| ^ ^
8989
|
90-
help: use `::<...>` instead of `<...>` to specify type or const arguments
90+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
9191
|
9292
LL | foo::<bar::<i32>()>();
9393
| ++
@@ -98,7 +98,7 @@ error: comparison operators cannot be chained
9898
LL | foo<bar::<i32>() + BAR>();
9999
| ^ ^
100100
|
101-
help: use `::<...>` instead of `<...>` to specify type or const arguments
101+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
102102
|
103103
LL | foo::<bar::<i32>() + BAR>();
104104
| ++
@@ -109,7 +109,7 @@ error: comparison operators cannot be chained
109109
LL | foo<bar::<i32>() - BAR>();
110110
| ^ ^
111111
|
112-
help: use `::<...>` instead of `<...>` to specify type or const arguments
112+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
113113
|
114114
LL | foo::<bar::<i32>() - BAR>();
115115
| ++
@@ -120,7 +120,7 @@ error: comparison operators cannot be chained
120120
LL | foo<BAR - bar::<i32>()>();
121121
| ^ ^
122122
|
123-
help: use `::<...>` instead of `<...>` to specify type or const arguments
123+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
124124
|
125125
LL | foo::<BAR - bar::<i32>()>();
126126
| ++
@@ -131,7 +131,7 @@ error: comparison operators cannot be chained
131131
LL | foo<BAR - bar::<i32>()>();
132132
| ^ ^
133133
|
134-
help: use `::<...>` instead of `<...>` to specify type or const arguments
134+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
135135
|
136136
LL | foo::<BAR - bar::<i32>()>();
137137
| ++

src/test/ui/did_you_mean/issue-40396.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: comparison operators cannot be chained
44
LL | (0..13).collect<Vec<i32>>();
55
| ^ ^
66
|
7-
help: use `::<...>` instead of `<...>` to specify type or const arguments
7+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
88
|
99
LL | (0..13).collect::<Vec<i32>>();
1010
| ++
@@ -15,7 +15,7 @@ error: comparison operators cannot be chained
1515
LL | Vec<i32>::new();
1616
| ^ ^
1717
|
18-
help: use `::<...>` instead of `<...>` to specify type or const arguments
18+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
1919
|
2020
LL | Vec::<i32>::new();
2121
| ++
@@ -26,7 +26,7 @@ error: comparison operators cannot be chained
2626
LL | (0..13).collect<Vec<i32>();
2727
| ^ ^
2828
|
29-
help: use `::<...>` instead of `<...>` to specify type or const arguments
29+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
3030
|
3131
LL | (0..13).collect::<Vec<i32>();
3232
| ++
@@ -37,7 +37,7 @@ error: expected one of `!`, `.`, `::`, `;`, `?`, `else`, `{`, or an operator, fo
3737
LL | let x = std::collections::HashMap<i128, i128>::new();
3838
| ^ expected one of 8 possible tokens
3939
|
40-
help: use `::<...>` instead of `<...>` to specify type or const arguments
40+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
4141
|
4242
LL | let x = std::collections::HashMap::<i128, i128>::new();
4343
| ++
@@ -48,7 +48,7 @@ error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found
4848
LL | std::collections::HashMap<i128, i128>::new()
4949
| ^ expected one of 8 possible tokens
5050
|
51-
help: use `::<...>` instead of `<...>` to specify type or const arguments
51+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
5252
|
5353
LL | std::collections::HashMap::<i128, i128>::new()
5454
| ++
@@ -59,7 +59,7 @@ error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found
5959
LL | std::collections::HashMap<i128, i128>::new();
6060
| ^ expected one of 8 possible tokens
6161
|
62-
help: use `::<...>` instead of `<...>` to specify type or const arguments
62+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
6363
|
6464
LL | std::collections::HashMap::<i128, i128>::new();
6565
| ++
@@ -70,7 +70,7 @@ error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found
7070
LL | std::collections::HashMap<i128, i128>::new(1, 2);
7171
| ^ expected one of 8 possible tokens
7272
|
73-
help: use `::<...>` instead of `<...>` to specify type or const arguments
73+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
7474
|
7575
LL | std::collections::HashMap::<i128, i128>::new(1, 2);
7676
| ++
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
fn f<T>() {}
2-
struct X;
3-
41
fn main() {
52
false == false == false;
63
//~^ ERROR comparison operators cannot be chained
@@ -12,15 +9,26 @@ fn main() {
129

1310
f<X>();
1411
//~^ ERROR comparison operators cannot be chained
15-
//~| HELP use `::<...>` instead of `<...>` to specify type or const arguments
12+
//~| HELP use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
1613

1714
f<Result<Option<X>, Option<Option<X>>>(1, 2);
1815
//~^ ERROR comparison operators cannot be chained
19-
//~| HELP use `::<...>` instead of `<...>` to specify type or const arguments
16+
//~| HELP use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
17+
18+
let _ = f<u8, i8>();
19+
//~^ ERROR expected one of
20+
//~| HELP use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
21+
22+
let _ = f<'_, i8>();
23+
//~^ ERROR expected one of
24+
//~| HELP use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
25+
26+
f<'_>();
27+
//~^ comparison operators cannot be chained
28+
//~| HELP use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
2029

21-
use std::convert::identity;
22-
let _ = identity<u8>;
30+
let _ = f<u8>;
2331
//~^ ERROR comparison operators cannot be chained
24-
//~| HELP use `::<...>` instead of `<...>` to specify type or const arguments
32+
//~| HELP use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
2533
//~| HELP or use `(...)` if you meant to specify fn arguments
2634
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: comparison operators cannot be chained
2-
--> $DIR/require-parens-for-chained-comparison.rs:5:11
2+
--> $DIR/require-parens-for-chained-comparison.rs:2:11
33
|
44
LL | false == false == false;
55
| ^^ ^^
@@ -10,7 +10,7 @@ LL | false == false && false == false;
1010
| ++++++++
1111

1212
error: comparison operators cannot be chained
13-
--> $DIR/require-parens-for-chained-comparison.rs:9:11
13+
--> $DIR/require-parens-for-chained-comparison.rs:6:11
1414
|
1515
LL | false == 0 < 2;
1616
| ^^ ^
@@ -21,35 +21,68 @@ LL | false == (0 < 2);
2121
| + +
2222

2323
error: comparison operators cannot be chained
24-
--> $DIR/require-parens-for-chained-comparison.rs:13:6
24+
--> $DIR/require-parens-for-chained-comparison.rs:10:6
2525
|
2626
LL | f<X>();
2727
| ^ ^
2828
|
29-
help: use `::<...>` instead of `<...>` to specify type or const arguments
29+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
3030
|
3131
LL | f::<X>();
3232
| ++
3333

3434
error: comparison operators cannot be chained
35-
--> $DIR/require-parens-for-chained-comparison.rs:17:6
35+
--> $DIR/require-parens-for-chained-comparison.rs:14:6
3636
|
3737
LL | f<Result<Option<X>, Option<Option<X>>>(1, 2);
3838
| ^ ^
3939
|
40-
help: use `::<...>` instead of `<...>` to specify type or const arguments
40+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
4141
|
4242
LL | f::<Result<Option<X>, Option<Option<X>>>(1, 2);
4343
| ++
4444

45+
error: expected one of `!`, `.`, `::`, `;`, `?`, `else`, `{`, or an operator, found `,`
46+
--> $DIR/require-parens-for-chained-comparison.rs:18:17
47+
|
48+
LL | let _ = f<u8, i8>();
49+
| ^ expected one of 8 possible tokens
50+
|
51+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
52+
|
53+
LL | let _ = f::<u8, i8>();
54+
| ++
55+
56+
error: expected one of `.`, `:`, `;`, `?`, `else`, `for`, `loop`, `while`, `{`, or an operator, found `,`
57+
--> $DIR/require-parens-for-chained-comparison.rs:22:17
58+
|
59+
LL | let _ = f<'_, i8>();
60+
| ^ expected one of 10 possible tokens
61+
|
62+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
63+
|
64+
LL | let _ = f::<'_, i8>();
65+
| ++
66+
67+
error: comparison operators cannot be chained
68+
--> $DIR/require-parens-for-chained-comparison.rs:26:6
69+
|
70+
LL | f<'_>();
71+
| ^ ^
72+
|
73+
help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
74+
|
75+
LL | f::<'_>();
76+
| ++
77+
4578
error: comparison operators cannot be chained
46-
--> $DIR/require-parens-for-chained-comparison.rs:22:21
79+
--> $DIR/require-parens-for-chained-comparison.rs:30:14
4780
|
48-
LL | let _ = identity<u8>;
49-
| ^ ^
81+
LL | let _ = f<u8>;
82+
| ^ ^
5083
|
51-
= help: use `::<...>` instead of `<...>` to specify type or const arguments
84+
= help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
5285
= help: or use `(...)` if you meant to specify fn arguments
5386

54-
error: aborting due to 5 previous errors
87+
error: aborting due to 8 previous errors
5588

0 commit comments

Comments
 (0)