Skip to content

Commit f50a8e2

Browse files
committed
syntax: Simplify deriving to handle classes that take generics, like Encodable
1 parent 419f6ac commit f50a8e2

File tree

4 files changed

+28
-34
lines changed

4 files changed

+28
-34
lines changed

src/libsyntax/ext/deriving/clone.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use ext::build;
1717
use ext::deriving::*;
1818
use codemap::{span, spanned};
1919
use ast_util;
20+
use opt_vec;
2021

2122
use core::uint;
2223

@@ -48,12 +49,13 @@ fn create_derived_clone_impl(cx: @ext_ctxt,
4849
method: @method)
4950
-> @item {
5051
let methods = [ method ];
51-
let trait_path = [
52+
let trait_path = ~[
5253
cx.ident_of(~"core"),
5354
cx.ident_of(~"clone"),
5455
cx.ident_of(~"Clone"),
5556
];
56-
create_derived_impl(cx, span, type_ident, generics, methods, trait_path)
57+
let trait_path = build::mk_raw_path_global(span, trait_path);
58+
create_derived_impl(cx, span, type_ident, generics, methods, trait_path, opt_vec::Empty)
5759
}
5860
// Creates a method from the given expression conforming to the signature of
5961
// the `clone` method.

src/libsyntax/ext/deriving/eq.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use ext::build;
1717
use ext::deriving::*;
1818
use codemap::{span, spanned};
1919
use ast_util;
20+
use opt_vec;
2021

2122
use core::uint;
2223

@@ -124,12 +125,13 @@ fn create_derived_eq_impl(cx: @ext_ctxt,
124125
ne_method: @method)
125126
-> @item {
126127
let methods = [ eq_method, ne_method ];
127-
let trait_path = [
128+
let trait_path = ~[
128129
cx.ident_of(~"core"),
129130
cx.ident_of(~"cmp"),
130131
cx.ident_of(~"Eq")
131132
];
132-
create_derived_impl(cx, span, type_ident, generics, methods, trait_path)
133+
let trait_path = build::mk_raw_path_global(span, trait_path);
134+
create_derived_impl(cx, span, type_ident, generics, methods, trait_path, opt_vec::Empty)
133135
}
134136

135137
fn call_substructure_eq_method(cx: @ext_ctxt,

src/libsyntax/ext/deriving/iter_bytes.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use ext::build;
1717
use ext::deriving::*;
1818
use codemap::{span, spanned};
1919
use ast_util;
20+
use opt_vec;
2021

2122
use core::uint;
2223

@@ -49,12 +50,13 @@ fn create_derived_iter_bytes_impl(cx: @ext_ctxt,
4950
method: @method)
5051
-> @item {
5152
let methods = [ method ];
52-
let trait_path = [
53+
let trait_path = ~[
5354
cx.ident_of(~"core"),
5455
cx.ident_of(~"to_bytes"),
5556
cx.ident_of(~"IterBytes")
5657
];
57-
create_derived_impl(cx, span, type_ident, generics, methods, trait_path)
58+
let trait_path = build::mk_raw_path_global(span, trait_path);
59+
create_derived_impl(cx, span, type_ident, generics, methods, trait_path, opt_vec::Empty)
5860
}
5961

6062
// Creates a method from the given set of statements conforming to the

src/libsyntax/ext/deriving/mod.rs

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use core::prelude::*;
1515

1616
use ast;
17-
use ast::{TraitTyParamBound, Ty, bind_by_ref, deref, enum_def};
17+
use ast::{Ty, bind_by_ref, deref, enum_def};
1818
use ast::{expr, expr_match, ident, item, item_};
1919
use ast::{item_enum, item_impl, item_struct, Generics};
2020
use ast::{m_imm, meta_item, method};
@@ -153,12 +153,13 @@ pub fn create_self_type_with_params(cx: @ext_ctxt,
153153
}
154154

155155
pub fn create_derived_impl(cx: @ext_ctxt,
156-
span: span,
157-
type_ident: ident,
158-
generics: &Generics,
159-
methods: &[@method],
160-
trait_path: &[ident])
161-
-> @item {
156+
span: span,
157+
type_ident: ident,
158+
generics: &Generics,
159+
methods: &[@method],
160+
trait_path: @ast::Path,
161+
mut impl_ty_params: opt_vec::OptVec<ast::TyParam>)
162+
-> @item {
162163
/*!
163164
*
164165
* Given that we are deriving a trait `Tr` for a type `T<'a, ...,
@@ -175,29 +176,16 @@ pub fn create_derived_impl(cx: @ext_ctxt,
175176
build::mk_lifetime(cx, l.span, l.ident)
176177
});
177178

178-
// Create the type parameters.
179-
let impl_ty_params = generics.ty_params.map(|ty_param| {
180-
let bound = build::mk_trait_ref_global(cx,
181-
span,
182-
trait_path.map(|x| *x));
183-
let bounds = @opt_vec::with(TraitTyParamBound(bound));
184-
build::mk_ty_param(cx, ty_param.ident, bounds)
185-
});
186-
187179
// Create the reference to the trait.
188-
let trait_path = ast::Path {
189-
span: span,
190-
global: true,
191-
idents: trait_path.map(|x| *x),
192-
rp: None,
193-
types: ~[]
194-
};
195-
let trait_path = @trait_path;
196-
let trait_ref = ast::trait_ref {
197-
path: trait_path,
198-
ref_id: cx.next_id()
180+
let trait_ref = build::mk_trait_ref_(cx, trait_path);
181+
182+
// Create the type parameters.
183+
for generics.ty_params.each |ty_param| {
184+
let bounds = @opt_vec::with(
185+
build::mk_trait_ty_param_bound_(cx, trait_path)
186+
);
187+
impl_ty_params.push(build::mk_ty_param(cx, ty_param.ident, bounds));
199188
};
200-
let trait_ref = @trait_ref;
201189

202190
// Create the type of `self`.
203191
let self_type = create_self_type_with_params(cx,

0 commit comments

Comments
 (0)