Skip to content

Commit 2a1d2ed

Browse files
committed
Declarative macros 2.0 without hygiene.
1 parent 9c6430b commit 2a1d2ed

File tree

14 files changed

+116
-46
lines changed

14 files changed

+116
-46
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# `decl_macro`
2+
3+
The tracking issue for this feature is: [#39412]
4+
5+
[#39412]: https://github.com/rust-lang/rust/issues/39412
6+
7+
------------------------
8+
9+
10+

src/librustc/hir/lowering.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,10 +1502,11 @@ impl<'a> LoweringContext<'a> {
15021502
pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item> {
15031503
let mut name = i.ident.name;
15041504
let attrs = self.lower_attrs(&i.attrs);
1505-
if let ItemKind::MacroDef(ref tts) = i.node {
1506-
if i.attrs.iter().any(|attr| attr.path == "macro_export") {
1505+
if let ItemKind::MacroDef(ref def) = i.node {
1506+
if !def.legacy || i.attrs.iter().any(|attr| attr.path == "macro_export") {
1507+
let (body, legacy) = (def.stream(), def.legacy);
15071508
self.exported_macros.push(hir::MacroDef {
1508-
name: name, attrs: attrs, id: i.id, span: i.span, body: tts.stream(),
1509+
name: name, attrs: attrs, id: i.id, span: i.span, body: body, legacy: legacy,
15091510
});
15101511
}
15111512
return None;

src/librustc/hir/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ pub struct MacroDef {
536536
pub id: NodeId,
537537
pub span: Span,
538538
pub body: TokenStream,
539+
pub legacy: bool,
539540
}
540541

541542
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]

src/librustc/ich/impls_hir.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ impl_stable_hash_for!(struct hir::MacroDef {
332332
attrs,
333333
id,
334334
span,
335+
legacy,
335336
body
336337
});
337338

src/librustc_metadata/cstore_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ impl CrateStore for cstore::CStore {
388388
attrs: attrs.iter().cloned().collect(),
389389
node: ast::ItemKind::MacroDef(ast::MacroDef {
390390
tokens: body.into(),
391+
legacy: true,
391392
}),
392393
vis: ast::Visibility::Inherited,
393394
})

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ struct LegacyMacroImports {
7777
impl<'a> Resolver<'a> {
7878
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
7979
/// otherwise, reports an error.
80-
fn define<T>(&mut self, parent: Module<'a>, ident: Ident, ns: Namespace, def: T)
80+
pub fn define<T>(&mut self, parent: Module<'a>, ident: Ident, ns: Namespace, def: T)
8181
where T: ToNameBinding<'a>,
8282
{
8383
let binding = def.to_name_binding(self.arenas);
@@ -730,7 +730,7 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> {
730730
fn visit_item(&mut self, item: &'a Item) {
731731
let macro_use = match item.node {
732732
ItemKind::MacroDef(..) => {
733-
self.resolver.define_macro(item, &mut self.legacy_scope);
733+
self.resolver.define_macro(item, self.expansion, &mut self.legacy_scope);
734734
return
735735
}
736736
ItemKind::Mac(..) => {

src/librustc_resolve/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,13 @@ impl<'a> NameBinding<'a> {
10581058
_ => true,
10591059
}
10601060
}
1061+
1062+
fn is_macro_def(&self) -> bool {
1063+
match self.kind {
1064+
NameBindingKind::Def(Def::Macro(..)) => true,
1065+
_ => false,
1066+
}
1067+
}
10611068
}
10621069

10631070
/// Interns the names of the primitive types.
@@ -1377,8 +1384,9 @@ impl<'a> Resolver<'a> {
13771384
vis: ty::Visibility::Public,
13781385
}),
13791386

1380-
// `#![feature(proc_macro)]` implies `#[feature(extern_macros)]`
1381-
use_extern_macros: features.use_extern_macros || features.proc_macro,
1387+
// The `proc_macro` and `decl_macro` features imply `use_extern_macros`
1388+
use_extern_macros:
1389+
features.use_extern_macros || features.proc_macro || features.decl_macro,
13821390

13831391
crate_loader: crate_loader,
13841392
macro_names: FxHashSet(),

src/librustc_resolve/macros.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,10 @@ impl<'a> Resolver<'a> {
687687
});
688688
}
689689

690-
pub fn define_macro(&mut self, item: &ast::Item, legacy_scope: &mut LegacyScope<'a>) {
690+
pub fn define_macro(&mut self,
691+
item: &ast::Item,
692+
expansion: Mark,
693+
legacy_scope: &mut LegacyScope<'a>) {
691694
self.local_macro_def_scopes.insert(item.id, self.current_module);
692695
let ident = item.ident;
693696
if ident.name == "macro_rules" {
@@ -699,16 +702,24 @@ impl<'a> Resolver<'a> {
699702
&self.session.features,
700703
item));
701704
self.macro_map.insert(def_id, ext);
702-
*legacy_scope = LegacyScope::Binding(self.arenas.alloc_legacy_binding(LegacyBinding {
703-
parent: Cell::new(*legacy_scope), name: ident.name, def_id: def_id, span: item.span,
704-
}));
705-
self.macro_names.insert(ident.name);
706705

707-
if attr::contains_name(&item.attrs, "macro_export") {
708-
let def = Def::Macro(def_id, MacroKind::Bang);
709-
self.macro_exports.push(Export { name: ident.name, def: def, span: item.span });
706+
let def = match item.node { ast::ItemKind::MacroDef(ref def) => def, _ => unreachable!() };
707+
if def.legacy {
708+
self.macro_names.insert(ident.name);
709+
*legacy_scope = LegacyScope::Binding(self.arenas.alloc_legacy_binding(LegacyBinding {
710+
parent: Cell::new(*legacy_scope), name: ident.name, def_id: def_id, span: item.span,
711+
}));
712+
if attr::contains_name(&item.attrs, "macro_export") {
713+
let def = Def::Macro(def_id, MacroKind::Bang);
714+
self.macro_exports.push(Export { name: ident.name, def: def, span: item.span });
715+
} else {
716+
self.unused_macros.insert(def_id);
717+
}
710718
} else {
711-
self.unused_macros.insert(def_id);
719+
let module = self.current_module;
720+
let def = Def::Macro(def_id, MacroKind::Bang);
721+
let vis = self.resolve_visibility(&item.vis);
722+
self.define(module, ident, MacroNS, (def, vis, item.span, expansion));
712723
}
713724
}
714725

src/librustc_resolve/resolve_imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
803803
};
804804

805805
if binding.vis == ty::Visibility::Public &&
806-
(binding.is_import() || binding.is_extern_crate()) {
806+
(binding.is_import() || binding.is_macro_def()) {
807807
let def = binding.def();
808808
if def != Def::Err {
809809
if !def.def_id().is_local() {

src/libsyntax/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,7 @@ impl Mac_ {
10221022
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
10231023
pub struct MacroDef {
10241024
pub tokens: ThinTokenStream,
1025+
pub legacy: bool,
10251026
}
10261027

10271028
impl MacroDef {

0 commit comments

Comments
 (0)