]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/min_const_fn/min_const_fn.rs
Rollup merge of #60897 - seanmonstar:patch-4, r=sfackler
[rust.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 { 0 as *const T }
73 const unsafe fn ret_null_mut_ptr_no_unsafe<T>() -> *mut T { 0 as *mut T }
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_4(b: bool) -> usize { if b { 1 } else { 42 } }
101 //~^ ERROR `if`, `match`, `&&` and `||` are not stable in const fn
102 const fn foo30_5(b: bool) { while b { } } //~ ERROR not stable in const fn
103 const fn foo30_6() -> bool { let x = true; x }
104 const fn foo36(a: bool, b: bool) -> bool { a && b }
105 //~^ ERROR `if`, `match`, `&&` and `||` are not stable in const fn
106 const fn foo37(a: bool, b: bool) -> bool { a || b }
107 //~^ ERROR `if`, `match`, `&&` and `||` are not stable in const fn
108 const fn inc(x: &mut i32) { *x += 1 }
109 //~^ ERROR mutable references in const fn are unstable
110
111 fn main() {}
112
113 impl<T: std::fmt::Debug> Foo<T> {
114 //~^ ERROR trait bounds other than `Sized` on const fn parameters are unstable
115     const fn foo(&self) {}
116 }
117
118 impl<T: std::fmt::Debug + Sized> Foo<T> {
119 //~^ ERROR trait bounds other than `Sized` on const fn parameters are unstable
120     const fn foo2(&self) {}
121 }
122
123 impl<T: Sync + Sized> Foo<T> {
124 //~^ ERROR trait bounds other than `Sized` on const fn parameters are unstable
125     const fn foo3(&self) {}
126 }
127
128 struct AlanTuring<T>(T);
129 const fn no_rpit2() -> AlanTuring<impl std::fmt::Debug> { AlanTuring(0) }
130 //~^ ERROR `impl Trait` in const fn is unstable
131 const fn no_apit2(_x: AlanTuring<impl std::fmt::Debug>) {}
132 //~^ ERROR trait bounds other than `Sized`
133 const fn no_apit(_x: impl std::fmt::Debug) {} //~ ERROR trait bounds other than `Sized`
134 const fn no_rpit() -> impl std::fmt::Debug {} //~ ERROR `impl Trait` in const fn is unstable
135 const fn no_dyn_trait(_x: &dyn std::fmt::Debug) {} //~ ERROR trait bounds other than `Sized`
136 const fn no_dyn_trait_ret() -> &'static dyn std::fmt::Debug { &() }
137 //~^ ERROR trait bounds other than `Sized`
138 //~| WARNING cannot return reference to temporary value
139 //~| WARNING this error has been downgraded to a warning
140 //~| WARNING this warning will become a hard error in the future
141
142 const fn no_unsafe() { unsafe {} }
143
144 const fn really_no_traits_i_mean_it() { (&() as &dyn std::fmt::Debug, ()).1 }
145 //~^ ERROR trait bounds other than `Sized`
146
147 const fn no_fn_ptrs(_x: fn()) {}
148 //~^ ERROR function pointers in const fn are unstable
149 const fn no_fn_ptrs2() -> fn() { fn foo() {} foo }
150 //~^ ERROR function pointers in const fn are unstable