Skip to content

Commit 961ba8f

Browse files
committed
syntax: Factor out common fields from SyntaxExtension variants
1 parent 704ab2b commit 961ba8f

File tree

22 files changed

+269
-349
lines changed

22 files changed

+269
-349
lines changed

src/doc/unstable-book/src/language-features/plugin.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ The advantages over a simple `fn(&str) -> u32` are:
132132
In addition to procedural macros, you can define new
133133
[`derive`](../../reference/attributes/derive.md)-like attributes and other kinds
134134
of extensions. See `Registry::register_syntax_extension` and the
135-
`SyntaxExtension` enum. For a more involved macro example, see
135+
`SyntaxExtension` struct. For a more involved macro example, see
136136
[`regex_macros`](https://github.com/rust-lang/regex/blob/master/regex_macros/src/lib.rs).
137137

138138

src/librustc_allocator/expand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl MutVisitor for ExpandAllocatorDirectives<'_> {
9191
call_site: item.span, // use the call site of the static
9292
def_site: None,
9393
format: MacroAttribute(Symbol::intern(name)),
94-
allow_internal_unstable: Some(vec![sym::rustc_attrs].into()),
94+
allow_internal_unstable: Some([sym::rustc_attrs][..].into()),
9595
allow_internal_unsafe: false,
9696
local_inner_macros: false,
9797
edition: self.sess.edition,

src/librustc_metadata/creader.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use std::{cmp, fs};
2626

2727
use syntax::ast;
2828
use syntax::attr;
29-
use syntax::ext::base::SyntaxExtension;
29+
use syntax::ext::base::{SyntaxExtension, SyntaxExtensionKind};
3030
use syntax::symbol::{Symbol, sym};
3131
use syntax::visit;
3232
use syntax::{span_err, span_fatal};
@@ -611,33 +611,31 @@ impl<'a> CrateLoader<'a> {
611611
};
612612

613613
let extensions = decls.iter().map(|&decl| {
614-
match decl {
614+
let (name, kind, helper_attrs) = match decl {
615615
ProcMacro::CustomDerive { trait_name, attributes, client } => {
616-
let attrs = attributes.iter().cloned().map(Symbol::intern).collect::<Vec<_>>();
617-
(trait_name, SyntaxExtension::Derive(
618-
Box::new(ProcMacroDerive {
619-
client,
620-
attrs: attrs.clone(),
621-
}),
622-
attrs,
623-
root.edition,
624-
))
616+
let helper_attrs =
617+
attributes.iter().cloned().map(Symbol::intern).collect::<Vec<_>>();
618+
(
619+
trait_name,
620+
SyntaxExtensionKind::Derive(Box::new(ProcMacroDerive {
621+
client, attrs: helper_attrs.clone()
622+
})),
623+
helper_attrs,
624+
)
625625
}
626-
ProcMacro::Attr { name, client } => {
627-
(name, SyntaxExtension::Attr(
628-
Box::new(AttrProcMacro { client }),
629-
root.edition,
630-
))
631-
}
632-
ProcMacro::Bang { name, client } => {
633-
(name, SyntaxExtension::Bang {
634-
expander: Box::new(BangProcMacro { client }),
635-
allow_internal_unstable: None,
636-
edition: root.edition,
637-
})
638-
}
639-
}
640-
}).map(|(name, ext)| (Symbol::intern(name), Lrc::new(ext))).collect();
626+
ProcMacro::Attr { name, client } => (
627+
name, SyntaxExtensionKind::Attr(Box::new(AttrProcMacro { client })), Vec::new()
628+
),
629+
ProcMacro::Bang { name, client } => (
630+
name, SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client })), Vec::new()
631+
)
632+
};
633+
634+
(Symbol::intern(name), Lrc::new(SyntaxExtension {
635+
helper_attrs,
636+
..SyntaxExtension::default(kind, root.edition)
637+
}))
638+
}).collect();
641639

642640
// Intentionally leak the dynamic library. We can't ever unload it
643641
// since the library can make things that will live arbitrarily long.

