Skip to content

Commit eca91bd

Browse files
Auto merge of #148735 - chenyukang:yukang-fix-ice-148732, r=<try>
Fix ICE caused by invalid spans for shrink_file try-job: aarch64-msvc-1
2 parents acda5e9 + 7262408 commit eca91bd

File tree

5 files changed

+85
-3
lines changed

5 files changed

+85
-3
lines changed

compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -745,14 +745,20 @@ fn shrink_file(
745745
) -> Option<(Span, String, usize)> {
746746
let lo_byte = spans.iter().map(|s| s.lo()).min()?;
747747
let lo_loc = sm.lookup_char_pos(lo_byte);
748-
let lo = lo_loc.file.line_bounds(lo_loc.line.saturating_sub(1)).start;
749748

750749
let hi_byte = spans.iter().map(|s| s.hi()).max()?;
751750
let hi_loc = sm.lookup_char_pos(hi_byte);
752-
let hi = lo_loc.file.line_bounds(hi_loc.line.saturating_sub(1)).end;
751+
752+
if lo_loc.file.name != hi_loc.file.name {
753+
// this may happen when spans cross file boundaries due to macro expansion.
754+
return None;
755+
}
756+
757+
let lo = lo_loc.file.line_bounds(lo_loc.line.saturating_sub(1)).start;
758+
let hi = hi_loc.file.line_bounds(hi_loc.line.saturating_sub(1)).end;
753759

754760
let bounding_span = Span::with_root_ctxt(lo, hi);
755-
let source = sm.span_to_snippet(bounding_span).unwrap_or_default();
761+
let source = sm.span_to_snippet(bounding_span).ok()?;
756762
let offset_line = sm.doctest_offset_line(file_name, lo_loc.line);
757763

758764
Some((bounding_span, source, offset_line))
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
reuse a as b {
2+
//~^ ERROR cannot find function `a` in this scope
3+
//~| ERROR functions delegation is not yet fully implemented
4+
dbg!(b);
5+
//~^ ERROR missing lifetime specifier
6+
}
7+
8+
fn main() {}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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" }) }
4+
error[E0106]: missing lifetime specifier
5+
--> $DIR/ice-line-bounds-issue-148732.rs:4:5
6+
|
7+
LL | dbg!(b);
8+
| ^^^^^^^ expected named lifetime parameter
9+
|
10+
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
|
12+
13+
error[E0425]: cannot find function `a` in this scope
14+
--> $DIR/ice-line-bounds-issue-148732.rs:1:7
15+
|
16+
LL | reuse a as b {
17+
| ^ not found in this scope
18+
19+
error[E0658]: functions delegation is not yet fully implemented
20+
--> $DIR/ice-line-bounds-issue-148732.rs:1:1
21+
|
22+
LL | / reuse a as b {
23+
LL | |
24+
LL | |
25+
LL | | dbg!(b);
26+
LL | |
27+
LL | | }
28+
| |_^
29+
|
30+
= note: see issue #118212 <https://github.com/rust-lang/rust/issues/118212> for more information
31+
= help: add `#![feature(fn_delegation)]` to the crate attributes to enable
32+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
33+
34+
error: aborting due to 3 previous errors
35+
36+
Some errors have detailed explanations: E0106, E0425, E0658.
37+
For more information about an error, try `rustc --explain E0106`.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
struct A {
2+
b: Vec<u8>,
3+
c: usize,
4+
}
5+
6+
fn main() {
7+
A(2, vec![])
8+
//~^ ERROR expected function, tuple struct or tuple variant, found struct `A`
9+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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" }) }
7+
error[E0423]: expected function, tuple struct or tuple variant, found struct `A`
8+
--> $DIR/ice-line-bounds-issue-148684.rs:7:5
9+
|
10+
LL | / struct A {
11+
LL | | b: Vec<u8>,
12+
LL | | c: usize,
13+
LL | | }
14+
| |_- `A` defined here
15+
...
16+
LL | A(2, vec![])
17+
| ^^^^^^^^^^^^
18+
|
19+
20+
error: aborting due to 1 previous error
21+
22+
For more information about this error, try `rustc --explain E0423`.

0 commit comments

Comments
 (0)