Skip to content

Commit 53c4644

Browse files
committed
Fix tests/incremental. Required making the arg TokenStream part of the key.
1 parent 64e6072 commit 53c4644

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

compiler/rustc_expand/src/expand.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ use rustc_ast::{
1515
NodeId, PatKind, StmtKind, TyKind, DUMMY_NODE_ID,
1616
};
1717
use rustc_ast_pretty::pprust;
18+
use rustc_data_structures::fingerprint::Fingerprint;
1819
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
20+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1921
use rustc_data_structures::sync::Lrc;
2022
use rustc_errors::PResult;
2123
use rustc_feature::Features;
@@ -396,14 +398,32 @@ pub struct MacroExpander<'a, 'b> {
396398
monotonic: bool, // cf. `cx.monotonic_expander()`
397399
}
398400

401+
#[tracing::instrument(level = "debug", skip(tcx))]
399402
pub fn expand_legacy_bang<'tcx>(
400403
tcx: TyCtxt<'tcx>,
401-
key: (LocalExpnId, LocalExpnId),
404+
key: (LocalExpnId, LocalExpnId, Fingerprint),
402405
) -> Result<(&'tcx TokenStream, usize), (Span, ErrorGuaranteed)> {
403-
let (invoc_id, current_expansion) = dbg!(key);
404-
dbg!(invoc_id.to_expn_id().expn_hash(), current_expansion.to_expn_id().expn_hash());
406+
use tracing::debug;
407+
408+
let (invoc_id, current_expansion, arg_fingerprint) = key;
409+
405410
let map = tcx.macro_map.borrow();
406411
let (arg, span, expander) = map.get(&invoc_id).as_ref().unwrap();
412+
debug!(?arg);
413+
414+
// this (i.e., debug-printing `span`) somehow made the test pass??
415+
// tracing::debug!(?span);
416+
417+
let arg_hash: Fingerprint = tcx.with_stable_hashing_context(|mut hcx| {
418+
let mut hasher = StableHasher::new();
419+
arg.flattened().hash_stable(&mut hcx, &mut hasher);
420+
hasher.finish()
421+
});
422+
423+
// sanity-check, to make sure we're not running for (maybe) old arguments
424+
// that were loaded from the cache. this would certainly be a bug.
425+
assert_eq!(arg_fingerprint, arg_hash);
426+
407427
expander
408428
.expand(&tcx.sess, *span, arg.clone(), current_expansion)
409429
.map(|(tts, i)| (tcx.arena.alloc(tts) as &TokenStream, i))

compiler/rustc_middle/src/query/keys.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Defines the set of legal keys that can be used in queries.
22
3+
use rustc_data_structures::fingerprint::Fingerprint;
34
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalModDefId, ModDefId, LOCAL_CRATE};
45
use rustc_hir::hir_id::{HirId, OwnerId};
56
use rustc_query_system::query::{DefIdCache, DefaultCache, SingleCache, VecCache};
@@ -592,7 +593,7 @@ impl<'tcx> Key for (ValidityRequirement, ty::ParamEnvAnd<'tcx, Ty<'tcx>>) {
592593
}
593594
}
594595

595-
impl Key for (LocalExpnId, LocalExpnId) {
596+
impl Key for (LocalExpnId, LocalExpnId, Fingerprint) {
596597
type Cache<V> = DefaultCache<Self, V>;
597598

598599
fn default_span(&self, _: TyCtxt<'_>) -> Span {

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ rustc_queries! {
110110
desc { "triggering a delayed bug for testing incremental" }
111111
}
112112

113-
query expand_legacy_bang(key: (LocalExpnId, LocalExpnId)) -> Result<(&'tcx TokenStream, usize), (Span, ErrorGuaranteed)> {
113+
query expand_legacy_bang(key: (LocalExpnId, LocalExpnId, Fingerprint)) -> Result<(&'tcx TokenStream, usize), (Span, ErrorGuaranteed)> {
114114
no_hash
115115
// eval_always
116116
cache_on_disk_if { true }

compiler/rustc_resolve/src/macros.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use rustc_ast::tokenstream::TokenStream;
99
use rustc_ast::{self as ast, attr, Crate, Inline, ItemKind, ModKind, NodeId};
1010
use rustc_ast_pretty::pprust;
1111
use rustc_attr::StabilityLevel;
12+
use rustc_data_structures::fingerprint::Fingerprint;
1213
use rustc_data_structures::intern::Interned;
14+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1315
use rustc_data_structures::sync::Lrc;
1416
use rustc_errors::{Applicability, StashKey};
1517
use rustc_expand::base::{
@@ -539,13 +541,26 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
539541
invoc_id: LocalExpnId,
540542
current_expansion: LocalExpnId,
541543
) -> Result<(TokenStream, usize), (Span, ErrorGuaranteed)> {
542-
dbg!((
543-
"resolver",
544-
invoc_id.to_expn_id().expn_hash(),
545-
current_expansion.to_expn_id().expn_hash()
546-
));
544+
use tracing::debug;
545+
546+
debug!(
547+
invoc_id_hash = ?invoc_id.to_expn_id().expn_hash(),
548+
current_expansion_hash = ?current_expansion.to_expn_id().expn_hash()
549+
);
550+
551+
let map = self.tcx().macro_map.borrow();
552+
let (arg, _span, _expander) = map.get(&invoc_id).as_ref().unwrap();
553+
554+
debug!(?arg);
555+
556+
let arg_hash: Fingerprint = self.tcx.with_stable_hashing_context(|mut hcx| {
557+
let mut hasher = StableHasher::new();
558+
arg.flattened().hash_stable(&mut hcx, &mut hasher);
559+
hasher.finish()
560+
});
561+
547562
self.tcx()
548-
.expand_legacy_bang((invoc_id, current_expansion))
563+
.expand_legacy_bang((invoc_id, current_expansion, arg_hash))
549564
.map(|(tts, i)| (tts.clone(), i))
550565
}
551566
}

0 commit comments

Comments
 (0)