Skip to content

Commit 560eae3

Browse files
authored
Rollup merge of #70199 - pnkfelix:issue-68808-dont-turn-dummy-spans-into-invalid-lines, r=estebank
Revised span-to-lines conversion to produce an empty vec on DUMMY_SP. This required revising some of the client code to stop relying on the returned set of lines being non-empty. Fix #68808
2 parents ad6d303 + 763121d commit 560eae3

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

src/librustc_errors/emitter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1574,7 +1574,7 @@ impl EmitterWriter {
15741574
.span_to_lines(parts[0].span)
15751575
.expect("span_to_lines failed when emitting suggestion");
15761576

1577-
assert!(!lines.lines.is_empty());
1577+
assert!(!lines.lines.is_empty() || parts[0].span.is_dummy());
15781578

15791579
let line_start = sm.lookup_char_pos(parts[0].span.lo()).line;
15801580
draw_col_separator_no_space(&mut buffer, 1, max_line_num_len + 1);

src/librustc_errors/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl CodeSuggestion {
194194
let bounding_span = Span::with_root_ctxt(lo, hi);
195195
// The different spans might belong to different contexts, if so ignore suggestion.
196196
let lines = sm.span_to_lines(bounding_span).ok()?;
197-
assert!(!lines.lines.is_empty());
197+
assert!(!lines.lines.is_empty() || bounding_span.is_dummy());
198198

199199
// We can't splice anything if the source is unavailable.
200200
if !sm.ensure_source_file_source_present(lines.file.clone()) {
@@ -213,8 +213,8 @@ impl CodeSuggestion {
213213
let sf = &lines.file;
214214
let mut prev_hi = sm.lookup_char_pos(bounding_span.lo());
215215
prev_hi.col = CharPos::from_usize(0);
216-
217-
let mut prev_line = sf.get_line(lines.lines[0].line_index);
216+
let mut prev_line =
217+
lines.lines.get(0).and_then(|line0| sf.get_line(line0.line_index));
218218
let mut buf = String::new();
219219

220220
for part in &substitution.parts {

src/librustc_span/source_map.rs

+7
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,10 @@ impl SourceMap {
535535
let (lo, hi) = self.is_valid_span(sp)?;
536536
assert!(hi.line >= lo.line);
537537

538+
if sp.is_dummy() {
539+
return Ok(FileLines { file: lo.file, lines: Vec::new() });
540+
}
541+
538542
let mut lines = Vec::with_capacity(hi.line - lo.line + 1);
539543

540544
// The span starts partway through the first line,
@@ -545,6 +549,9 @@ impl SourceMap {
545549
// and to the end of the line. Be careful because the line
546550
// numbers in Loc are 1-based, so we subtract 1 to get 0-based
547551
// lines.
552+
//
553+
// FIXME: now that we handle DUMMY_SP up above, we should consider
554+
// asserting that the line numbers here are all indeed 1-based.
548555
let hi_line = hi.line.saturating_sub(1);
549556
for line_index in lo.line.saturating_sub(1)..hi_line {
550557
let line_len = lo.file.get_line(line_index).map(|s| s.chars().count()).unwrap_or(0);

0 commit comments

Comments
 (0)