]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/consts/const-block-item-macro-codegen.rs
Add `#![allow(..)]` as necessary to get re-migrated run-pass tests compiling with...
[rust.git] / src / test / run-pass / consts / const-block-item-macro-codegen.rs
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 // run-pass
12 #![allow(dead_code)]
13 // General test that function items in static blocks
14 // can be generated with a macro.
15
16
17 struct MyType {
18     desc: &'static str,
19     data: usize,
20     code: fn(usize, usize) -> usize
21 }
22
23 impl MyType {
24     fn eval(&self, a: usize) -> usize {
25         (self.code)(self.data, a)
26     }
27 }
28
29 macro_rules! codegen {
30     ($e:expr, $v:expr) => {
31         {
32             fn generated(a: usize, b: usize) -> usize {
33                 a - ($e * b)
34             }
35             MyType {
36                 desc: "test",
37                 data: $v,
38                 code: generated
39             }
40         }
41     }
42 }
43
44 static GENERATED_CODE_1: MyType = codegen!(2, 100);
45 static GENERATED_CODE_2: MyType = codegen!(5, 1000);
46
47 pub fn main() {
48     assert_eq!(GENERATED_CODE_1.eval(10), 80);
49     assert_eq!(GENERATED_CODE_2.eval(100), 500);
50 }