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