]> git.lizzy.rs Git - rust.git/blob - tests/ui/consts/const-fn.rs
Rollup merge of #107004 - compiler-errors:new-solver-new-candidates-2, r=lcnr
[rust.git] / tests / ui / consts / const-fn.rs
1 // run-pass
2 #![allow(stable_features)]
3
4 // A very basic test of const fn functionality.
5
6 #![feature(const_indexing)]
7
8 const fn add(x: u32, y: u32) -> u32 {
9     x + y
10 }
11
12 const fn sub(x: u32, y: u32) -> u32 {
13     x - y
14 }
15
16 const unsafe fn div(x: u32, y: u32) -> u32 {
17     x / y
18 }
19
20 const fn generic<T>(t: T) -> T {
21     t
22 }
23
24 const fn generic_arr<T: Copy>(t: [T; 1]) -> T {
25     t[0]
26 }
27
28 const SUM: u32 = add(44, 22);
29 const DIFF: u32 = sub(44, 22);
30 const DIV: u32 = unsafe{div(44, 22)};
31
32 fn main() {
33     assert_eq!(SUM, 66);
34     assert!(SUM != 88);
35
36     assert_eq!(DIFF, 22);
37     assert_eq!(DIV, 2);
38
39     let _: [&'static str; sub(100, 99) as usize] = ["hi"];
40     let _: [&'static str; generic(1)] = ["hi"];
41     let _: [&'static str; generic_arr([1])] = ["hi"];
42 }