Skip to content

Commit d4488b7

Browse files
committed
Simplify hygiene::Mark application, and
remove variant `Token::SubstNt` in favor of `quoted::TokenTree::MetaVar`.
1 parent fc9ccfd commit d4488b7

File tree

26 files changed

+160
-172
lines changed

26 files changed

+160
-172
lines changed

src/libproc_macro/lib.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ pub mod __internal {
8787
use std::rc::Rc;
8888

8989
use syntax::ast;
90+
use syntax::ext::base::ExtCtxt;
91+
use syntax::ext::hygiene::Mark;
9092
use syntax::ptr::P;
9193
use syntax::parse::{self, token, ParseSess};
9294
use syntax::tokenstream::{TokenTree, TokenStream as TokenStream_};
@@ -107,7 +109,7 @@ pub mod __internal {
107109
}
108110

109111
pub fn token_stream_parse_items(stream: TokenStream) -> Result<Vec<P<ast::Item>>, LexError> {
110-
with_parse_sess(move |sess| {
112+
with_sess(move |(sess, _)| {
111113
let mut parser = parse::stream_to_parser(sess, stream.inner);
112114
let mut items = Vec::new();
113115

@@ -140,13 +142,14 @@ pub mod __internal {
140142

141143
// Emulate scoped_thread_local!() here essentially
142144
thread_local! {
143-
static CURRENT_SESS: Cell<*const ParseSess> = Cell::new(0 as *const _);
145+
static CURRENT_SESS: Cell<(*const ParseSess, Mark)> =
146+
Cell::new((0 as *const _, Mark::root()));
144147
}
145148

146-
pub fn set_parse_sess<F, R>(sess: &ParseSess, f: F) -> R
149+
pub fn set_sess<F, R>(cx: &ExtCtxt, f: F) -> R
147150
where F: FnOnce() -> R
148151
{
149-
struct Reset { prev: *const ParseSess }
152+
struct Reset { prev: (*const ParseSess, Mark) }
150153

151154
impl Drop for Reset {
152155
fn drop(&mut self) {
@@ -156,18 +159,18 @@ pub mod __internal {
156159

157160
CURRENT_SESS.with(|p| {
158161
let _reset = Reset { prev: p.get() };
159-
p.set(sess);
162+
p.set((cx.parse_sess, cx.current_expansion.mark));
160163
f()
161164
})
162165
}
163166

164-
pub fn with_parse_sess<F, R>(f: F) -> R
165-
where F: FnOnce(&ParseSess) -> R
167+
pub fn with_sess<F, R>(f: F) -> R
168+
where F: FnOnce((&ParseSess, Mark)) -> R
166169
{
167170
let p = CURRENT_SESS.with(|p| p.get());
168-
assert!(!p.is_null(), "proc_macro::__internal::with_parse_sess() called \
169-
before set_parse_sess()!");
170-
f(unsafe { &*p })
171+
assert!(!p.0.is_null(), "proc_macro::__internal::with_sess() called \
172+
before set_parse_sess()!");
173+
f(unsafe { (&*p.0, p.1) })
171174
}
172175
}
173176

@@ -181,10 +184,11 @@ impl FromStr for TokenStream {
181184
type Err = LexError;
182185

183186
fn from_str(src: &str) -> Result<TokenStream, LexError> {
184-
__internal::with_parse_sess(|sess| {
187+
__internal::with_sess(|(sess, mark)| {
185188
let src = src.to_string();
186189
let name = "<proc-macro source code>".to_string();
187-
let stream = parse::parse_stream_from_source_str(name, src, sess);
190+
let call_site = mark.expn_info().unwrap().call_site;
191+
let stream = parse::parse_stream_from_source_str(name, src, sess, Some(call_site));
188192
Ok(__internal::token_stream_wrap(stream))
189193
})
190194
}

src/librustc/ich/impls_syntax.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,7 @@ fn hash_token<'a, 'gcx, 'tcx, W: StableHasherResult>(token: &token::Token,
283283
}
284284

285285
token::Token::Ident(ident) |
286-
token::Token::Lifetime(ident) |
287-
token::Token::SubstNt(ident) => ident.name.hash_stable(hcx, hasher),
286+
token::Token::Lifetime(ident) => ident.name.hash_stable(hcx, hasher),
288287

289288
token::Token::Interpolated(ref non_terminal) => {
290289
// FIXME(mw): This could be implemented properly. It's just a

src/librustc_metadata/cstore_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ impl CrateStore for cstore::CStore {
372372

373373
let filemap = sess.parse_sess.codemap().new_filemap(source_name, def.body);
374374
let local_span = Span { lo: filemap.start_pos, hi: filemap.end_pos, ctxt: NO_EXPANSION };
375-
let body = filemap_to_stream(&sess.parse_sess, filemap);
375+
let body = filemap_to_stream(&sess.parse_sess, filemap, None);
376376

377377
// Mark the attrs as used
378378
let attrs = data.get_item_attrs(id.index, &self.dep_graph);

src/librustdoc/html/highlight.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ impl<'a> Classifier<'a> {
319319
token::Lifetime(..) => Class::Lifetime,
320320

321321
token::Underscore | token::Eof | token::Interpolated(..) |
322-
token::SubstNt(..) | token::Tilde | token::At => Class::None,
322+
token::Tilde | token::At => Class::None,
323323
};
324324

325325
// Anything that didn't return above is the simple case where we the

src/libsyntax/ext/base.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -903,17 +903,3 @@ pub fn get_exprs_from_tts(cx: &mut ExtCtxt,
903903
}
904904
Some(es)
905905
}
906-
907-
pub struct ChangeSpan {
908-
pub span: Span
909-
}
910-
911-
impl Folder for ChangeSpan {
912-
fn new_span(&mut self, _sp: Span) -> Span {
913-
self.span
914-
}
915-
916-
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
917-
fold::noop_fold_mac(mac, self)
918-
}
919-
}

src/libsyntax/ext/expand.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use config::{is_test_or_bench, StripUnconfigured};
1616
use errors::FatalError;
1717
use ext::base::*;
1818
use ext::derive::{add_derived_markers, collect_derives};
19-
use ext::hygiene::Mark;
19+
use ext::hygiene::{Mark, SyntaxContext};
2020
use ext::placeholders::{placeholder, PlaceholderExpander};
2121
use feature_gate::{self, Features, is_builtin_attr};
2222
use fold;
@@ -470,15 +470,14 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
470470
Ok(())
471471
};
472472

473-
let marked_tts = noop_fold_tts(mac.node.stream(), &mut Marker(mark));
474473
let opt_expanded = match *ext {
475474
SyntaxExtension::DeclMacro(ref expand, def_span) => {
476475
if let Err(msg) = validate_and_set_expn_info(def_span.map(|(_, s)| s),
477476
false) {
478477
self.cx.span_err(path.span, &msg);
479478
return kind.dummy(span);
480479
}
481-
kind.make_from(expand.expand(self.cx, span, marked_tts))
480+
kind.make_from(expand.expand(self.cx, span, mac.node.stream()))
482481
}
483482

484483
NormalTT(ref expandfun, def_info, allow_internal_unstable) => {
@@ -487,7 +486,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
487486
self.cx.span_err(path.span, &msg);
488487
return kind.dummy(span);
489488
}
490-
kind.make_from(expandfun.expand(self.cx, span, marked_tts))
489+
kind.make_from(expandfun.expand(self.cx, span, mac.node.stream()))
491490
}
492491

493492
IdentTT(ref expander, tt_span, allow_internal_unstable) => {
@@ -506,7 +505,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
506505
}
507506
});
508507

509-
let input: Vec<_> = marked_tts.into_trees().collect();
508+
let input: Vec<_> = mac.node.stream().into_trees().collect();
510509
kind.make_from(expander.expand(self.cx, span, ident, input))
511510
}
512511

@@ -541,21 +540,17 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
541540
},
542541
});
543542

544-
let tok_result = expandfun.expand(self.cx, span, marked_tts);
543+
let tok_result = expandfun.expand(self.cx, span, mac.node.stream());
545544
Some(self.parse_expansion(tok_result, kind, path, span))
546545
}
547546
};
548547

549-
let expanded = if let Some(expanded) = opt_expanded {
550-
expanded
551-
} else {
548+
unwrap_or!(opt_expanded, {
552549
let msg = format!("non-{kind} macro in {kind} position: {name}",
553550
name = path.segments[0].identifier.name, kind = kind.name());
554551
self.cx.span_err(path.span, &msg);
555-
return kind.dummy(span);
556-
};
557-
558-
expanded.fold_with(&mut Marker(mark))
552+
kind.dummy(span)
553+
})
559554
}
560555

561556
/// Expand a derive invocation. Returns the result of expansion.
@@ -621,8 +616,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
621616
}
622617
};
623618
parser.ensure_complete_parse(path, kind.name(), span);
624-
// FIXME better span info
625-
expansion.fold_with(&mut ChangeSpan { span: span })
619+
expansion
626620
}
627621
}
628622

