Skip to content

Commit 35d6344

Browse files
authored
Rollup merge of rust-lang#127279 - bvanjoi:fix-112680, r=petrochenkov
use old ctx if has same expand environment during decode span Fixes rust-lang#112680 The root reason why rust-lang#112680 failed with incremental compilation on the second attempt is the difference in `opaque` between the span of the field [`ident`](https://github.com/rust-lang/rust/blob/master/compiler/rustc_hir_typeck/src/expr.rs#L2348) and the span in the incremental cache at `tcx.def_ident_span(field.did)`. - Let's call the span of `ident` as `span_a`, which is generated by [`apply_mark_internal`](https://github.com/rust-lang/rust/blob/master/compiler/rustc_span/src/hygiene.rs#L553-L554). Its content is similar to: ```rs span_a_ctx -> SyntaxContextData { opaque: span_a_ctx, opaque_and_semitransparent: span_a_ctx, // .... } ``` - And call the span of `tcx.def_ident_span` as `span_b`, which is generated by [`decode_syntax_context`](https://github.com/rust-lang/rust/blob/master/compiler/rustc_span/src/hygiene.rs#L1390). Its content is: ```rs span_b_ctx -> SyntaxContextData { opaque: span_b_ctx, // note `span_b_ctx` is not same as `span_a_ctx` opaque_and_semitransparent: span_b_ctx, // .... } ``` Although they have the same `parent` (both refer to the root) and `outer_expn`, I cannot find the specific connection between them. Therefore, I chose a solution that may not be the best: give up the incremental compile cache to ensure we can use `span_a` in this case. r? ``@petrochenkov`` Do you have any advice on this? Or perhaps this solution is acceptable?
2 parents 0dbfe1b + 07481b9 commit 35d6344

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

compiler/rustc_span/src/hygiene.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,14 @@ pub fn decode_syntax_context<D: Decoder, F: FnOnce(&mut D, u32) -> SyntaxContext
14151415

14161416
// Overwrite the dummy data with our decoded SyntaxContextData
14171417
HygieneData::with(|hygiene_data| {
1418+
if let Some(old) = hygiene_data.syntax_context_data.get(raw_id as usize)
1419+
&& old.outer_expn == ctxt_data.outer_expn
1420+
&& old.outer_transparency == ctxt_data.outer_transparency
1421+
&& old.parent == ctxt_data.parent
1422+
{
1423+
ctxt_data = old.clone();
1424+
}
1425+
14181426
let dummy = std::mem::replace(
14191427
&mut hygiene_data.syntax_context_data[ctxt.as_u32() as usize],
14201428
ctxt_data,

tests/incremental/decl_macro.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//@ revisions: rpass1 rpass2
2+
3+
// issue#112680
4+
5+
#![feature(decl_macro)]
6+
7+
pub trait T {
8+
type Key;
9+
fn index_from_key(key: Self::Key) -> usize;
10+
}
11+
12+
pub macro m($key_ty:ident, $val_ty:ident) {
13+
struct $key_ty {
14+
inner: usize,
15+
}
16+
17+
impl T for $val_ty {
18+
type Key = $key_ty;
19+
20+
fn index_from_key(key: Self::Key) -> usize {
21+
key.inner
22+
}
23+
}
24+
}
25+
26+
m!(TestId, Test);
27+
28+
#[cfg(rpass1)]
29+
struct Test(u32);
30+
31+
#[cfg(rpass2)]
32+
struct Test;
33+
34+
fn main() {}

0 commit comments

Comments
 (0)