Skip to content

Commit c40c283

Browse files
committed
add some run-pass tests for NLL showing that things work as expected
1 parent f408c63 commit c40c283

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

src/test/run-pass/nll/get_default.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
use std::collections::HashMap;
12+
13+
fn get_default(map: &mut HashMap<usize, String>, key: usize) -> &mut String {
14+
match map.get_mut(&key) {
15+
Some(value) => value,
16+
None => {
17+
map.insert(key, "".to_string());
18+
map.get_mut(&key).unwrap()
19+
}
20+
}
21+
}
22+
23+
fn main() {
24+
let map = &mut HashMap::new();
25+
map.insert(22, format!("Hello, world"));
26+
map.insert(44, format!("Goodbye, world"));
27+
assert_eq!(&*get_default(map, 22), "Hello, world");
28+
assert_eq!(&*get_default(map, 66), "");
29+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
use std::collections::HashMap;
12+
13+
fn process_or_insert_default(map: &mut HashMap<usize, String>, key: usize) {
14+
match map.get_mut(&key) {
15+
Some(value) => {
16+
process(value);
17+
}
18+
None => {
19+
map.insert(key, "".to_string());
20+
}
21+
}
22+
}
23+
24+
fn process(x: &str) {
25+
assert_eq!(x, "Hello, world");
26+
}
27+
28+
fn main() {
29+
let map = &mut HashMap::new();
30+
map.insert(22, format!("Hello, world"));
31+
map.insert(44, format!("Goodbye, world"));
32+
process_or_insert_default(map, 22);
33+
process_or_insert_default(map, 66);
34+
assert_eq!(map[&66], "");
35+
}

src/test/run-pass/nll/rc-loop.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
// A test for something that NLL enables. It sometimes happens that
12+
// the `while let` pattern makes some borrows from a variable (in this
13+
// case, `x`) that you need in order to compute the next value for
14+
// `x`. The lexical checker makes this very painful. The NLL checker
15+
// does not.
16+
17+
// compile-flags:-Znll -Zborrowck=mir
18+
19+
#![feature(match_default_bindings)]
20+
21+
use std::rc::Rc;
22+
23+
#[derive(Debug, PartialEq, Eq)]
24+
enum Foo {
25+
Base(usize),
26+
Next(Rc<Foo>),
27+
}
28+
29+
fn find_base(mut x: Rc<Foo>) -> Rc<Foo> {
30+
while let Foo::Next(n) = &*x {
31+
x = n.clone();
32+
}
33+
x
34+
}
35+
36+
fn main() {
37+
let chain = Rc::new(Foo::Next(Rc::new(Foo::Base(44))));
38+
let base = find_base(chain);
39+
assert_eq!(&*base, &Foo::Base(44));
40+
}
41+

0 commit comments

Comments
 (0)