Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 7e0d3fd

Browse files
committed
Add test for hygiene caching issue
1 parent dc21612 commit 7e0d3fd

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// revisions:rpass1 rpass2
2+
// compile-flags: -Z query-dep-graph
3+
4+
// We use #[inline(always)] to ensure that the downstream crate
5+
// will always load the MIR for these functions
6+
7+
#![feature(rustc_attrs)]
8+
9+
#[allow(unused)]
10+
macro_rules! first_macro {
11+
() => {
12+
println!("New call!");
13+
}
14+
}
15+
16+
#[rustc_dirty(label="typeck", cfg="rpass2")]
17+
#[inline(always)]
18+
pub fn changed_fn() {
19+
// This will cause additional hygiene to be generate,
20+
// which will cause the SyntaxContext/ExpnId raw ids to be
21+
// different when we write out `my_fn` to the crate metadata.
22+
#[cfg(rpass2)]
23+
first_macro!();
24+
}
25+
26+
macro_rules! print_loc {
27+
() => {
28+
println!("Caller loc: {}", std::panic::Location::caller());
29+
}
30+
}
31+
32+
#[rustc_clean(cfg="rpass2")]
33+
#[inline(always)]
34+
pub fn unchanged_fn() {
35+
print_loc!();
36+
}
37+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// revisions:rpass1 rpass2
2+
// compile-flags: -Z query-dep-graph
3+
// aux-build:cached_hygiene.rs
4+
5+
// This tests the folllowing scenario
6+
// 1. A foreign crate is compiled with incremental compilation.
7+
// This causes hygiene information to be saved to the incr cache.
8+
// 2. One function is the foreign crate is modified. This causes the
9+
// optimized mir for an unmodified function to be loaded from the
10+
// incremental cache and written out to the crate metadata.
11+
// 3. In the process of loading and writing out this function's MIR,
12+
// we load hygiene information from the incremental cache and
13+
// write it to our metadata.
14+
// 4. This hygiene information is loaded by another crate (this file)
15+
16+
// Previously, this situation would cause hygiene identifiers
17+
// (SyntaxContexts and ExpnIds) to get corrupted when we tried to
18+
// serialize the hygiene information loaded from the incr cache into
19+
// the metadata. Specifically, we were not resetting `orig_id`
20+
// for an `EpxnData` generate in the current crate, which would cause
21+
// us to serialize the `ExpnId` pointing to a garbage location in
22+
// the metadata.
23+
24+
#![feature(rustc_attrs)]
25+
26+
#![rustc_partition_reused(module="load_cached_hygiene-call_unchanged_function", cfg="rpass2")]
27+
#![rustc_partition_codegened(module="load_cached_hygiene-call_changed_function", cfg="rpass2")]
28+
29+
30+
extern crate cached_hygiene;
31+
32+
pub mod call_unchanged_function {
33+
34+
pub fn unchanged() {
35+
cached_hygiene::unchanged_fn();
36+
}
37+
}
38+
39+
pub mod call_changed_function {
40+
pub fn changed() {
41+
cached_hygiene::changed_fn();
42+
}
43+
}
44+
45+
pub fn main() {
46+
call_unchanged_function::unchanged();
47+
call_changed_function::changed();
48+
}

0 commit comments

Comments
 (0)