Skip to content

Commit a8ccbf5

Browse files
committed
Account for typo in turbofish and suggest ::
1 parent 3bbfc73 commit a8ccbf5

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

Diff for: src/libsyntax/parse/parser/diagnostics.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ impl<'a> Parser<'a> {
358358
err: &mut DiagnosticBuilder<'_>,
359359
maybe_expected_semicolon: bool,
360360
) {
361-
if let Some((sp, likely_path)) = self.last_type_ascription {
361+
if let Some((sp, likely_path)) = self.last_type_ascription.take() {
362362
let sm = self.sess.source_map();
363363
let next_pos = sm.lookup_char_pos(self.token.span.lo());
364364
let op_pos = sm.lookup_char_pos(sp.hi());
@@ -395,7 +395,6 @@ impl<'a> Parser<'a> {
395395
err.note("for more information, see \
396396
https://github.com/rust-lang/rust/issues/23416");
397397
}
398-
self.last_type_ascription = None;
399398
}
400399
}
401400

@@ -1083,8 +1082,15 @@ impl<'a> Parser<'a> {
10831082
}
10841083

10851084
pub(super) fn could_ascription_be_path(&self, node: &ast::ExprKind) -> bool {
1086-
self.token.is_ident() &&
1087-
if let ast::ExprKind::Path(..) = node { true } else { false } &&
1085+
(self.token == token::Lt && // `foo:<bar`, likely a typoed turbofish.
1086+
self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
1087+
) ||
1088+
self.token.is_ident() &&
1089+
match node {
1090+
// `foo::` → `foo:` or `foo.bar::` → `foo.bar:`
1091+
ast::ExprKind::Path(..) | ast::ExprKind::Field(..) => true,
1092+
_ => false,
1093+
} &&
10881094
!self.token.is_reserved_ident() && // v `foo:bar(baz)`
10891095
self.look_ahead(1, |t| t == &token::OpenDelim(token::Paren)) ||
10901096
self.look_ahead(1, |t| t == &token::Lt) && // `foo:bar<baz`

Diff for: src/test/ui/suggestions/type-ascription-instead-of-path-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: expected `::`, found `(`
44
LL | vec![Ok(2)].into_iter().collect:<Result<Vec<_>,_>>()?;
55
| - ^ expected `::`
66
| |
7-
| tried to parse a type due to this type ascription
7+
| help: maybe write a path separator here: `::`
88
|
99
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
1010
= note: for more information, see https://github.com/rust-lang/rust/issues/23416

0 commit comments

Comments
 (0)