Skip to content

Commit 2d99e68

Browse files
committed
Emit more pretty diagnostics for qualified paths
1 parent 8240f1a commit 2d99e68

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ impl<'a> Parser<'a> {
640640
}
641641
}
642642
Err(mut err) => {
643-
// We could't parse generic parameters, unlikely to be a turbofish. Rely on
643+
// We couldn't parse generic parameters, unlikely to be a turbofish. Rely on
644644
// generic parse error instead.
645645
err.cancel();
646646
*self = snapshot;
@@ -1242,7 +1242,7 @@ impl<'a> Parser<'a> {
12421242
let is_question = self.eat(&token::Question); // Handle `await? <expr>`.
12431243
let expr = if self.token == token::OpenDelim(token::Brace) {
12441244
// Handle `await { <expr> }`.
1245-
// This needs to be handled separatedly from the next arm to avoid
1245+
// This needs to be handled separately from the next arm to avoid
12461246
// interpreting `await { <expr> }?` as `<expr>?.await`.
12471247
self.parse_block_expr(None, self.token.span, BlockCheckMode::Default, AttrVec::new())
12481248
} else {
@@ -1618,6 +1618,8 @@ impl<'a> Parser<'a> {
16181618
|| self.token == token::Lt
16191619
|| self.token == token::CloseDelim(token::Paren))
16201620
{
1621+
let rfc_note = "anonymous parameters are removed in the 2018 edition (see RFC 1685)";
1622+
16211623
let (ident, self_sugg, param_sugg, type_sugg) = match pat.kind {
16221624
PatKind::Ident(_, ident, _) => (
16231625
ident,
@@ -1626,7 +1628,9 @@ impl<'a> Parser<'a> {
16261628
format!("_: {}", ident),
16271629
),
16281630
// Also catches `fn foo(&a)`.
1629-
PatKind::Ref(ref pat, mutab) => {
1631+
PatKind::Ref(ref pat, mutab)
1632+
if matches!(pat.clone().into_inner().kind, PatKind::Ident(..)) =>
1633+
{
16301634
match pat.clone().into_inner().kind {
16311635
PatKind::Ident(_, ident, _) => {
16321636
let mutab = mutab.prefix_str();
@@ -1637,20 +1641,23 @@ impl<'a> Parser<'a> {
16371641
format!("_: &{}{}", mutab, ident),
16381642
)
16391643
}
1640-
PatKind::Path(..) => {
1641-
err.note("anonymous parameters are removed in the 2018 edition (see RFC 1685)");
1642-
return None;
1643-
}
1644-
_ => return None,
1644+
_ => unreachable!(),
16451645
}
16461646
}
1647-
// Also catches `fn foo(<Bar as T>::Baz)`
1648-
PatKind::Path(..) => {
1649-
err.note("anonymous parameters are removed in the 2018 edition (see RFC 1685)");
1647+
_ => {
1648+
// Otherwise, try to get a type and emit a suggestion.
1649+
if let Some(ty) = pat.to_ty() {
1650+
err.span_suggestion_verbose(
1651+
pat.span,
1652+
"explicitly ignore the parameter name",
1653+
format!("_: {}", pprust::ty_to_string(&ty)),
1654+
Applicability::MachineApplicable,
1655+
);
1656+
err.note(rfc_note);
1657+
}
1658+
16501659
return None;
16511660
}
1652-
// Ignore other `PatKind`.
1653-
_ => return None,
16541661
};
16551662

16561663
// `fn foo(a, b) {}`, `fn foo(a<x>, b<y>) {}` or `fn foo(usize, usize) {}`
@@ -1678,7 +1685,7 @@ impl<'a> Parser<'a> {
16781685
type_sugg,
16791686
Applicability::MachineApplicable,
16801687
);
1681-
err.note("anonymous parameters are removed in the 2018 edition (see RFC 1685)");
1688+
err.note(rfc_note);
16821689

16831690
// Don't attempt to recover by using the `X` in `X<Y>` as the parameter name.
16841691
return if self.token == token::Lt { None } else { Some(ident) };

src/test/ui/anon-params/anon-params-denied-2018.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ LL | fn foo_with_qualified_path(<Bar as T>::Baz);
4545
| ^ expected one of 8 possible tokens
4646
|
4747
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
48+
help: explicitly ignore the parameter name
49+
|
50+
LL | fn foo_with_qualified_path(_: <Bar as T>::Baz);
51+
| ^^^^^^^^^^^^^^^^^^
4852

4953
error: expected one of `(`, `...`, `..=`, `..`, `::`, `:`, `{`, or `|`, found `)`
5054
--> $DIR/anon-params-denied-2018.rs:15:56
@@ -53,6 +57,10 @@ LL | fn foo_with_qualified_path_and_ref(&<Bar as T>::Baz);
5357
| ^ expected one of 8 possible tokens
5458
|
5559
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
60+
help: explicitly ignore the parameter name
61+
|
62+
LL | fn foo_with_qualified_path_and_ref(_: &<Bar as T>::Baz);
63+
| ^^^^^^^^^^^^^^^^^^^
5664

5765
error: expected one of `:`, `@`, or `|`, found `,`
5866
--> $DIR/anon-params-denied-2018.rs:18:36

0 commit comments

Comments
 (0)