Skip to content

Commit defb251

Browse files
authored
Rollup merge of rust-lang#66098 - estebank:path-asciption-typo, r=Centril
Detect `::` -> `:` typo when involving turbofish Fix rust-lang#65569.
2 parents c17de6b + a8ccbf5 commit defb251

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

src/libsyntax/parse/parser/diagnostics.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,11 @@ impl<'a> Parser<'a> {
360360
}
361361

362362
pub fn maybe_annotate_with_ascription(
363-
&self,
363+
&mut self,
364364
err: &mut DiagnosticBuilder<'_>,
365365
maybe_expected_semicolon: bool,
366366
) {
367-
if let Some((sp, likely_path)) = self.last_type_ascription {
367+
if let Some((sp, likely_path)) = self.last_type_ascription.take() {
368368
let sm = self.sess.source_map();
369369
let next_pos = sm.lookup_char_pos(self.token.span.lo());
370370
let op_pos = sm.lookup_char_pos(sp.hi());
@@ -1088,8 +1088,15 @@ impl<'a> Parser<'a> {
10881088
}
10891089

10901090
pub(super) fn could_ascription_be_path(&self, node: &ast::ExprKind) -> bool {
1091-
self.token.is_ident() &&
1092-
if let ast::ExprKind::Path(..) = node { true } else { false } &&
1091+
(self.token == token::Lt && // `foo:<bar`, likely a typoed turbofish.
1092+
self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
1093+
) ||
1094+
self.token.is_ident() &&
1095+
match node {
1096+
// `foo::` → `foo:` or `foo.bar::` → `foo.bar:`
1097+
ast::ExprKind::Path(..) | ast::ExprKind::Field(..) => true,
1098+
_ => false,
1099+
} &&
10931100
!self.token.is_reserved_ident() && // v `foo:bar(baz)`
10941101
self.look_ahead(1, |t| t == &token::OpenDelim(token::Paren)) ||
10951102
self.look_ahead(1, |t| t == &token::Lt) && // `foo:bar<baz`

src/libsyntax/parse/parser/stmt.rs

+1
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ impl<'a> Parser<'a> {
397397
}
398398
let stmt = match self.parse_full_stmt(false) {
399399
Err(mut err) => {
400+
self.maybe_annotate_with_ascription(&mut err, false);
400401
err.emit();
401402
self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Ignore);
402403
Some(Stmt {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() -> Result<(), ()> {
2+
vec![Ok(2)].into_iter().collect:<Result<Vec<_>,_>>()?;
3+
//~^ ERROR expected `::`, found `(`
4+
Ok(())
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: expected `::`, found `(`
2+
--> $DIR/type-ascription-instead-of-path-2.rs:2:55
3+
|
4+
LL | vec![Ok(2)].into_iter().collect:<Result<Vec<_>,_>>()?;
5+
| - ^ expected `::`
6+
| |
7+
| help: maybe write a path separator here: `::`
8+
|
9+
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
10+
= note: for more information, see https://github.com/rust-lang/rust/issues/23416
11+
12+
error: aborting due to previous error
13+

0 commit comments

Comments
 (0)