Skip to content

Commit d51002c

Browse files
authored
Rollup merge of rust-lang#61898 - petrochenkov:sekind, r=eddyb
syntax: Factor out common fields from `SyntaxExtension` variants And some other related cleanups. Continuation of rust-lang#61606. This will also help to unblock rust-lang#61877.
2 parents dcd5b20 + e152554 commit d51002c

File tree

26 files changed

+379
-513
lines changed

26 files changed

+379
-513
lines changed

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

+1-1
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/hir/lowering.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ use syntax::errors;
6262
use syntax::ext::hygiene::{Mark, SyntaxContext};
6363
use syntax::print::pprust;
6464
use syntax::ptr::P;
65-
use syntax::source_map::{self, respan, CompilerDesugaringKind, Spanned};
65+
use syntax::source_map::{self, respan, ExpnInfo, CompilerDesugaringKind, Spanned};
6666
use syntax::source_map::CompilerDesugaringKind::IfTemporary;
6767
use syntax::std_inject;
6868
use syntax::symbol::{kw, sym, Symbol};
6969
use syntax::tokenstream::{TokenStream, TokenTree};
7070
use syntax::parse::token::{self, Token};
7171
use syntax::visit::{self, Visitor};
72-
use syntax_pos::{DUMMY_SP, edition, Span};
72+
use syntax_pos::{DUMMY_SP, Span};
7373

7474
const HIR_ID_COUNTER_LOCKED: u32 = 0xFFFFFFFF;
7575

@@ -142,6 +142,9 @@ pub struct LoweringContext<'a> {
142142
current_hir_id_owner: Vec<(DefIndex, u32)>,
143143
item_local_id_counters: NodeMap<u32>,
144144
node_id_to_hir_id: IndexVec<NodeId, hir::HirId>,
145+
146+
allow_try_trait: Option<Lrc<[Symbol]>>,
147+
allow_gen_future: Option<Lrc<[Symbol]>>,
145148
}
146149

147150
pub trait Resolver {
@@ -267,6 +270,8 @@ pub fn lower_crate(
267270
lifetimes_to_define: Vec::new(),
268271
is_collecting_in_band_lifetimes: false,
269272
in_scope_lifetimes: Vec::new(),
273+
allow_try_trait: Some([sym::try_trait][..].into()),
274+
allow_gen_future: Some([sym::gen_future][..].into()),
270275
}.lower_crate(krate)
271276
}
272277

