diff options
| author | bors <bors@rust-lang.org> | 2021-11-20 10:28:05 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-11-20 10:28:05 +0000 |
| commit | 3d789740b09002e3d2be3ab1cf53fdca3995034c (patch) | |
| tree | 760446b1d10e5a8b280fa4e791609da5ac3739d0 /compiler/rustc_resolve/src/diagnostics.rs | |
| parent | 6d48ee90f51dd5793b425c6593581fd108ead398 (diff) | |
| parent | 3379721a30d87c396df69efa15b1307389d408df (diff) | |
| download | rust-3d789740b09002e3d2be3ab1cf53fdca3995034c.tar.gz rust-3d789740b09002e3d2be3ab1cf53fdca3995034c.zip | |
Auto merge of #91080 - matthiaskrgr:rollup-znh88cy, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - #90575 (Improve suggestions for compatible variants on type mismatch.) - #90628 (Clarify error messages caused by re-exporting `pub(crate)` visibility to outside) - #90930 (Fix `non-constant value` ICE (#90878)) - #90983 (Make scrollbar in the sidebar always visible for visual consistency) - #91021 (Elaborate `Future::Output` when printing opaque `impl Future` type) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_resolve/src/diagnostics.rs')
| -rw-r--r-- | compiler/rustc_resolve/src/diagnostics.rs | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index c46a18e5103..2e4cb4ff727 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -450,12 +450,24 @@ impl<'a> Resolver<'a> { // let foo =... // ^^^ given this Span // ------- get this Span to have an applicable suggestion + + // 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); - if sp.lo().0 == 0 { + + 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(sp.lo().0 - current.len() as u32)); + let sp = sp.with_lo(BytePos(pos_for_suggestion)); err.span_suggestion( sp, &format!("consider using `{}` instead of `{}`", sugg, current), |
