Skip to content

Commit

Permalink
Tweak :: -> : typo heuristic and reduce verbosity
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
estebank committed Jul 9, 2020
1 parent fc6ee8f commit e771a4f
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 26 deletions.
21 changes: 13 additions & 8 deletions src/librustc_parse/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,16 @@ impl<'a> Parser<'a> {
if allow_unstable {
// Give extra information about type ascription only if it's a nightly compiler.
err.note(
"`#![feature(type_ascription)]` lets you annotate an expression with a \
type: `<expr>: <type>`",
);
err.note(
"see issue #23416 <https://github.com/rust-lang/rust/issues/23416> \
for more information",
"`#![feature(type_ascription)]` lets you annotate an expression with a type: \
`<expr>: <type>`",
);
if !likely_path {
// Avoid giving too much info when it was likely an unrelated typo.
err.note(
"see issue #23416 <https://github.com/rust-lang/rust/issues/23416> \
for more information",
);
}
}
}
}
Expand Down Expand Up @@ -1152,8 +1155,10 @@ impl<'a> Parser<'a> {
} &&
!self.token.is_reserved_ident() && // v `foo:bar(baz)`
self.look_ahead(1, |t| t == &token::OpenDelim(token::Paren))
|| self.look_ahead(1, |t| t == &token::Lt) && // `foo:bar<baz`
self.look_ahead(2, |t| t.is_ident())
|| self.look_ahead(1, |t| t == &token::OpenDelim(token::Brace)) // `foo:bar {`
|| self.look_ahead(1, |t| t == &token::Colon) && // `foo:bar::<baz`
self.look_ahead(2, |t| t == &token::Lt) &&
self.look_ahead(3, |t| t.is_ident())
|| self.look_ahead(1, |t| t == &token::Colon) && // `foo:bar:baz`
self.look_ahead(2, |t| t.is_ident())
|| self.look_ahead(1, |t| t == &token::ModSep)
Expand Down
3 changes: 1 addition & 2 deletions src/test/ui/lifetime_starts_expressions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ error: expected type, found keyword `loop`
LL | loop { break 'label: loop { break 'label 42; }; }
| - ^^^^ expected type
| |
| tried to parse a type due to this type ascription
| help: maybe write a path separator here: `::`
|
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information

error: aborting due to 2 previous errors

20 changes: 10 additions & 10 deletions src/test/ui/parser/issue-35813-postfix-after-cast.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,12 @@ error: casts cannot be followed by ?
--> $DIR/issue-35813-postfix-after-cast.rs:121:5
|
LL | Err(0u64): Result<u64,u64>?;
| ^^^^^^^^^-^^^^^^^^^^^^^^^^
| |
| help: maybe write a path separator here: `::`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information
help: try surrounding the expression in parentheses
|
LL | (Err(0u64): Result<u64,u64>)?;
| ^ ^

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

error: casts cannot be followed by a field access
--> $DIR/issue-35813-postfix-after-cast.rs:167:5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ LL | Box:new("foo".to_string())
| help: maybe write a path separator here: `::`
|
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information

error: aborting due to previous error

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

error: aborting due to previous error

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

error: aborting due to previous error

1 change: 0 additions & 1 deletion src/test/ui/type/ascription/issue-47666.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ LL | let _ = Option:Some(vec![0, 1]);
| help: maybe write a path separator here: `::`
|
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0423]: expected value, found enum `Option`
Expand Down
1 change: 0 additions & 1 deletion src/test/ui/type/ascription/issue-54516.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ LL | println!("{}", std::mem:size_of::<BTreeMap<u32, u32>>());
| help: maybe write a path separator here: `::`
|
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information

error[E0423]: expected value, found module `std::mem`
--> $DIR/issue-54516.rs:4:20
Expand Down
1 change: 0 additions & 1 deletion src/test/ui/type/ascription/issue-60933.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ LL | let u: usize = std::mem:size_of::<u32>();
| help: maybe write a path separator here: `::`
|
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information

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

0 comments on commit e771a4f

Please sign in to comment.