Skip to content

Commit 2de2278

Browse files
committed
syntax: Move default_transparency into ExpnInfo
1 parent 8ec502e commit 2de2278

File tree

4 files changed

+27
-28
lines changed

4 files changed

+27
-28
lines changed

src/librustc/ich/impls_syntax.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,17 @@ impl_stable_hash_for!(enum ::syntax::ast::MetaItemKind {
391391
NameValue(lit)
392392
});
393393

394+
impl_stable_hash_for!(enum ::syntax_pos::hygiene::Transparency {
395+
Transparent,
396+
SemiTransparent,
397+
Opaque,
398+
});
399+
394400
impl_stable_hash_for!(struct ::syntax_pos::hygiene::ExpnInfo {
395401
call_site,
396-
def_site,
397402
format,
403+
def_site,
404+
default_transparency,
398405
allow_internal_unstable,
399406
allow_internal_unsafe,
400407
local_inner_macros,

src/librustc_resolve/macros.rs

-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ impl<'a> base::Resolver for Resolver<'a> {
226226
self.macro_def_scope(invoc.expansion_data.mark).normal_ancestor_id;
227227
self.definitions.add_parent_module_of_macro_def(invoc.expansion_data.mark,
228228
normal_module_def_id);
229-
invoc.expansion_data.mark.set_default_transparency(ext.default_transparency);
230229
}
231230

232231
Ok(Some(ext))

src/libsyntax/ext/base.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -680,8 +680,9 @@ impl SyntaxExtension {
680680
crate fn expn_info(&self, call_site: Span, format: &str) -> ExpnInfo {
681681
ExpnInfo {
682682
call_site,
683-
def_site: self.def_info.map(|(_, span)| span),
684683
format: self.expn_format(Symbol::intern(format)),
684+
def_site: self.def_info.map(|(_, span)| span),
685+
default_transparency: self.default_transparency,
685686
allow_internal_unstable: self.allow_internal_unstable.clone(),
686687
allow_internal_unsafe: self.allow_internal_unsafe,
687688
local_inner_macros: self.local_inner_macros,

src/libsyntax_pos/hygiene.rs

+17-25
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,12 @@ pub struct Mark(u32);
5959
#[derive(Clone, Debug)]
6060
struct MarkData {
6161
parent: Mark,
62-
default_transparency: Transparency,
6362
expn_info: Option<ExpnInfo>,
6463
}
6564

6665
/// A property of a macro expansion that determines how identifiers
6766
/// produced by that expansion are resolved.
68-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Hash, Debug)]
67+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Hash, Debug, RustcEncodable, RustcDecodable)]
6968
pub enum Transparency {
7069
/// Identifier produced by a transparent expansion is always resolved at call-site.
7170
/// Call-site spans in procedural macros, hygiene opt-out in `macro` should use this.
@@ -85,12 +84,7 @@ pub enum Transparency {
8584
impl Mark {
8685
pub fn fresh(parent: Mark) -> Self {
8786
HygieneData::with(|data| {
88-
data.marks.push(MarkData {
89-
parent,
90-
// By default expansions behave like `macro_rules`.
91-
default_transparency: Transparency::SemiTransparent,
92-
expn_info: None,
93-
});
87+
data.marks.push(MarkData { parent, expn_info: None });
9488
Mark(data.marks.len() as u32 - 1)
9589
})
9690
}
@@ -126,12 +120,6 @@ impl Mark {
126120
HygieneData::with(|data| data.marks[self.0 as usize].expn_info = Some(info))
127121
}
128122

129-
#[inline]
130-
pub fn set_default_transparency(self, transparency: Transparency) {
131-
assert_ne!(self, Mark::root());
132-
HygieneData::with(|data| data.marks[self.0 as usize].default_transparency = transparency)
133-
}
134-
135123
pub fn is_descendant_of(self, ancestor: Mark) -> bool {
136124
HygieneData::with(|data| data.is_descendant_of(self, ancestor))
137125
}
@@ -172,9 +160,8 @@ impl Mark {
172160
#[inline]
173161
pub fn looks_like_proc_macro_derive(self) -> bool {
174162
HygieneData::with(|data| {
175-
let mark_data = &data.marks[self.0 as usize];
176-
if mark_data.default_transparency == Transparency::Opaque {
177-
if let Some(expn_info) = &mark_data.expn_info {
163+
if data.default_transparency(self) == Transparency::Opaque {
164+
if let Some(expn_info) = &data.marks[self.0 as usize].expn_info {
178165
if let ExpnFormat::MacroAttribute(name) = expn_info.format {
179166
if name.as_str().starts_with("derive(") {
180167
return true;
@@ -199,9 +186,6 @@ impl HygieneData {
199186
HygieneData {
200187
marks: vec![MarkData {
201188
parent: Mark::root(),
202-
// If the root is opaque, then loops searching for an opaque mark
203-
// will automatically stop after reaching it.
204-
default_transparency: Transparency::Opaque,
205189
expn_info: None,
206190
}],
207191
syntax_contexts: vec![SyntaxContextData {
@@ -235,7 +219,9 @@ impl HygieneData {
235219
}
236220

237221
fn default_transparency(&self, mark: Mark) -> Transparency {
238-
self.marks[mark.0 as usize].default_transparency
222+
self.marks[mark.0 as usize].expn_info.as_ref().map_or(
223+
Transparency::SemiTransparent, |einfo| einfo.default_transparency
224+
)
239225
}
240226

241227
fn modern(&self, ctxt: SyntaxContext) -> SyntaxContext {
@@ -427,7 +413,6 @@ impl SyntaxContext {
427413
HygieneData::with(|data| {
428414
data.marks.push(MarkData {
429415
parent: Mark::root(),
430-
default_transparency: Transparency::SemiTransparent,
431416
expn_info: Some(expansion_info),
432417
});
433418

@@ -651,6 +636,7 @@ impl fmt::Debug for SyntaxContext {
651636
/// Extra information for tracking spans of macro and syntax sugar expansion
652637
#[derive(Clone, Hash, Debug, RustcEncodable, RustcDecodable)]
653638
pub struct ExpnInfo {
639+
// --- The part unique to each expansion.
654640
/// The location of the actual macro invocation or syntax sugar , e.g.
655641
/// `let x = foo!();` or `if let Some(y) = x {}`
656642
///
@@ -661,13 +647,18 @@ pub struct ExpnInfo {
661647
/// call_site span would have its own ExpnInfo, with the call_site
662648
/// pointing to the `foo!` invocation.
663649
pub call_site: Span,
650+
/// The format with which the macro was invoked.
651+
pub format: ExpnFormat,
652+
653+
// --- The part specific to the macro/desugaring definition.
654+
// --- FIXME: Share it between expansions with the same definition.
664655
/// The span of the macro definition itself. The macro may not
665656
/// have a sensible definition span (e.g., something defined
666657
/// completely inside libsyntax) in which case this is None.
667658
/// This span serves only informational purpose and is not used for resolution.
668659
pub def_site: Option<Span>,
669-
/// The format with which the macro was invoked.
670-
pub format: ExpnFormat,
660+
/// Transparency used by `apply_mark` for mark with this expansion info by default.
661+
pub default_transparency: Transparency,
671662
/// List of #[unstable]/feature-gated features that the macro is allowed to use
672663
/// internally without forcing the whole crate to opt-in
673664
/// to them.
@@ -687,8 +678,9 @@ impl ExpnInfo {
687678
pub fn default(format: ExpnFormat, call_site: Span, edition: Edition) -> ExpnInfo {
688679
ExpnInfo {
689680
call_site,
690-
def_site: None,
691681
format,
682+
def_site: None,
683+
default_transparency: Transparency::SemiTransparent,
692684
allow_internal_unstable: None,
693685
allow_internal_unsafe: false,
694686
local_inner_macros: false,

0 commit comments

Comments
 (0)