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

Lines changed: 0 additions & 3 deletions
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

Lines changed: 5 additions & 7 deletions
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

Lines changed: 18 additions & 13 deletions
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

Lines changed: 2 additions & 6 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 6 additions & 5 deletions
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

Lines changed: 6 additions & 8 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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())

0 commit comments

Comments
 (0)