Skip to content

Commit

Permalink
Auto merge of #86676 - cjgillot:localexpn, r=petrochenkov
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
bors committed Jul 17, 2021
2 parents c78ebb7 + b35ceee commit 68511b5
Show file tree
Hide file tree
Showing 27 changed files with 615 additions and 436 deletions.
8 changes: 4 additions & 4 deletions compiler/rustc_ast/src/node_id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_span::ExpnId;
use rustc_span::LocalExpnId;
use std::fmt;

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

impl NodeId {
pub fn placeholder_from_expn_id(expn_id: ExpnId) -> Self {
pub fn placeholder_from_expn_id(expn_id: LocalExpnId) -> Self {
NodeId::from_u32(expn_id.as_u32())
}

pub fn placeholder_to_expn_id(self) -> ExpnId {
ExpnId::from_u32(self.as_u32())
pub fn placeholder_to_expn_id(self) -> LocalExpnId {
LocalExpnId::from_u32(self.as_u32())
}
}

Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_builtin_macros/src/deriving/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ pub fn expand_deriving_clone(
Annotatable::Item(ref annitem) => match annitem.kind {
ItemKind::Struct(_, Generics { ref params, .. })
| ItemKind::Enum(_, Generics { ref params, .. }) => {
let container_id = cx.current_expansion.id.expn_data().parent;
if cx.resolver.has_derive_copy(container_id)
let container_id = cx.current_expansion.id.expn_data().parent.expect_local();
let has_derive_copy = cx.resolver.has_derive_copy(container_id);
if has_derive_copy
&& !params
.iter()
.any(|param| matches!(param.kind, ast::GenericParamKind::Type { .. }))
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ impl<'a> TraitDef<'a> {
.any(|param| matches!(param.kind, ast::GenericParamKind::Type { .. })),
_ => unreachable!(),
};
let container_id = cx.current_expansion.id.expn_data().parent;
let container_id = cx.current_expansion.id.expn_data().parent.expect_local();
let always_copy = has_no_type_params && cx.resolver.has_derive_copy(container_id);
let use_temporaries = is_packed && always_copy;

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/proc_macro_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ fn mk_decls(
&[sym::rustc_attrs, sym::proc_macro_internals],
None,
);
let span = DUMMY_SP.with_def_site_ctxt(expn_id);
let span = DUMMY_SP.with_def_site_ctxt(expn_id.to_expn_id());

let proc_macro = Ident::new(sym::proc_macro, span);
let krate = cx.item(span, proc_macro, Vec::new(), ast::ItemKind::ExternCrate(None));
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/standard_library_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ pub fn inject(
&[sym::prelude_import],
None,
);
let span = DUMMY_SP.with_def_site_ctxt(expn_id);
let call_site = DUMMY_SP.with_call_site_ctxt(expn_id);
let span = DUMMY_SP.with_def_site_ctxt(expn_id.to_expn_id());
let call_site = DUMMY_SP.with_call_site_ctxt(expn_id.to_expn_id());

let ecfg = ExpansionConfig::default("std_lib_injection".to_string());
let cx = ExtCtxt::new(sess, ecfg, resolver, None);
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_builtin_macros/src/test_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ impl<'a> MutVisitor for TestHarnessGenerator<'a> {
for test in &mut tests {
// See the comment on `mk_main` for why we're using
// `apply_mark` directly.
test.ident.span = test.ident.span.apply_mark(expn_id, Transparency::Opaque);
test.ident.span =
test.ident.span.apply_mark(expn_id.to_expn_id(), Transparency::Opaque);
}
self.cx.test_cases.extend(tests);
}
Expand Down Expand Up @@ -223,7 +224,7 @@ fn generate_test_harness(
&[sym::test, sym::rustc_attrs],
None,
);
let def_site = DUMMY_SP.with_def_site_ctxt(expn_id);
let def_site = DUMMY_SP.with_def_site_ctxt(expn_id.to_expn_id());

// Remove the entry points
let mut cleaner = EntryPointCleaner { sess, depth: 0, def_site };
Expand Down
40 changes: 24 additions & 16 deletions compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_parse::{self, nt_to_tokenstream, parser, MACRO_ARGUMENTS};
use rustc_session::{parse::ParseSess, Limit, Session};
use rustc_span::def_id::{CrateNum, DefId};
use rustc_span::edition::Edition;
use rustc_span::hygiene::{AstPass, ExpnData, ExpnId, ExpnKind};
use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId};
use rustc_span::source_map::SourceMap;
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{FileName, MultiSpan, Span, DUMMY_SP};
Expand Down Expand Up @@ -813,15 +813,15 @@ impl SyntaxExtension {

pub fn expn_data(
&self,
parent: ExpnId,
parent: LocalExpnId,
call_site: Span,
descr: Symbol,
macro_def_id: Option<DefId>,
parent_module: Option<DefId>,
) -> ExpnData {
ExpnData::new(
ExpnKind::Macro(self.macro_kind(), descr),
parent,
parent.to_expn_id(),
call_site,
self.span,
self.allow_internal_unstable.clone(),
Expand All @@ -843,7 +843,11 @@ pub trait ResolverExpand {
fn next_node_id(&mut self) -> NodeId;

fn resolve_dollar_crates(&mut self);
fn visit_ast_fragment_with_placeholders(&mut self, expn_id: ExpnId, fragment: &AstFragment);
fn visit_ast_fragment_with_placeholders(
&mut self,
expn_id: LocalExpnId,
fragment: &AstFragment,
);
fn register_builtin_macro(&mut self, name: Symbol, ext: SyntaxExtensionKind);

fn expansion_for_ast_pass(
Expand All @@ -852,37 +856,41 @@ pub trait ResolverExpand {
pass: AstPass,
features: &[Symbol],
parent_module_id: Option<NodeId>,
) -> ExpnId;
) -> LocalExpnId;

fn resolve_imports(&mut self);

fn resolve_macro_invocation(
&mut self,
invoc: &Invocation,
eager_expansion_root: ExpnId,
eager_expansion_root: LocalExpnId,
force: bool,
) -> Result<Lrc<SyntaxExtension>, Indeterminate>;

fn check_unused_macros(&mut self);

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

// Resolver interfaces for specific built-in macros.
/// Does `#[derive(...)]` attribute with the given `ExpnId` have built-in `Copy` inside it?
fn has_derive_copy(&self, expn_id: ExpnId) -> bool;
fn has_derive_copy(&self, expn_id: LocalExpnId) -> bool;
/// Resolve paths inside the `#[derive(...)]` attribute with the given `ExpnId`.
fn resolve_derives(
&mut self,
expn_id: ExpnId,
expn_id: LocalExpnId,
force: bool,
derive_paths: &dyn Fn() -> DeriveResolutions,
) -> Result<(), Indeterminate>;
/// Take resolutions for paths inside the `#[derive(...)]` attribute with the given `ExpnId`
/// back from resolver.
fn take_derive_resolutions(&mut self, expn_id: ExpnId) -> Option<DeriveResolutions>;
fn take_derive_resolutions(&mut self, expn_id: LocalExpnId) -> Option<DeriveResolutions>;
/// Path resolution logic for `#[cfg_accessible(path)]`.
fn cfg_accessible(&mut self, expn_id: ExpnId, path: &ast::Path) -> Result<bool, Indeterminate>;
fn cfg_accessible(
&mut self,
expn_id: LocalExpnId,
path: &ast::Path,
) -> Result<bool, Indeterminate>;

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

#[derive(Clone)]
pub struct ExpansionData {
pub id: ExpnId,
pub id: LocalExpnId,
pub depth: usize,
pub module: Rc<ModuleData>,
pub dir_ownership: DirOwnership,
Expand Down Expand Up @@ -958,7 +966,7 @@ impl<'a> ExtCtxt<'a> {
extern_mod_loaded,
root_path: PathBuf::new(),
current_expansion: ExpansionData {
id: ExpnId::root(),
id: LocalExpnId::ROOT,
depth: 0,
module: Default::default(),
dir_ownership: DirOwnership::Owned { relative: None },
Expand Down Expand Up @@ -995,19 +1003,19 @@ impl<'a> ExtCtxt<'a> {
/// Equivalent of `Span::def_site` from the proc macro API,
/// except that the location is taken from the span passed as an argument.
pub fn with_def_site_ctxt(&self, span: Span) -> Span {
span.with_def_site_ctxt(self.current_expansion.id)
span.with_def_site_ctxt(self.current_expansion.id.to_expn_id())
}

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

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

/// Returns span for the macro which originally caused the current expansion to happen.
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use rustc_session::lint::BuiltinLintDiagnostics;
use rustc_session::parse::{feature_err, ParseSess};
use rustc_session::Limit;
use rustc_span::symbol::{sym, Ident};
use rustc_span::{ExpnId, FileName, Span};
use rustc_span::{FileName, LocalExpnId, Span};

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

impl<'a, 'b> InvocationCollector<'a, 'b> {
fn collect(&mut self, fragment_kind: AstFragmentKind, kind: InvocationKind) -> AstFragment {
let expn_id = ExpnId::fresh_empty();
let expn_id = LocalExpnId::fresh_empty();
let vis = kind.placeholder_visibility();
self.invocations.push((
Invocation {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_expand/src/mbe/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree, TreeAndSpacing};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sync::Lrc;
use rustc_errors::{pluralize, PResult};
use rustc_span::hygiene::{ExpnId, Transparency};
use rustc_span::hygiene::{LocalExpnId, Transparency};
use rustc_span::symbol::MacroRulesNormalizedIdent;
use rustc_span::Span;

use smallvec::{smallvec, SmallVec};
use std::mem;

// A Marker adds the given mark to the syntax context.
struct Marker(ExpnId, Transparency);
struct Marker(LocalExpnId, Transparency);

impl MutVisitor for Marker {
fn token_visiting_enabled(&self) -> bool {
true
}

fn visit_span(&mut self, span: &mut Span) {
*span = span.apply_mark(self.0, self.1)
*span = span.apply_mark(self.0.to_expn_id(), self.1)
}
}

Expand Down
Loading

0 comments on commit 68511b5

Please sign in to comment.