Skip to content

Commit 3874676

Browse files
committed
Auto merge of #51726 - petrochenkov:hygclean, r=oli-obk
expansion/hygiene: Some renaming, refactoring and comments Pure refactoring, no functional changes. Commits are isolated and self-descriptive.
2 parents 8fb1180 + 20ce910 commit 3874676

File tree

26 files changed

+495
-445
lines changed

26 files changed

+495
-445
lines changed

src/libproc_macro/quote.rs

-3
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ impl ProcMacro for Quoter {
8585
_: ::syntax_pos::Span,
8686
stream: tokenstream::TokenStream)
8787
-> tokenstream::TokenStream {
88-
let mut info = cx.current_expansion.mark.expn_info().unwrap();
89-
info.callee.allow_internal_unstable = true;
90-
cx.current_expansion.mark.set_expn_info(info);
9188
::__internal::set_sess(cx, || TokenStream(stream).quote().0)
9289
}
9390
}

src/librustc/hir/lowering.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -612,13 +612,11 @@ impl<'a> LoweringContext<'a> {
612612
let mark = Mark::fresh(Mark::root());
613613
mark.set_expn_info(codemap::ExpnInfo {
614614
call_site: span,
615-
callee: codemap::NameAndSpan {
616-
format: codemap::CompilerDesugaring(reason),
617-
span: Some(span),
618-
allow_internal_unstable: true,
619-
allow_internal_unsafe: false,
620-
edition: codemap::hygiene::default_edition(),
621-
},
615+
def_site: Some(span),
616+
format: codemap::CompilerDesugaring(reason),
617+
allow_internal_unstable: true,
618+
allow_internal_unsafe: false,
619+
edition: codemap::hygiene::default_edition(),
622620
});
623621
span.with_ctxt(SyntaxContext::empty().apply_mark(mark))
624622
}

src/librustc/hir/map/definitions.rs

