Skip to content

Commit 68511b5

Browse files
committed
Auto merge of #86676 - cjgillot:localexpn, r=petrochenkov
Make expansions stable for incr. comp. This PR aims to make expansions stable for incr. comp. by using the same architecture as definitions: - the interned identifier `ExpnId` contains a `CrateNum` and a crate-local id; - bidirectional maps `ExpnHash <-> ExpnId` are setup; - incr. comp. on-disk cache saves and reconstructs expansions using their `ExpnHash`. I tried to use as many `LocalExpnId` as I could in the resolver code, but I may have missed a few opportunities. All this will allow to use an `ExpnId` as a query key, and to force this query without recomputing caller queries. For instance, this will be used to implement #85999. r? `@petrochenkov`
2 parents c78ebb7 + b35ceee commit 68511b5

File tree

27 files changed

+615
-436
lines changed

27 files changed

+615
-436
lines changed

Diff for: compiler/rustc_ast/src/node_id.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_span::ExpnId;
1+
use rustc_span::LocalExpnId;
22
use std::fmt;
33

44
rustc_index::newtype_index! {
@@ -24,12 +24,12 @@ pub const CRATE_NODE_ID: NodeId = NodeId::from_u32(0);
2424
pub const DUMMY_NODE_ID: NodeId = NodeId::MAX;
2525

2626
impl NodeId {
27-
pub fn placeholder_from_expn_id(expn_id: ExpnId) -> Self {
27+
pub fn placeholder_from_expn_id(expn_id: LocalExpnId) -> Self {
2828
NodeId::from_u32(expn_id.as_u32())
2929
}
3030

31-
pub fn placeholder_to_expn_id(self) -> ExpnId {
32-
ExpnId::from_u32(self.as_u32())
31+
pub fn placeholder_to_expn_id(self) -> LocalExpnId {
32+
LocalExpnId::from_u32(self.as_u32())
3333
}
3434
}
3535

Diff for: compiler/rustc_builtin_macros/src/deriving/clone.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ pub fn expand_deriving_clone(
3636
Annotatable::Item(ref annitem) => match annitem.kind {
3737
ItemKind::Struct(_, Generics { ref params, .. })
3838
| ItemKind::Enum(_, Generics { ref params, .. }) => {
39-
let container_id = cx.current_expansion.id.expn_data().parent;
40-
if cx.resolver.has_derive_copy(container_id)
39+
let container_id = cx.current_expansion.id.expn_data().parent.expect_local();
40+
let has_derive_copy = cx.resolver.has_derive_copy(container_id);
41+
if has_derive_copy
4142
&& !params
4243
.iter()
4344
.any(|param| matches!(param.kind, ast::GenericParamKind::Type { .. }))

Diff for: compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ impl<'a> TraitDef<'a> {
410410
.any(|param| matches!(param.kind, ast::GenericParamKind::Type { .. })),
411411
_ => unreachable!(),
412412
};
413-
let container_id = cx.current_expansion.id.expn_data().parent;
413+
let container_id = cx.current_expansion.id.expn_data().parent.expect_local();
414414
let always_copy = has_no_type_params && cx.resolver.has_derive_copy(container_id);
415415
let use_temporaries = is_packed && always_copy;
416416

Diff for: compiler/rustc_builtin_macros/src/proc_macro_harness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ fn mk_decls(
304304
&[sym::rustc_attrs, sym::proc_macro_internals],
305305
None,
306306
);
307-
let span = DUMMY_SP.with_def_site_ctxt(expn_id);
307+
let span = DUMMY_SP.with_def_site_ctxt(expn_id.to_expn_id());
308308

309309
let proc_macro = Ident::new(sym::proc_macro, span);
310310
let krate = cx.item(span, proc_macro, Vec::new(), ast::ItemKind::ExternCrate(None));

Diff for: compiler/rustc_builtin_macros/src/standard_library_imports.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ pub fn inject(
3434
&[sym::prelude_import],
3535
None,
3636
);
37-
let span = DUMMY_SP.with_def_site_ctxt(expn_id);
38-
let call_site = DUMMY_SP.with_call_site_ctxt(expn_id);
37+
let span = DUMMY_SP.with_def_site_ctxt(expn_id.to_expn_id());
38+
let call_site = DUMMY_SP.with_call_site_ctxt(expn_id.to_expn_id());
3939

4040
let ecfg = ExpansionConfig::default("std_lib_injection".to_string());
4141
let cx = ExtCtxt::new(sess, ecfg, resolver, None);

Diff for: compiler/rustc_builtin_macros/src/test_harness.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ impl<'a> MutVisitor for TestHarnessGenerator<'a> {
126126
for test in &mut tests {
127127
// See the comment on `mk_main` for why we're using
128128
// `apply_mark` directly.
129-
test.ident.span = test.ident.span.apply_mark(expn_id, Transparency::Opaque);
129+
test.ident.span =
130+
test.ident.span.apply_mark(expn_id.to_expn_id(), Transparency::Opaque);
130131
}
131132
self.cx.test_cases.extend(tests);
132133
}
@@ -223,7 +224,7 @@ fn generate_test_harness(
223224
&[sym::test, sym::rustc_attrs],
224225
None,
225226
);
226-
let def_site = DUMMY_SP.with_def_site_ctxt(expn_id);
227+
let def_site = DUMMY_SP.with_def_site_ctxt(expn_id.to_expn_id());
227228

228229
// Remove the entry points
229230
let mut cleaner = EntryPointCleaner { sess, depth: 0, def_site };

Diff for: compiler/rustc_expand/src/base.rs

+24-16
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_parse::{self, nt_to_tokenstream, parser, MACRO_ARGUMENTS};
1616
use rustc_session::{parse::ParseSess, Limit, Session};
1717
use rustc_span::def_id::{CrateNum, DefId};
1818
use rustc_span::edition::Edition;
19-
use rustc_span::hygiene::{AstPass, ExpnData, ExpnId, ExpnKind};
19+
use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId};
2020
use rustc_span::source_map::SourceMap;
2121
use rustc_span::symbol::{kw, sym, Ident, Symbol};
2222
use rustc_span::{FileName, MultiSpan, Span, DUMMY_SP};
@@ -813,15 +813,15 @@ impl SyntaxExtension {
813813

814814
pub fn expn_data(
815815
&self,
816-
parent: ExpnId,
816+
parent: LocalExpnId,
817817
call_site: Span,
818818
descr: Symbol,
819819
macro_def_id: Option<DefId>,
820820
parent_module: Option<DefId>,
821821
) -> ExpnData {
822822
ExpnData::new(
823823
ExpnKind::Macro(self.macro_kind(), descr),
824-
parent,
824+
parent.to_expn_id(),
825825
call_site,
826826
self.span,
827827
self.allow_internal_unstable.clone(),
@@ -843,7 +843,11 @@ pub trait ResolverExpand {
843843
fn next_node_id(&mut self) -> NodeId;
844844

845845
fn resolve_dollar_crates(&mut self);
846-
fn visit_ast_fragment_with_placeholders(&mut self, expn_id: ExpnId, fragment: &AstFragment);
846+
fn visit_ast_fragment_with_placeholders(
847+
&mut self,
848+
expn_id: LocalExpnId,
849+
fragment: &AstFragment,
850+
);
847851
fn register_builtin_macro(&mut self, name: Symbol, ext: SyntaxExtensionKind);
848852

849853
fn expansion_for_ast_pass(
@@ -852,37 +856,41 @@ pub trait ResolverExpand {
852856
pass: AstPass,
853857
features: &[Symbol],
854858
parent_module_id: Option<NodeId>,
855-
) -> ExpnId;
859+
) -> LocalExpnId;
856860

857861
fn resolve_imports(&mut self);
858862

859863
fn resolve_macro_invocation(
860864
&mut self,
861865
invoc: &Invocation,
862-
eager_expansion_root: ExpnId,
866+
eager_expansion_root: LocalExpnId,
863867
force: bool,
864868
) -> Result<Lrc<SyntaxExtension>, Indeterminate>;
865869

866870
fn check_unused_macros(&mut self);
867871

868872
/// Some parent node that is close enough to the given macro call.
869-
fn lint_node_id(&self, expn_id: ExpnId) -> NodeId;
873+
fn lint_node_id(&self, expn_id: LocalExpnId) -> NodeId;
870874

871875
// Resolver interfaces for specific built-in macros.
872876
/// Does `#[derive(...)]` attribute with the given `ExpnId` have built-in `Copy` inside it?
873-
fn has_derive_copy(&self, expn_id: ExpnId) -> bool;
877+
fn has_derive_copy(&self, expn_id: LocalExpnId) -> bool;
874878
/// Resolve paths inside the `#[derive(...)]` attribute with the given `ExpnId`.
875879
fn resolve_derives(
876880
&mut self,
877-
expn_id: ExpnId,
881+
expn_id: LocalExpnId,
878882
force: bool,
879883
derive_paths: &dyn Fn() -> DeriveResolutions,
880884
) -> Result<(), Indeterminate>;
881885
/// Take resolutions for paths inside the `#[derive(...)]` attribute with the given `ExpnId`
882886
/// back from resolver.
883-
fn take_derive_resolutions(&mut self, expn_id: ExpnId) -> Option<DeriveResolutions>;
887+
fn take_derive_resolutions(&mut self, expn_id: LocalExpnId) -> Option<DeriveResolutions>;
884888
/// Path resolution logic for `#[cfg_accessible(path)]`.
885-
fn cfg_accessible(&mut self, expn_id: ExpnId, path: &ast::Path) -> Result<bool, Indeterminate>;
889+
fn cfg_accessible(
890+
&mut self,
891+
expn_id: LocalExpnId,
892+
path: &ast::Path,
893+
) -> Result<bool, Indeterminate>;
886894

887895
/// Decodes the proc-macro quoted span in the specified crate, with the specified id.
888896
/// No caching is performed.
@@ -913,7 +921,7 @@ impl ModuleData {
913921

914922
#[derive(Clone)]
915923
pub struct ExpansionData {
916-
pub id: ExpnId,
924+
pub id: LocalExpnId,
917925
pub depth: usize,
918926
pub module: Rc<ModuleData>,
919927
pub dir_ownership: DirOwnership,
@@ -958,7 +966,7 @@ impl<'a> ExtCtxt<'a> {
958966
extern_mod_loaded,
959967
root_path: PathBuf::new(),
960968
current_expansion: ExpansionData {
961-
id: ExpnId::root(),
969+
id: LocalExpnId::ROOT,
962970
depth: 0,
963971
module: Default::default(),
964972
dir_ownership: DirOwnership::Owned { relative: None },
@@ -995,19 +1003,19 @@ impl<'a> ExtCtxt<'a> {
9951003
/// Equivalent of `Span::def_site` from the proc macro API,
9961004
/// except that the location is taken from the span passed as an argument.
9971005
pub fn with_def_site_ctxt(&self, span: Span) -> Span {
998-
span.with_def_site_ctxt(self.current_expansion.id)
1006+
span.with_def_site_ctxt(self.current_expansion.id.to_expn_id())
9991007
}
10001008

10011009
/// Equivalent of `Span::call_site` from the proc macro API,
10021010
/// except that the location is taken from the span passed as an argument.
10031011
pub fn with_call_site_ctxt(&self, span: Span) -> Span {
1004-
span.with_call_site_ctxt(self.current_expansion.id)
1012+
span.with_call_site_ctxt(self.current_expansion.id.to_expn_id())
10051013
}
10061014

10071015
/// Equivalent of `Span::mixed_site` from the proc macro API,
10081016
/// except that the location is taken from the span passed as an argument.
10091017
pub fn with_mixed_site_ctxt(&self, span: Span) -> Span {
1010-
span.with_mixed_site_ctxt(self.current_expansion.id)
1018+
span.with_mixed_site_ctxt(self.current_expansion.id.to_expn_id())
10111019
}
10121020

10131021
/// Returns span for the macro which originally caused the current expansion to happen.

Diff for: compiler/rustc_expand/src/expand.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rustc_session::lint::BuiltinLintDiagnostics;
3131
use rustc_session::parse::{feature_err, ParseSess};
3232
use rustc_session::Limit;
3333
use rustc_span::symbol::{sym, Ident};
34-
use rustc_span::{ExpnId, FileName, Span};
34+
use rustc_span::{FileName, LocalExpnId, Span};
3535

3636
use smallvec::{smallvec, SmallVec};
3737
use std::ops::DerefMut;
@@ -508,7 +508,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
508508
.map(|(path, item, _exts)| {
509509
// FIXME: Consider using the derive resolutions (`_exts`)
510510
// instead of enqueuing the derives to be resolved again later.
511-
let expn_id = ExpnId::fresh_empty();
511+
let expn_id = LocalExpnId::fresh_empty();
512512
derive_invocations.push((
513513
Invocation {
514514
kind: InvocationKind::Derive { path, item },
@@ -993,7 +993,7 @@ struct InvocationCollector<'a, 'b> {
993993

994994
impl<'a, 'b> InvocationCollector<'a, 'b> {
995995
fn collect(&mut self, fragment_kind: AstFragmentKind, kind: InvocationKind) -> AstFragment {
996-
let expn_id = ExpnId::fresh_empty();
996+
let expn_id = LocalExpnId::fresh_empty();
997997
let vis = kind.placeholder_visibility();
998998
self.invocations.push((
999999
Invocation {

Diff for: compiler/rustc_expand/src/mbe/transcribe.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree, TreeAndSpacing};
88
use rustc_data_structures::fx::FxHashMap;
99
use rustc_data_structures::sync::Lrc;
1010
use rustc_errors::{pluralize, PResult};
11-
use rustc_span::hygiene::{ExpnId, Transparency};
11+
use rustc_span::hygiene::{LocalExpnId, Transparency};
1212
use rustc_span::symbol::MacroRulesNormalizedIdent;
1313
use rustc_span::Span;
1414

1515
use smallvec::{smallvec, SmallVec};
1616
use std::mem;
1717

1818
// A Marker adds the given mark to the syntax context.
19-
struct Marker(ExpnId, Transparency);
19+
struct Marker(LocalExpnId, Transparency);
2020

2121
impl MutVisitor for Marker {
2222
fn token_visiting_enabled(&self) -> bool {
2323
true
2424
}
2525

2626
fn visit_span(&mut self, span: &mut Span) {
27-
*span = span.apply_mark(self.0, self.1)
27+
*span = span.apply_mark(self.0.to_expn_id(), self.1)
2828
}
2929
}
3030

0 commit comments

Comments
 (0)