Skip to content

Commit 4a2c9a1

Browse files
committed
skip invalid span in error emitter
1 parent 725b213 commit 4a2c9a1

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -350,22 +350,27 @@ impl AnnotateSnippetEmitter {
350350
"all spans must be disjoint",
351351
);
352352

353+
let lo = subst.parts.iter().map(|part| part.span.lo()).min()?;
354+
let lo_file = sm.lookup_source_file(lo);
355+
let hi = subst.parts.iter().map(|part| part.span.hi()).max()?;
356+
let hi_file = sm.lookup_source_file(hi);
357+
358+
// The different spans might belong to different contexts, if so ignore suggestion.
359+
if lo_file.stable_id != hi_file.stable_id {
360+
return None;
361+
}
362+
363+
// We can't splice anything if the source is unavailable.
364+
if !sm.ensure_source_file_source_present(&lo_file) {
365+
return None;
366+
}
367+
353368
// Account for cases where we are suggesting the same code that's already
354369
// there. This shouldn't happen often, but in some cases for multipart
355370
// suggestions it's much easier to handle it here than in the origin.
356371
subst.parts.retain(|p| is_different(sm, &p.snippet, p.span));
357372

358-
let item_span = subst.parts.first()?;
359-
let file = sm.lookup_source_file(item_span.span.lo());
360-
if should_show_source_code(
361-
&self.ignored_directories_in_source_blocks,
362-
sm,
363-
&file,
364-
) {
365-
Some(subst)
366-
} else {
367-
None
368-
}
373+
if subst.parts.is_empty() { None } else { Some(subst) }
369374
})
370375
.collect::<Vec<_>>();
371376

tests/ui/delegation/ice-line-bounds-issue-148732.stderr

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
WARN rustc_errors::emitter Invalid span $SRC_DIR/std/src/macros.rs:LL:COL (#4), error=SourceNotAvailable { filename: Real(Remapped { local_path: None, virtual_name: "$SRC_DIR/std/src/macros.rs" }) }
2-
WARN rustc_errors::emitter Invalid span $SRC_DIR/std/src/macros.rs:LL:COL (#4), error=SourceNotAvailable { filename: Real(Remapped { local_path: None, virtual_name: "$SRC_DIR/std/src/macros.rs" }) }
3-
WARN rustc_errors::emitter Invalid span $SRC_DIR/std/src/macros.rs:LL:COL (#4), error=SourceNotAvailable { filename: Real(Remapped { local_path: None, virtual_name: "$SRC_DIR/std/src/macros.rs" }) }
41
error[E0106]: missing lifetime specifier
52
--> $DIR/ice-line-bounds-issue-148732.rs:4:5
63
|

tests/ui/structs/ice-line-bounds-issue-148684.stderr

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
WARN rustc_errors::emitter Invalid span $SRC_DIR/alloc/src/macros.rs:LL:COL (#4), error=SourceNotAvailable { filename: Real(Remapped { local_path: None, virtual_name: "$SRC_DIR/alloc/src/macros.rs" }) }
2-
WARN rustc_errors::emitter Invalid span $SRC_DIR/alloc/src/macros.rs:LL:COL (#4), error=SourceNotAvailable { filename: Real(Remapped { local_path: None, virtual_name: "$SRC_DIR/alloc/src/macros.rs" }) }
3-
WARN rustc_errors::emitter Invalid span $SRC_DIR/alloc/src/macros.rs:LL:COL (#4), error=SourceNotAvailable { filename: Real(Remapped { local_path: None, virtual_name: "$SRC_DIR/alloc/src/macros.rs" }) }
4-
WARN rustc_errors::emitter Invalid span $SRC_DIR/alloc/src/macros.rs:LL:COL (#4), error=SourceNotAvailable { filename: Real(Remapped { local_path: None, virtual_name: "$SRC_DIR/alloc/src/macros.rs" }) }
5-
WARN rustc_errors::emitter Invalid span $SRC_DIR/alloc/src/macros.rs:LL:COL (#4), error=SourceNotAvailable { filename: Real(Remapped { local_path: None, virtual_name: "$SRC_DIR/alloc/src/macros.rs" }) }
6-
WARN rustc_errors::emitter Invalid span $SRC_DIR/alloc/src/macros.rs:LL:COL (#4), error=SourceNotAvailable { filename: Real(Remapped { local_path: None, virtual_name: "$SRC_DIR/alloc/src/macros.rs" }) }
71
error[E0423]: expected function, tuple struct or tuple variant, found struct `A`
82
--> $DIR/ice-line-bounds-issue-148684.rs:7:5
93
|

0 commit comments

Comments
 (0)