diff options
| author | bors <bors@rust-lang.org> | 2021-09-13 16:31:12 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-09-13 16:31:12 +0000 |
| commit | 9bb77da74dac4768489127d21e32db19b59ada5b (patch) | |
| tree | 5bcc7471a627d3d440646060935da4dcfcfd501b /compiler/rustc_parse/src | |
| parent | b0ee4951f0289aa31e54b101c5706f220315e197 (diff) | |
| parent | 34d19634f5ae448ff3fb281b22306cc8cfa3ee8c (diff) | |
| download | rust-9bb77da74dac4768489127d21e32db19b59ada5b.tar.gz rust-9bb77da74dac4768489127d21e32db19b59ada5b.zip | |
Auto merge of #87915 - estebank:fancy-spans, r=oli-obk
Use smaller spans for some structured suggestions Use more accurate suggestion spans for * argument parse error * fully qualified path * missing code block type * numeric casts
Diffstat (limited to 'compiler/rustc_parse/src')
| -rw-r--r-- | compiler/rustc_parse/src/parser/diagnostics.rs | 85 |
1 files changed, 46 insertions, 39 deletions
diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 94ca70d3f95..4fccfc287fd 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -1633,50 +1633,57 @@ impl<'a> Parser<'a> { { let rfc_note = "anonymous parameters are removed in the 2018 edition (see RFC 1685)"; - let (ident, self_sugg, param_sugg, type_sugg) = match pat.kind { - PatKind::Ident(_, ident, _) => ( - ident, - format!("self: {}", ident), - format!("{}: TypeName", ident), - format!("_: {}", ident), - ), - // Also catches `fn foo(&a)`. - PatKind::Ref(ref pat, mutab) - if matches!(pat.clone().into_inner().kind, PatKind::Ident(..)) => - { - match pat.clone().into_inner().kind { - PatKind::Ident(_, ident, _) => { - let mutab = mutab.prefix_str(); - ( - ident, - format!("self: &{}{}", mutab, ident), - format!("{}: &{}TypeName", ident, mutab), - format!("_: &{}{}", mutab, ident), - ) + let (ident, self_sugg, param_sugg, type_sugg, self_span, param_span, type_span) = + match pat.kind { + PatKind::Ident(_, ident, _) => ( + ident, + "self: ".to_string(), + ": TypeName".to_string(), + "_: ".to_string(), + pat.span.shrink_to_lo(), + pat.span.shrink_to_hi(), + pat.span.shrink_to_lo(), + ), + // Also catches `fn foo(&a)`. + PatKind::Ref(ref inner_pat, mutab) + if matches!(inner_pat.clone().into_inner().kind, PatKind::Ident(..)) => + { + match inner_pat.clone().into_inner().kind { + PatKind::Ident(_, ident, _) => { + let mutab = mutab.prefix_str(); + ( + ident, + "self: ".to_string(), + format!("{}: &{}TypeName", ident, mutab), + "_: ".to_string(), + pat.span.shrink_to_lo(), + pat.span, + pat.span.shrink_to_lo(), + ) + } + _ => unreachable!(), } - _ => unreachable!(), - } - } - _ => { - // Otherwise, try to get a type and emit a suggestion. - if let Some(ty) = pat.to_ty() { - err.span_suggestion_verbose( - pat.span, - "explicitly ignore the parameter name", - format!("_: {}", pprust::ty_to_string(&ty)), - Applicability::MachineApplicable, - ); - err.note(rfc_note); } + _ => { + // Otherwise, try to get a type and emit a suggestion. + if let Some(ty) = pat.to_ty() { + err.span_suggestion_verbose( + pat.span, + "explicitly ignore the parameter name", + format!("_: {}", pprust::ty_to_string(&ty)), + Applicability::MachineApplicable, + ); + err.note(rfc_note); + } - return None; - } - }; + return None; + } + }; // `fn foo(a, b) {}`, `fn foo(a<x>, b<y>) {}` or `fn foo(usize, usize) {}` if first_param { err.span_suggestion( - pat.span, + self_span, "if this is a `self` type, give it a parameter name", self_sugg, Applicability::MaybeIncorrect, @@ -1686,14 +1693,14 @@ impl<'a> Parser<'a> { // `fn foo(HashMap: TypeName<u32>)`. if self.token != token::Lt { err.span_suggestion( - pat.span, + param_span, "if this is a parameter name, give it a type", param_sugg, Applicability::HasPlaceholders, ); } err.span_suggestion( - pat.span, + type_span, "if this is a type, explicitly ignore the parameter name", type_sugg, Applicability::MachineApplicable, |