src/librustc_metadata/cstore_impl.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ use syntax::ast;
3030
use syntax::attr;
3131
use syntax::source_map;
3232
use syntax::edition::Edition;
33+
use syntax::ext::base::{SyntaxExtension, SyntaxExtensionKind};
3334
use syntax::parse::source_file_to_stream;
3435
use syntax::parse::parser::emit_unclosed_delims;
3536
use syntax::symbol::{Symbol, sym};
37+
use syntax_ext::proc_macro_impl::BangProcMacro;
3638
use syntax_pos::{Span, NO_EXPANSION, FileName};
3739
use rustc_data_structures::bit_set::BitSet;
3840

@@ -427,14 +429,11 @@ impl cstore::CStore {
427429
if let Some(ref proc_macros) = data.proc_macros {
428430
return LoadedMacro::ProcMacro(proc_macros[id.index.to_proc_macro_index()].1.clone());
429431
} else if data.name == sym::proc_macro && data.item_name(id.index) == sym::quote {
430-
use syntax::ext::base::SyntaxExtension;
431-
use syntax_ext::proc_macro_impl::BangProcMacro;
432-
433432
let client = proc_macro::bridge::client::Client::expand1(proc_macro::quote);
434-
let ext = SyntaxExtension::Bang {
435-
expander: Box::new(BangProcMacro { client }),
436-
allow_internal_unstable: Some(vec![sym::proc_macro_def_site].into()),
437-
edition: data.root.edition,
433+
let kind = SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client }));
434+
let ext = SyntaxExtension {
435+
allow_internal_unstable: Some([sym::proc_macro_def_site][..].into()),
436+
..SyntaxExtension::default(kind, data.root.edition)
438437
};
439438
return LoadedMacro::ProcMacro(Lrc::new(ext));
440439
}

src/librustc_metadata/decoder.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,9 @@ impl<'a, 'tcx> CrateMetadata {
511511
if !self.is_proc_macro(index) {
512512
self.entry(index).kind.def_kind()
513513
} else {
514-
let kind = self.proc_macros.as_ref().unwrap()[index.to_proc_macro_index()].1.kind();
515-
Some(DefKind::Macro(kind))
514+
Some(DefKind::Macro(
515+
self.proc_macros.as_ref().unwrap()[index.to_proc_macro_index()].1.macro_kind()
516+
))
516517
}
517518
}
518519

