]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/min_const_fn/min_const_fn.rs
Auto merge of #53830 - davidtwco:issue-53228, r=nikomatsakis
[rust.git] / src / test / ui / consts / min_const_fn / min_const_fn.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(min_const_fn)]
12
13 // ok
14 const fn foo1() {}
15 const fn foo2(x: i32) -> i32 { x }
16 const fn foo3<T>(x: T) -> T { x }
17 const fn foo7() {
18     (
19         foo1(),
20         foo2(420),
21         foo3(69),
22     ).0
23 }
24 const fn foo12<T: Sized>(t: T) -> T { t }
25 const fn foo13<T: ?Sized>(t: &T) -> &T { t }
26 const fn foo14<'a, T: 'a>(t: &'a T) -> &'a T { t }
27 const fn foo15<T>(t: T) -> T where T: Sized { t }
28 const fn foo15_2<T>(t: &T) -> &T where T: ?Sized { t }
29 const fn foo16(f: f32) -> f32 { f }
30 const fn foo17(f: f32) -> u32 { f as u32 }
31 const fn foo18(i: i32) -> i32 { i * 3 }
32 const fn foo20(b: bool) -> bool { !b }
33 const fn foo21<T, U>(t: T, u: U) -> (T, U) { (t, u) }
34 const fn foo22(s: &[u8], i: usize) -> u8 { s[i] }
35 const FOO: u32 = 42;
36 const fn foo23() -> u32 { FOO }
37 const fn foo24() -> &'static u32 { &FOO }
38 const fn foo27(x: &u32) -> u32 { *x }
39 const fn foo28(x: u32) -> u32 { *&x }
40 const fn foo29(x: u32) -> i32 { x as i32 }
41 const fn foo31(a: bool, b: bool) -> bool { a & b }
42 const fn foo32(a: bool, b: bool) -> bool { a | b }
43 const fn foo33(a: bool, b: bool) -> bool { a & b }
44 const fn foo34(a: bool, b: bool) -> bool { a | b }
45 const fn foo35(a: bool, b: bool) -> bool { a ^ b }
46 struct Foo<T: ?Sized>(T);
47 impl<T> Foo<T> {
48     const fn new(t: T) -> Self { Foo(t) }
49     const fn into_inner(self) -> T { self.0 } //~ destructors cannot be evaluated
50     const fn get(&self) -> &T { &self.0 }
51     const fn get_mut(&mut self) -> &mut T { &mut self.0 }
52     //~^ mutable references in const fn are unstable
53 }
54 impl<'a, T> Foo<T> {
55     const fn new_lt(t: T) -> Self { Foo(t) }
56     const fn into_inner_lt(self) -> T { self.0 } //~ destructors cannot be evaluated
57     const fn get_lt(&'a self) -> &T { &self.0 }
58     const fn get_mut_lt(&'a mut self) -> &mut T { &mut self.0 }
59     //~^ mutable references in const fn are unstable
60 }
61 impl<T: Sized> Foo<T> {
62     const fn new_s(t: T) -> Self { Foo(t) }
63     const fn into_inner_s(self) -> T { self.0 } //~ ERROR destructors
64     const fn get_s(&self) -> &T { &self.0 }
65     const fn get_mut_s(&mut self) -> &mut T { &mut self.0 }
66     //~^ mutable references in const fn are unstable
67 }
68 impl<T: ?Sized> Foo<T> {
69     const fn get_sq(&self) -> &T { &self.0 }
70     const fn get_mut_sq(&mut self) -> &mut T { &mut self.0 }
71     //~^ mutable references in const fn are unstable
72 }
73
74
75 const fn char_ops(c: char, d: char) -> bool { c == d }
76 const fn char_ops2(c: char, d: char) -> bool { c < d }
77 const fn char_ops3(c: char, d: char) -> bool { c != d }
78 const fn i32_ops(c: i32, d: i32) -> bool { c == d }
79 const fn i32_ops2(c: i32, d: i32) -> bool { c < d }
80 const fn i32_ops3(c: i32, d: i32) -> bool { c != d }
81 const fn i32_ops4(c: i32, d: i32) -> i32 { c + d }
82 const fn char_cast(u: u8) -> char { u as char }
83 const unsafe fn foo4() -> i32 { 42 }
84 const unsafe fn foo5<T>() -> *const T { 0 as *const T }
85 const unsafe fn foo6<T>() -> *mut T { 0 as *mut T }
86
87 // not ok
88 const fn foo11<T: std::fmt::Display>(t: T) -> T { t }
89 //~^ ERROR trait bounds other than `Sized` on const fn parameters are unstable
90 const fn foo11_2<T: Send>(t: T) -> T { t }
91 //~^ ERROR trait bounds other than `Sized` on const fn parameters are unstable
92 const fn foo19(f: f32) -> f32 { f * 2.0 }
93 //~^ ERROR only int, `bool` and `char` operations are stable in const fn
94 const fn foo19_2(f: f32) -> f32 { 2.0 - f }
95 //~^ ERROR only int, `bool` and `char` operations are stable in const fn
96 const fn foo19_3(f: f32) -> f32 { -f }
97 //~^ ERROR only int and `bool` operations are stable in const fn
98 const fn foo19_4(f: f32, g: f32) -> f32 { f / g }
99 //~^ ERROR only int, `bool` and `char` operations are stable in const fn
100
101 static BAR: u32 = 42;
102 const fn foo25() -> u32 { BAR } //~ ERROR cannot access `static` items in const fn
103 const fn foo26() -> &'static u32 { &BAR } //~ ERROR cannot access `static` items
104 const fn foo30(x: *const u32) -> usize { x as usize }
105 //~^ ERROR casting pointers to int
106 const fn foo30_2(x: *mut u32) -> usize { x as usize }
107 //~^ ERROR casting pointers to int
108 const fn foo30_4(b: bool) -> usize { if b { 1 } else { 42 } }
109 //~^ ERROR `if`, `match`, `&&` and `||` are not stable in const fn
110 const fn foo30_5(b: bool) { while b { } } //~ ERROR not stable in const fn
111 const fn foo30_6() -> bool { let x = true; x } //~ ERROR local variables in const fn are unstable
112 const fn foo36(a: bool, b: bool) -> bool { a && b }
113 //~^ ERROR `if`, `match`, `&&` and `||` are not stable in const fn
114 const fn foo37(a: bool, b: bool) -> bool { a || b }
115 //~^ ERROR `if`, `match`, `&&` and `||` are not stable in const fn
116 const fn inc(x: &mut i32) { *x += 1 }
117 //~^ ERROR mutable references in const fn are unstable
118
119 fn main() {}
120
121 impl<T: std::fmt::Debug> Foo<T> {
122 //~^ ERROR trait bounds other than `Sized` on const fn parameters are unstable
123     const fn foo(&self) {}
124 }
125
126 impl<T: std::fmt::Debug + Sized> Foo<T> {
127 //~^ ERROR trait bounds other than `Sized` on const fn parameters are unstable
128     const fn foo2(&self) {}
129 }
130
131 impl<T: Sync + Sized> Foo<T> {
132 //~^ ERROR trait bounds other than `Sized` on const fn parameters are unstable
133     const fn foo3(&self) {}
134 }
135
136 struct AlanTuring<T>(T);
137 const fn no_rpit2() -> AlanTuring<impl std::fmt::Debug> { AlanTuring(0) }
138 //~^ ERROR `impl Trait` in const fn is unstable
139 const fn no_apit2(_x: AlanTuring<impl std::fmt::Debug>) {}
140 //~^ ERROR trait bounds other than `Sized`
141 const fn no_apit(_x: impl std::fmt::Debug) {} //~ ERROR trait bounds other than `Sized`
142 const fn no_rpit() -> impl std::fmt::Debug {} //~ ERROR `impl Trait` in const fn is unstable
143 const fn no_dyn_trait(_x: &dyn std::fmt::Debug) {} //~ ERROR trait bounds other than `Sized`
144 const fn no_dyn_trait_ret() -> &'static dyn std::fmt::Debug { &() }
145 //~^ ERROR trait bounds other than `Sized`
146
147 const fn no_unsafe() { unsafe {} }
148
149 const fn really_no_traits_i_mean_it() { (&() as &std::fmt::Debug, ()).1 }
150 //~^ ERROR trait bounds other than `Sized`
151
152 const fn no_fn_ptrs(_x: fn()) {}
153 //~^ ERROR function pointers in const fn are unstable
154 const fn no_fn_ptrs2() -> fn() { fn foo() {} foo }
155 //~^ ERROR function pointers in const fn are unstable
156