@@ -673,7 +667,9 @@ impl<'a> Parser<'a> {
673667
if self.token != token::Eof {
674668
let msg = format!("macro expansion ignores token `{}` and any following",
675669
self.this_token_to_string());
676-
let mut err = self.diagnostic().struct_span_err(self.span, &msg);
670+
let mut def_site_span = self.span;
671+
def_site_span.ctxt = SyntaxContext::empty(); // Avoid emitting backtrace info twice.
672+
let mut err = self.diagnostic().struct_span_err(def_site_span, &msg);
677673
let msg = format!("caused by the macro expansion here; the usage \
678674
of `{}!` is likely invalid in {} context",
679675
macro_path, kind_name);
@@ -787,12 +783,12 @@ fn stream_for_item(item: &Annotatable, parse_sess: &ParseSess) -> TokenStream {
787783
Annotatable::TraitItem(ref ti) => pprust::trait_item_to_string(ti),
788784
Annotatable::ImplItem(ref ii) => pprust::impl_item_to_string(ii),
789785
};
790-
string_to_stream(text, parse_sess)
786+
string_to_stream(text, parse_sess, item.span())
791787
}
792788

793-
fn string_to_stream(text: String, parse_sess: &ParseSess) -> TokenStream {
789+
fn string_to_stream(text: String, parse_sess: &ParseSess, span: Span) -> TokenStream {
794790
let filename = String::from("<macro expansion>");
795-
filemap_to_stream(parse_sess, parse_sess.codemap().new_filemap(filename, text))
791+
filemap_to_stream(parse_sess, parse_sess.codemap().new_filemap(filename, text), Some(span))
796792
}
797793

798794
impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
@@ -1070,7 +1066,7 @@ impl<'feat> ExpansionConfig<'feat> {
10701066
}
10711067

10721068
// A Marker adds the given mark to the syntax context.
1073-
struct Marker(Mark);
1069+
pub struct Marker(pub Mark);
10741070

10751071
impl Folder for Marker {
10761072
fn fold_ident(&mut self, mut ident: Ident) -> Ident {

src/libsyntax/ext/quote.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ pub mod rt {
364364

365365
fn parse_tts(&self, s: String) -> Vec<TokenTree> {
366366
let source_name = "<quote expansion>".to_owned();
367-
parse::parse_stream_from_source_str(source_name, s, self.parse_sess())
367+
parse::parse_stream_from_source_str(source_name, s, self.parse_sess(), None)
368368
.into_trees().collect()
369369
}
370370
}
@@ -700,7 +700,7 @@ fn expr_mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> P<ast::Expr> {
700700
token::Underscore => "Underscore",
701701
token::Eof => "Eof",
702702

703-
token::Whitespace | token::SubstNt(_) | token::Comment | token::Shebang(_) => {
703+
token::Whitespace | token::Comment | token::Shebang(_) => {
704704
panic!("unhandled token in quote!");
705705
}
706706
};

src/libsyntax/ext/tt/macro_parser.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,10 @@ pub type NamedParseResult = ParseResult<HashMap<Ident, Rc<NamedMatch>>>;
158158
pub fn count_names(ms: &[TokenTree]) -> usize {
159159
ms.iter().fold(0, |count, elt| {
160160
count + match *elt {
161-
TokenTree::Sequence(_, ref seq) => {
162-
seq.num_captures
163-
}
164-
TokenTree::Delimited(_, ref delim) => {
165-
count_names(&delim.tts)
166-
}
167-
TokenTree::MetaVarDecl(..) => {
168-
1
169-
}
161+
TokenTree::Sequence(_, ref seq) => seq.num_captures,
162+
TokenTree::Delimited(_, ref delim) => count_names(&delim.tts),
163+
TokenTree::MetaVar(..) => 0,
164+
TokenTree::MetaVarDecl(..) => 1,
170165
TokenTree::Token(..) => 0,
171166
}
172167
})
@@ -244,7 +239,7 @@ fn nameize<I: Iterator<Item=NamedMatch>>(sess: &ParseSess, ms: &[TokenTree], mut
244239
}
245240
}
246241
}
247-
TokenTree::Token(..) => (),
242+
TokenTree::MetaVar(..) | TokenTree::Token(..) => (),
248243
}
249244

250245
Ok(())
@@ -409,12 +404,11 @@ fn inner_parse_loop(sess: &ParseSess,
409404
ei.idx = 0;
410405
cur_eis.push(ei);
411406
}
412-
TokenTree::Token(_, ref t) => {
413-
if token_name_eq(t, token) {
414-
ei.idx += 1;
415-
next_eis.push(ei);
416-
}
407+
TokenTree::Token(_, ref t) if token_name_eq(t, token) => {
408+
ei.idx += 1;
409+
next_eis.push(ei);
417410
}
411+
TokenTree::Token(..) | TokenTree::MetaVar(..) => {}
418412
}
419413
}
420414
}

src/libsyntax/ext/tt/macro_rules.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fn generic_extension<'cx>(cx: &'cx mut ExtCtxt,
120120
_ => cx.span_bug(sp, "malformed macro rhs"),
121121
};
122122
// rhs has holes ( `$id` and `$(...)` that need filled)
123-
let tts = transcribe(&cx.parse_sess.span_diagnostic, Some(named_matches), rhs);
123+
let tts = transcribe(cx, Some(named_matches), rhs);
124124