@@ -739,7 +740,7 @@ impl<'a, 'tcx> CrateMetadata {
739740
if id == CRATE_DEF_INDEX {
740741
for (id, &(name, ref ext)) in proc_macros.iter().enumerate() {
741742
let res = Res::Def(
742-
DefKind::Macro(ext.kind()),
743+
DefKind::Macro(ext.macro_kind()),
743744
self.local_def_id(DefIndex::from_proc_macro_index(id)),
744745
);
745746
let ident = Ident::with_empty_ctxt(name);

src/librustc_plugin/registry.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ use rustc::lint::{EarlyLintPassObject, LateLintPassObject, LintId, Lint};
44
use rustc::session::Session;
55
use rustc::util::nodemap::FxHashMap;
66

7-
use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension};
7+
use syntax::ext::base::{SyntaxExtension, SyntaxExtensionKind, NamedSyntaxExtension};
88
use syntax::ext::base::MacroExpanderFn;
9-
use syntax::ext::hygiene::Transparency;
109
use syntax::symbol::{Symbol, sym};
1110
use syntax::ast;
1211
use syntax::feature_gate::AttributeType;
@@ -89,28 +88,21 @@ impl<'a> Registry<'a> {
8988
if name == sym::macro_rules {
9089
panic!("user-defined macros may not be named `macro_rules`");
9190
}
92-
if let SyntaxExtension::LegacyBang { def_info: ref mut def_info @ None, .. } = extension {
93-
*def_info = Some((ast::CRATE_NODE_ID, self.krate_span));
91+
if extension.def_info.is_none() {
92+
extension.def_info = Some((ast::CRATE_NODE_ID, self.krate_span));
9493
}
9594
self.syntax_exts.push((name, extension));
9695
}
9796

9897
/// Register a macro of the usual kind.
9998
///
10099
/// This is a convenience wrapper for `register_syntax_extension`.
101-
/// It builds for you a `SyntaxExtension::LegacyBang` that calls `expander`,
100+
/// It builds for you a `SyntaxExtensionKind::LegacyBang` that calls `expander`,
102101
/// and also takes care of interning the macro's name.
103102
pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
104-
self.register_syntax_extension(Symbol::intern(name), SyntaxExtension::LegacyBang {
105-
expander: Box::new(expander),
106-
def_info: None,
107-
transparency: Transparency::SemiTransparent,
108-
allow_internal_unstable: None,
109-
allow_internal_unsafe: false,
110-
local_inner_macros: false,
111-
unstable_feature: None,
112-
edition: self.sess.edition(),
113-
});
103+
let kind = SyntaxExtensionKind::LegacyBang(Box::new(expander));
104+
let ext = SyntaxExtension::default(kind, self.sess.edition());
105+
self.register_syntax_extension(Symbol::intern(name), ext);
114106
}
115107

116108
/// Register a compiler lint pass.

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use syntax::attr;
2929

3030
use syntax::ast::{self, Block, ForeignItem, ForeignItemKind, Item, ItemKind, NodeId};
3131
use syntax::ast::{MetaItemKind, StmtKind, TraitItem, TraitItemKind, Variant};
32-
use syntax::ext::base::{MacroKind, SyntaxExtension};
32+
use syntax::ext::base::{MacroKind, SyntaxExtension, SyntaxExtensionKind};
3333
use syntax::ext::base::Determinacy::Undetermined;
3434
use syntax::ext::hygiene::Mark;
3535
use syntax::ext::tt::macro_rules;
@@ -772,9 +772,12 @@ impl<'a> Resolver<'a> {
772772
pub fn get_macro(&mut self, res: Res) -> Lrc<SyntaxExtension> {
773773
let def_id = match res {
774774
Res::Def(DefKind::Macro(..), def_id) => def_id,
775-
Res::NonMacroAttr(attr_kind) => return Lrc::new(SyntaxExtension::NonMacroAttr {
776-
mark_used: attr_kind == NonMacroAttrKind::Tool,
777-
}),
775+
Res::NonMacroAttr(attr_kind) => return Lrc::new(SyntaxExtension::default(
776+
SyntaxExtensionKind::NonMacroAttr {
777+
mark_used: attr_kind == NonMacroAttrKind::Tool
778+
},
779+
self.session.edition(),
780+
)),
778781
_ => panic!("expected `DefKind::Macro` or `Res::NonMacroAttr`"),
779782
};
780783
if let Some(ext) = self.macro_map.get(&def_id) {

src/librustc_resolve/macros.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use syntax::ast::{self, Ident};
1515
use syntax::attr;
1616
use syntax::errors::DiagnosticBuilder;
1717
use syntax::ext::base::{self, Determinacy};
18-
use syntax::ext::base::{MacroKind, SyntaxExtension};
18+
use syntax::ext::base::{MacroKind, SyntaxExtension, SyntaxExtensionKind};
1919
use syntax::ext::expand::{AstFragment, Invocation, InvocationKind};
2020
use syntax::ext::hygiene::Mark;
2121
use syntax::ext::tt::macro_rules;
@@ -174,7 +174,7 @@ impl<'a> base::Resolver for Resolver<'a> {
174174
krate: CrateNum::BuiltinMacros,
175175
index: DefIndex::from(self.macro_map.len()),
176176
};
177-
let kind = ext.kind();
177+
let kind = ext.macro_kind();
178178
self.macro_map.insert(def_id, ext);
179179
let binding = self.arenas.alloc_name_binding(NameBinding {
180180
kind: NameBindingKind::Res(Res::Def(DefKind::Macro(kind), def_id), false),
@@ -211,7 +211,8 @@ impl<'a> base::Resolver for Resolver<'a> {
211211
Ok((res, ext)) => (res, ext),
212212
Err(Determinacy::Determined) if kind == MacroKind::Attr => {
213213
// Replace unresolved attributes with used inert attributes for better recovery.
214-
return Ok(Some(Lrc::new(SyntaxExtension::NonMacroAttr { mark_used: true })));
214+
let kind = SyntaxExtensionKind::NonMacroAttr { mark_used: true };
215+
return Ok(Some(Lrc::new(SyntaxExtension::default(kind, self.session.edition()))));
215216
}
216217
Err(determinacy) => return Err(determinacy),
217218
};
@@ -226,7 +227,7 @@ impl<'a> base::Resolver for Resolver<'a> {
226227
self.macro_def_scope(invoc.expansion_data.mark).normal_ancestor_id;
227228
self.definitions.add_parent_module_of_macro_def(invoc.expansion_data.mark,
228229
normal_module_def_id);
229-
invoc.expansion_data.mark.set_default_transparency(ext.default_transparency());
230+
invoc.expansion_data.mark.set_default_transparency(ext.default_transparency);
230231
}
231232

232233
Ok(Some(ext))
@@ -241,11 +242,7 @@ impl<'a> base::Resolver for Resolver<'a> {
241242

242243
fn check_unused_macros(&self) {
243244
for did in self.unused_macros.iter() {
244-
let id_span = match *self.macro_map[did] {
245-
SyntaxExtension::LegacyBang { def_info, .. } => def_info,
246-
_ => None,
247-
};
248-
if let Some((id, span)) = id_span {
245+
if let Some((id, span)) = self.macro_map[did].def_info {
249246
let lint = lint::builtin::UNUSED_MACROS;
250247
let msg = "unused macro definition";
251248
self.session.buffer_lint(lint, id, span, msg);
@@ -585,17 +582,12 @@ impl<'a> Resolver<'a> {
585582
let parent_scope = ParentScope { derives: Vec::new(), ..*parent_scope };
586583
match self.resolve_macro_to_res(derive, MacroKind::Derive,
587584
&parent_scope, true, force) {
588-
Ok((_, ext)) => {
589-
if let SyntaxExtension::Derive(_, helpers, _) = &*ext {
590-
if helpers.contains(&ident.name) {
591-
let binding =
592-
(Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
593-
ty::Visibility::Public, derive.span, Mark::root())
594-
.to_name_binding(self.arenas);
595-
result = Ok((binding, Flags::empty()));
596-
break;
597-
}
598-
}
585+
Ok((_, ext)) => if ext.helper_attrs.contains(&ident.name) {
586+
let binding = (Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
587+
ty::Visibility::Public, derive.span, Mark::root())
588+
.to_name_binding(self.arenas);
589+
result = Ok((binding, Flags::empty()));
590+
break;
599591
}
600592
Err(Determinacy::Determined) => {}
601593
Err(Determinacy::Undetermined) =>

src/librustdoc/clean/inline.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::iter::once;
44

55
use syntax::ast;
6-
use syntax::ext::base::{MacroKind, SyntaxExtension};
6+
use syntax::ext::base::MacroKind;
77
use syntax::symbol::sym;
88
use syntax_pos::Span;
99

@@ -470,18 +470,12 @@ fn build_macro(cx: &DocContext<'_>, did: DefId, name: ast::Name) -> clean::ItemE
470470
})
471471
}
472472
LoadedMacro::ProcMacro(ext) => {
473-
let helpers = match &*ext {
474-
&SyntaxExtension::Derive(_, ref syms, ..) => { syms.clean(cx) }
475-
_ => Vec::new(),
476-
};
477-
478473
clean::ProcMacroItem(clean::ProcMacro {
479-
kind: ext.kind(),
480-
helpers,
474+
kind: ext.macro_kind(),
475+
helpers: ext.helper_attrs.clean(cx),
481476
})
482477
}
483478
}
484-
485479
}
486480

487481
/// A trait's generics clause actually contains all of the predicates for all of

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
423423

424424
/// Resolves a string as a macro.
425425
fn macro_resolve(cx: &DocContext<'_>, path_str: &str) -> Option<Res> {
426-
use syntax::ext::base::{MacroKind, SyntaxExtension};
426+
use syntax::ext::base::{MacroKind, SyntaxExtensionKind};
427427
let segment = ast::PathSegment::from_ident(Ident::from_str(path_str));
428428
let path = ast::Path { segments: vec![segment], span: DUMMY_SP };
429429
cx.enter_resolver(|resolver| {
@@ -433,7 +433,7 @@ fn macro_resolve(cx: &DocContext<'_>, path_str: &str) -> Option<Res> {
433433
if let Res::Def(DefKind::Macro(MacroKind::ProcMacroStub), _) = res {
434434
// skip proc-macro stubs, they'll cause `get_macro` to crash
435435
} else {
436-
if let SyntaxExtension::LegacyBang { .. } = *resolver.get_macro(res) {
436+
if let SyntaxExtensionKind::LegacyBang(..) = resolver.get_macro(res).kind {
437437
return Some(res.map_id(|_| panic!("unexpected id")));
438438
}
439439
}

0 commit comments

Comments
 (0)