@@ -848,14 +853,10 @@ impl<'a> LoweringContext<'a> {
848853
allow_internal_unstable: Option<Lrc<[Symbol]>>,
849854
) -> Span {
850855
let mark = Mark::fresh(Mark::root());
851-
mark.set_expn_info(source_map::ExpnInfo {
852-
call_site: span,
856+
mark.set_expn_info(ExpnInfo {
853857
def_site: Some(span),
854-
format: source_map::CompilerDesugaring(reason),
855858
allow_internal_unstable,
856-
allow_internal_unsafe: false,
857-
local_inner_macros: false,
858-
edition: edition::Edition::from_session(),
859+
..ExpnInfo::default(source_map::CompilerDesugaring(reason), span, self.sess.edition())
859860
});
860861
span.with_ctxt(SyntaxContext::empty().apply_mark(mark))
861862
}
@@ -1156,7 +1157,7 @@ impl<'a> LoweringContext<'a> {
11561157
let unstable_span = self.mark_span_with_reason(
11571158
CompilerDesugaringKind::Async,
11581159
span,
1159-
Some(vec![sym::gen_future].into()),
1160+
self.allow_gen_future.clone(),
11601161
);
11611162
let gen_future = self.expr_std_path(
11621163
unstable_span, &[sym::future, sym::from_generator], None, ThinVec::new());
@@ -4382,7 +4383,7 @@ impl<'a> LoweringContext<'a> {
43824383
let unstable_span = this.mark_span_with_reason(
43834384
CompilerDesugaringKind::TryBlock,
43844385
body.span,
4385-
Some(vec![sym::try_trait].into()),
4386+
this.allow_try_trait.clone(),
43864387
);
43874388
let mut block = this.lower_block(body, true).into_inner();
43884389
let tail = block.expr.take().map_or_else(
@@ -4968,13 +4969,13 @@ impl<'a> LoweringContext<'a> {
49684969
let unstable_span = self.mark_span_with_reason(
49694970
CompilerDesugaringKind::QuestionMark,
49704971
e.span,
4971-
Some(vec![sym::try_trait].into()),
4972+
self.allow_try_trait.clone(),
49724973
);
49734974
let try_span = self.sess.source_map().end_point(e.span);
49744975
let try_span = self.mark_span_with_reason(
49754976
CompilerDesugaringKind::QuestionMark,
49764977
try_span,
4977-
Some(vec![sym::try_trait].into()),
4978+
self.allow_try_trait.clone(),
49784979
);
49794980

49804981
// `Try::into_result(<expr>)`
@@ -5776,7 +5777,7 @@ impl<'a> LoweringContext<'a> {
57765777
let gen_future_span = self.mark_span_with_reason(
57775778
CompilerDesugaringKind::Await,
57785779
await_span,
5779-
Some(vec![sym::gen_future].into()),
5780+
self.allow_gen_future.clone(),
57805781
);
57815782

57825783
// let mut pinned = <expr>;

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_allocator/expand.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use syntax::{
1919
mut_visit::{self, MutVisitor},
2020
parse::ParseSess,
2121
ptr::P,
22-
symbol::{kw, sym, Symbol}
22+
symbol::{kw, sym}
2323
};
2424
use syntax_pos::Span;
2525

@@ -58,11 +58,10 @@ impl MutVisitor for ExpandAllocatorDirectives<'_> {
5858
fn flat_map_item(&mut self, item: P<Item>) -> SmallVec<[P<Item>; 1]> {
5959
debug!("in submodule {}", self.in_submod);
6060

61-
let name = if attr::contains_name(&item.attrs, sym::global_allocator) {
62-
"global_allocator"
63-
} else {
61+
if !attr::contains_name(&item.attrs, sym::global_allocator) {
6462
return mut_visit::noop_flat_map_item(item, self);
65-
};
63+
}
64+
6665
match item.node {
6766
ItemKind::Static(..) => {}
6867
_ => {
@@ -87,15 +86,9 @@ impl MutVisitor for ExpandAllocatorDirectives<'_> {
8786

8887
// Create a fresh Mark for the new macro expansion we are about to do
8988
let mark = Mark::fresh(Mark::root());
90-
mark.set_expn_info(ExpnInfo {
91-
call_site: item.span, // use the call site of the static
92-
def_site: None,
93-
format: MacroAttribute(Symbol::intern(name)),
94-
allow_internal_unstable: Some(vec![sym::rustc_attrs].into()),
95-
allow_internal_unsafe: false,
96-
local_inner_macros: false,
97-
edition: self.sess.edition,
98-
});
89+
mark.set_expn_info(ExpnInfo::with_unstable(
90+
MacroAttribute(sym::global_allocator), item.span, self.sess.edition, &[sym::rustc_attrs]
91+
));
9992

10093
// Tie the span to the macro expansion info we just created
10194
let span = item.span.with_ctxt(SyntaxContext::empty().apply_mark(mark));

src/librustc_metadata/creader.rs

+24-26
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

+6-7
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

+4-3
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,9 @@ impl<'a, 'tcx> CrateMetadata {
509509
if !self.is_proc_macro(index) {
510510
self.entry(index).kind.def_kind()
511511
} else {
512-
let kind = self.proc_macros.as_ref().unwrap()[index.to_proc_macro_index()].1.kind();
513-
Some(DefKind::Macro(kind))
512+
Some(DefKind::Macro(
513+
self.proc_macros.as_ref().unwrap()[index.to_proc_macro_index()].1.macro_kind()
514+
))
514515
}
515516
}
516517

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

src/librustc_plugin/registry.rs

+7-15
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

+2-3
Original file line numberDiff line numberDiff line change
@@ -772,9 +772,8 @@ 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) =>
776+
return self.non_macro_attr(attr_kind == NonMacroAttrKind::Tool),
778777
_ => panic!("expected `DefKind::Macro` or `Res::NonMacroAttr`"),
779778
};
780779
if let Some(ext) = self.macro_map.get(&def_id) {

src/librustc_resolve/lib.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use rustc_metadata::cstore::CStore;
4141
use syntax::source_map::SourceMap;
4242
use syntax::ext::hygiene::{Mark, Transparency, SyntaxContext};
4343
use syntax::ast::{self, Name, NodeId, Ident, FloatTy, IntTy, UintTy};
44-
use syntax::ext::base::SyntaxExtension;
44+
use syntax::ext::base::{SyntaxExtension, SyntaxExtensionKind};
4545
use syntax::ext::base::Determinacy::{self, Determined, Undetermined};
4646
use syntax::ext::base::MacroKind;
4747
use syntax::symbol::{Symbol, kw, sym};
@@ -1668,6 +1668,7 @@ pub struct Resolver<'a> {
16681668
macro_use_prelude: FxHashMap<Name, &'a NameBinding<'a>>,
16691669
pub all_macros: FxHashMap<Name, Res>,
16701670
macro_map: FxHashMap<DefId, Lrc<SyntaxExtension>>,
1671+
non_macro_attrs: [Lrc<SyntaxExtension>; 2],
16711672
macro_defs: FxHashMap<Mark, DefId>,
16721673
local_macro_def_scopes: FxHashMap<NodeId, Module<'a>>,
16731674

@@ -1941,6 +1942,10 @@ impl<'a> Resolver<'a> {
19411942
let mut macro_defs = FxHashMap::default();
19421943
macro_defs.insert(Mark::root(), root_def_id);
19431944

1945+
let non_macro_attr = |mark_used| Lrc::new(SyntaxExtension::default(
1946+
SyntaxExtensionKind::NonMacroAttr { mark_used }, session.edition()
1947+
));
1948+
19441949
Resolver {
19451950
session,
19461951

@@ -2014,6 +2019,7 @@ impl<'a> Resolver<'a> {
20142019
macro_use_prelude: FxHashMap::default(),
20152020
all_macros: FxHashMap::default(),
20162021
macro_map: FxHashMap::default(),
2022+
non_macro_attrs: [non_macro_attr(false), non_macro_attr(true)],
20172023
invocations,
20182024
macro_defs,
20192025
local_macro_def_scopes: FxHashMap::default(),
@@ -2030,6 +2036,10 @@ impl<'a> Resolver<'a> {
20302036
Default::default()
20312037
}
20322038

2039+
fn non_macro_attr(&self, mark_used: bool) -> Lrc<SyntaxExtension> {
2040+
self.non_macro_attrs[mark_used as usize].clone()
2041+
}
2042+
20332043
/// Runs the function on each namespace.
20342044
fn per_ns<F: FnMut(&mut Self, Namespace)>(&mut self, mut f: F) {
20352045
f(self, TypeNS);

0 commit comments

Comments
 (0)