]> git.proxmox.com Git - rustc.git/blame - src/test/ui/consts/min_const_fn/min_const_fn.rs
New upstream version 1.43.0+dfsg1
[rustc.git] / src / test / ui / consts / min_const_fn / min_const_fn.rs
CommitLineData
b7449926
XL
1// ok
2const fn foo1() {}
3const fn foo2(x: i32) -> i32 { x }
4const fn foo3<T>(x: T) -> T { x }
5const fn foo7() {
6 (
7 foo1(),
8 foo2(420),
9 foo3(69),
10 ).0
11}
12const fn foo12<T: Sized>(t: T) -> T { t }
13const fn foo13<T: ?Sized>(t: &T) -> &T { t }
14const fn foo14<'a, T: 'a>(t: &'a T) -> &'a T { t }
15const fn foo15<T>(t: T) -> T where T: Sized { t }
16const fn foo15_2<T>(t: &T) -> &T where T: ?Sized { t }
17const fn foo16(f: f32) -> f32 { f }
18const fn foo17(f: f32) -> u32 { f as u32 }
19const fn foo18(i: i32) -> i32 { i * 3 }
20const fn foo20(b: bool) -> bool { !b }
21const fn foo21<T, U>(t: T, u: U) -> (T, U) { (t, u) }
22const fn foo22(s: &[u8], i: usize) -> u8 { s[i] }
23const FOO: u32 = 42;
24const fn foo23() -> u32 { FOO }
25const fn foo24() -> &'static u32 { &FOO }
26const fn foo27(x: &u32) -> u32 { *x }
27const fn foo28(x: u32) -> u32 { *&x }
28const fn foo29(x: u32) -> i32 { x as i32 }
29const fn foo31(a: bool, b: bool) -> bool { a & b }
30const fn foo32(a: bool, b: bool) -> bool { a | b }
31const fn foo33(a: bool, b: bool) -> bool { a & b }
32const fn foo34(a: bool, b: bool) -> bool { a | b }
33const fn foo35(a: bool, b: bool) -> bool { a ^ b }
34struct Foo<T: ?Sized>(T);
35impl<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}
42impl<'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}
49impl<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}
56impl<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
63const fn char_ops(c: char, d: char) -> bool { c == d }
64const fn char_ops2(c: char, d: char) -> bool { c < d }
65const fn char_ops3(c: char, d: char) -> bool { c != d }
66const fn i32_ops(c: i32, d: i32) -> bool { c == d }
67const fn i32_ops2(c: i32, d: i32) -> bool { c < d }
68const fn i32_ops3(c: i32, d: i32) -> bool { c != d }
69const fn i32_ops4(c: i32, d: i32) -> i32 { c + d }
70const fn char_cast(u: u8) -> char { u as char }
0731742a 71const unsafe fn ret_i32_no_unsafe() -> i32 { 42 }
dc9dc135
XL
72const unsafe fn ret_null_ptr_no_unsafe<T>() -> *const T { core::ptr::null() }
73const unsafe fn ret_null_mut_ptr_no_unsafe<T>() -> *mut T { core::ptr::null_mut() }
b7449926
XL
74
75// not ok
76const fn foo11<T: std::fmt::Display>(t: T) -> T { t }
77//~^ ERROR trait bounds other than `Sized` on const fn parameters are unstable
78const fn foo11_2<T: Send>(t: T) -> T { t }
79//~^ ERROR trait bounds other than `Sized` on const fn parameters are unstable
80const fn foo19(f: f32) -> f32 { f * 2.0 }
81//~^ ERROR only int, `bool` and `char` operations are stable in const fn
82const fn foo19_2(f: f32) -> f32 { 2.0 - f }
83//~^ ERROR only int, `bool` and `char` operations are stable in const fn
84const fn foo19_3(f: f32) -> f32 { -f }
85//~^ ERROR only int and `bool` operations are stable in const fn
86const fn foo19_4(f: f32, g: f32) -> f32 { f / g }
87//~^ ERROR only int, `bool` and `char` operations are stable in const fn
88
89static BAR: u32 = 42;
90const fn foo25() -> u32 { BAR } //~ ERROR cannot access `static` items in const fn
91const fn foo26() -> &'static u32 { &BAR } //~ ERROR cannot access `static` items
92const fn foo30(x: *const u32) -> usize { x as usize }
9fa01778
XL
93//~^ ERROR casting pointers to ints is unstable
94const fn foo30_with_unsafe(x: *const u32) -> usize { unsafe { x as usize } }
95//~^ ERROR casting pointers to ints is unstable
b7449926 96const fn foo30_2(x: *mut u32) -> usize { x as usize }
9fa01778
XL
97//~^ ERROR casting pointers to ints is unstable
98const fn foo30_2_with_unsafe(x: *mut u32) -> usize { unsafe { x as usize } }
99//~^ ERROR casting pointers to ints is unstable
0731742a 100const fn foo30_6() -> bool { let x = true; x }
b7449926 101const fn foo36(a: bool, b: bool) -> bool { a && b }
dc9dc135 102//~^ ERROR loops and conditional expressions are not stable in const fn
b7449926 103const fn foo37(a: bool, b: bool) -> bool { a || b }
dc9dc135 104//~^ ERROR loops and conditional expressions are not stable in const fn
b7449926
XL
105const fn inc(x: &mut i32) { *x += 1 }
106//~^ ERROR mutable references in const fn are unstable
107
108fn main() {}
109
110impl<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
115impl<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
120impl<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
125struct AlanTuring<T>(T);
b7449926
XL
126const fn no_apit2(_x: AlanTuring<impl std::fmt::Debug>) {}
127//~^ ERROR trait bounds other than `Sized`
128const fn no_apit(_x: impl std::fmt::Debug) {} //~ ERROR trait bounds other than `Sized`
b7449926
XL
129const fn no_dyn_trait(_x: &dyn std::fmt::Debug) {} //~ ERROR trait bounds other than `Sized`
130const fn no_dyn_trait_ret() -> &'static dyn std::fmt::Debug { &() }
131//~^ ERROR trait bounds other than `Sized`
132
133const fn no_unsafe() { unsafe {} }
134
dc9dc135 135const fn really_no_traits_i_mean_it() { (&() as &dyn std::fmt::Debug, ()).1 }
b7449926
XL
136//~^ ERROR trait bounds other than `Sized`
137
138const fn no_fn_ptrs(_x: fn()) {}
139//~^ ERROR function pointers in const fn are unstable
140const fn no_fn_ptrs2() -> fn() { fn foo() {} foo }
141//~^ ERROR function pointers in const fn are unstable