Skip to content

Commit f756c97

Browse files
committed
rollup merge of #19831: luqmana/deriving-where
Fixes #19358.
2 parents 2ad1e0f + ab1bdde commit f756c97

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

src/libsyntax/ext/deriving/generic/mod.rs

+25-5
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ impl<'a> TraitDef<'a> {
388388
methods: Vec<P<ast::Method>>) -> P<ast::Item> {
389389
let trait_path = self.path.to_path(cx, self.span, type_ident, generics);
390390

391-
let Generics { mut lifetimes, ty_params, where_clause: _ } =
391+
let Generics { mut lifetimes, ty_params, mut where_clause } =
392392
self.generics.to_generics(cx, self.span, type_ident, generics);
393393
let mut ty_params = ty_params.into_vec();
394394

@@ -420,13 +420,33 @@ impl<'a> TraitDef<'a> {
420420
ty_param.unbound.clone(),
421421
None)
422422
}));
423+
424+
// and similarly for where clauses
425+
where_clause.predicates.extend(generics.where_clause.predicates.iter().map(|clause| {
426+
match *clause {
427+
ast::WherePredicate::BoundPredicate(ref wb) => {
428+
ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
429+
id: ast::DUMMY_NODE_ID,
430+
span: self.span,
431+
ident: wb.ident,
432+
bounds: OwnedSlice::from_vec(wb.bounds.iter().map(|b| b.clone()).collect())
433+
})
434+
}
435+
ast::WherePredicate::EqPredicate(ref we) => {
436+
ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
437+
id: ast::DUMMY_NODE_ID,
438+
span: self.span,
439+
path: we.path.clone(),
440+
ty: we.ty.clone()
441+
})
442+
}
443+
}
444+
}));
445+
423446
let trait_generics = Generics {
424447
lifetimes: lifetimes,
425448
ty_params: OwnedSlice::from_vec(ty_params),
426-
where_clause: ast::WhereClause {
427-
id: ast::DUMMY_NODE_ID,
428-
predicates: Vec::new(),
429-
},
449+
where_clause: where_clause
430450
};
431451

432452
// Create the reference to the trait.

src/libsyntax/print/pprust.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,7 @@ impl<'a> State<'a> {
10621062
span: codemap::Span) -> IoResult<()> {
10631063
try!(self.print_ident(ident));
10641064
try!(self.print_generics(generics));
1065+
try!(self.print_where_clause(generics));
10651066
if ast_util::struct_def_is_tuple_like(struct_def) {
10661067
if !struct_def.fields.is_empty() {
10671068
try!(self.popen());

src/test/run-pass/issue-19358.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
trait Trait {}
12+
13+
#[deriving(Show)]
14+
struct Foo<T: Trait> {
15+
foo: T,
16+
}
17+
18+
#[deriving(Show)]
19+
struct Bar<T> where T: Trait {
20+
bar: T,
21+
}
22+
23+
impl Trait for int {}
24+
25+
fn main() {
26+
let a = Foo { foo: 12i };
27+
let b = Bar { bar: 12i };
28+
println!("{} {}", a, b);
29+
}

0 commit comments

Comments
 (0)