Skip to content

Instantly share code, notes, and snippets.

@tilpner
Last active August 29, 2015 14:11
Show Gist options
  • Save tilpner/2e1a49594befb1617012 to your computer and use it in GitHub Desktop.
Save tilpner/2e1a49594befb1617012 to your computer and use it in GitHub Desktop.
Icky Rust AST
[package]
name = "trace"
version = "0.0.1"
authors = ["Till Hoeppner <till@hoeppner.ws>"]
[lib]
name = "trace"
path = "src/lib.rs"
plugin = true
src/trace.rs:32:15: 32:44 error: the trait `core::ops::Fn<(syntax::ptr::P<syntax::ast::Item>,), ()>` is not implemented for the type `|syntax::ptr::P<syntax::ast::Item>|`
src/trace.rs:32 trait_def.expand(cx, mitem, item, push)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
Could not compile `trace`.
#![feature(macro_rules, plugin_registrar, quote, phase)]
#![deny(unused_imports)]
#![deny(unused_variables)]
#[phase(plugin,link)]
extern crate syntax;
#[phase(plugin, link)]
extern crate rustc;
#[cfg(test)]
extern crate sync;
use rustc::plugin::Registry;
use syntax::ext::base::Decorator;
use syntax::parse::token::intern;
pub mod trace;
#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
reg.register_syntax_extension(intern("trace"), Decorator(box trace::expand_trace));
}
use syntax::ext::base::ExtCtxt;
use syntax::codemap::Span;
use syntax::ptr::P;
use syntax::ast::{Item, MetaItem, Expr};
use syntax::attr;
use syntax::ast;
use syntax::ext::build::AstBuilder;
use syntax::ext::deriving::generic::{combine_substructure, EnumMatching, FieldInfo, MethodDef, Struct, Substructure, TraitDef, ty};
use syntax::parse::token::InternedString;
pub fn expand_trace(cx: &mut ExtCtxt, span: Span, mitem: &MetaItem, item: &Item, push: |P<Item>|) {
let trait_def = TraitDef {
span: span,
attributes: Vec::new(),
path: ty::Path::new(vec!("gc","Trace")),
additional_bounds: Vec::new(),
generics: ty::LifetimeBounds::empty(),
methods: vec!(
MethodDef {
name: "trace",
generics: ty::LifetimeBounds::empty(),
explicit_self: Some(Some(ty::PtrTy::Borrowed(None, ast::MutImmutable))),
args: vec!(ty::Ptr(box ty::Literal(ty::Path::new(vec!("gc", "Manager"))), ty::Raw(ast::MutMutable))),
ret_ty: ty::nil_ty(),
attributes: vec!(attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(InternedString::new("inline")))),
combine_substructure: combine_substructure(|a, b, c| {
traceable_substructure(a, b, c)
})
}
)
};
trait_def.expand(cx, mitem, item, push)
}
// Mostly copied from syntax::ext::deriving::hash
/// Defines how the implementation for `trace()` is to be generated
fn traceable_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P<Expr> {
let state_expr = match substr.nonself_args {
[ref state_expr] => state_expr,
_ => cx.span_bug(trait_span, "incorrect number of arguments in `trace`")
};
let trace_ident = substr.method_ident;
let call_trace = |span, thing_expr| {
let expr = cx.expr_method_call(span, thing_expr, trace_ident, vec!(state_expr.clone()));
cx.stmt_expr(expr)
};
let mut stmts = Vec::new();
let fields = match *substr.fields {
Struct(ref fs) | EnumMatching(_, _, ref fs) => fs,
_ => cx.span_bug(trait_span, "impossible substructure in `trace`")
};
for &FieldInfo { ref self_, span, .. } in fields.iter() {
stmts.push(call_trace(span, self_.clone()));
}
cx.expr_block(cx.block(trait_span, stmts, None))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment