Skip to content

Commit

Permalink
Use LocalExpnId where possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Jul 17, 2021
1 parent 6e78d6c commit 078dd37
Show file tree
Hide file tree
Showing 19 changed files with 169 additions and 147 deletions.
10 changes: 5 additions & 5 deletions compiler/rustc_ast/src/node_id.rs
@@ -1,4 +1,4 @@
use rustc_span::{ExpnId, LocalExpnId};
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 {
NodeId::from_u32(expn_id.expect_local().as_u32())
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 {
LocalExpnId::from_u32(self.as_u32()).to_expn_id()
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
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
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
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
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
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
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
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
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
37 changes: 24 additions & 13 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Expand Up @@ -31,7 +31,7 @@ use rustc_middle::bug;
use rustc_middle::hir::exports::Export;
use rustc_middle::middle::cstore::CrateStore;
use rustc_middle::ty;
use rustc_span::hygiene::{ExpnId, MacroKind};
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind};
use rustc_span::source_map::{respan, Spanned};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::Span;
Expand All @@ -42,7 +42,7 @@ use tracing::debug;

type Res = def::Res<NodeId>;

impl<'a> ToNameBinding<'a> for (Module<'a>, ty::Visibility, Span, ExpnId) {
impl<'a> ToNameBinding<'a> for (Module<'a>, ty::Visibility, Span, LocalExpnId) {
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
arenas.alloc_name_binding(NameBinding {
kind: NameBindingKind::Module(self.0),
Expand All @@ -54,7 +54,7 @@ impl<'a> ToNameBinding<'a> for (Module<'a>, ty::Visibility, Span, ExpnId) {
}
}

impl<'a> ToNameBinding<'a> for (Res, ty::Visibility, Span, ExpnId) {
impl<'a> ToNameBinding<'a> for (Res, ty::Visibility, Span, LocalExpnId) {
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
arenas.alloc_name_binding(NameBinding {
kind: NameBindingKind::Res(self.0, false),
Expand All @@ -68,7 +68,7 @@ impl<'a> ToNameBinding<'a> for (Res, ty::Visibility, Span, ExpnId) {

struct IsMacroExport;

impl<'a> ToNameBinding<'a> for (Res, ty::Visibility, Span, ExpnId, IsMacroExport) {
impl<'a> ToNameBinding<'a> for (Res, ty::Visibility, Span, LocalExpnId, IsMacroExport) {
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
arenas.alloc_name_binding(NameBinding {
kind: NameBindingKind::Res(self.0, true),
Expand Down Expand Up @@ -157,7 +157,12 @@ impl<'a> Resolver<'a> {
crate fn macro_def_scope(&mut self, expn_id: ExpnId) -> Module<'a> {
let def_id = match expn_id.expn_data().macro_def_id {
Some(def_id) => def_id,
None => return self.ast_transform_scopes.get(&expn_id).unwrap_or(&self.graph_root),
None => {
return expn_id
.as_local()
.and_then(|expn_id| self.ast_transform_scopes.get(&expn_id))
.unwrap_or(&self.graph_root);
}
};
self.macro_def_scope_from_def_id(def_id)
}
Expand Down Expand Up @@ -739,7 +744,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
if ptr::eq(parent, self.r.graph_root) {
if let Some(entry) = self.r.extern_prelude.get(&ident.normalize_to_macros_2_0())
{
if expansion != ExpnId::root()
if expansion != LocalExpnId::ROOT
&& orig_name.is_some()
&& entry.extern_crate_item.is_none()
{
Expand Down Expand Up @@ -769,7 +774,13 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
no_implicit_prelude: parent.no_implicit_prelude || {
self.r.session.contains_name(&item.attrs, sym::no_implicit_prelude)
},
..ModuleData::new(Some(parent), module_kind, def_id, expansion, item.span)
..ModuleData::new(
Some(parent),
module_kind,
def_id,
expansion.to_expn_id(),
item.span,
)
});
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
self.r.module_map.insert(local_def_id, module);
Expand Down Expand Up @@ -808,7 +819,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
parent,
module_kind,
parent.nearest_parent_mod,
expansion,
expansion.to_expn_id(),
item.span,
);
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
Expand Down Expand Up @@ -883,7 +894,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
parent,
module_kind,
parent.nearest_parent_mod,
expansion,
expansion.to_expn_id(),
item.span,
);
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
Expand Down Expand Up @@ -926,7 +937,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
parent,
ModuleKind::Block(block.id),
parent.nearest_parent_mod,
expansion,
expansion.to_expn_id(),
block.span,
);
self.r.block_map.insert(block.id, module);
Expand All @@ -946,7 +957,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
parent,
ModuleKind::Def(kind, def_id, ident.name),
def_id,
expansion,
expansion.to_expn_id(),
span,
);
self.r.define(parent, ident, TypeNS, (module, vis, span, expansion));
Expand Down Expand Up @@ -1112,7 +1123,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
})
};

let allow_shadowing = self.parent_scope.expansion == ExpnId::root();
let allow_shadowing = self.parent_scope.expansion == LocalExpnId::ROOT;
if let Some(span) = import_all {
let import = macro_use_import(self, span);
self.r.potentially_unused_imports.push(import);
Expand Down Expand Up @@ -1175,7 +1186,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
false
}

fn visit_invoc(&mut self, id: NodeId) -> ExpnId {
fn visit_invoc(&mut self, id: NodeId) -> LocalExpnId {
let invoc_id = id.placeholder_to_expn_id();
let old_parent_scope = self.r.invocation_parent_scopes.insert(invoc_id, self.parent_scope);
assert!(old_parent_scope.is_none(), "invocation data is reset for an invocation");
Expand Down

0 comments on commit 078dd37

Please sign in to comment.