]> git.lizzy.rs Git - rust.git/blob - tests/ui/escape_analysis.rs
Merge pull request #3265 from mikerite/fix-export
[rust.git] / tests / ui / escape_analysis.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10
11 #![feature(box_syntax)]
12
13 #![allow(warnings, clippy)]
14
15 #![warn(boxed_local)]
16
17 #[derive(Clone)]
18 struct A;
19
20 impl A {
21     fn foo(&self){}
22 }
23
24 trait Z {
25     fn bar(&self);
26 }
27
28 impl Z for A {
29     fn bar(&self) {
30         //nothing
31     }
32 }
33
34 fn main() {
35 }
36
37 fn ok_box_trait(boxed_trait: &Box<Z>) {
38     let boxed_local = boxed_trait;
39     // done
40 }
41
42 fn warn_call() {
43     let x = box A;
44     x.foo();
45 }
46
47 fn warn_arg(x: Box<A>) {
48     x.foo();
49 }
50
51 fn nowarn_closure_arg() {
52     let x = Some(box A);
53     x.map_or((), |x| take_ref(&x));
54 }
55
56 fn warn_rename_call() {
57     let x = box A;
58
59     let y = x;
60     y.foo(); // via autoderef
61 }
62
63 fn warn_notuse() {
64     let bz = box A;
65 }
66
67 fn warn_pass() {
68     let bz = box A;
69     take_ref(&bz); // via deref coercion
70 }
71
72 fn nowarn_return() -> Box<A> {
73     let fx = box A;
74     fx // moved out, "escapes"
75 }
76
77 fn nowarn_move() {
78     let bx = box A;
79     drop(bx) // moved in, "escapes"
80 }
81 fn nowarn_call() {
82     let bx = box A;
83     bx.clone(); // method only available to Box, not via autoderef
84 }
85
86 fn nowarn_pass() {
87     let bx = box A;
88     take_box(&bx); // fn needs &Box
89 }
90
91
92 fn take_box(x: &Box<A>) {}
93 fn take_ref(x: &A) {}
94
95
96 fn nowarn_ref_take() {
97     // false positive, should actually warn
98     let x = box A;
99     let y = &x;
100     take_box(y);
101 }
102
103 fn nowarn_match() {
104     let x = box A; // moved into a match
105     match x {
106         y => drop(y)
107     }
108 }
109
110 fn warn_match() {
111     let x = box A;
112     match &x { // not moved
113         ref y => ()
114     }
115 }
116
117 fn nowarn_large_array() {
118     // should not warn, is large array
119     // and should not be on stack
120     let x = box [1; 10000];
121     match &x { // not moved
122         ref y => ()
123     }
124 }
125
126
127 /// ICE regression test
128 pub trait Foo {
129     type Item;
130 }
131
132 impl<'a> Foo for &'a () {
133     type Item = ();
134 }
135
136 pub struct PeekableSeekable<I: Foo> {
137     _peeked: I::Item,
138 }
139
140 pub fn new(_needs_name: Box<PeekableSeekable<&()>>) -> () {
141 }