diff options
| author | Camille GILLOT <gillot.camille@gmail.com> | 2022-03-10 23:12:35 +0100 |
|---|---|---|
| committer | Camille GILLOT <gillot.camille@gmail.com> | 2022-04-17 11:03:34 +0200 |
| commit | a9e13fa5533df7fc004aeab00d45822eacb8461e (patch) | |
| tree | 6d759bc7958cc987f346f6e7f1d00b3487fc7b01 /compiler/rustc_errors/src | |
| parent | ca57bada05373ff227c661cc542e99c92b70c0ee (diff) | |
| download | rust-a9e13fa5533df7fc004aeab00d45822eacb8461e.tar.gz rust-a9e13fa5533df7fc004aeab00d45822eacb8461e.zip | |
Lint elided lifetimes in path on the AST.
Diffstat (limited to 'compiler/rustc_errors/src')
| -rw-r--r-- | compiler/rustc_errors/src/lib.rs | 38 |
1 files changed, 10 insertions, 28 deletions
diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index 416bc4e2e3b..d7c5df7d8e2 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -1511,35 +1511,17 @@ pub fn add_elided_lifetime_in_path_suggestion( path_span: Span, incl_angl_brckt: bool, insertion_span: Span, - anon_lts: String, ) { - let (replace_span, suggestion) = if incl_angl_brckt { - (insertion_span, anon_lts) - } else { - // When possible, prefer a suggestion that replaces the whole - // `Path<T>` expression with `Path<'_, T>`, rather than inserting `'_, ` - // at a point (which makes for an ugly/confusing label) - if let Ok(snippet) = source_map.span_to_snippet(path_span) { - // But our spans can get out of whack due to macros; if the place we think - // we want to insert `'_` isn't even within the path expression's span, we - // should bail out of making any suggestion rather than panicking on a - // subtract-with-overflow or string-slice-out-out-bounds (!) - // FIXME: can we do better? - if insertion_span.lo().0 < path_span.lo().0 { - return; - } - let insertion_index = (insertion_span.lo().0 - path_span.lo().0) as usize; - if insertion_index > snippet.len() { - return; - } - let (before, after) = snippet.split_at(insertion_index); - (path_span, format!("{}{}{}", before, anon_lts, after)) - } else { - (insertion_span, anon_lts) - } - }; - diag.span_suggestion( - replace_span, + diag.span_label(path_span, format!("expected lifetime parameter{}", pluralize!(n))); + if source_map.span_to_snippet(insertion_span).is_err() { + // Do not try to suggest anything if generated by a proc-macro. + return; + } + let anon_lts = vec!["'_"; n].join(", "); + let suggestion = + if incl_angl_brckt { format!("<{}>", anon_lts) } else { format!("{}, ", anon_lts) }; + diag.span_suggestion_verbose( + insertion_span.shrink_to_hi(), &format!("indicate the anonymous lifetime{}", pluralize!(n)), suggestion, Applicability::MachineApplicable, |