+18-13
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,13 @@ pub struct Definitions {
157157
node_to_def_index: NodeMap<DefIndex>,
158158
def_index_to_node: [Vec<ast::NodeId>; 2],
159159
pub(super) node_to_hir_id: IndexVec<ast::NodeId, hir::HirId>,
160-
macro_def_scopes: FxHashMap<Mark, DefId>,
161-
expansions: FxHashMap<DefIndex, Mark>,
160+
/// If `Mark` is an ID of some macro expansion,
161+
/// then `DefId` is the normal module (`mod`) in which the expanded macro was defined.
162+
parent_modules_of_macro_defs: FxHashMap<Mark, DefId>,
163+
/// Item with a given `DefIndex` was defined during opaque macro expansion with ID `Mark`.
164+
/// It can actually be defined during transparent macro expansions inside that opaque expansion,
165+
/// but transparent expansions are ignored here.
166+
opaque_expansions_that_defined: FxHashMap<DefIndex, Mark>,
162167
next_disambiguator: FxHashMap<(DefIndex, DefPathData), u32>,
163168
def_index_to_span: FxHashMap<DefIndex, Span>,
164169
}
@@ -175,8 +180,8 @@ impl Clone for Definitions {
175180
self.def_index_to_node[1].clone(),
176181
],
177182
node_to_hir_id: self.node_to_hir_id.clone(),
178-
macro_def_scopes: self.macro_def_scopes.clone(),
179-
expansions: self.expansions.clone(),
183+
parent_modules_of_macro_defs: self.parent_modules_of_macro_defs.clone(),
184+
opaque_expansions_that_defined: self.opaque_expansions_that_defined.clone(),
180185
next_disambiguator: self.next_disambiguator.clone(),
181186
def_index_to_span: self.def_index_to_span.clone(),
182187
}
@@ -397,8 +402,8 @@ impl Definitions {
397402
node_to_def_index: NodeMap(),
398403
def_index_to_node: [vec![], vec![]],
399404
node_to_hir_id: IndexVec::new(),
400-
macro_def_scopes: FxHashMap(),
401-
expansions: FxHashMap(),
405+
parent_modules_of_macro_defs: FxHashMap(),
406+
opaque_expansions_that_defined: FxHashMap(),
402407
next_disambiguator: FxHashMap(),
403408
def_index_to_span: FxHashMap(),
404409
}
@@ -580,7 +585,7 @@ impl Definitions {
580585

581586
let expansion = expansion.modern();
582587
if expansion != Mark::root() {
583-
self.expansions.insert(index, expansion);
588+
self.opaque_expansions_that_defined.insert(index, expansion);
584589
}
585590

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

603-
pub fn expansion(&self, index: DefIndex) -> Mark {
604-
self.expansions.get(&index).cloned().unwrap_or(Mark::root())
608+
pub fn opaque_expansion_that_defined(&self, index: DefIndex) -> Mark {
609+
self.opaque_expansions_that_defined.get(&index).cloned().unwrap_or(Mark::root())
605610
}
606611

607-
pub fn macro_def_scope(&self, mark: Mark) -> DefId {
608-
self.macro_def_scopes[&mark]
612+
pub fn parent_module_of_macro_def(&self, mark: Mark) -> DefId {
613+
self.parent_modules_of_macro_defs[&mark]
609614
}
610615

611-
pub fn add_macro_def_scope(&mut self, mark: Mark, scope: DefId) {
612-
self.macro_def_scopes.insert(mark, scope);
616+
pub fn add_parent_module_of_macro_def(&mut self, mark: Mark, module: DefId) {
617+
self.parent_modules_of_macro_defs.insert(mark, module);
613618
}
614619
}
615620

src/librustc/ich/impls_syntax.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -391,15 +391,11 @@ impl_stable_hash_for!(enum ::syntax::ast::MetaItemKind {
391391

392392
impl_stable_hash_for!(struct ::syntax_pos::hygiene::ExpnInfo {
393393
call_site,
394-
callee
395-
});
396-
397-
impl_stable_hash_for!(struct ::syntax_pos::hygiene::NameAndSpan {
394+
def_site,
398395
format,
399396
allow_internal_unstable,
400397
allow_internal_unsafe,
401-
edition,
402-
span
398+
edition
403399
});
404400

405401
impl_stable_hash_for!(enum ::syntax_pos::hygiene::ExpnFormat {

src/librustc/traits/error_reporting.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
366366
}
367367

368368
if let Some(k) = obligation.cause.span.compiler_desugaring_kind() {
369-
let desugaring = k.as_symbol().as_str();
370369
flags.push(("from_desugaring".to_string(), None));
371-
flags.push(("from_desugaring".to_string(), Some(desugaring.to_string())));
370+
flags.push(("from_desugaring".to_string(), Some(k.name().to_string())));
372371
}
373372
let generics = self.tcx.generics_of(def_id);
374373
let self_ty = trait_ref.self_ty();

src/librustc/ty/mod.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -2732,13 +2732,14 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
27322732
}
27332733

27342734
pub fn adjust_ident(self, mut ident: Ident, scope: DefId, block: NodeId) -> (Ident, DefId) {
2735-
let expansion = match scope.krate {
2736-
LOCAL_CRATE => self.hir.definitions().expansion(scope.index),
2735+
ident = ident.modern();
2736+
let target_expansion = match scope.krate {
2737+
LOCAL_CRATE => self.hir.definitions().opaque_expansion_that_defined(scope.index),
27372738
_ => Mark::root(),
27382739
};
2739-
ident = ident.modern();
2740-
let scope = match ident.span.adjust(expansion) {
2741-
Some(macro_def) => self.hir.definitions().macro_def_scope(macro_def),
2740+
let scope = match ident.span.adjust(target_expansion) {
2741+
Some(actual_expansion) =>
2742+
self.hir.definitions().parent_module_of_macro_def(actual_expansion),
27422743
None if block == DUMMY_NODE_ID => DefId::local(CRATE_DEF_INDEX), // Dummy DefId
27432744
None => self.hir.get_module_parent(block),
27442745
};

src/librustc_allocator/expand.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use syntax::ast::{Arg, FnHeader, Generics, Mac, Mutability, Ty, Unsafety};
1515
use syntax::ast::{self, Expr, Ident, Item, ItemKind, TyKind, VisibilityKind};
1616
use syntax::attr;
1717
use syntax::codemap::respan;
18-
use syntax::codemap::{ExpnInfo, MacroAttribute, NameAndSpan};
18+
use syntax::codemap::{ExpnInfo, MacroAttribute};
1919
use syntax::ext::base::ExtCtxt;
2020
use syntax::ext::base::Resolver;
2121
use syntax::ext::build::AstBuilder;
@@ -80,13 +80,11 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
8080
let mark = Mark::fresh(Mark::root());
8181
mark.set_expn_info(ExpnInfo {
8282
call_site: DUMMY_SP,
83-
callee: NameAndSpan {
84-
format: MacroAttribute(Symbol::intern(name)),
85-
span: None,
86-
allow_internal_unstable: true,
87-
allow_internal_unsafe: false,
88-
edition: hygiene::default_edition(),
89-
},
83+
def_site: None,
84+
format: MacroAttribute(Symbol::intern(name)),
85+
allow_internal_unstable: true,
86+
allow_internal_unsafe: false,
87+
edition: hygiene::default_edition(),
9088
});
9189
let span = item.span.with_ctxt(SyntaxContext::empty().apply_mark(mark));
9290
let ecfg = ExpansionConfig::default(name.to_string());

src/librustc_metadata/creader.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ impl<'a> CrateLoader<'a> {
570570
name: &str,
571571
expand: fn(TokenStream) -> TokenStream) {
572572
let expand = SyntaxExtension::ProcMacro(
573-
Box::new(BangProcMacro { inner: expand }), self.edition
573+
Box::new(BangProcMacro { inner: expand }), false, self.edition
574574
);
575575
self.extensions.push((Symbol::intern(name), Lrc::new(expand)));
576576
}

src/librustc_metadata/cstore_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ impl CrateStore for cstore::CStore {
519519
} else if data.name == "proc_macro" &&
520520
self.get_crate_data(id.krate).item_name(id.index) == "quote" {
521521
let ext = SyntaxExtension::ProcMacro(Box::new(::proc_macro::__internal::Quoter),
522-
data.root.edition);
522+
true, data.root.edition);
523523
return LoadedMacro::ProcMacro(Lrc::new(ext));
524524
}
525525

src/librustc_resolve/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use rustc::hir::{Freevar, FreevarMap, TraitCandidate, TraitMap, GlobMap};
4545
use rustc::util::nodemap::{NodeMap, NodeSet, FxHashMap, FxHashSet, DefIdMap};
4646

4747
use syntax::codemap::CodeMap;
48-
use syntax::ext::hygiene::{Mark, MarkKind, SyntaxContext};
48+
use syntax::ext::hygiene::{Mark, Transparency, SyntaxContext};
4949
use syntax::ast::{self, Name, NodeId, Ident, FloatTy, IntTy, UintTy};
5050
use syntax::ext::base::SyntaxExtension;
5151
use syntax::ext::base::Determinacy::{self, Determined, Undetermined};
@@ -1988,7 +1988,7 @@ impl<'a> Resolver<'a> {
19881988
// When resolving `$crate` from a `macro_rules!` invoked in a `macro`,
19891989
// we don't want to pretend that the `macro_rules!` definition is in the `macro`
19901990
// as described in `SyntaxContext::apply_mark`, so we ignore prepended modern marks.
1991-
ctxt.marks().into_iter().find(|&mark| mark.kind() != MarkKind::Modern)
1991+
ctxt.marks().into_iter().find(|&mark| mark.transparency() != Transparency::Opaque)
19921992
} else {
19931993
ctxt = ctxt.modern();
19941994
ctxt.adjust(Mark::root())

src/librustc_resolve/macros.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use syntax::attr::{self, HasAttrs};
2323
use syntax::errors::DiagnosticBuilder;
2424
use syntax::ext::base::{self, Annotatable, Determinacy, MultiModifier, MultiDecorator};
2525
use syntax::ext::base::{MacroKind, SyntaxExtension, Resolver as SyntaxResolver};
26-
use syntax::ext::expand::{Expansion, ExpansionKind, Invocation, InvocationKind, find_attr_invoc};
27-
use syntax::ext::hygiene::{self, Mark, MarkKind};
26+
use syntax::ext::expand::{self, AstFragment, AstFragmentKind, Invocation, InvocationKind};
27+
use syntax::ext::hygiene::{self, Mark, Transparency};
2828
use syntax::ext::placeholders::placeholder;
2929
use syntax::ext::tt::macro_rules;
3030
use syntax::feature_gate::{self, emit_feature_err, GateIssue};
@@ -187,9 +187,10 @@ impl<'a> base::Resolver for Resolver<'a> {
187187
self.whitelisted_legacy_custom_derives.contains(&name)
188188
}
189189

190-
fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion, derives: &[Mark]) {
190+
fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment,
191+
derives: &[Mark]) {
191192
let invocation = self.invocations[&mark];
192-
self.collect_def_ids(mark, invocation, expansion);
193+
self.collect_def_ids(mark, invocation, fragment);
193194

194195
self.current_module = invocation.module.get();
195196
self.current_module.unresolved_invocations.borrow_mut().remove(&mark);
@@ -202,7 +203,7 @@ impl<'a> base::Resolver for Resolver<'a> {
202203
legacy_scope: LegacyScope::Invocation(invocation),
203204
expansion: mark,
204205
};
205-
expansion.visit_with(&mut visitor);
206+
fragment.visit_with(&mut visitor);
206207
invocation.expansion.set(visitor.legacy_scope);
207208
}
208209

@@ -326,14 +327,15 @@ impl<'a> base::Resolver for Resolver<'a> {
326327
self.macro_defs.insert(invoc.expansion_data.mark, def_id);
327328
let normal_module_def_id =
328329
self.macro_def_scope(invoc.expansion_data.mark).normal_ancestor_id;
329-
self.definitions.add_macro_def_scope(invoc.expansion_data.mark, normal_module_def_id);
330+
self.definitions.add_parent_module_of_macro_def(invoc.expansion_data.mark,
331+
normal_module_def_id);
330332

331333
self.unused_macros.remove(&def_id);
332334
let ext = self.get_macro(def);
333335
if ext.is_modern() {
334-
invoc.expansion_data.mark.set_kind(MarkKind::Modern);
336+
invoc.expansion_data.mark.set_transparency(Transparency::Opaque);
335337
} else if def_id.krate == BUILTIN_MACROS_CRATE {
336-
invoc.expansion_data.mark.set_kind(MarkKind::Builtin);
338+
invoc.expansion_data.mark.set_is_builtin(true);
337339
}
338340
Ok(Some(ext))
339341
}
@@ -396,14 +398,14 @@ impl<'a> Resolver<'a> {
396398
Ok(ext) => if let SyntaxExtension::ProcMacroDerive(_, ref inert_attrs, _) = *ext {
397399
if inert_attrs.contains(&attr_name) {
398400
// FIXME(jseyfried) Avoid `mem::replace` here.
399-
let dummy_item = placeholder(ExpansionKind::Items, ast::DUMMY_NODE_ID)
401+
let dummy_item = placeholder(AstFragmentKind::Items, ast::DUMMY_NODE_ID)
400402
.make_items().pop().unwrap();
401403
let dummy_item = Annotatable::Item(dummy_item);
402404
*item = mem::replace(item, dummy_item).map_attrs(|mut attrs| {
403405
let inert_attr = attr.take().unwrap();
404406
attr::mark_known(&inert_attr);
405407
if self.proc_macro_enabled {
406-
*attr = find_attr_invoc(&mut attrs);
408+
*attr = expand::find_attr_invoc(&mut attrs);
407409
}
408410
attrs.push(inert_attr);
409411
attrs
@@ -769,7 +771,7 @@ impl<'a> Resolver<'a> {
769771
fn collect_def_ids(&mut self,
770772
mark: Mark,
771773
invocation: &'a InvocationData<'a>,
772-
expansion: &Expansion) {
774+
fragment: &AstFragment) {
773775
let Resolver { ref mut invocations, arenas, graph_root, .. } = *self;
774776
let InvocationData { def_index, .. } = *invocation;
775777

@@ -787,7 +789,7 @@ impl<'a> Resolver<'a> {
787789
let mut def_collector = DefCollector::new(&mut self.definitions, mark);
788790
def_collector.visit_macro_invoc = Some(visit_macro_invoc);
789791
def_collector.with_parent(def_index, |def_collector| {
790-
expansion.visit_with(def_collector)
792+
fragment.visit_with(def_collector)
791793
});
792794
}
793795

src/librustc_save_analysis/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
844844
let callsite = span.source_callsite();
845845
let callsite_span = self.span_from_span(callsite);
846846
let callee = span.source_callee()?;
847-
let callee_span = callee.span?;
847+
let callee_span = callee.def_site?;
848848

849849
// Ignore attribute macros, their spans are usually mangled
850850
if let MacroAttribute(_) = callee.format {
@@ -872,7 +872,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
872872
let callee_span = self.span_from_span(callee_span);
873873
Some(MacroRef {
874874
span: callsite_span,
875-
qualname: callee.name().to_string(), // FIXME: generate the real qualname
875+
qualname: callee.format.name().to_string(), // FIXME: generate the real qualname
876876
callee_span,
877877
})
878878
}

src/libsyntax/codemap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
2020

2121
pub use syntax_pos::*;
22-
pub use syntax_pos::hygiene::{ExpnFormat, ExpnInfo, NameAndSpan};
22+
pub use syntax_pos::hygiene::{ExpnFormat, ExpnInfo};
2323
pub use self::ExpnFormat::*;
2424

2525
use rustc_data_structures::fx::FxHashMap;

src/libsyntax/ext/base.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use codemap::{self, CodeMap, Spanned, respan};
1616
use syntax_pos::{Span, MultiSpan, DUMMY_SP};
1717
use edition::Edition;
1818
use errors::{DiagnosticBuilder, DiagnosticId};
19-
use ext::expand::{self, Expansion, Invocation};
19+
use ext::expand::{self, AstFragment, Invocation};
2020
use ext::hygiene::{self, Mark, SyntaxContext};
2121
use fold::{self, Folder};
2222
use parse::{self, parser, DirectoryOwnership};
@@ -597,7 +597,11 @@ pub enum SyntaxExtension {
597597
MultiModifier(Box<MultiItemModifier + sync::Sync + sync::Send>),
598598

599599
/// A function-like procedural macro. TokenStream -> TokenStream.
600-
ProcMacro(Box<ProcMacro + sync::Sync + sync::Send>, Edition),
600+
ProcMacro(
601+
/* expander: */ Box<ProcMacro + sync::Sync + sync::Send>,
602+
/* allow_internal_unstable: */ bool,
603+
/* edition: */ Edition,
604+
),
601605

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

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

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

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

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

0 commit comments

Comments
 (0)