Skip to content

Commit d2faba2

Browse files
committed
Auto merge of #28552 - apasel422:issue-28527, r=Manishearth
Closes #28527. r? @Manishearth
2 parents 0418a43 + 85b8b44 commit d2faba2

File tree

7 files changed

+114
-139
lines changed

7 files changed

+114
-139
lines changed

src/libsyntax/ast.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ pub use self::Item_::*;
2828
pub use self::KleeneOp::*;
2929
pub use self::Lit_::*;
3030
pub use self::LitIntType::*;
31-
pub use self::Mac_::*;
3231
pub use self::MacStmtStyle::*;
3332
pub use self::MetaItem_::*;
3433
pub use self::Mutability::*;
@@ -1132,12 +1131,13 @@ pub type Mac = Spanned<Mac_>;
11321131
/// is being invoked, and the vector of token-trees contains the source
11331132
/// of the macro invocation.
11341133
///
1135-
/// There's only one flavor, now, so this could presumably be simplified.
1134+
/// NB: the additional ident for a macro_rules-style macro is actually
1135+
/// stored in the enclosing item. Oog.
11361136
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1137-
pub enum Mac_ {
1138-
// NB: the additional ident for a macro_rules-style macro is actually
1139-
// stored in the enclosing item. Oog.
1140-
MacInvocTT(Path, Vec<TokenTree>, SyntaxContext), // new macro-invocation
1137+
pub struct Mac_ {
1138+
pub path: Path,
1139+
pub tts: Vec<TokenTree>,
1140+
pub ctxt: SyntaxContext,
11411141
}
11421142

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

src/libsyntax/ext/expand.rs

Lines changed: 75 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use ast::{Block, Crate, DeclLocal, ExprMac, PatMac};
12-
use ast::{Local, Ident, MacInvocTT};
12+
use ast::{Local, Ident, Mac_};
1313
use ast::{ItemMac, MacStmtWithSemicolon, Mrk, Stmt, StmtDecl, StmtMac};
1414
use ast::{StmtExpr, StmtSemi};
1515
use ast::TokenTree;
@@ -509,78 +509,75 @@ fn expand_mac_invoc<T, F, G>(mac: ast::Mac,
509509
F: for<'a> FnOnce(Box<MacResult+'a>) -> Option<T>,
510510
G: FnOnce(T, Mrk) -> T,
511511
{
512-
match mac.node {
513-
// it would almost certainly be cleaner to pass the whole
514-
// macro invocation in, rather than pulling it apart and
515-
// marking the tts and the ctxt separately. This also goes
516-
// for the other three macro invocation chunks of code
517-
// in this file.
518-
// Token-tree macros:
519-
MacInvocTT(pth, tts, _) => {
520-
if pth.segments.len() > 1 {
521-
fld.cx.span_err(pth.span,
522-
"expected macro name without module \
523-
separators");
524-
// let compilation continue
525-
return None;
526-
}
527-
let extname = pth.segments[0].identifier.name;
528-
match fld.cx.syntax_env.find(&extname) {
529-
None => {
530-
fld.cx.span_err(
531-
pth.span,
532-
&format!("macro undefined: '{}!'",
533-
&extname));
512+
// it would almost certainly be cleaner to pass the whole
513+
// macro invocation in, rather than pulling it apart and
514+
// marking the tts and the ctxt separately. This also goes
515+
// for the other three macro invocation chunks of code
516+
// in this file.
517+
518+
let Mac_ { path: pth, tts, .. } = mac.node;
519+
if pth.segments.len() > 1 {
520+
fld.cx.span_err(pth.span,
521+
"expected macro name without module \
522+
separators");
523+
// let compilation continue
524+
return None;
525+
}
526+
let extname = pth.segments[0].identifier.name;
527+
match fld.cx.syntax_env.find(&extname) {
528+
None => {
529+
fld.cx.span_err(
530+
pth.span,
531+
&format!("macro undefined: '{}!'",
532+
&extname));
534533

535-
// let compilation continue
536-
None
537-
}
538-
Some(rc) => match *rc {
539-
NormalTT(ref expandfun, exp_span, allow_internal_unstable) => {
540-
fld.cx.bt_push(ExpnInfo {
541-
call_site: span,
542-
callee: NameAndSpan {
543-
format: MacroBang(extname),
544-
span: exp_span,
545-
allow_internal_unstable: allow_internal_unstable,
546-
},
547-
});
548-
let fm = fresh_mark();
549-
let marked_before = mark_tts(&tts[..], fm);
550-
551-
// The span that we pass to the expanders we want to
552-
// be the root of the call stack. That's the most
553-
// relevant span and it's the actual invocation of
554-
// the macro.
555-
let mac_span = fld.cx.original_span();
556-
557-
let opt_parsed = {
558-
let expanded = expandfun.expand(fld.cx,
559-
mac_span,
560-
&marked_before[..]);
561-
parse_thunk(expanded)
562-
};
563-
let parsed = match opt_parsed {
564-
Some(e) => e,
565-
None => {
566-
fld.cx.span_err(
567-
pth.span,
568-
&format!("non-expression macro in expression position: {}",
569-
extname
570-
));
571-
return None;
572-
}
573-
};
574-
Some(mark_thunk(parsed,fm))
575-
}
576-
_ => {
534+
// let compilation continue
535+
None
536+
}
537+
Some(rc) => match *rc {
538+
NormalTT(ref expandfun, exp_span, allow_internal_unstable) => {
539+
fld.cx.bt_push(ExpnInfo {
540+
call_site: span,
541+
callee: NameAndSpan {
542+
format: MacroBang(extname),
543+
span: exp_span,
544+
allow_internal_unstable: allow_internal_unstable,
545+
},
546+
});
547+
let fm = fresh_mark();
548+
let marked_before = mark_tts(&tts[..], fm);
549+
550+
// The span that we pass to the expanders we want to
551+
// be the root of the call stack. That's the most
552+
// relevant span and it's the actual invocation of
553+
// the macro.
554+
let mac_span = fld.cx.original_span();
555+
556+
let opt_parsed = {
557+
let expanded = expandfun.expand(fld.cx,
558+
mac_span,
559+
&marked_before[..]);
560+
parse_thunk(expanded)
561+
};
562+
let parsed = match opt_parsed {
563+
Some(e) => e,
564+
None => {
577565
fld.cx.span_err(
578566
pth.span,
579-
&format!("'{}' is not a tt-style macro",
580-
extname));
581-
None
567+
&format!("non-expression macro in expression position: {}",
568+
extname
569+
));
570+
return None;
582571
}
583-
}
572+
};
573+
Some(mark_thunk(parsed,fm))
574+
}
575+
_ => {
576+
fld.cx.span_err(
577+
pth.span,
578+
&format!("'{}' is not a tt-style macro",
579+
extname));
580+
None
584581
}
585582
}
586583
}
@@ -684,15 +681,11 @@ fn contains_macro_use(fld: &mut MacroExpander, attrs: &[ast::Attribute]) -> bool
684681
// logic as for expression-position macro invocations.
685682
pub fn expand_item_mac(it: P<ast::Item>,
686683
fld: &mut MacroExpander) -> SmallVector<P<ast::Item>> {
687-
let (extname, path_span, tts, span, attrs, ident) = it.and_then(|it| { match it.node {
688-
ItemMac(codemap::Spanned {
689-
node: MacInvocTT(pth, tts, _),
690-
..
691-
}) => {
692-
(pth.segments[0].identifier.name, pth.span, tts, it.span, it.attrs, it.ident)
693-
}
684+
let (extname, path_span, tts, span, attrs, ident) = it.and_then(|it| match it.node {
685+
ItemMac(codemap::Spanned { node: Mac_ { path, tts, .. }, .. }) =>
686+
(path.segments[0].identifier.name, path.span, tts, it.span, it.attrs, it.ident),
694687
_ => fld.cx.span_bug(it.span, "invalid item macro invocation")
695-
}});
688+
});
696689

697690
let fm = fresh_mark();
698691
let items = {
@@ -1060,11 +1053,7 @@ fn expand_pat(p: P<ast::Pat>, fld: &mut MacroExpander) -> P<ast::Pat> {
10601053
}
10611054
p.map(|ast::Pat {node, span, ..}| {
10621055
let (pth, tts) = match node {
1063-
PatMac(mac) => match mac.node {
1064-
MacInvocTT(pth, tts, _) => {
1065-
(pth, tts)
1066-
}
1067-
},
1056+
PatMac(mac) => (mac.node.path, mac.node.tts),
10681057
_ => unreachable!()
10691058
};
10701059
if pth.segments.len() > 1 {
@@ -1646,12 +1635,10 @@ impl Folder for Marker {
16461635
}
16471636
fn fold_mac(&mut self, Spanned {node, span}: ast::Mac) -> ast::Mac {
16481637
Spanned {
1649-
node: match node {
1650-
MacInvocTT(path, tts, ctxt) => {
1651-
MacInvocTT(self.fold_path(path),
1652-
self.fold_tts(&tts[..]),
1653-
mtwt::apply_mark(self.mark, ctxt))
1654-
}
1638+
node: Mac_ {
1639+
path: self.fold_path(node.path),
1640+
tts: self.fold_tts(&node.tts),
1641+
ctxt: mtwt::apply_mark(self.mark, node.ctxt),
16551642
},
16561643
span: span,
16571644
}

src/libsyntax/feature_gate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ struct MacroVisitor<'a> {
666666

667667
impl<'a, 'v> Visitor<'v> for MacroVisitor<'a> {
668668
fn visit_mac(&mut self, mac: &ast::Mac) {
669-
let ast::MacInvocTT(ref path, _, _) = mac.node;
669+
let path = &mac.node.path;
670670
let id = path.segments.last().unwrap().identifier;
671671

672672
// Issue 22234: If you add a new case here, make sure to also

src/libsyntax/fold.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,10 @@ pub fn noop_fold_explicit_self<T: Folder>(Spanned {span, node}: ExplicitSelf, fl
567567

568568
pub fn noop_fold_mac<T: Folder>(Spanned {node, span}: Mac, fld: &mut T) -> Mac {
569569
Spanned {
570-
node: match node {
571-
MacInvocTT(p, tts, ctxt) => {
572-
MacInvocTT(fld.fold_path(p), fld.fold_tts(&tts), ctxt)
573-
}
570+
node: Mac_ {
571+
path: fld.fold_path(node.path),
572+
tts: fld.fold_tts(&node.tts),
573+
ctxt: node.ctxt,
574574
},
575575
span: fld.new_span(span)
576576
}

src/libsyntax/parse/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,10 +1090,7 @@ mod tests {
10901090
"foo!( fn main() { body } )".to_string(), vec![], &sess);
10911091

10921092
let tts = match expr.node {
1093-
ast::ExprMac(ref mac) => {
1094-
let ast::MacInvocTT(_, ref tts, _) = mac.node;
1095-
tts.clone()
1096-
}
1093+
ast::ExprMac(ref mac) => mac.node.tts.clone(),
10971094
_ => panic!("not a macro"),
10981095
};
10991096

src/libsyntax/parse/parser.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use ast::{LifetimeDef, Lit, Lit_};
3737
use ast::{LitBool, LitChar, LitByte, LitByteStr};
3838
use ast::{LitStr, LitInt, Local};
3939
use ast::{MacStmtWithBraces, MacStmtWithSemicolon, MacStmtWithoutBraces};
40-
use ast::{MutImmutable, MutMutable, Mac_, MacInvocTT, MatchSource};
40+
use ast::{MutImmutable, MutMutable, Mac_, MatchSource};
4141
use ast::{MutTy, BiMul, Mutability};
4242
use ast::{MethodImplItem, NamedField, UnNeg, NoReturn, UnNot};
4343
use ast::{Pat, PatBox, PatEnum, PatIdent, PatLit, PatQPath, PatMac, PatRange};
@@ -1381,7 +1381,7 @@ impl<'a> Parser<'a> {
13811381
seq_sep_none(),
13821382
|p| p.parse_token_tree()));
13831383
let hi = self.span.hi;
1384-
TyMac(spanned(lo, hi, MacInvocTT(path, tts, EMPTY_CTXT)))
1384+
TyMac(spanned(lo, hi, Mac_ { path: path, tts: tts, ctxt: EMPTY_CTXT }))
13851385
} else {
13861386
// NAMED TYPE
13871387
TyPath(None, path)
@@ -2203,9 +2203,7 @@ impl<'a> Parser<'a> {
22032203

22042204
return Ok(self.mk_mac_expr(lo,
22052205
hi,
2206-
MacInvocTT(pth,
2207-
tts,
2208-
EMPTY_CTXT)));
2206+
Mac_ { path: pth, tts: tts, ctxt: EMPTY_CTXT }));
22092207
}
22102208
if self.check(&token::OpenDelim(token::Brace)) {
22112209
// This is a struct literal, unless we're prohibited
@@ -3289,7 +3287,7 @@ impl<'a> Parser<'a> {
32893287
let delim = try!(self.expect_open_delim());
32903288
let tts = try!(self.parse_seq_to_end(&token::CloseDelim(delim),
32913289
seq_sep_none(), |p| p.parse_token_tree()));
3292-
let mac = MacInvocTT(path, tts, EMPTY_CTXT);
3290+
let mac = Mac_ { path: path, tts: tts, ctxt: EMPTY_CTXT };
32933291
pat = PatMac(codemap::Spanned {node: mac, span: self.span});
32943292
} else {
32953293
// Parse ident @ pat
@@ -3557,7 +3555,7 @@ impl<'a> Parser<'a> {
35573555
spanned(lo, hi,
35583556
StmtMac(P(spanned(lo,
35593557
hi,
3560-
MacInvocTT(pth, tts, EMPTY_CTXT))),
3558+
Mac_ { path: pth, tts: tts, ctxt: EMPTY_CTXT })),
35613559
style))
35623560
} else {
35633561
// if it has a special ident, it's definitely an item
@@ -3576,7 +3574,8 @@ impl<'a> Parser<'a> {
35763574
P(spanned(lo, hi, DeclItem(
35773575
self.mk_item(
35783576
lo, hi, id /*id is good here*/,
3579-
ItemMac(spanned(lo, hi, MacInvocTT(pth, tts, EMPTY_CTXT))),
3577+
ItemMac(spanned(lo, hi,
3578+
Mac_ { path: pth, tts: tts, ctxt: EMPTY_CTXT })),
35803579
Inherited, Vec::new(/*no attrs*/))))),
35813580
ast::DUMMY_NODE_ID))
35823581
}
@@ -4524,7 +4523,7 @@ impl<'a> Parser<'a> {
45244523
let tts = try!(self.parse_seq_to_end(&token::CloseDelim(delim),
45254524
seq_sep_none(),
45264525
|p| p.parse_token_tree()));
4527-
let m_ = ast::MacInvocTT(pth, tts, EMPTY_CTXT);
4526+
let m_ = Mac_ { path: pth, tts: tts, ctxt: EMPTY_CTXT };
45284527
let m: ast::Mac = codemap::Spanned { node: m_,
45294528
span: mk_sp(self.span.lo,
45304529
self.span.hi) };
@@ -5606,7 +5605,7 @@ impl<'a> Parser<'a> {
56065605
seq_sep_none(),
56075606
|p| p.parse_token_tree()));
56085607
// single-variant-enum... :
5609-
let m = ast::MacInvocTT(pth, tts, EMPTY_CTXT);
5608+
let m = Mac_ { path: pth, tts: tts, ctxt: EMPTY_CTXT };
56105609
let m: ast::Mac = codemap::Spanned { node: m,
56115610
span: mk_sp(self.span.lo,
56125611
self.span.hi) };

0 commit comments

Comments
 (0)