From ac7dc03a52d94fe62c367e4783d0a8b915554b75 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Sun, 14 Dec 2014 00:35:35 -0500 Subject: [PATCH 1/2] libsyntax: Make deriving also respect where bounds. --- src/libsyntax/ext/deriving/generic/mod.rs | 30 +++++++++++++++++++---- src/test/run-pass/issue-19358.rs | 29 ++++++++++++++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 src/test/run-pass/issue-19358.rs diff --git a/src/libsyntax/ext/deriving/generic/mod.rs b/src/libsyntax/ext/deriving/generic/mod.rs index a75be40604ea6..fd0fa9a70d422 100644 --- a/src/libsyntax/ext/deriving/generic/mod.rs +++ b/src/libsyntax/ext/deriving/generic/mod.rs @@ -388,7 +388,7 @@ impl<'a> TraitDef<'a> { methods: Vec>) -> P { 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(); @@ -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. diff --git a/src/test/run-pass/issue-19358.rs b/src/test/run-pass/issue-19358.rs new file mode 100644 index 0000000000000..e4c190f41169e --- /dev/null +++ b/src/test/run-pass/issue-19358.rs @@ -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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +trait Trait {} + +#[deriving(Show)] +struct Foo { + foo: T, +} + +#[deriving(Show)] +struct Bar where T: Trait { + bar: T, +} + +impl Trait for int {} + +fn main() { + let a = Foo { foo: 12i }; + let b = Bar { bar: 12i }; + println!("{} {}", a, b); +} From ab1bdde536668e752aaf4e9dbfa0acf058d0240b Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Sun, 14 Dec 2014 00:51:42 -0500 Subject: [PATCH 2/2] libsyntax: Output where clauses in pretty printer for structs. --- src/libsyntax/print/pprust.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 6d8b8dcb8ba5d..d01b5a0fee2cf 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1070,6 +1070,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());