Skip to content

Commit 7ed39c6

Browse files
committed
Fallout in tests -- explain an interesting test failure having to
do with dropck and the new outlives rules
1 parent dee8b54 commit 7ed39c6

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 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 test used to be part of a run-pass test, but revised outlives
12+
// rule means that it no longer compiles.
13+
14+
#![allow(unused_variables)]
15+
16+
trait Trait<'a> {
17+
fn long(&'a self) -> isize;
18+
fn short<'b>(&'b self) -> isize;
19+
}
20+
21+
fn object_invoke1<'d>(x: &'d Trait<'d>) -> (isize, isize) { loop { } }
22+
23+
trait MakerTrait {
24+
fn mk() -> Self;
25+
}
26+
27+
fn make_val<T:MakerTrait>() -> T {
28+
MakerTrait::mk()
29+
}
30+
31+
impl<'t> MakerTrait for Box<Trait<'t>+'static> {
32+
fn mk() -> Box<Trait<'t>+'static> { loop { } }
33+
}
34+
35+
pub fn main() {
36+
let m : Box<Trait+'static> = make_val();
37+
assert_eq!(object_invoke1(&*m), (4,5));
38+
//~^ ERROR `*m` does not live long enough
39+
40+
// the problem here is that the full type of `m` is
41+
//
42+
// Box<Trait<'m>+'static>
43+
//
44+
// Here `'m` must be exactly the lifetime of the variable `m`.
45+
// This is because of two requirements:
46+
// 1. First, the basic type rules require that the
47+
// type of `m`'s value outlives the lifetime of `m`. This puts a lower
48+
// bound `'m`.
49+
//
50+
// 2. Meanwhile, the signature of `object_invoke1` requires that
51+
// we create a reference of type `&'d Trait<'d>` for some `'d`.
52+
// `'d` cannot outlive `'m`, so that forces the lifetime to be `'m`.
53+
//
54+
// This then conflicts with the dropck rules, which require that
55+
// the type of `m` *strictly outlives* `'m`. Hence we get an
56+
// error.
57+
}

src/test/run-pass/regions-early-bound-trait-param.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn field_invoke1<'f, 'g>(x: &'g Struct1<'f>) -> (isize,isize) {
4242
(l,s)
4343
}
4444

45-
struct Struct2<'h, 'i> {
45+
struct Struct2<'h, 'i:'h> {
4646
f: &'h (Trait<'i>+'h)
4747
}
4848

@@ -126,7 +126,10 @@ pub fn main() {
126126
assert_eq!(field_invoke2(&s2), 3);
127127

128128
let m : Box<Trait> = make_val();
129-
assert_eq!(object_invoke1(&*m), (4,5));
129+
// assert_eq!(object_invoke1(&*m), (4,5));
130+
// ~~~~~~~~~~~~~~~~~~~
131+
// this call yields a compilation error; see compile-fail/dropck-object-cycle.rs
132+
// for details.
130133
assert_eq!(object_invoke2(&*m), 5);
131134

132135
// The RefMakerTrait above is pretty strange (i.e. it is strange

0 commit comments

Comments
 (0)