Skip to content

Commit aff9738

Browse files
committed
hygiene: Reuse MacroKind in ExpnKind
Orthogonality and reuse are good.
1 parent 4dcf9b1 commit aff9738

File tree

12 files changed

+67
-77
lines changed

12 files changed

+67
-77
lines changed

src/librustc/ich/impls_syntax.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,7 @@ impl_stable_hash_for!(struct ::syntax_pos::hygiene::ExpnInfo {
408408
});
409409

410410
impl_stable_hash_for!(enum ::syntax_pos::hygiene::ExpnKind {
411-
MacroAttribute(sym),
412-
MacroBang(sym),
411+
Macro(kind, descr),
413412
Desugaring(kind)
414413
});
415414

src/librustc/lint/mod.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use syntax::ast;
3838
use syntax::source_map::{MultiSpan, ExpnKind, DesugaringKind};
3939
use syntax::early_buffered_lints::BufferedEarlyLintId;
4040
use syntax::edition::Edition;
41+
use syntax::ext::base::MacroKind;
4142
use syntax::symbol::{Symbol, sym};
4243
use syntax_pos::Span;
4344

@@ -884,10 +885,9 @@ pub fn in_external_macro(sess: &Session, span: Span) -> bool {
884885
};
885886

886887
match info.kind {
887-
ExpnKind::MacroAttribute(..) => true, // definitely a plugin
888888
ExpnKind::Desugaring(DesugaringKind::ForLoop) => false,
889889
ExpnKind::Desugaring(_) => true, // well, it's "external"
890-
ExpnKind::MacroBang(..) => {
890+
ExpnKind::Macro(MacroKind::Bang, _) => {
891891
if info.def_site.is_dummy() {
892892
// dummy span for the def_site means it's an external macro
893893
return true;
@@ -898,19 +898,16 @@ pub fn in_external_macro(sess: &Session, span: Span) -> bool {
898898
Err(_) => true,
899899
}
900900
}
901+
ExpnKind::Macro(..) => true, // definitely a plugin
901902
}
902903
}
903904

904905
/// Returns whether `span` originates in a derive macro's expansion
905906
pub fn in_derive_expansion(span: Span) -> bool {
906-
let info = match span.ctxt().outer_expn_info() {
907-
Some(info) => info,
908-
// no ExpnInfo means this span doesn't come from a macro
909-
None => return false,
910-
};
911-
912-
match info.kind {
913-
ExpnKind::MacroAttribute(symbol) => symbol.as_str().starts_with("derive("),
914-
_ => false,
907+
if let Some(info) = span.ctxt().outer_expn_info() {
908+
if let ExpnKind::Macro(MacroKind::Derive, _) = info.kind {
909+
return true;
910+
}
915911
}
912+
false
916913
}

src/librustc_allocator/expand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use syntax::{
1111
respan, ExpnInfo, ExpnKind,
1212
},
1313
ext::{
14-
base::{ExtCtxt, Resolver},
14+
base::{ExtCtxt, MacroKind, Resolver},
1515
build::AstBuilder,
1616
expand::ExpansionConfig,
1717
hygiene::{Mark, SyntaxContext},
@@ -87,7 +87,7 @@ impl MutVisitor for ExpandAllocatorDirectives<'_> {
8787
// Create a fresh Mark for the new macro expansion we are about to do
8888
let mark = Mark::fresh(Mark::root());
8989
mark.set_expn_info(ExpnInfo::with_unstable(
90-
ExpnKind::MacroAttribute(sym::global_allocator), item.span, self.sess.edition,
90+
ExpnKind::Macro(MacroKind::Attr, sym::global_allocator), item.span, self.sess.edition,
9191
&[sym::rustc_attrs],
9292
));
9393

src/librustc_resolve/macros.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,21 @@ fn sub_namespace_match(candidate: Option<MacroKind>, requirement: Option<MacroKi
114114
// We don't want to format a path using pretty-printing,
115115
// `format!("{}", path)`, because that tries to insert
116116
// line-breaks and is slow.
117-
fn fast_print_path(path: &ast::Path) -> String {
118-
let mut path_str = String::with_capacity(64);
119-
for (i, segment) in path.segments.iter().enumerate() {
120-
if i != 0 {
121-
path_str.push_str("::");
122-
}
123-
if segment.ident.name != kw::PathRoot {
124-
path_str.push_str(&segment.ident.as_str())
117+
fn fast_print_path(path: &ast::Path) -> Symbol {
118+
if path.segments.len() == 1 {
119+
return path.segments[0].ident.name
120+
} else {
121+
let mut path_str = String::with_capacity(64);
122+
for (i, segment) in path.segments.iter().enumerate() {
123+
if i != 0 {
124+
path_str.push_str("::");
125+
}
126+
if segment.ident.name != kw::PathRoot {
127+
path_str.push_str(&segment.ident.as_str())
128+
}
125129
}
130+
Symbol::intern(&path_str)
126131
}
127-
path_str
128132
}
129133

130134
impl<'a> base::Resolver for Resolver<'a> {
@@ -219,14 +223,10 @@ impl<'a> base::Resolver for Resolver<'a> {
219223
};
220224

221225
let span = invoc.span();
222-
let path = fast_print_path(path);
223-
let format = match kind {
224-
MacroKind::Derive => format!("derive({})", path),
225-
_ => path.clone(),
226-
};
227-
invoc.expansion_data.mark.set_expn_info(ext.expn_info(span, &format));
226+
let descr = fast_print_path(path);
227+
invoc.expansion_data.mark.set_expn_info(ext.expn_info(span, descr));
228228

229-
self.check_stability_and_deprecation(&ext, &path, span);
229+
self.check_stability_and_deprecation(&ext, descr, span);
230230

231231
if let Res::Def(_, def_id) = res {
232232
if after_derive {
@@ -991,7 +991,7 @@ impl<'a> Resolver<'a> {
991991
}
992992
}
993993

994-
fn check_stability_and_deprecation(&self, ext: &SyntaxExtension, path: &str, span: Span) {
994+
fn check_stability_and_deprecation(&self, ext: &SyntaxExtension, descr: Symbol, span: Span) {
995995
if let Some(stability) = &ext.stability {
996996
if let StabilityLevel::Unstable { reason, issue } = stability.level {
997997
let feature = stability.feature;
@@ -1000,14 +1000,14 @@ impl<'a> Resolver<'a> {
10001000
}
10011001
}
10021002
if let Some(depr) = &stability.rustc_depr {
1003-
let (message, lint) = stability::rustc_deprecation_message(depr, path);
1003+
let (message, lint) = stability::rustc_deprecation_message(depr, &descr.as_str());
10041004
stability::early_report_deprecation(
10051005
self.session, &message, depr.suggestion, lint, span
10061006
);
10071007
}
10081008
}
10091009
if let Some(depr) = &ext.deprecation {
1010-
let (message, lint) = stability::deprecation_message(depr, path);
1010+
let (message, lint) = stability::deprecation_message(depr, &descr.as_str());
10111011
stability::early_report_deprecation(self.session, &message, None, lint, span);
10121012
}
10131013
}

src/librustc_save_analysis/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,8 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
843843
let callee = span.source_callee()?;
844844

845845
// Ignore attribute macros, their spans are usually mangled
846-
if let ExpnKind::MacroAttribute(_) = callee.kind {
846+
if let ExpnKind::Macro(MacroKind::Attr, _) |
847+
ExpnKind::Macro(MacroKind::Derive, _) = callee.kind {
847848
return None;
848849
}
849850

src/libsyntax/ext/base.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -640,18 +640,10 @@ impl SyntaxExtension {
640640
}
641641
}
642642

643-
fn expn_kind(&self, descr: Symbol) -> ExpnKind {
644-
match self.kind {
645-
SyntaxExtensionKind::Bang(..) |
646-
SyntaxExtensionKind::LegacyBang(..) => ExpnKind::MacroBang(descr),
647-
_ => ExpnKind::MacroAttribute(descr),
648-
}
649-
}
650-
651-
pub fn expn_info(&self, call_site: Span, descr: &str) -> ExpnInfo {
643+
pub fn expn_info(&self, call_site: Span, descr: Symbol) -> ExpnInfo {
652644
ExpnInfo {
653645
call_site,
654-
kind: self.expn_kind(Symbol::intern(descr)),
646+
kind: ExpnKind::Macro(self.macro_kind(), descr),
655647
def_site: self.span,
656648
default_transparency: self.default_transparency,
657649
allow_internal_unstable: self.allow_internal_unstable.clone(),

src/libsyntax/ext/derive.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::attr::HasAttrs;
22
use crate::ast;
33
use crate::source_map::{ExpnInfo, ExpnKind};
4-
use crate::ext::base::ExtCtxt;
4+
use crate::ext::base::{ExtCtxt, MacroKind};
55
use crate::ext::build::AstBuilder;
66
use crate::parse::parser::PathStyle;
77
use crate::symbol::{Symbol, sym};
@@ -46,19 +46,18 @@ pub fn collect_derives(cx: &mut ExtCtxt<'_>, attrs: &mut Vec<ast::Attribute>) ->
4646
pub fn add_derived_markers<T>(cx: &mut ExtCtxt<'_>, span: Span, traits: &[ast::Path], item: &mut T)
4747
where T: HasAttrs,
4848
{
49-
let (mut names, mut pretty_name) = (FxHashSet::default(), "derive(".to_owned());
49+
let (mut names, mut pretty_name) = (FxHashSet::default(), String::new());
5050
for (i, path) in traits.iter().enumerate() {
5151
if i > 0 {
5252
pretty_name.push_str(", ");
5353
}
5454
pretty_name.push_str(&path.to_string());
5555
names.insert(unwrap_or!(path.segments.get(0), continue).ident.name);
5656
}
57-
pretty_name.push(')');
5857

5958
cx.current_expansion.mark.set_expn_info(ExpnInfo::with_unstable(
60-
ExpnKind::MacroAttribute(Symbol::intern(&pretty_name)), span, cx.parse_sess.edition,
61-
&[sym::rustc_attrs, sym::structural_match],
59+
ExpnKind::Macro(MacroKind::Derive, Symbol::intern(&pretty_name)), span,
60+
cx.parse_sess.edition, &[sym::rustc_attrs, sym::structural_match],
6261
));
6362

6463
let span = span.with_ctxt(cx.backtrace());

src/libsyntax/std_inject.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::ast;
22
use crate::attr;
33
use crate::edition::Edition;
4-
use crate::ext::hygiene::{Mark, SyntaxContext};
4+
use crate::ext::hygiene::{Mark, SyntaxContext, MacroKind};
55
use crate::symbol::{Ident, Symbol, kw, sym};
66
use crate::source_map::{ExpnInfo, ExpnKind, dummy_spanned, respan};
77
use crate::ptr::P;
@@ -17,7 +17,8 @@ use syntax_pos::{DUMMY_SP, Span};
1717
fn ignored_span(sp: Span, edition: Edition) -> Span {
1818
let mark = Mark::fresh(Mark::root());
1919
mark.set_expn_info(ExpnInfo::with_unstable(
20-
ExpnKind::MacroAttribute(Symbol::intern("std_inject")), sp, edition, &[sym::prelude_import]
20+
ExpnKind::Macro(MacroKind::Attr, Symbol::intern("std_inject")), sp, edition,
21+
&[sym::prelude_import],
2122
));
2223
sp.with_ctxt(SyntaxContext::empty().apply_mark(mark))
2324
}

src/libsyntax/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::entry::{self, EntryPointType};
2121
use crate::ext::base::{ExtCtxt, Resolver};
2222
use crate::ext::build::AstBuilder;
2323
use crate::ext::expand::ExpansionConfig;
24-
use crate::ext::hygiene::{self, Mark, SyntaxContext};
24+
use crate::ext::hygiene::{self, Mark, SyntaxContext, MacroKind};
2525
use crate::mut_visit::{*, ExpectOne};
2626
use crate::feature_gate::Features;
2727
use crate::util::map_in_place::MapInPlace;
@@ -280,7 +280,7 @@ fn generate_test_harness(sess: &ParseSess,
280280
};
281281

282282
mark.set_expn_info(ExpnInfo::with_unstable(
283-
ExpnKind::MacroAttribute(sym::test_case), DUMMY_SP, sess.edition,
283+
ExpnKind::Macro(MacroKind::Attr, sym::test_case), DUMMY_SP, sess.edition,
284284
&[sym::main, sym::test, sym::rustc_attrs],
285285
));
286286

src/libsyntax_ext/proc_macro_decls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::deriving;
55
use syntax::ast::{self, Ident};
66
use syntax::attr;
77
use syntax::source_map::{ExpnInfo, ExpnKind, respan};
8-
use syntax::ext::base::ExtCtxt;
8+
use syntax::ext::base::{ExtCtxt, MacroKind};
99
use syntax::ext::build::AstBuilder;
1010
use syntax::ext::expand::ExpansionConfig;
1111
use syntax::ext::hygiene::Mark;
@@ -348,7 +348,7 @@ fn mk_decls(
348348
) -> P<ast::Item> {
349349
let mark = Mark::fresh(Mark::root());
350350
mark.set_expn_info(ExpnInfo::with_unstable(
351-
ExpnKind::MacroAttribute(sym::proc_macro), DUMMY_SP, cx.parse_sess.edition,
351+
ExpnKind::Macro(MacroKind::Attr, sym::proc_macro), DUMMY_SP, cx.parse_sess.edition,
352352
&[sym::rustc_attrs, Symbol::intern("proc_macro_internals")],
353353
));
354354
let span = DUMMY_SP.apply_mark(mark);

0 commit comments

Comments
 (0)