Skip to content

Commit 34d1963

Browse files
committed
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 * E0212
1 parent eb2226b commit 34d1963

File tree

60 files changed

+1200
-958
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1200
-958
lines changed

Diff for: compiler/rustc_errors/src/diagnostic.rs

+15
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,21 @@ impl Diagnostic {
298298
)
299299
}
300300

301+
/// Show a suggestion that has multiple parts to it, always as it's own subdiagnostic.
302+
/// In other words, multiple changes need to be applied as part of this suggestion.
303+
pub fn multipart_suggestion_verbose(
304+
&mut self,
305+
msg: &str,
306+
suggestion: Vec<(Span, String)>,
307+
applicability: Applicability,
308+
) -> &mut Self {
309+
self.multipart_suggestion_with_style(
310+
msg,
311+
suggestion,
312+
applicability,
313+
SuggestionStyle::ShowAlways,
314+
)
315+
}
301316
/// [`Diagnostic::multipart_suggestion()`] but you can set the [`SuggestionStyle`].
302317
pub fn multipart_suggestion_with_style(
303318
&mut self,

Diff for: compiler/rustc_errors/src/diagnostic_builder.rs

+14
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,20 @@ impl<'a> DiagnosticBuilder<'a> {
257257
self
258258
}
259259

260+
/// See [`Diagnostic::multipart_suggestion()`].
261+
pub fn multipart_suggestion_verbose(
262+
&mut self,
263+
msg: &str,
264+
suggestion: Vec<(Span, String)>,
265+
applicability: Applicability,
266+
) -> &mut Self {
267+
if !self.0.allow_suggestions {
268+
return self;
269+
}
270+
self.0.diagnostic.multipart_suggestion_verbose(msg, suggestion, applicability);
271+
self
272+
}
273+
260274
/// See [`Diagnostic::tool_only_multipart_suggestion()`].
261275
pub fn tool_only_multipart_suggestion(
262276
&mut self,

Diff for: compiler/rustc_parse/src/parser/diagnostics.rs

+46-39
Original file line numberDiff line numberDiff line change
@@ -1618,50 +1618,57 @@ impl<'a> Parser<'a> {
16181618
{
16191619
let rfc_note = "anonymous parameters are removed in the 2018 edition (see RFC 1685)";
16201620

1621-
let (ident, self_sugg, param_sugg, type_sugg) = match pat.kind {
1622-
PatKind::Ident(_, ident, _) => (
1623-
ident,
1624-
format!("self: {}", ident),
1625-
format!("{}: TypeName", ident),
1626-
format!("_: {}", ident),
1627-
),
1628-
// Also catches `fn foo(&a)`.
1629-
PatKind::Ref(ref pat, mutab)
1630-
if matches!(pat.clone().into_inner().kind, PatKind::Ident(..)) =>
1631-
{
1632-
match pat.clone().into_inner().kind {
1633-
PatKind::Ident(_, ident, _) => {
1634-
let mutab = mutab.prefix_str();
1635-
(
1636-
ident,
1637-
format!("self: &{}{}", mutab, ident),
1638-
format!("{}: &{}TypeName", ident, mutab),
1639-
format!("_: &{}{}", mutab, ident),
1640-
)
1621+
let (ident, self_sugg, param_sugg, type_sugg, self_span, param_span, type_span) =
1622+
match pat.kind {
1623+
PatKind::Ident(_, ident, _) => (
1624+
ident,
1625+
"self: ".to_string(),
1626+
": TypeName".to_string(),
1627+
"_: ".to_string(),
1628+
pat.span.shrink_to_lo(),
1629+
pat.span.shrink_to_hi(),
1630+
pat.span.shrink_to_lo(),
1631+
),
1632+
// Also catches `fn foo(&a)`.
1633+
PatKind::Ref(ref inner_pat, mutab)
1634+
if matches!(inner_pat.clone().into_inner().kind, PatKind::Ident(..)) =>
1635+
{
1636+
match inner_pat.clone().into_inner().kind {
1637+
PatKind::Ident(_, ident, _) => {
1638+
let mutab = mutab.prefix_str();
1639+
(
1640+
ident,
1641+
"self: ".to_string(),
1642+
format!("{}: &{}TypeName", ident, mutab),
1643+
"_: ".to_string(),
1644+
pat.span.shrink_to_lo(),
1645+
pat.span,
1646+
pat.span.shrink_to_lo(),
1647+
)
1648+
}
1649+
_ => unreachable!(),
16411650
}
1642-
_ => unreachable!(),
1643-
}
1644-
}
1645-
_ => {
1646-
// Otherwise, try to get a type and emit a suggestion.
1647-
if let Some(ty) = pat.to_ty() {
1648-
err.span_suggestion_verbose(
1649-
pat.span,
1650-
"explicitly ignore the parameter name",
1651-
format!("_: {}", pprust::ty_to_string(&ty)),
1652-
Applicability::MachineApplicable,
1653-
);
1654-
err.note(rfc_note);
16551651
}
1652+
_ => {
1653+
// Otherwise, try to get a type and emit a suggestion.
1654+
if let Some(ty) = pat.to_ty() {
1655+
err.span_suggestion_verbose(
1656+
pat.span,
1657+
"explicitly ignore the parameter name",
1658+
format!("_: {}", pprust::ty_to_string(&ty)),
1659+
Applicability::MachineApplicable,
1660+
);
1661+
err.note(rfc_note);
1662+
}
16561663

1657-
return None;
1658-
}
1659-
};
1664+
return None;
1665+
}
1666+
};
16601667

16611668
// `fn foo(a, b) {}`, `fn foo(a<x>, b<y>) {}` or `fn foo(usize, usize) {}`
16621669
if first_param {
16631670
err.span_suggestion(
1664-
pat.span,
1671+
self_span,
16651672
"if this is a `self` type, give it a parameter name",
16661673
self_sugg,
16671674
Applicability::MaybeIncorrect,
@@ -1671,14 +1678,14 @@ impl<'a> Parser<'a> {
16711678
// `fn foo(HashMap: TypeName<u32>)`.
16721679
if self.token != token::Lt {
16731680
err.span_suggestion(
1674-
pat.span,
1681+
param_span,
16751682
"if this is a parameter name, give it a type",
16761683
param_sugg,
16771684
Applicability::HasPlaceholders,
16781685
);
16791686
}
16801687
err.span_suggestion(
1681-
pat.span,
1688+
type_span,
16821689
"if this is a type, explicitly ignore the parameter name",
16831690
type_sugg,
16841691
Applicability::MachineApplicable,

Diff for: compiler/rustc_resolve/src/late/diagnostics.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_ast::{
1212
};
1313
use rustc_ast_pretty::pprust::path_segment_to_string;
1414
use rustc_data_structures::fx::FxHashSet;
15-
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder, SuggestionStyle};
15+
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
1616
use rustc_hir as hir;
1717
use rustc_hir::def::Namespace::{self, *};
1818
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind};
@@ -1950,11 +1950,10 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
19501950
introduce_suggestion.push((*span, formatter(&lt_name)));
19511951
}
19521952
}
1953-
err.multipart_suggestion_with_style(
1953+
err.multipart_suggestion_verbose(
19541954
&msg,
19551955
introduce_suggestion,
19561956
Applicability::MaybeIncorrect,
1957-
SuggestionStyle::ShowAlways,
19581957
);
19591958
}
19601959

@@ -1966,14 +1965,13 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
19661965
})
19671966
.map(|(formatter, span)| (*span, formatter(name)))
19681967
.collect();
1969-
err.multipart_suggestion_with_style(
1968+
err.multipart_suggestion_verbose(
19701969
&format!(
19711970
"consider using the `{}` lifetime",
19721971
lifetime_names.iter().next().unwrap()
19731972
),
19741973
spans_suggs,
19751974
Applicability::MaybeIncorrect,
1976-
SuggestionStyle::ShowAlways,
19771975
);
19781976
};
19791977
let suggest_new = |err: &mut DiagnosticBuilder<'_>, suggs: Vec<Option<String>>| {
@@ -2064,11 +2062,10 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
20642062
};
20652063
spans_suggs.push((span, sugg.to_string()));
20662064
}
2067-
err.multipart_suggestion_with_style(
2065+
err.multipart_suggestion_verbose(
20682066
"consider using the `'static` lifetime",
20692067
spans_suggs,
20702068
Applicability::MaybeIncorrect,
2071-
SuggestionStyle::ShowAlways,
20722069
);
20732070
continue;
20742071
}
@@ -2088,11 +2085,10 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
20882085
introduce_suggestion.push((span, sugg.to_string()));
20892086
}
20902087
}
2091-
err.multipart_suggestion_with_style(
2088+
err.multipart_suggestion_verbose(
20922089
&msg,
20932090
introduce_suggestion,
20942091
Applicability::MaybeIncorrect,
2095-
SuggestionStyle::ShowAlways,
20962092
);
20972093
if should_break {
20982094
break;
@@ -2167,11 +2163,10 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
21672163
if spans_suggs.len() > 0 {
21682164
// This happens when we have `Foo<T>` where we point at the space before `T`,
21692165
// but this can be confusing so we give a suggestion with placeholders.
2170-
err.multipart_suggestion_with_style(
2166+
err.multipart_suggestion_verbose(
21712167
"consider using one of the available lifetimes here",
21722168
spans_suggs,
21732169
Applicability::HasPlaceholders,
2174-
SuggestionStyle::ShowAlways,
21752170
);
21762171
}
21772172
}

Diff for: compiler/rustc_typeck/src/astconv/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1648,14 +1648,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
16481648
constraint=constraint,
16491649
));
16501650
} else {
1651-
err.span_suggestion(
1652-
span,
1651+
err.span_suggestion_verbose(
1652+
span.with_hi(assoc_name.span.lo()),
16531653
"use fully qualified syntax to disambiguate",
16541654
format!(
1655-
"<{} as {}>::{}",
1655+
"<{} as {}>::",
16561656
ty_param_name(),
16571657
bound.print_only_trait_path(),
1658-
assoc_name,
16591658
),
16601659
Applicability::MaybeIncorrect,
16611660
);

0 commit comments

Comments
 (0)