Skip to content

Commit

Permalink
rollup merge of rust-lang#19831: luqmana/deriving-where
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Dec 17, 2014
2 parents 823cd7a + ab1bdde commit bdb1146
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/libsyntax/ext/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ 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);

let Generics { mut lifetimes, ty_params, where_clause: _ } =
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 @@ -420,13 +420,33 @@ impl<'a> TraitDef<'a> {
ty_param.unbound.clone(),
None)
}));

// and similarly for where clauses
where_clause.predicates.extend(generics.where_clause.predicates.iter().map(|clause| {
match *clause {
ast::WherePredicate::BoundPredicate(ref wb) => {
ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
id: ast::DUMMY_NODE_ID,
span: self.span,
ident: wb.ident,
bounds: OwnedSlice::from_vec(wb.bounds.iter().map(|b| b.clone()).collect())
})
}
ast::WherePredicate::EqPredicate(ref we) => {
ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
id: ast::DUMMY_NODE_ID,
span: self.span,
path: we.path.clone(),
ty: we.ty.clone()
})
}
}
}));

let trait_generics = Generics {
lifetimes: lifetimes,
ty_params: OwnedSlice::from_vec(ty_params),
where_clause: ast::WhereClause {
id: ast::DUMMY_NODE_ID,
predicates: Vec::new(),
},
where_clause: where_clause
};

// Create the reference to the trait.
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,7 @@ impl<'a> State<'a> {
span: codemap::Span) -> IoResult<()> {
try!(self.print_ident(ident));
try!(self.print_generics(generics));
try!(self.print_where_clause(generics));
if ast_util::struct_def_is_tuple_like(struct_def) {
if !struct_def.fields.is_empty() {
try!(self.popen());
Expand Down
29 changes: 29 additions & 0 deletions src/test/run-pass/issue-19358.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

trait Trait {}

#[deriving(Show)]
struct Foo<T: Trait> {
foo: T,
}

#[deriving(Show)]
struct Bar<T> where T: Trait {
bar: T,
}

impl Trait for int {}

fn main() {
let a = Foo { foo: 12i };
let b = Bar { bar: 12i };
println!("{} {}", a, b);
}

0 comments on commit bdb1146

Please sign in to comment.