]> git.proxmox.com Git - rustc.git/blob - src/test/ui/consts/min_const_fn/min_const_fn.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / test / ui / consts / min_const_fn / min_const_fn.rs
1 // ok
2 const fn foo1() {}
3 const fn foo2(x: i32) -> i32 { x }
4 const fn foo3<T>(x: T) -> T { x }
5 const fn foo7() {
6 (
7 foo1(),
8 foo2(420),
9 foo3(69),
10 ).0
11 }
12 const fn foo12<T: Sized>(t: T) -> T { t }
13 const fn foo13<T: ?Sized>(t: &T) -> &T { t }
14 const fn foo14<'a, T: 'a>(t: &'a T) -> &'a T { t }
15 const fn foo15<T>(t: T) -> T where T: Sized { t }
16 const fn foo15_2<T>(t: &T) -> &T where T: ?Sized { t }
17 const fn foo16(f: f32) -> f32 { f }
18 const fn foo17(f: f32) -> u32 { f as u32 }
19 const fn foo18(i: i32) -> i32 { i * 3 }
20 const fn foo20(b: bool) -> bool { !b }
21 const fn foo21<T, U>(t: T, u: U) -> (T, U) { (t, u) }
22 const fn foo22(s: &[u8], i: usize) -> u8 { s[i] }
23 const FOO: u32 = 42;
24 const fn foo23() -> u32 { FOO }
25 const fn foo24() -> &'static u32 { &FOO }
26 const fn foo27(x: &u32) -> u32 { *x }
27 const fn foo28(x: u32) -> u32 { *&x }
28 const fn foo29(x: u32) -> i32 { x as i32 }
29 const fn foo31(a: bool, b: bool) -> bool { a & b }
30 const fn foo32(a: bool, b: bool) -> bool { a | b }
31 const fn foo33(a: bool, b: bool) -> bool { a & b }
32 const fn foo34(a: bool, b: bool) -> bool { a | b }
33 const fn foo35(a: bool, b: bool) -> bool { a ^ b }
34 struct Foo<T: ?Sized>(T);
35 impl<T> Foo<T> {
36 const fn new(t: T) -> Self { Foo(t) }
37 const fn into_inner(self) -> T { self.0 } //~ destructors cannot be evaluated
38 const fn get(&self) -> &T { &self.0 }
39 const fn get_mut(&mut self) -> &mut T { &mut self.0 }
40 //~^ mutable references in const fn are unstable
41 }
42 impl<'a, T> Foo<T> {
43 const fn new_lt(t: T) -> Self { Foo(t) }
44 const fn into_inner_lt(self) -> T { self.0 } //~ destructors cannot be evaluated
45 const fn get_lt(&'a self) -> &T { &self.0 }
46 const fn get_mut_lt(&'a mut self) -> &mut T { &mut self.0 }
47 //~^ mutable references in const fn are unstable
48 }
49 impl<T: Sized> Foo<T> {
50 const fn new_s(t: T) -> Self { Foo(t) }
51 const fn into_inner_s(self) -> T { self.0 } //~ ERROR destructors
52 const fn get_s(&self) -> &T { &self.0 }
53 const fn get_mut_s(&mut self) -> &mut T { &mut self.0 }
54 //~^ mutable references in const fn are unstable
55 }
56 impl<T: ?Sized> Foo<T> {
57 const fn get_sq(&self) -> &T { &self.0 }
58 const fn get_mut_sq(&mut self) -> &mut T { &mut self.0 }
59 //~^ mutable references in const fn are unstable
60 }
61
62
63 const fn char_ops(c: char, d: char) -> bool { c == d }
64 const fn char_ops2(c: char, d: char) -> bool { c < d }
65 const fn char_ops3(c: char, d: char) -> bool { c != d }
66 const fn i32_ops(c: i32, d: i32) -> bool { c == d }
67 const fn i32_ops2(c: i32, d: i32) -> bool { c < d }
68 const fn i32_ops3(c: i32, d: i32) -> bool { c != d }
69 const fn i32_ops4(c: i32, d: i32) -> i32 { c + d }
70 const fn char_cast(u: u8) -> char { u as char }
71 const unsafe fn ret_i32_no_unsafe() -> i32 { 42 }
72 const unsafe fn ret_null_ptr_no_unsafe<T>() -> *const T { core::ptr::null() }
73 const unsafe fn ret_null_mut_ptr_no_unsafe<T>() -> *mut T { core::ptr::null_mut() }
74
75 // not ok
76 const fn foo11<T: std::fmt::Display>(t: T) -> T { t }
77 //~^ ERROR trait bounds other than `Sized` on const fn parameters are unstable
78 const fn foo11_2<T: Send>(t: T) -> T { t }
79 //~^ ERROR trait bounds other than `Sized` on const fn parameters are unstable
80 const fn foo19(f: f32) -> f32 { f * 2.0 }
81 //~^ ERROR only int, `bool` and `char` operations are stable in const fn
82 const fn foo19_2(f: f32) -> f32 { 2.0 - f }
83 //~^ ERROR only int, `bool` and `char` operations are stable in const fn
84 const fn foo19_3(f: f32) -> f32 { -f }
85 //~^ ERROR only int and `bool` operations are stable in const fn
86 const fn foo19_4(f: f32, g: f32) -> f32 { f / g }
87 //~^ ERROR only int, `bool` and `char` operations are stable in const fn
88
89 static BAR: u32 = 42;
90 const fn foo25() -> u32 { BAR } //~ ERROR cannot access `static` items in const fn
91 const fn foo26() -> &'static u32 { &BAR } //~ ERROR cannot access `static` items
92 const fn foo30(x: *const u32) -> usize { x as usize }
93 //~^ ERROR casting pointers to ints is unstable
94 const fn foo30_with_unsafe(x: *const u32) -> usize { unsafe { x as usize } }
95 //~^ ERROR casting pointers to ints is unstable
96 const fn foo30_2(x: *mut u32) -> usize { x as usize }
97 //~^ ERROR casting pointers to ints is unstable
98 const fn foo30_2_with_unsafe(x: *mut u32) -> usize { unsafe { x as usize } }
99 //~^ ERROR casting pointers to ints is unstable
100 const fn foo30_6() -> bool { let x = true; x }
101 const fn foo36(a: bool, b: bool) -> bool { a && b }
102 //~^ ERROR loops and conditional expressions are not stable in const fn
103 const fn foo37(a: bool, b: bool) -> bool { a || b }
104 //~^ ERROR loops and conditional expressions are not stable in const fn
105 const fn inc(x: &mut i32) { *x += 1 }
106 //~^ ERROR mutable references in const fn are unstable
107
108 fn main() {}
109
110 impl<T: std::fmt::Debug> Foo<T> {
111 //~^ ERROR trait bounds other than `Sized` on const fn parameters are unstable
112 const fn foo(&self) {}
113 }
114
115 impl<T: std::fmt::Debug + Sized> Foo<T> {
116 //~^ ERROR trait bounds other than `Sized` on const fn parameters are unstable
117 const fn foo2(&self) {}
118 }
119
120 impl<T: Sync + Sized> Foo<T> {
121 //~^ ERROR trait bounds other than `Sized` on const fn parameters are unstable
122 const fn foo3(&self) {}
123 }
124
125 struct AlanTuring<T>(T);
126 const fn no_rpit2() -> AlanTuring<impl std::fmt::Debug> { AlanTuring(0) }
127 //~^ ERROR `impl Trait` in const fn is unstable
128 const fn no_apit2(_x: AlanTuring<impl std::fmt::Debug>) {}
129 //~^ ERROR trait bounds other than `Sized`
130 const fn no_apit(_x: impl std::fmt::Debug) {} //~ ERROR trait bounds other than `Sized`
131 const fn no_rpit() -> impl std::fmt::Debug {} //~ ERROR `impl Trait` in const fn is unstable
132 const fn no_dyn_trait(_x: &dyn std::fmt::Debug) {} //~ ERROR trait bounds other than `Sized`
133 const fn no_dyn_trait_ret() -> &'static dyn std::fmt::Debug { &() }
134 //~^ ERROR trait bounds other than `Sized`
135
136 const fn no_unsafe() { unsafe {} }
137
138 const fn really_no_traits_i_mean_it() { (&() as &dyn std::fmt::Debug, ()).1 }
139 //~^ ERROR trait bounds other than `Sized`
140
141 const fn no_fn_ptrs(_x: fn()) {}
142 //~^ ERROR function pointers in const fn are unstable
143 const fn no_fn_ptrs2() -> fn() { fn foo() {} foo }
144 //~^ ERROR function pointers in const fn are unstable