Skip to content

Associated types support for deriving::TraitDef #21614

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 26, 2015
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
3 changes: 2 additions & 1 deletion src/libsyntax/ext/deriving/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ pub fn expand_deriving_bound<F>(cx: &mut ExtCtxt,
path: Path::new(vec!("std", "marker", name)),
additional_bounds: Vec::new(),
generics: LifetimeBounds::empty(),
methods: vec!()
methods: Vec::new(),
associated_types: Vec::new(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer vec![] personally.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

either way it is now more consistent (used to be both Vec::new() and vec!() here)

};

trait_def.expand(cx, mitem, item, push)
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/ext/deriving/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ pub fn expand_deriving_clone<F>(cx: &mut ExtCtxt,
cs_clone("Clone", c, s, sub)
}),
}
)
),
associated_types: Vec::new(),
};

trait_def.expand(cx, mitem, item, push)
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/ext/deriving/cmp/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ pub fn expand_deriving_eq<F>(cx: &mut ExtCtxt,
methods: vec!(
md!("eq", cs_eq),
md!("ne", cs_ne)
)
),
associated_types: Vec::new(),
};
trait_def.expand(cx, mitem, item, push)
}
3 changes: 2 additions & 1 deletion src/libsyntax/ext/deriving/cmp/ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ pub fn expand_deriving_ord<F>(cx: &mut ExtCtxt,
md!("le", true, true),
md!("gt", false, false),
md!("ge", false, true)
]
],
associated_types: Vec::new(),
};
trait_def.expand(cx, mitem, item, push)
}
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/ext/deriving/cmp/totaleq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ pub fn expand_deriving_totaleq<F>(cx: &mut ExtCtxt,
cs_total_eq_assert(a, b, c)
})
}
)
),
associated_types: Vec::new(),
};
trait_def.expand(cx, mitem, item, push)
}
3 changes: 2 additions & 1 deletion src/libsyntax/ext/deriving/cmp/totalord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ pub fn expand_deriving_totalord<F>(cx: &mut ExtCtxt,
cs_cmp(a, b, c)
}),
}
)
),
associated_types: Vec::new(),
};

trait_def.expand(cx, mitem, item, push)
Expand Down
4 changes: 3 additions & 1 deletion src/libsyntax/ext/deriving/decodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ fn expand_deriving_decodable_imp<F>(cx: &mut ExtCtxt,
combine_substructure: combine_substructure(box |a, b, c| {
decodable_substructure(a, b, c, krate)
}),
})
}
),
associated_types: Vec::new(),
};

trait_def.expand(cx, mitem, item, push)
Expand Down
4 changes: 3 additions & 1 deletion src/libsyntax/ext/deriving/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ pub fn expand_deriving_default<F>(cx: &mut ExtCtxt,
combine_substructure: combine_substructure(box |a, b, c| {
default_substructure(a, b, c)
})
})
}
),
associated_types: Vec::new(),
};
trait_def.expand(cx, mitem, item, push)
}
Expand Down
4 changes: 3 additions & 1 deletion src/libsyntax/ext/deriving/encodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ fn expand_deriving_encodable_imp<F>(cx: &mut ExtCtxt,
combine_substructure: combine_substructure(box |a, b, c| {
encodable_substructure(a, b, c)
}),
})
}
),
associated_types: Vec::new(),
};

trait_def.expand(cx, mitem, item, push)
Expand Down
24 changes: 23 additions & 1 deletion src/libsyntax/ext/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ pub struct TraitDef<'a> {
pub generics: LifetimeBounds<'a>,

pub methods: Vec<MethodDef<'a>>,

pub associated_types: Vec<(ast::Ident, Ty<'a>)>,
}


Expand Down Expand Up @@ -387,6 +389,22 @@ impl<'a> TraitDef<'a> {
methods: Vec<P<ast::Method>>) -> P<ast::Item> {
let trait_path = self.path.to_path(cx, self.span, type_ident, generics);

// Transform associated types from `deriving::ty::Ty` into `ast::Typedef`
let associated_types = self.associated_types.iter().map(|&(ident, ref type_def)| {
P(ast::Typedef {
id: ast::DUMMY_NODE_ID,
span: self.span,
ident: ident,
vis: ast::Inherited,
attrs: Vec::new(),
typ: type_def.to_ty(cx,
self.span,
type_ident,
generics
),
})
});

let Generics { mut lifetimes, ty_params, mut where_clause } =
self.generics.to_generics(cx, self.span, type_ident, generics);
let mut ty_params = ty_params.into_vec();
Expand Down Expand Up @@ -494,7 +512,11 @@ impl<'a> TraitDef<'a> {
methods.into_iter()
.map(|method| {
ast::MethodImplItem(method)
}).collect()))
}).chain(
associated_types.map(|type_| {
ast::TypeImplItem(type_)
})
).collect()))
}

fn expand_struct_def(&self,
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/ext/deriving/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ pub fn expand_deriving_hash<F>(cx: &mut ExtCtxt,
hash_substructure(a, b, c)
})
}
)
),
associated_types: Vec::new(),
};

hash_trait_def.expand(cx, mitem, item, push);
Expand Down
4 changes: 3 additions & 1 deletion src/libsyntax/ext/deriving/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ pub fn expand_deriving_from_primitive<F>(cx: &mut ExtCtxt,
combine_substructure: combine_substructure(box |c, s, sub| {
cs_from("u64", c, s, sub)
}),
})
}
),
associated_types: Vec::new(),
};

trait_def.expand(cx, mitem, item, push)
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/ext/deriving/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ pub fn expand_deriving_rand<F>(cx: &mut ExtCtxt,
rand_substructure(a, b, c)
})
}
)
),
associated_types: Vec::new(),
};
trait_def.expand(cx, mitem, item, push)
}
Expand Down
7 changes: 4 additions & 3 deletions src/libsyntax/ext/deriving/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ pub fn expand_deriving_show<F>(cx: &mut ExtCtxt,
let trait_def = TraitDef {
span: span,
attributes: Vec::new(),
path: Path::new(vec!("std", "fmt", "Debug")),
path: Path::new(vec!["std", "fmt", "Debug"]),
additional_bounds: Vec::new(),
generics: LifetimeBounds::empty(),
methods: vec!(
methods: vec![
MethodDef {
name: "fmt",
generics: LifetimeBounds::empty(),
Expand All @@ -50,7 +50,8 @@ pub fn expand_deriving_show<F>(cx: &mut ExtCtxt,
show_substructure(a, b, c)
})
}
)
],
associated_types: Vec::new(),
};
trait_def.expand(cx, mitem, item, push)
}
Expand Down