Skip to content

Use ~-objects instead of @-objects for syntax exts #11332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 6, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ pub enum SyntaxExtension {
ItemDecorator(ItemDecorator),

// Token-tree expanders
NormalTT(@SyntaxExpanderTTTrait, Option<Span>),
NormalTT(~SyntaxExpanderTTTrait:'static, Option<Span>),

// An IdentTT is a macro that has an
// identifier in between the name of the
Expand All @@ -148,7 +148,7 @@ pub enum SyntaxExtension {

// perhaps macro_rules! will lose its odd special identifier argument,
// and this can go away also
IdentTT(@SyntaxExpanderTTItemTrait, Option<Span>),
IdentTT(~SyntaxExpanderTTItemTrait:'static, Option<Span>),
}


Expand Down Expand Up @@ -182,20 +182,20 @@ pub fn syntax_expander_table() -> SyntaxEnv {
// utility function to simplify creating NormalTT syntax extensions
fn builtin_normal_tt_no_ctxt(f: SyntaxExpanderTTFunNoCtxt)
-> SyntaxExtension {
NormalTT(@SyntaxExpanderTT{
NormalTT(~SyntaxExpanderTT{
expander: SyntaxExpanderTTExpanderWithoutContext(f),
span: None,
} as @SyntaxExpanderTTTrait,
},
None)
}

let mut syntax_expanders = MapChain::new();
syntax_expanders.insert(intern(&"macro_rules"),
IdentTT(@SyntaxExpanderTTItem {
IdentTT(~SyntaxExpanderTTItem {
expander: SyntaxExpanderTTItemExpanderWithContext(
ext::tt::macro_rules::add_new_extension),
span: None,
} as @SyntaxExpanderTTItemTrait,
},
None));
syntax_expanders.insert(intern(&"fmt"),
builtin_normal_tt_no_ctxt(
Expand Down
105 changes: 52 additions & 53 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ pub fn expand_expr(e: @ast::Expr, fld: &mut MacroExpander) -> @ast::Expr {
let extname = &pth.segments[0].identifier;
let extnamestr = ident_to_str(extname);
// leaving explicit deref here to highlight unbox op:
match fld.extsbox.find(&extname.name) {
let marked_after = match fld.extsbox.find(&extname.name) {
None => {
fld.cx.span_fatal(
pth.span,
format!("macro undefined: '{}'", extnamestr))
}
Some(&NormalTT(expandfun, exp_span)) => {
Some(&NormalTT(ref expandfun, exp_span)) => {
fld.cx.bt_push(ExpnInfo {
call_site: e.span,
callee: NameAndSpan {
Expand All @@ -79,46 +79,46 @@ pub fn expand_expr(e: @ast::Expr, fld: &mut MacroExpander) -> @ast::Expr {
// the macro.
let mac_span = original_span(fld.cx);

let expanded =
match expandfun.expand(fld.cx,
mac_span.call_site,
marked_before,
marked_ctxt) {
MRExpr(e) => e,
MRAny(any_macro) => any_macro.make_expr(),
_ => {
fld.cx.span_fatal(
pth.span,
format!(
"non-expr macro in expr pos: {}",
extnamestr
)
let expanded = match expandfun.expand(fld.cx,
mac_span.call_site,
marked_before,
marked_ctxt) {
MRExpr(e) => e,
MRAny(any_macro) => any_macro.make_expr(),
_ => {
fld.cx.span_fatal(
pth.span,
format!(
"non-expr macro in expr pos: {}",
extnamestr
)
}
};
)
}
};

// mark after:
let marked_after = mark_expr(expanded,fm);

// Keep going, outside-in.
//
// XXX(pcwalton): Is it necessary to clone the
// node here?
let fully_expanded =
fld.fold_expr(marked_after).node.clone();
fld.cx.bt_pop();

@ast::Expr {
id: ast::DUMMY_NODE_ID,
node: fully_expanded,
span: e.span,
}
mark_expr(expanded,fm)
}
_ => {
fld.cx.span_fatal(
pth.span,
format!("'{}' is not a tt-style macro", extnamestr)
)
}
};

// Keep going, outside-in.
//
// XXX(pcwalton): Is it necessary to clone the
// node here?
let fully_expanded =
fld.fold_expr(marked_after).node.clone();
fld.cx.bt_pop();

@ast::Expr {
id: ast::DUMMY_NODE_ID,
node: fully_expanded,
span: e.span,
}
}
}
Expand Down Expand Up @@ -301,7 +301,7 @@ pub fn expand_item_mac(it: @ast::item, fld: &mut MacroExpander)
None => fld.cx.span_fatal(pth.span,
format!("macro undefined: '{}!'", extnamestr)),

Some(&NormalTT(expander, span)) => {
Some(&NormalTT(ref expander, span)) => {
if it.ident.name != parse::token::special_idents::invalid.name {
fld.cx.span_fatal(pth.span,
format!("macro {}! expects no ident argument, \
Expand All @@ -321,7 +321,7 @@ pub fn expand_item_mac(it: @ast::item, fld: &mut MacroExpander)
let marked_ctxt = new_mark(fm,ctxt);
expander.expand(fld.cx, it.span, marked_before, marked_ctxt)
}
Some(&IdentTT(expander, span)) => {
Some(&IdentTT(ref expander, span)) => {
if it.ident.name == parse::token::special_idents::invalid.name {
fld.cx.span_fatal(pth.span,
format!("macro {}! expects an ident argument",
Expand Down Expand Up @@ -361,10 +361,10 @@ pub fn expand_item_mac(it: @ast::item, fld: &mut MacroExpander)
.flat_map(|i| fld.fold_item(i).move_iter())
.collect()
}
MRDef(ref mdef) => {
MRDef(MacroDef { name, ext }) => {
// yikes... no idea how to apply the mark to this. I'm afraid
// we're going to have to wait-and-see on this one.
fld.extsbox.insert(intern(mdef.name), (*mdef).ext);
fld.extsbox.insert(intern(name), ext);
SmallVector::zero()
}
};
Expand Down Expand Up @@ -392,12 +392,12 @@ pub fn expand_stmt(s: &Stmt, fld: &mut MacroExpander) -> SmallVector<@Stmt> {
}
let extname = &pth.segments[0].identifier;
let extnamestr = ident_to_str(extname);
let fully_expanded: SmallVector<@Stmt> = match fld.extsbox.find(&extname.name) {
let marked_after = match fld.extsbox.find(&extname.name) {
None => {
fld.cx.span_fatal(pth.span, format!("macro undefined: '{}'", extnamestr))
}

Some(&NormalTT(expandfun, exp_span)) => {
Some(&NormalTT(ref expandfun, exp_span)) => {
fld.cx.bt_push(ExpnInfo {
call_site: s.span,
callee: NameAndSpan {
Expand Down Expand Up @@ -430,18 +430,8 @@ pub fn expand_stmt(s: &Stmt, fld: &mut MacroExpander) -> SmallVector<@Stmt> {
pth.span,
format!("non-stmt macro in stmt pos: {}", extnamestr))
};
let marked_after = mark_stmt(expanded,fm);

// Keep going, outside-in.
let fully_expanded = fld.fold_stmt(marked_after);
if fully_expanded.is_empty() {
fld.cx.span_fatal(pth.span,
"macro didn't expand to a statement");
}
fld.cx.bt_pop();
fully_expanded.move_iter()
.map(|s| @Spanned { span: s.span, node: s.node.clone() })
.collect()
mark_stmt(expanded,fm)
}

_ => {
Expand All @@ -451,6 +441,17 @@ pub fn expand_stmt(s: &Stmt, fld: &mut MacroExpander) -> SmallVector<@Stmt> {
}
};

// Keep going, outside-in.
let fully_expanded = fld.fold_stmt(marked_after);
if fully_expanded.is_empty() {
fld.cx.span_fatal(pth.span,
"macro didn't expand to a statement");
}
fld.cx.bt_pop();
let fully_expanded: SmallVector<@Stmt> = fully_expanded.move_iter()
.map(|s| @Spanned { span: s.span, node: s.node.clone() })
.collect();

fully_expanded.move_iter().map(|s| {
match s.node {
StmtExpr(e, stmt_id) if semi => {
Expand Down Expand Up @@ -1109,10 +1110,8 @@ mod test {
use codemap::Spanned;
use fold;
use parse;
use parse::token::{fresh_mark, gensym, intern, get_ident_interner, ident_to_str};
use parse::token::{fresh_mark, gensym, intern, ident_to_str};
use parse::token;
use print::pprust;
use std;
use util::parser_testing::{string_to_crate, string_to_crate_and_sess};
use util::parser_testing::{string_to_pat, string_to_tts, strs_to_idents};
use visit;
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/ext/tt/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,11 @@ pub fn add_new_extension(cx: &mut ExtCtxt,
_ => cx.span_bug(sp, "wrong-structured rhs")
};

let exp = @MacroRulesSyntaxExpanderTTFun {
let exp = ~MacroRulesSyntaxExpanderTTFun {
name: name,
lhses: lhses,
rhses: rhses,
} as @SyntaxExpanderTTTrait;
};

return MRDef(MacroDef {
name: ident_to_str(&name),
Expand Down