rust/src/test/run-pass-fulldeps/derive-totalsum.rs
Keegan McAllister 491054f08e Make #[derive(Anything)] into sugar for #[derive_Anything]
This is a hack, but I don't think we can do much better as long as `derive` is
running at the syntax expansion phase.

If the custom_derive feature gate is enabled, this works with user-defined
traits and syntax extensions. Without the gate, you can't use e.g. #[derive_Clone]
directly, so this does not change the stable language.

This commit also cleans up the deriving code somewhat, and forbids some
previously-meaningless attribute syntax. For this reason it's technically a

    [breaking-change]
2015-03-06 18:20:16 -08:00

60 lines
1.1 KiB
Rust

// Copyright 2015 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.
// aux-build:custom_derive_plugin.rs
// ignore-stage1
#![feature(plugin, custom_derive)]
#![plugin(custom_derive_plugin)]
trait TotalSum {
fn total_sum(&self) -> isize;
}
impl TotalSum for isize {
fn total_sum(&self) -> isize {
*self
}
}
struct Seven;
impl TotalSum for Seven {
fn total_sum(&self) -> isize {
7
}
}
#[derive(TotalSum)]
struct Foo {
seven: Seven,
bar: Bar,
baz: isize,
}
#[derive(TotalSum)]
struct Bar {
quux: isize,
bleh: isize,
}
pub fn main() {
let v = Foo {
seven: Seven,
bar: Bar {
quux: 9,
bleh: 3,
},
baz: 80,
};
assert_eq!(v.total_sum(), 99);
}