Skip to content

Commit 9353e21

Browse files
authored
Rollup merge of #74188 - estebank:tweak-ascription-typo-heuristic, r=petrochenkov
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. Clean up indentation. r? @petrochenkov
2 parents 31d53de + e771a4f commit 9353e21

10 files changed

+57
-61
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
}
@@ -1161,8 +1164,10 @@ impl<'a> Parser<'a> {
11611164
} &&
11621165
!self.token.is_reserved_ident() && // v `foo:bar(baz)`
11631166
self.look_ahead(1, |t| t == &token::OpenDelim(token::Paren))
1164-
|| self.look_ahead(1, |t| t == &token::Lt) && // `foo:bar<baz`
1165-
self.look_ahead(2, |t| t.is_ident())
1167+
|| self.look_ahead(1, |t| t == &token::OpenDelim(token::Brace)) // `foo:bar {`
1168+
|| self.look_ahead(1, |t| t == &token::Colon) && // `foo:bar::<baz`
1169+
self.look_ahead(2, |t| t == &token::Lt) &&
1170+
self.look_ahead(3, |t| t.is_ident())
11661171
|| self.look_ahead(1, |t| t == &token::Colon) && // `foo:bar:baz`
11671172
self.look_ahead(2, |t| t.is_ident())
11681173
|| self.look_ahead(1, |t| t == &token::ModSep)

src/librustc_resolve/diagnostics.rs

+33-35
Original file line numberDiff line numberDiff line change
@@ -898,44 +898,42 @@ impl<'a> Resolver<'a> {
898898
suggestion: Option<TypoSuggestion>,
899899
span: Span,
900900
) -> bool {
901-
if let Some(suggestion) = suggestion {
901+
let suggestion = match suggestion {
902+
None => return false,
902903
// We shouldn't suggest underscore.
903-
if suggestion.candidate == kw::Underscore {
904-
return false;
905-
}
906-
907-
let msg = format!(
908-
"{} {} with a similar name exists",
909-
suggestion.res.article(),
910-
suggestion.res.descr()
911-
);
912-
err.span_suggestion(
913-
span,
914-
&msg,
915-
suggestion.candidate.to_string(),
916-
Applicability::MaybeIncorrect,
917-
);
918-
let def_span = suggestion.res.opt_def_id().and_then(|def_id| match def_id.krate {
919-
LOCAL_CRATE => self.opt_span(def_id),
920-
_ => Some(
921-
self.session
922-
.source_map()
923-
.guess_head_span(self.cstore().get_span_untracked(def_id, self.session)),
904+
Some(suggestion) if suggestion.candidate == kw::Underscore => return false,
905+
Some(suggestion) => suggestion,
906+
};
907+
let msg = format!(
908+
"{} {} with a similar name exists",
909+
suggestion.res.article(),
910+
suggestion.res.descr()
911+
);
912+
err.span_suggestion(
913+
span,
914+
&msg,
915+
suggestion.candidate.to_string(),
916+
Applicability::MaybeIncorrect,
917+
);
918+
let def_span = suggestion.res.opt_def_id().and_then(|def_id| match def_id.krate {
919+
LOCAL_CRATE => self.opt_span(def_id),
920+
_ => Some(
921+
self.session
922+
.source_map()
923+
.guess_head_span(self.cstore().get_span_untracked(def_id, self.session)),
924+
),
925+
});
926+
if let Some(span) = def_span {
927+
err.span_label(
928+
self.session.source_map().guess_head_span(span),
929+
&format!(
930+
"similarly named {} `{}` defined here",
931+
suggestion.res.descr(),
932+
suggestion.candidate.as_str(),
924933
),
925-
});
926-
if let Some(span) = def_span {
927-
err.span_label(
928-
self.session.source_map().guess_head_span(span),
929-
&format!(
930-
"similarly named {} `{}` defined here",
931-
suggestion.res.descr(),
932-
suggestion.candidate.as_str(),
933-
),
934-
);
935-
}
936-
return true;
934+
);
937935
}
938-
false
936+
true
939937
}
940938

941939
fn binding_description(&self, b: &NameBinding<'_>, ident: Ident, from_prelude: bool) -> String {

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)