]> git.lizzy.rs Git - rust.git/blob - src/test/ui/nll/get_default.rs
Auto merge of #53002 - QuietMisdreavus:brother-may-i-have-some-loops, r=pnkfelix
[rust.git] / src / test / ui / nll / get_default.rs
1 // Copyright 2016 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 // Basic test for free regions in the NLL code. This test ought to
12 // report an error due to a reborrowing constraint. Right now, we get
13 // a variety of errors from the older, AST-based machinery (notably
14 // borrowck), and then we get the NLL error at the end.
15
16 // compile-flags:-Zborrowck=compare
17
18 struct Map {
19 }
20
21 impl Map {
22     fn get(&self) -> Option<&String> { None }
23     fn set(&mut self, v: String) { }
24 }
25
26 fn ok(map: &mut Map) -> &String {
27     loop {
28         match map.get() {
29             Some(v) => {
30                 return v;
31             }
32             None => {
33                 map.set(String::new()); // Ideally, this would not error.
34                 //~^ ERROR borrowed as immutable (Ast)
35                 //~| ERROR borrowed as immutable (Mir)
36             }
37         }
38     }
39 }
40
41 fn err(map: &mut Map) -> &String {
42     loop {
43         match map.get() {
44             Some(v) => {
45                 map.set(String::new()); // Both AST and MIR error here
46                 //~^ ERROR borrowed as immutable (Mir)
47                 //~| ERROR borrowed as immutable (Ast)
48                 return v;
49             }
50             None => {
51                 map.set(String::new()); // Ideally, just AST would error here
52                 //~^ ERROR borrowed as immutable (Ast)
53                 //~| ERROR borrowed as immutable (Mir)
54             }
55         }
56     }
57 }
58
59 fn main() { }