Make span_extend_to_prev_str() more robust

This commit is contained in:
Fabian Wolff 2022-01-15 22:22:22 +01:00
parent 24b8bb13bf
commit 69803f7888
6 changed files with 116 additions and 39 deletions

View file

@ -453,28 +453,28 @@ impl<'a> Resolver<'a> {
// edit:
// only do this if the const and usage of the non-constant value are on the same line
// the further the two are apart, the higher the chance of the suggestion being wrong
// also make sure that the pos for the suggestion is not 0 (ICE #90878)
let sp =
self.session.source_map().span_extend_to_prev_str(ident.span, current, true);
let sp = self
.session
.source_map()
.span_extend_to_prev_str(ident.span, current, true, false);
let pos_for_suggestion = sp.lo().0.saturating_sub(current.len() as u32);
if sp.lo().0 == 0
|| pos_for_suggestion == 0
|| self.session.source_map().is_multiline(sp)
{
err.span_label(ident.span, &format!("this would need to be a `{}`", sugg));
} else {
let sp = sp.with_lo(BytePos(pos_for_suggestion));
err.span_suggestion(
sp,
&format!("consider using `{}` instead of `{}`", sugg, current),
format!("{} {}", sugg, ident),
Applicability::MaybeIncorrect,
);
err.span_label(span, "non-constant value");
match sp {
Some(sp) if !self.session.source_map().is_multiline(sp) => {
let sp = sp.with_lo(BytePos(sp.lo().0 - (current.len() as u32)));
err.span_suggestion(
sp,
&format!("consider using `{}` instead of `{}`", sugg, current),
format!("{} {}", sugg, ident),
Applicability::MaybeIncorrect,
);
err.span_label(span, "non-constant value");
}
_ => {
err.span_label(ident.span, &format!("this would need to be a `{}`", sugg));
}
}
err
}
ResolutionError::BindingShadowsSomethingUnacceptable {