Skip to content

Commit 01a0c6d

Browse files
committed
rustc_metadata: Remove rmeta::MacroDef
Use `ast::MacroDef` instead. Also remove `Session::imported_macro_spans`, external macros have spans now.
1 parent d607231 commit 01a0c6d

File tree

12 files changed

+21
-60
lines changed

12 files changed

+21
-60
lines changed

src/librustc_ast/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ impl MacDelimiter {
14461446
}
14471447

14481448
/// Represents a macro definition.
1449-
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1449+
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
14501450
pub struct MacroDef {
14511451
pub body: P<MacArgs>,
14521452
/// `true` if macro was defined with `macro_rules`.

src/librustc_ast_lowering/item.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc::bug;
66
use rustc_ast::ast::*;
77
use rustc_ast::attr;
88
use rustc_ast::node_id::NodeMap;
9+
use rustc_ast::ptr::P;
910
use rustc_ast::visit::{self, AssocCtxt, Visitor};
1011
use rustc_errors::struct_span_err;
1112
use rustc_hir as hir;
@@ -219,18 +220,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
219220
let mut vis = self.lower_visibility(&i.vis, None);
220221
let attrs = self.lower_attrs(&i.attrs);
221222

222-
if let ItemKind::MacroDef(ref def) = i.kind {
223-
if !def.legacy || attr::contains_name(&i.attrs, sym::macro_export) {
224-
let body = self.lower_token_stream(def.body.inner_tokens());
223+
if let ItemKind::MacroDef(MacroDef { ref body, legacy }) = i.kind {
224+
if !legacy || attr::contains_name(&i.attrs, sym::macro_export) {
225225
let hir_id = self.lower_node_id(i.id);
226+
let body = P(self.lower_mac_args(body));
226227
self.exported_macros.push(hir::MacroDef {
227-
name: ident.name,
228+
ident,
228229
vis,
229230
attrs,
230231
hir_id,
231232
span: i.span,
232-
body,
233-
legacy: def.legacy,
233+
ast: MacroDef { body, legacy },
234234
});
235235
} else {
236236
self.non_exported_macro_attrs.extend(attrs.iter().cloned());

src/librustc_hir/hir.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use rustc_ast::ast::{AttrVec, Attribute, FloatTy, IntTy, Label, LitKind, StrStyl
1313
pub use rustc_ast::ast::{BorrowKind, ImplPolarity, IsAuto};
1414
pub use rustc_ast::ast::{CaptureBy, Movability, Mutability};
1515
use rustc_ast::node_id::NodeMap;
16-
use rustc_ast::tokenstream::TokenStream;
1716
use rustc_ast::util::parser::ExprPrecedence;
1817
use rustc_data_structures::fx::FxHashSet;
1918
use rustc_data_structures::sync::{par_for_each_in, Send, Sync};
@@ -722,13 +721,12 @@ impl Crate<'_> {
722721
/// Not parsed directly, but created on macro import or `macro_rules!` expansion.
723722
#[derive(RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
724723
pub struct MacroDef<'hir> {
725-
pub name: Name,
724+
pub ident: Ident,
726725
pub vis: Visibility<'hir>,
727726
pub attrs: &'hir [Attribute],
728727
pub hir_id: HirId,
729728
pub span: Span,
730-
pub body: TokenStream,
731-
pub legacy: bool,
729+
pub ast: ast::MacroDef,
732730
}
733731

734732
/// A block of statements `{ .. }`, which may have a label (in this case the

src/librustc_hir/intravisit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate<'v>) {
445445

446446
pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef<'v>) {
447447
visitor.visit_id(macro_def.hir_id);
448-
visitor.visit_name(macro_def.span, macro_def.name);
448+
visitor.visit_ident(macro_def.ident);
449449
walk_list!(visitor, visit_attribute, macro_def.attrs);
450450
}
451451

src/librustc_metadata/rmeta/decoder/cstore_impl.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ use rustc::ty::{self, TyCtxt};
1717
use rustc_ast::ast;
1818
use rustc_ast::attr;
1919
use rustc_ast::expand::allocator::AllocatorKind;
20-
use rustc_ast::ptr::P;
21-
use rustc_ast::tokenstream::DelimSpan;
2220
use rustc_data_structures::svh::Svh;
2321
use rustc_hir as hir;
2422
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE};
@@ -415,34 +413,28 @@ impl CStore {
415413
}
416414

417415
let span = data.get_span(id.index, sess);
418-
let dspan = DelimSpan::from_single(span);
419-
let rmeta::MacroDef { body, legacy } = data.get_macro(id.index, sess);
420416

421417
// Mark the attrs as used
422418
let attrs = data.get_item_attrs(id.index, sess);
423419
for attr in attrs.iter() {
424420
attr::mark_used(attr);
425421
}
426422

427-
let name = data
423+
let ident = data
428424
.def_key(id.index)
429425
.disambiguated_data
430426
.data
431427
.get_opt_name()
428+
.map(ast::Ident::with_dummy_span) // FIXME: cross-crate hygiene
432429
.expect("no name in load_macro");
433-
sess.imported_macro_spans.borrow_mut().insert(span, (name.to_string(), span));
434430

435431
LoadedMacro::MacroDef(
436432
ast::Item {
437-
// FIXME: cross-crate hygiene
438-
ident: ast::Ident::with_dummy_span(name),
433+
ident,
439434
id: ast::DUMMY_NODE_ID,
440435
span,
441436
attrs: attrs.iter().cloned().collect(),
442-
kind: ast::ItemKind::MacroDef(ast::MacroDef {
443-
body: P(ast::MacArgs::Delimited(dspan, ast::MacDelimiter::Brace, body)),
444-
legacy,
445-
}),
437+
kind: ast::ItemKind::MacroDef(data.get_macro(id.index, sess)),
446438
vis: source_map::respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
447439
tokens: None,
448440
},

src/librustc_metadata/rmeta/encoder.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1236,10 +1236,7 @@ impl EncodeContext<'tcx> {
12361236
/// Serialize the text of exported macros
12371237
fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef<'_>) {
12381238
let def_id = self.tcx.hir().local_def_id(macro_def.hir_id);
1239-
record!(self.per_def.kind[def_id] <- EntryKind::MacroDef(self.lazy(MacroDef {
1240-
body: macro_def.body.clone(),
1241-
legacy: macro_def.legacy,
1242-
})));
1239+
record!(self.per_def.kind[def_id] <- EntryKind::MacroDef(self.lazy(macro_def.ast.clone())));
12431240
record!(self.per_def.visibility[def_id] <- ty::Visibility::Public);
12441241
record!(self.per_def.span[def_id] <- macro_def.span);
12451242
record!(self.per_def.attributes[def_id] <- macro_def.attrs);

src/librustc_metadata/rmeta/mod.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ use rustc::mir;
1010
use rustc::session::config::SymbolManglingVersion;
1111
use rustc::session::CrateDisambiguator;
1212
use rustc::ty::{self, ReprOptions, Ty};
13-
use rustc_ast::ast;
14-
use rustc_ast::tokenstream::TokenStream;
13+
use rustc_ast::ast::{self, MacroDef};
1514
use rustc_attr as attr;
1615
use rustc_data_structures::svh::Svh;
1716
use rustc_data_structures::sync::MetadataRef;
@@ -323,12 +322,6 @@ struct ModData {
323322
reexports: Lazy<[Export<hir::HirId>]>,
324323
}
325324

326-
#[derive(RustcEncodable, RustcDecodable)]
327-
struct MacroDef {
328-
body: TokenStream,
329-
legacy: bool,
330-
}
331-
332325
#[derive(RustcEncodable, RustcDecodable)]
333326
struct FnData {
334327
asyncness: hir::IsAsync,

src/librustc_privacy/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
920920
}
921921

922922
fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef<'tcx>) {
923-
if attr::find_transparency(&md.attrs, md.legacy).0 != Transparency::Opaque {
923+
if attr::find_transparency(&md.attrs, md.ast.legacy).0 != Transparency::Opaque {
924924
self.update(md.hir_id, Some(AccessLevel::Public));
925925
return;
926926
}

src/librustc_save_analysis/lib.rs

-13
Original file line numberDiff line numberDiff line change
@@ -794,19 +794,6 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
794794
ExpnKind::Root | ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => return None,
795795
};
796796

797-
// If the callee is an imported macro from an external crate, need to get
798-
// the source span and name from the session, as their spans are localized
799-
// when read in, and no longer correspond to the source.
800-
if let Some(mac) = self.tcx.sess.imported_macro_spans.borrow().get(&callee.def_site) {
801-
let &(ref mac_name, mac_span) = mac;
802-
let mac_span = self.span_from_span(mac_span);
803-
return Some(MacroRef {
804-
span: callsite_span,
805-
qualname: mac_name.clone(), // FIXME: generate the real qualname
806-
callee_span: mac_span,
807-
});
808-
}
809-
810797
let callee_span = self.span_from_span(callee.def_site);
811798
Some(MacroRef {
812799
span: callsite_span,

src/librustc_session/session.rs

-6
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,6 @@ pub struct Session {
103103
/// The maximum blocks a const expression can evaluate.
104104
pub const_eval_limit: Once<usize>,
105105

106-
/// Map from imported macro spans (which consist of
107-
/// the localized span for the macro body) to the
108-
/// macro name and definition span in the source crate.
109-
pub imported_macro_spans: OneThread<RefCell<FxHashMap<Span, (String, Span)>>>,
110-
111106
incr_comp_session: OneThread<RefCell<IncrCompSession>>,
112107
/// Used for incremental compilation tests. Will only be populated if
113108
/// `-Zquery-dep-graph` is specified.
@@ -1080,7 +1075,6 @@ fn build_session_(
10801075
recursion_limit: Once::new(),
10811076
type_length_limit: Once::new(),
10821077
const_eval_limit: Once::new(),
1083-
imported_macro_spans: OneThread::new(RefCell::new(FxHashMap::default())),
10841078
incr_comp_session: OneThread::new(RefCell::new(IncrCompSession::NotInitialized)),
10851079
cgu_reuse_tracker,
10861080
prof,

src/librustdoc/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirCollector<'a, 'hir> {
955955
}
956956

957957
fn visit_macro_def(&mut self, macro_def: &'hir hir::MacroDef) {
958-
self.visit_testable(macro_def.name.to_string(), &macro_def.attrs, |_| ());
958+
self.visit_testable(macro_def.ident.to_string(), &macro_def.attrs, |_| ());
959959
}
960960
}
961961

src/librustdoc/visit_ast.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -620,16 +620,16 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
620620
def: &'tcx hir::MacroDef,
621621
renamed: Option<ast::Name>,
622622
) -> Macro<'tcx> {
623-
debug!("visit_local_macro: {}", def.name);
624-
let tts = def.body.trees().collect::<Vec<_>>();
623+
debug!("visit_local_macro: {}", def.ident);
624+
let tts = def.ast.body.inner_tokens().trees().collect::<Vec<_>>();
625625
// Extract the spans of all matchers. They represent the "interface" of the macro.
626626
let matchers = tts.chunks(4).map(|arm| arm[0].span()).collect();
627627

628628
Macro {
629629
hid: def.hir_id,
630630
def_id: self.cx.tcx.hir().local_def_id(def.hir_id),
631631
attrs: &def.attrs,
632-
name: renamed.unwrap_or(def.name),
632+
name: renamed.unwrap_or(def.ident.name),
633633
whence: def.span,
634634
matchers,
635635
imported_from: None,

0 commit comments

Comments
 (0)