Skip to content

Commit 07e21b1

Browse files
committed
add a test for variables used twice
1 parent aa52e12 commit 07e21b1

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2017 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+
// Test that the NLL `relate_tys` code correctly deduces that a
12+
// function returning always its first argument can be upcast to one
13+
// that returns either first or second argument.
14+
15+
#![feature(nll)]
16+
#![allow(warnings)]
17+
18+
use std::cell::Cell;
19+
20+
type DoubleCell<A> = Cell<(A, A)>;
21+
type DoublePair<A> = (A, A);
22+
23+
fn make_cell<'b>(x: &'b u32) -> Cell<(&'static u32, &'b u32)> {
24+
panic!()
25+
}
26+
27+
fn main() {
28+
let a: &'static u32 = &22;
29+
let b = 44;
30+
31+
// Here we get an error because `DoubleCell<_>` requires the same type
32+
// on both parts of the `Cell`, and we can't have that.
33+
let x: DoubleCell<_> = make_cell(&b); //~ ERROR
34+
35+
// Here we do not get an error because `DoublePair<_>` permits
36+
// variance on the lifetimes involved.
37+
let y: DoublePair<_> = make_cell(&b).get();
38+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0597]: `b` does not live long enough
2+
--> $DIR/var-appears-twice.rs:33:38
3+
|
4+
LL | let x: DoubleCell<_> = make_cell(&b); //~ ERROR
5+
| ^^ borrowed value does not live long enough
6+
...
7+
LL | }
8+
| - `b` dropped here while still borrowed
9+
|
10+
= note: borrowed value must be valid for the static lifetime...
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0597`.

0 commit comments

Comments
 (0)