|
| 1 | +// Copyright 2012-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 | +// This is a regression test for something that only came up while |
| 12 | +// attempting to bootstrap librustc with new destructor lifetime |
| 13 | +// semantics. |
| 14 | + |
| 15 | +use std::collections::HashMap; |
| 16 | +use std::cell::RefCell; |
| 17 | + |
| 18 | +// This version does not yet work (associated type issues)... |
| 19 | +#[cfg(cannot_use_this_yet)] |
| 20 | +fn foo<'a>(map: RefCell<HashMap<&'static str, &'a [u8]>>) { |
| 21 | + let one = [1u]; |
| 22 | + assert_eq!(map.borrow().get("one"), Some(&one[])); |
| 23 | +} |
| 24 | + |
| 25 | +#[cfg(cannot_use_this_yet_either)] |
| 26 | +// ... and this version does not work (the lifetime of `one` is |
| 27 | +// supposed to match the lifetime `'a`) ... |
| 28 | +fn foo<'a>(map: RefCell<HashMap<&'static str, &'a [u8]>>) { |
| 29 | + let one = [1u]; |
| 30 | + assert_eq!(map.borrow().get("one"), Some(&one.as_slice())); |
| 31 | +} |
| 32 | + |
| 33 | +#[cfg(all(not(cannot_use_this_yet),not(cannot_use_this_yet_either)))] |
| 34 | +fn foo<'a>(map: RefCell<HashMap<&'static str, &'a [u8]>>) { |
| 35 | + // ...so instead we walk through the trivial slice and make sure |
| 36 | + // it contains the element we expect. |
| 37 | + |
| 38 | + for (i, &x) in map.borrow().get("one").unwrap().iter().enumerate() { |
| 39 | + assert_eq!((i, x), (0, 1)); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +fn main() { |
| 44 | + let zer = [0u8]; |
| 45 | + let one = [1u8]; |
| 46 | + let two = [2u8]; |
| 47 | + let mut map = HashMap::new(); |
| 48 | + map.insert("zero", &zer[]); |
| 49 | + map.insert("one", &one[]); |
| 50 | + map.insert("two", &two[]); |
| 51 | + let map = RefCell::new(map); |
| 52 | + foo(map); |
| 53 | +} |
0 commit comments