125125
if cx.trace_macros() {
126126
trace_macros_note(cx, sp, format!("to `{}`", tts));
@@ -292,7 +292,7 @@ fn check_lhs_no_empty_seq(sess: &ParseSess, tts: &[quoted::TokenTree]) -> bool {
292292
use self::quoted::TokenTree;
293293
for tt in tts {
294294
match *tt {
295-
TokenTree::Token(..) | TokenTree::MetaVarDecl(..) => (),
295+
TokenTree::Token(..) | TokenTree::MetaVar(..) | TokenTree::MetaVarDecl(..) => (),
296296
TokenTree::Delimited(_, ref del) => if !check_lhs_no_empty_seq(sess, &del.tts) {
297297
return false;
298298
},
@@ -372,7 +372,7 @@ impl FirstSets {
372372
let mut first = TokenSet::empty();
373373
for tt in tts.iter().rev() {
374374
match *tt {
375-
TokenTree::Token(..) | TokenTree::MetaVarDecl(..) => {
375+
TokenTree::Token(..) | TokenTree::MetaVar(..) | TokenTree::MetaVarDecl(..) => {
376376
first.replace_with(tt.clone());
377377
}
378378
TokenTree::Delimited(span, ref delimited) => {
@@ -432,7 +432,7 @@ impl FirstSets {
432432
for tt in tts.iter() {
433433
assert!(first.maybe_empty);
434434
match *tt {
435-
TokenTree::Token(..) | TokenTree::MetaVarDecl(..) => {
435+
TokenTree::Token(..) | TokenTree::MetaVar(..) | TokenTree::MetaVarDecl(..) => {
436436
first.add_one(tt.clone());
437437
return first;
438438
}
@@ -602,7 +602,7 @@ fn check_matcher_core(sess: &ParseSess,
602602
// First, update `last` so that it corresponds to the set
603603
// of NT tokens that might end the sequence `... token`.
604604
match *token {
605-
TokenTree::Token(..) | TokenTree::MetaVarDecl(..) => {
605+
TokenTree::Token(..) | TokenTree::MetaVar(..) | TokenTree::MetaVarDecl(..) => {
606606
let can_be_followed_by_any;
607607
if let Err(bad_frag) = has_legal_fragment_specifier(sess, features, token) {
608608
let msg = format!("invalid fragment specifier `{}`", bad_frag);
@@ -872,6 +872,7 @@ fn is_legal_fragment_specifier(sess: &ParseSess,
872872
fn quoted_tt_to_string(tt: &quoted::TokenTree) -> String {
873873
match *tt {
874874
quoted::TokenTree::Token(_, ref tok) => ::print::pprust::token_to_string(tok),
875+
quoted::TokenTree::MetaVar(_, name) => format!("${}", name),
875876
quoted::TokenTree::MetaVarDecl(_, name, kind) => format!("${}:{}", name, kind),
876877
_ => panic!("unexpected quoted::TokenTree::{{Sequence or Delimited}} \
877878
in follow set checker"),

0 commit comments

Comments
 (0)