Skip to content

Commit 7f6fdef

Browse files
authored
Rollup merge of rust-lang#68256 - estebank:bad-sugg-span, r=petrochenkov
Do not ICE on malformed suggestion spans Under the assumption that suggestions are by their very nature always "best effort", it is ok if we don't display them instead of having an ICE. The underlying issue of the malformed span being _created_ is left unaddressed. Fix rust-lang#67567. r? @petrochenkov
2 parents fca3264 + 03240e1 commit 7f6fdef

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

Diff for: src/librustc_errors/emitter.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,15 @@ impl EmitterWriter {
14761476
None => return Ok(()),
14771477
};
14781478

1479+
// Render the replacements for each suggestion
1480+
let suggestions = suggestion.splice_lines(&**sm);
1481+
1482+
if suggestions.is_empty() {
1483+
// Suggestions coming from macros can have malformed spans. This is a heavy handed
1484+
// approach to avoid ICEs by ignoring the suggestion outright.
1485+
return Ok(());
1486+
}
1487+
14791488
let mut buffer = StyledBuffer::new();
14801489

14811490
// Render the suggestion message
@@ -1492,9 +1501,6 @@ impl EmitterWriter {
14921501
Some(Style::HeaderMsg),
14931502
);
14941503

1495-
// Render the replacements for each suggestion
1496-
let suggestions = suggestion.splice_lines(&**sm);
1497-
14981504
let mut row_num = 2;
14991505
let mut notice_capitalization = false;
15001506
for (complete, parts, only_capitalization) in suggestions.iter().take(MAX_SUGGESTIONS) {
@@ -1505,7 +1511,9 @@ impl EmitterWriter {
15051511
let show_underline = !(parts.len() == 1 && parts[0].snippet.trim() == complete.trim())
15061512
&& complete.lines().count() == 1;
15071513

1508-
let lines = sm.span_to_lines(parts[0].span).unwrap();
1514+
let lines = sm
1515+
.span_to_lines(parts[0].span)
1516+
.expect("span_to_lines failed when emitting suggestion");
15091517

15101518
assert!(!lines.lines.is_empty());
15111519

Diff for: src/librustc_errors/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
pub use emitter::ColorConfig;
1212

13+
use log::debug;
1314
use Level::*;
1415

1516
use emitter::{is_case_difference, Emitter, EmitterWriter};
@@ -174,6 +175,15 @@ impl CodeSuggestion {
174175

175176
self.substitutions
176177
.iter()
178+
.filter(|subst| {
179+
// Suggestions coming from macros can have malformed spans. This is a heavy
180+
// handed approach to avoid ICEs by ignoring the suggestion outright.
181+
let invalid = subst.parts.iter().any(|item| cm.is_valid_span(item.span).is_err());
182+
if invalid {
183+
debug!("splice_lines: suggestion contains an invalid span: {:?}", subst);
184+
}
185+
!invalid
186+
})
177187
.cloned()
178188
.map(|mut substitution| {
179189
// Assumption: all spans are in the same file, and all spans

Diff for: src/librustc_span/source_map.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -473,20 +473,23 @@ impl SourceMap {
473473
lo.line != hi.line
474474
}
475475

476-
pub fn span_to_lines(&self, sp: Span) -> FileLinesResult {
477-
debug!("span_to_lines(sp={:?})", sp);
478-
476+
pub fn is_valid_span(&self, sp: Span) -> Result<(Loc, Loc), SpanLinesError> {
479477
let lo = self.lookup_char_pos(sp.lo());
480478
debug!("span_to_lines: lo={:?}", lo);
481479
let hi = self.lookup_char_pos(sp.hi());
482480
debug!("span_to_lines: hi={:?}", hi);
483-
484481
if lo.file.start_pos != hi.file.start_pos {
485482
return Err(SpanLinesError::DistinctSources(DistinctSources {
486483
begin: (lo.file.name.clone(), lo.file.start_pos),
487484
end: (hi.file.name.clone(), hi.file.start_pos),
488485
}));
489486
}
487+
Ok((lo, hi))
488+
}
489+
490+
pub fn span_to_lines(&self, sp: Span) -> FileLinesResult {
491+
debug!("span_to_lines(sp={:?})", sp);
492+
let (lo, hi) = self.is_valid_span(sp)?;
490493
assert!(hi.line >= lo.line);
491494

492495
let mut lines = Vec::with_capacity(hi.line - lo.line + 1);

Diff for: src/test/ui/consts/miri_unleashed/mutable_const2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error: internal compiler error: mutable allocation in constant
1010
LL | const MUTABLE_BEHIND_RAW: *mut i32 = &UnsafeCell::new(42) as *const _ as *mut _;
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

13-
thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', src/librustc_errors/lib.rs:346:17
13+
thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', src/librustc_errors/lib.rs:356:17
1414
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1515

1616
error: internal compiler error: unexpected panic

0 commit comments

Comments
 (0)