Skip to content

Commit 9722952

Browse files
committed
Auto merge of #76256 - tgnottingham:issue-74890, r=nikomatsakis
incr-comp: hash and serialize span end line/column Hash both the length and the end location (line/column) of a span. If we hash only the length, for example, then two otherwise equal spans with different end locations will have the same hash. This can cause a problem during incremental compilation wherein a previous result for a query that depends on the end location of a span will be incorrectly reused when the end location of the span it depends on has changed. A similar analysis applies if some query depends specifically on the length of the span, but we only hash the end location. So hash both. Fix #46744, fix #59954, fix #63161, fix #73640, fix #73967, fix #74890, fix #75900 --- See #74890 for a more in-depth analysis. I haven't thought about what other problems this root cause could be responsible for. Please let me know if anything springs to mind. I believe the issue has existed since the inception of incremental compilation.
2 parents 7f5a42b + dac57e6 commit 9722952

File tree

5 files changed

+74
-5
lines changed

5 files changed

+74
-5
lines changed

compiler/rustc_span/src/lib.rs

+26-5
Original file line numberDiff line numberDiff line change
@@ -1894,16 +1894,37 @@ where
18941894
return;
18951895
}
18961896

1897+
let (_, line_hi, col_hi) = match ctx.byte_pos_to_line_and_col(span.hi) {
1898+
Some(pos) => pos,
1899+
None => {
1900+
Hash::hash(&TAG_INVALID_SPAN, hasher);
1901+
span.ctxt.hash_stable(ctx, hasher);
1902+
return;
1903+
}
1904+
};
1905+
18971906
Hash::hash(&TAG_VALID_SPAN, hasher);
18981907
// We truncate the stable ID hash and line and column numbers. The chances
18991908
// of causing a collision this way should be minimal.
19001909
Hash::hash(&(file_lo.name_hash as u64), hasher);
19011910

1902-
let col = (col_lo.0 as u64) & 0xFF;
1903-
let line = ((line_lo as u64) & 0xFF_FF_FF) << 8;
1904-
let len = ((span.hi - span.lo).0 as u64) << 32;
1905-
let line_col_len = col | line | len;
1906-
Hash::hash(&line_col_len, hasher);
1911+
// Hash both the length and the end location (line/column) of a span. If we
1912+
// hash only the length, for example, then two otherwise equal spans with
1913+
// different end locations will have the same hash. This can cause a problem
1914+
// during incremental compilation wherein a previous result for a query that
1915+
// depends on the end location of a span will be incorrectly reused when the
1916+
// end location of the span it depends on has changed (see issue #74890). A
1917+
// similar analysis applies if some query depends specifically on the length
1918+
// of the span, but we only hash the end location. So hash both.
1919+
1920+
let col_lo_trunc = (col_lo.0 as u64) & 0xFF;
1921+
let line_lo_trunc = ((line_lo as u64) & 0xFF_FF_FF) << 8;
1922+
let col_hi_trunc = (col_hi.0 as u64) & 0xFF << 32;
1923+
let line_hi_trunc = ((line_hi as u64) & 0xFF_FF_FF) << 40;
1924+
let col_line = col_lo_trunc | line_lo_trunc | col_hi_trunc | line_hi_trunc;
1925+
let len = (span.hi - span.lo).0;
1926+
Hash::hash(&col_line, hasher);
1927+
Hash::hash(&len, hasher);
19071928
span.ctxt.hash_stable(ctx, hasher);
19081929
}
19091930
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
include ../../run-make-fulldeps/tools.mk
2+
3+
# FIXME https://github.com/rust-lang/rust/issues/78911
4+
# ignore-32bit wrong/no cross compiler and sometimes we pass wrong gcc args (-m64)
5+
6+
# Tests that we don't ICE during incremental compilation after modifying a
7+
# function span such that its previous end line exceeds the number of lines
8+
# in the new file, but its start line/column and length remain the same.
9+
10+
SRC=$(TMPDIR)/src
11+
INCR=$(TMPDIR)/incr
12+
13+
all:
14+
mkdir $(SRC)
15+
mkdir $(INCR)
16+
cp a.rs $(SRC)/main.rs
17+
$(RUSTC) -C incremental=$(INCR) $(SRC)/main.rs
18+
cp b.rs $(SRC)/main.rs
19+
$(RUSTC) -C incremental=$(INCR) $(SRC)/main.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
fn main() {
2+
// foo must be used.
3+
foo();
4+
}
5+
6+
// For this test to operate correctly, foo's body must start on exactly the same
7+
// line and column and have the exact same length in bytes in a.rs and b.rs. In
8+
// a.rs, the body must end on a line number which does not exist in b.rs.
9+
// Basically, avoid modifying this file, including adding or removing whitespace!
10+
fn foo() {
11+
assert_eq!(1, 1);
12+
13+
14+
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fn main() {
2+
// foo must be used.
3+
foo();
4+
}
5+
6+
// For this test to operate correctly, foo's body must start on exactly the same
7+
// line and column and have the exact same length in bytes in a.rs and b.rs. In
8+
// a.rs, the body must end on a line number which does not exist in b.rs.
9+
// Basically, avoid modifying this file, including adding or removing whitespace!
10+
fn foo() {
11+
assert_eq!(1, 1);////
12+
}

src/test/run-make/issue-36710/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
include ../../run-make-fulldeps/tools.mk
22

3+
# FIXME https://github.com/rust-lang/rust/issues/78911
34
# ignore-32bit wrong/no cross compiler and sometimes we pass wrong gcc args (-m64)
45

56
all: foo

0 commit comments

Comments
 (0)