Skip to content

Commit e771a4f

Browse files
committed
Tweak :: -> : typo heuristic and reduce verbosity
Do not trigger on correct type ascription expressions with trailing operators and _do_ trigger on likely path typos where a turbofish is used. On likely path typos, remove note explaining type ascription.
1 parent fc6ee8f commit e771a4f

9 files changed

+24
-26
lines changed

src/librustc_parse/parser/diagnostics.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,16 @@ impl<'a> Parser<'a> {
346346
if allow_unstable {
347347
// Give extra information about type ascription only if it's a nightly compiler.
348348
err.note(
349-
"`#![feature(type_ascription)]` lets you annotate an expression with a \
350-
type: `<expr>: <type>`",
351-
);
352-
err.note(
353-
"see issue #23416 <https://github.com/rust-lang/rust/issues/23416> \
354-
for more information",
349+
"`#![feature(type_ascription)]` lets you annotate an expression with a type: \
350+
`<expr>: <type>`",
355351
);
352+
if !likely_path {
353+
// Avoid giving too much info when it was likely an unrelated typo.
354+
err.note(
355+
"see issue #23416 <https://github.com/rust-lang/rust/issues/23416> \
356+
for more information",
357+
);
358+
}
356359
}
357360
}
358361
}
@@ -1152,8 +1155,10 @@ impl<'a> Parser<'a> {
11521155
} &&
11531156
!self.token.is_reserved_ident() && // v `foo:bar(baz)`
11541157
self.look_ahead(1, |t| t == &token::OpenDelim(token::Paren))
1155-
|| self.look_ahead(1, |t| t == &token::Lt) && // `foo:bar<baz`
1156-
self.look_ahead(2, |t| t.is_ident())
1158+
|| self.look_ahead(1, |t| t == &token::OpenDelim(token::Brace)) // `foo:bar {`
1159+
|| self.look_ahead(1, |t| t == &token::Colon) && // `foo:bar::<baz`
1160+
self.look_ahead(2, |t| t == &token::Lt) &&
1161+
self.look_ahead(3, |t| t.is_ident())
11571162
|| self.look_ahead(1, |t| t == &token::Colon) && // `foo:bar:baz`
11581163
self.look_ahead(2, |t| t.is_ident())
11591164
|| self.look_ahead(1, |t| t == &token::ModSep)

src/test/ui/lifetime_starts_expressions.stderr

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ error: expected type, found keyword `loop`
1515
LL | loop { break 'label: loop { break 'label 42; }; }
1616
| - ^^^^ expected type
1717
| |
18-
| tried to parse a type due to this type ascription
18+
| help: maybe write a path separator here: `::`
1919
|
2020
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
21-
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information
2221

2322
error: aborting due to 2 previous errors
2423

src/test/ui/parser/issue-35813-postfix-after-cast.stderr

+10-10
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,12 @@ error: casts cannot be followed by ?
280280
--> $DIR/issue-35813-postfix-after-cast.rs:121:5
281281
|
282282
LL | Err(0u64): Result<u64,u64>?;
283-
| ^^^^^^^^^-^^^^^^^^^^^^^^^^
284-
| |
285-
| help: maybe write a path separator here: `::`
283+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
286284
|
287-
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
288-
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information
285+
help: try surrounding the expression in parentheses
286+
|
287+
LL | (Err(0u64): Result<u64,u64>)?;
288+
| ^ ^
289289

290290
error: casts cannot be followed by a function call
291291
--> $DIR/issue-35813-postfix-after-cast.rs:145:5
@@ -324,12 +324,12 @@ error: casts cannot be followed by `.await`
324324
--> $DIR/issue-35813-postfix-after-cast.rs:155:5
325325
|
326326
LL | Box::pin(noop()): Pin<Box<_>>.await;
327-
| ^^^^^^^^^^^^^^^^-^^^^^^^^^^^^
328-
| |
329-
| help: maybe write a path separator here: `::`
327+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
328+
|
329+
help: try surrounding the expression in parentheses
330330
|
331-
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
332-
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information
331+
LL | (Box::pin(noop()): Pin<Box<_>>).await;
332+
| ^ ^
333333

334334
error: casts cannot be followed by a field access
335335
--> $DIR/issue-35813-postfix-after-cast.rs:167:5

src/test/ui/suggestions/type-ascription-instead-of-method.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ LL | Box:new("foo".to_string())
77
| help: maybe write a path separator here: `::`
88
|
99
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
10-
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information
1110

1211
error: aborting due to previous error
1312

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

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ LL | vec![Ok(2)].into_iter().collect:<Result<Vec<_>,_>>()?;
77
| help: maybe write a path separator here: `::`
88
|
99
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
10-
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information
1110

1211
error: aborting due to previous error
1312

src/test/ui/suggestions/type-ascription-instead-of-variant.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ LL | let _ = Option:Some("");
77
| help: maybe write a path separator here: `::`
88
|
99
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
10-
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information
1110

1211
error: aborting due to previous error
1312

src/test/ui/type/ascription/issue-47666.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ LL | let _ = Option:Some(vec![0, 1]);
1010
| help: maybe write a path separator here: `::`
1111
|
1212
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
13-
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information
1413
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
1514

1615
error[E0423]: expected value, found enum `Option`

src/test/ui/type/ascription/issue-54516.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ LL | println!("{}", std::mem:size_of::<BTreeMap<u32, u32>>());
77
| help: maybe write a path separator here: `::`
88
|
99
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
10-
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information
1110

1211
error[E0423]: expected value, found module `std::mem`
1312
--> $DIR/issue-54516.rs:4:20

src/test/ui/type/ascription/issue-60933.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ LL | let u: usize = std::mem:size_of::<u32>();
77
| help: maybe write a path separator here: `::`
88
|
99
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
10-
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information
1110

1211
error[E0423]: expected value, found module `std::mem`
1312
--> $DIR/issue-60933.rs:2:20

0 commit comments

Comments
 (0)