1
Fork 0

use smaller span for suggestions

This commit is contained in:
Takayuki Maeda 2022-08-27 02:23:21 +09:00
parent 9845f4c47e
commit b33c3d6cd6
10 changed files with 35 additions and 33 deletions

View file

@ -1299,7 +1299,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// local binding
if let hir::def::Res::Local(hir_id) = path.res {
let span = tcx.hir().span(hir_id);
let snippet = tcx.sess.source_map().span_to_snippet(span);
let filename = tcx.sess.source_map().span_to_filename(span);
let parent_node =
@ -1309,7 +1308,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
concrete_type,
);
match (filename, parent_node, snippet) {
match (filename, parent_node) {
(
FileName::Real(_),
Node::Local(hir::Local {
@ -1317,14 +1316,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ty,
..
}),
Ok(ref snippet),
) => {
let type_span = ty.map(|ty| ty.span.with_lo(span.hi())).unwrap_or(span.shrink_to_hi());
err.span_suggestion(
// account for `let x: _ = 42;`
// ^^^^
span.to(ty.as_ref().map(|ty| ty.span).unwrap_or(span)),
// ^^^
type_span,
&msg,
format!("{}: {}", snippet, concrete_type),
format!(": {concrete_type}"),
Applicability::MaybeIncorrect,
);
}

View file

@ -763,16 +763,13 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
// If there is a single unbound associated type and a single excess generic param
// suggest replacing the generic param with the associated type bound
if provided_args_matches_unbound_traits && !unbound_types.is_empty() {
let mut suggestions = vec![];
let unused_generics = &self.gen_args.args[self.num_expected_type_or_const_args()..];
for (potential, name) in iter::zip(unused_generics, &unbound_types) {
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(potential.span()) {
suggestions.push((potential.span(), format!("{} = {}", name, snippet)));
}
}
let suggestions = iter::zip(unused_generics, &unbound_types)
.map(|(potential, name)| (potential.span().shrink_to_lo(), format!("{name} = ")))
.collect::<Vec<_>>();
if !suggestions.is_empty() {
err.multipart_suggestion(
err.multipart_suggestion_verbose(
&format!(
"replace the generic bound{s} with the associated type{s}",
s = pluralize!(unbound_types.len())