Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expansion/hygiene: Some renaming, refactoring and comments #51726

Merged
merged 11 commits into from
Jun 24, 2018
Merged
3 changes: 0 additions & 3 deletions src/libproc_macro/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ impl ProcMacro for Quoter {
_: ::syntax_pos::Span,
stream: tokenstream::TokenStream)
-> tokenstream::TokenStream {
let mut info = cx.current_expansion.mark.expn_info().unwrap();
info.callee.allow_internal_unstable = true;
cx.current_expansion.mark.set_expn_info(info);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this work, exactly? Does it rely on hygiene instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, I see that SyntaxExtension::ProcMacro got an extra field.

::__internal::set_sess(cx, || TokenStream(stream).quote().0)
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,13 +612,11 @@ impl<'a> LoweringContext<'a> {
let mark = Mark::fresh(Mark::root());
mark.set_expn_info(codemap::ExpnInfo {
call_site: span,
callee: codemap::NameAndSpan {
format: codemap::CompilerDesugaring(reason),
span: Some(span),
allow_internal_unstable: true,
allow_internal_unsafe: false,
edition: codemap::hygiene::default_edition(),
},
def_site: Some(span),
format: codemap::CompilerDesugaring(reason),
allow_internal_unstable: true,
allow_internal_unsafe: false,
edition: codemap::hygiene::default_edition(),
});
span.with_ctxt(SyntaxContext::empty().apply_mark(mark))
}
Expand Down
31 changes: 18 additions & 13 deletions src/librustc/hir/map/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,13 @@ pub struct Definitions {
node_to_def_index: NodeMap<DefIndex>,
def_index_to_node: [Vec<ast::NodeId>; 2],
pub(super) node_to_hir_id: IndexVec<ast::NodeId, hir::HirId>,
macro_def_scopes: FxHashMap<Mark, DefId>,
expansions: FxHashMap<DefIndex, Mark>,
/// If `Mark` is an ID of some macro expansion,
/// then `DefId` is the normal module (`mod`) in which the expanded macro was defined.
parent_modules_of_macro_defs: FxHashMap<Mark, DefId>,
/// Item with a given `DefIndex` was defined during opaque macro expansion with ID `Mark`.
/// It can actually be defined during transparent macro expansions inside that opaque expansion,
/// but transparent expansions are ignored here.
opaque_expansions_that_defined: FxHashMap<DefIndex, Mark>,
next_disambiguator: FxHashMap<(DefIndex, DefPathData), u32>,
def_index_to_span: FxHashMap<DefIndex, Span>,
}
Expand All @@ -175,8 +180,8 @@ impl Clone for Definitions {
self.def_index_to_node[1].clone(),
],
node_to_hir_id: self.node_to_hir_id.clone(),
macro_def_scopes: self.macro_def_scopes.clone(),
expansions: self.expansions.clone(),
parent_modules_of_macro_defs: self.parent_modules_of_macro_defs.clone(),
opaque_expansions_that_defined: self.opaque_expansions_that_defined.clone(),
next_disambiguator: self.next_disambiguator.clone(),
def_index_to_span: self.def_index_to_span.clone(),
}
Expand Down Expand Up @@ -397,8 +402,8 @@ impl Definitions {
node_to_def_index: NodeMap(),
def_index_to_node: [vec![], vec![]],
node_to_hir_id: IndexVec::new(),
macro_def_scopes: FxHashMap(),
expansions: FxHashMap(),
parent_modules_of_macro_defs: FxHashMap(),
opaque_expansions_that_defined: FxHashMap(),
next_disambiguator: FxHashMap(),
def_index_to_span: FxHashMap(),
}
Expand Down Expand Up @@ -580,7 +585,7 @@ impl Definitions {

let expansion = expansion.modern();
if expansion != Mark::root() {
self.expansions.insert(index, expansion);
self.opaque_expansions_that_defined.insert(index, expansion);
}

// The span is added if it isn't DUMMY_SP
Expand All @@ -600,16 +605,16 @@ impl Definitions {
self.node_to_hir_id = mapping;
}

pub fn expansion(&self, index: DefIndex) -> Mark {
self.expansions.get(&index).cloned().unwrap_or(Mark::root())
pub fn opaque_expansion_that_defined(&self, index: DefIndex) -> Mark {
self.opaque_expansions_that_defined.get(&index).cloned().unwrap_or(Mark::root())
}

pub fn macro_def_scope(&self, mark: Mark) -> DefId {
self.macro_def_scopes[&mark]
pub fn parent_module_of_macro_def(&self, mark: Mark) -> DefId {
self.parent_modules_of_macro_defs[&mark]
}

pub fn add_macro_def_scope(&mut self, mark: Mark, scope: DefId) {
self.macro_def_scopes.insert(mark, scope);
pub fn add_parent_module_of_macro_def(&mut self, mark: Mark, module: DefId) {
self.parent_modules_of_macro_defs.insert(mark, module);
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/librustc/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,15 +391,11 @@ impl_stable_hash_for!(enum ::syntax::ast::MetaItemKind {

impl_stable_hash_for!(struct ::syntax_pos::hygiene::ExpnInfo {
call_site,
callee
});

impl_stable_hash_for!(struct ::syntax_pos::hygiene::NameAndSpan {
def_site,
format,
allow_internal_unstable,
allow_internal_unsafe,
edition,
span
edition
});

impl_stable_hash_for!(enum ::syntax_pos::hygiene::ExpnFormat {
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
}

if let Some(k) = obligation.cause.span.compiler_desugaring_kind() {
let desugaring = k.as_symbol().as_str();
flags.push(("from_desugaring".to_string(), None));
flags.push(("from_desugaring".to_string(), Some(desugaring.to_string())));
flags.push(("from_desugaring".to_string(), Some(k.name().to_string())));
}
let generics = self.tcx.generics_of(def_id);
let self_ty = trait_ref.self_ty();
Expand Down
11 changes: 6 additions & 5 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2732,13 +2732,14 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}

pub fn adjust_ident(self, mut ident: Ident, scope: DefId, block: NodeId) -> (Ident, DefId) {
let expansion = match scope.krate {
LOCAL_CRATE => self.hir.definitions().expansion(scope.index),
ident = ident.modern();
let target_expansion = match scope.krate {
LOCAL_CRATE => self.hir.definitions().opaque_expansion_that_defined(scope.index),
_ => Mark::root(),
};
ident = ident.modern();
let scope = match ident.span.adjust(expansion) {
Some(macro_def) => self.hir.definitions().macro_def_scope(macro_def),
let scope = match ident.span.adjust(target_expansion) {
Some(actual_expansion) =>
self.hir.definitions().parent_module_of_macro_def(actual_expansion),
None if block == DUMMY_NODE_ID => DefId::local(CRATE_DEF_INDEX), // Dummy DefId
None => self.hir.get_module_parent(block),
};
Expand Down
14 changes: 6 additions & 8 deletions src/librustc_allocator/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use syntax::ast::{Arg, FnHeader, Generics, Mac, Mutability, Ty, Unsafety};
use syntax::ast::{self, Expr, Ident, Item, ItemKind, TyKind, VisibilityKind};
use syntax::attr;
use syntax::codemap::respan;
use syntax::codemap::{ExpnInfo, MacroAttribute, NameAndSpan};
use syntax::codemap::{ExpnInfo, MacroAttribute};
use syntax::ext::base::ExtCtxt;
use syntax::ext::base::Resolver;
use syntax::ext::build::AstBuilder;
Expand Down Expand Up @@ -80,13 +80,11 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
let mark = Mark::fresh(Mark::root());
mark.set_expn_info(ExpnInfo {
call_site: DUMMY_SP,
callee: NameAndSpan {
format: MacroAttribute(Symbol::intern(name)),
span: None,
allow_internal_unstable: true,
allow_internal_unsafe: false,
edition: hygiene::default_edition(),
},
def_site: None,
format: MacroAttribute(Symbol::intern(name)),
allow_internal_unstable: true,
allow_internal_unsafe: false,
edition: hygiene::default_edition(),
});
let span = item.span.with_ctxt(SyntaxContext::empty().apply_mark(mark));
let ecfg = ExpansionConfig::default(name.to_string());
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ impl<'a> CrateLoader<'a> {
name: &str,
expand: fn(TokenStream) -> TokenStream) {
let expand = SyntaxExtension::ProcMacro(
Box::new(BangProcMacro { inner: expand }), self.edition
Box::new(BangProcMacro { inner: expand }), false, self.edition
);
self.extensions.push((Symbol::intern(name), Lrc::new(expand)));
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ impl CrateStore for cstore::CStore {
} else if data.name == "proc_macro" &&
self.get_crate_data(id.krate).item_name(id.index) == "quote" {
let ext = SyntaxExtension::ProcMacro(Box::new(::proc_macro::__internal::Quoter),
data.root.edition);
true, data.root.edition);
return LoadedMacro::ProcMacro(Lrc::new(ext));
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use rustc::hir::{Freevar, FreevarMap, TraitCandidate, TraitMap, GlobMap};
use rustc::util::nodemap::{NodeMap, NodeSet, FxHashMap, FxHashSet, DefIdMap};

use syntax::codemap::CodeMap;
use syntax::ext::hygiene::{Mark, MarkKind, SyntaxContext};
use syntax::ext::hygiene::{Mark, Transparency, SyntaxContext};
use syntax::ast::{self, Name, NodeId, Ident, FloatTy, IntTy, UintTy};
use syntax::ext::base::SyntaxExtension;
use syntax::ext::base::Determinacy::{self, Determined, Undetermined};
Expand Down Expand Up @@ -1988,7 +1988,7 @@ impl<'a> Resolver<'a> {
// When resolving `$crate` from a `macro_rules!` invoked in a `macro`,
// we don't want to pretend that the `macro_rules!` definition is in the `macro`
// as described in `SyntaxContext::apply_mark`, so we ignore prepended modern marks.
ctxt.marks().into_iter().find(|&mark| mark.kind() != MarkKind::Modern)
ctxt.marks().into_iter().find(|&mark| mark.transparency() != Transparency::Opaque)
} else {
ctxt = ctxt.modern();
ctxt.adjust(Mark::root())
Expand Down
26 changes: 14 additions & 12 deletions src/librustc_resolve/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use syntax::attr::{self, HasAttrs};
use syntax::errors::DiagnosticBuilder;
use syntax::ext::base::{self, Annotatable, Determinacy, MultiModifier, MultiDecorator};
use syntax::ext::base::{MacroKind, SyntaxExtension, Resolver as SyntaxResolver};
use syntax::ext::expand::{Expansion, ExpansionKind, Invocation, InvocationKind, find_attr_invoc};
use syntax::ext::hygiene::{self, Mark, MarkKind};
use syntax::ext::expand::{self, AstFragment, AstFragmentKind, Invocation, InvocationKind};
use syntax::ext::hygiene::{self, Mark, Transparency};
use syntax::ext::placeholders::placeholder;
use syntax::ext::tt::macro_rules;
use syntax::feature_gate::{self, emit_feature_err, GateIssue};
Expand Down Expand Up @@ -187,9 +187,10 @@ impl<'a> base::Resolver for Resolver<'a> {
self.whitelisted_legacy_custom_derives.contains(&name)
}

fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion, derives: &[Mark]) {
fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment,
derives: &[Mark]) {
let invocation = self.invocations[&mark];
self.collect_def_ids(mark, invocation, expansion);
self.collect_def_ids(mark, invocation, fragment);

self.current_module = invocation.module.get();
self.current_module.unresolved_invocations.borrow_mut().remove(&mark);
Expand All @@ -202,7 +203,7 @@ impl<'a> base::Resolver for Resolver<'a> {
legacy_scope: LegacyScope::Invocation(invocation),
expansion: mark,
};
expansion.visit_with(&mut visitor);
fragment.visit_with(&mut visitor);
invocation.expansion.set(visitor.legacy_scope);
}

Expand Down Expand Up @@ -326,14 +327,15 @@ impl<'a> base::Resolver for Resolver<'a> {
self.macro_defs.insert(invoc.expansion_data.mark, def_id);
let normal_module_def_id =
self.macro_def_scope(invoc.expansion_data.mark).normal_ancestor_id;
self.definitions.add_macro_def_scope(invoc.expansion_data.mark, normal_module_def_id);
self.definitions.add_parent_module_of_macro_def(invoc.expansion_data.mark,
normal_module_def_id);

self.unused_macros.remove(&def_id);
let ext = self.get_macro(def);
if ext.is_modern() {
invoc.expansion_data.mark.set_kind(MarkKind::Modern);
invoc.expansion_data.mark.set_transparency(Transparency::Opaque);
} else if def_id.krate == BUILTIN_MACROS_CRATE {
invoc.expansion_data.mark.set_kind(MarkKind::Builtin);
invoc.expansion_data.mark.set_is_builtin(true);
}
Ok(Some(ext))
}
Expand Down Expand Up @@ -396,14 +398,14 @@ impl<'a> Resolver<'a> {
Ok(ext) => if let SyntaxExtension::ProcMacroDerive(_, ref inert_attrs, _) = *ext {
if inert_attrs.contains(&attr_name) {
// FIXME(jseyfried) Avoid `mem::replace` here.
let dummy_item = placeholder(ExpansionKind::Items, ast::DUMMY_NODE_ID)
let dummy_item = placeholder(AstFragmentKind::Items, ast::DUMMY_NODE_ID)
.make_items().pop().unwrap();
let dummy_item = Annotatable::Item(dummy_item);
*item = mem::replace(item, dummy_item).map_attrs(|mut attrs| {
let inert_attr = attr.take().unwrap();
attr::mark_known(&inert_attr);
if self.proc_macro_enabled {
*attr = find_attr_invoc(&mut attrs);
*attr = expand::find_attr_invoc(&mut attrs);
}
attrs.push(inert_attr);
attrs
Expand Down Expand Up @@ -769,7 +771,7 @@ impl<'a> Resolver<'a> {
fn collect_def_ids(&mut self,
mark: Mark,
invocation: &'a InvocationData<'a>,
expansion: &Expansion) {
fragment: &AstFragment) {
let Resolver { ref mut invocations, arenas, graph_root, .. } = *self;
let InvocationData { def_index, .. } = *invocation;

Expand All @@ -787,7 +789,7 @@ impl<'a> Resolver<'a> {
let mut def_collector = DefCollector::new(&mut self.definitions, mark);
def_collector.visit_macro_invoc = Some(visit_macro_invoc);
def_collector.with_parent(def_index, |def_collector| {
expansion.visit_with(def_collector)
fragment.visit_with(def_collector)
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc_save_analysis/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
let callsite = span.source_callsite();
let callsite_span = self.span_from_span(callsite);
let callee = span.source_callee()?;
let callee_span = callee.span?;
let callee_span = callee.def_site?;

// Ignore attribute macros, their spans are usually mangled
if let MacroAttribute(_) = callee.format {
Expand Down Expand Up @@ -872,7 +872,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
let callee_span = self.span_from_span(callee_span);
Some(MacroRef {
span: callsite_span,
qualname: callee.name().to_string(), // FIXME: generate the real qualname
qualname: callee.format.name().to_string(), // FIXME: generate the real qualname
callee_span,
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/codemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


pub use syntax_pos::*;
pub use syntax_pos::hygiene::{ExpnFormat, ExpnInfo, NameAndSpan};
pub use syntax_pos::hygiene::{ExpnFormat, ExpnInfo};
pub use self::ExpnFormat::*;

use rustc_data_structures::fx::FxHashMap;
Expand Down
16 changes: 11 additions & 5 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use codemap::{self, CodeMap, Spanned, respan};
use syntax_pos::{Span, MultiSpan, DUMMY_SP};
use edition::Edition;
use errors::{DiagnosticBuilder, DiagnosticId};
use ext::expand::{self, Expansion, Invocation};
use ext::expand::{self, AstFragment, Invocation};
use ext::hygiene::{self, Mark, SyntaxContext};
use fold::{self, Folder};
use parse::{self, parser, DirectoryOwnership};
Expand Down Expand Up @@ -597,7 +597,11 @@ pub enum SyntaxExtension {
MultiModifier(Box<MultiItemModifier + sync::Sync + sync::Send>),

/// A function-like procedural macro. TokenStream -> TokenStream.
ProcMacro(Box<ProcMacro + sync::Sync + sync::Send>, Edition),
ProcMacro(
/* expander: */ Box<ProcMacro + sync::Sync + sync::Send>,
/* allow_internal_unstable: */ bool,
/* edition: */ Edition,
),

/// An attribute-like procedural macro. TokenStream, TokenStream -> TokenStream.
/// The first TokenSteam is the attribute, the second is the annotated item.
Expand Down Expand Up @@ -697,7 +701,8 @@ pub trait Resolver {
fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item>;
fn is_whitelisted_legacy_custom_derive(&self, name: Name) -> bool;

fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion, derives: &[Mark]);
fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment,
derives: &[Mark]);
fn add_builtin(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>);

fn resolve_imports(&mut self);
Expand Down Expand Up @@ -726,7 +731,8 @@ impl Resolver for DummyResolver {
fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item> { item }
fn is_whitelisted_legacy_custom_derive(&self, _name: Name) -> bool { false }

fn visit_expansion(&mut self, _invoc: Mark, _expansion: &Expansion, _derives: &[Mark]) {}
fn visit_ast_fragment_with_placeholders(&mut self, _invoc: Mark, _fragment: &AstFragment,
_derives: &[Mark]) {}
fn add_builtin(&mut self, _ident: ast::Ident, _ext: Lrc<SyntaxExtension>) {}

fn resolve_imports(&mut self) {}
Expand Down Expand Up @@ -828,7 +834,7 @@ impl<'a> ExtCtxt<'a> {
let mut last_macro = None;
loop {
if ctxt.outer().expn_info().map_or(None, |info| {
if info.callee.name() == "include" {
if info.format.name() == "include" {
// Stop going up the backtrace once include! is encountered
return None;
}
Expand Down
Loading