]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-block-item-macro-codegen.rs
Rollup merge of #98609 - TaKO8Ki:fix-ice-for-associated-constant-generics, r=lcnr
[rust.git] / src / test / ui / consts / const-block-item-macro-codegen.rs
1 // run-pass
2 #![allow(dead_code)]
3 // General test that function items in static blocks
4 // can be generated with a macro.
5
6
7 struct MyType {
8     desc: &'static str,
9     data: usize,
10     code: fn(usize, usize) -> usize
11 }
12
13 impl MyType {
14     fn eval(&self, a: usize) -> usize {
15         (self.code)(self.data, a)
16     }
17 }
18
19 macro_rules! codegen {
20     ($e:expr, $v:expr) => {
21         {
22             fn generated(a: usize, b: usize) -> usize {
23                 a - ($e * b)
24             }
25             MyType {
26                 desc: "test",
27                 data: $v,
28                 code: generated
29             }
30         }
31     }
32 }
33
34 static GENERATED_CODE_1: MyType = codegen!(2, 100);
35 static GENERATED_CODE_2: MyType = codegen!(5, 1000);
36
37 pub fn main() {
38     assert_eq!(GENERATED_CODE_1.eval(10), 80);
39     assert_eq!(GENERATED_CODE_2.eval(100), 500);
40 }