]> git.proxmox.com Git - rustc.git/blob - src/test/compile-fail/lint-unconditional-recursion.rs
Imported Upstream version 1.3.0+dfsg1
[rustc.git] / src / test / compile-fail / lint-unconditional-recursion.rs
1 // Copyright 2014 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 #![deny(unconditional_recursion)]
12 #![allow(dead_code)]
13 fn foo() { //~ ERROR function cannot return without recurring
14 foo(); //~ NOTE recursive call site
15 }
16
17 fn bar() {
18 if true {
19 bar()
20 }
21 }
22
23 fn baz() { //~ ERROR function cannot return without recurring
24 if true {
25 baz() //~ NOTE recursive call site
26 } else {
27 baz() //~ NOTE recursive call site
28 }
29 }
30
31 fn qux() {
32 loop {}
33 }
34
35 fn quz() -> bool { //~ ERROR function cannot return without recurring
36 if true {
37 while quz() {} //~ NOTE recursive call site
38 true
39 } else {
40 loop { quz(); } //~ NOTE recursive call site
41 }
42 }
43
44 // Trait method calls.
45 trait Foo {
46 fn bar(&self) { //~ ERROR function cannot return without recurring
47 self.bar() //~ NOTE recursive call site
48 }
49 }
50
51 impl Foo for Box<Foo+'static> {
52 fn bar(&self) { //~ ERROR function cannot return without recurring
53 loop {
54 self.bar() //~ NOTE recursive call site
55 }
56 }
57 }
58
59 // Trait method call with integer fallback after method resolution.
60 impl Foo for i32 {
61 fn bar(&self) { //~ ERROR function cannot return without recurring
62 0.bar() //~ NOTE recursive call site
63 }
64 }
65
66 impl Foo for u32 {
67 fn bar(&self) {
68 0.bar()
69 }
70 }
71
72 // Trait method calls via paths.
73 trait Foo2 {
74 fn bar(&self) { //~ ERROR function cannot return without recurring
75 Foo2::bar(self) //~ NOTE recursive call site
76 }
77 }
78
79 impl Foo2 for Box<Foo2+'static> {
80 fn bar(&self) { //~ ERROR function cannot return without recurring
81 loop {
82 Foo2::bar(self) //~ NOTE recursive call site
83 }
84 }
85 }
86
87 struct Baz;
88 impl Baz {
89 // Inherent method call.
90 fn qux(&self) { //~ ERROR function cannot return without recurring
91 self.qux(); //~ NOTE recursive call site
92 }
93
94 // Inherent method call via path.
95 fn as_ref(&self) -> &Self { //~ ERROR function cannot return without recurring
96 Baz::as_ref(self) //~ NOTE recursive call site
97 }
98 }
99
100 // Trait method calls to impls via paths.
101 impl Default for Baz {
102 fn default() -> Baz { //~ ERROR function cannot return without recurring
103 let x = Default::default(); //~ NOTE recursive call site
104 x
105 }
106 }
107
108 // Overloaded operators.
109 impl std::ops::Deref for Baz {
110 type Target = ();
111 fn deref(&self) -> &() { //~ ERROR function cannot return without recurring
112 &**self //~ NOTE recursive call site
113 }
114 }
115
116 impl std::ops::Index<usize> for Baz {
117 type Output = Baz;
118 fn index(&self, x: usize) -> &Baz { //~ ERROR function cannot return without recurring
119 &self[x] //~ NOTE recursive call site
120 }
121 }
122
123 // Overloaded autoderef.
124 struct Quux;
125 impl std::ops::Deref for Quux {
126 type Target = Baz;
127 fn deref(&self) -> &Baz { //~ ERROR function cannot return without recurring
128 self.as_ref() //~ NOTE recursive call site
129 }
130 }
131
132 fn all_fine() {
133 let _f = all_fine;
134 }
135
136 // issue 26333
137 trait Bar {
138 fn method<T: Bar>(&self, x: &T) {
139 x.method(x)
140 }
141 }
142
143 fn main() {}