From 7ed39c6d9bddb6f11b4681574cc0375446eb594e Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Fri, 7 Aug 2015 13:47:54 -0400 Subject: [PATCH] Fallout in tests -- explain an interesting test failure having to do with dropck and the new outlives rules --- src/test/compile-fail/dropck-object-cycle.rs | 57 +++++++++++++++++++ .../regions-early-bound-trait-param.rs | 7 ++- 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/test/compile-fail/dropck-object-cycle.rs diff --git a/src/test/compile-fail/dropck-object-cycle.rs b/src/test/compile-fail/dropck-object-cycle.rs new file mode 100644 index 00000000000..5432cbf402a --- /dev/null +++ b/src/test/compile-fail/dropck-object-cycle.rs @@ -0,0 +1,57 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// This test used to be part of a run-pass test, but revised outlives +// rule means that it no longer compiles. + +#![allow(unused_variables)] + +trait Trait<'a> { + fn long(&'a self) -> isize; + fn short<'b>(&'b self) -> isize; +} + +fn object_invoke1<'d>(x: &'d Trait<'d>) -> (isize, isize) { loop { } } + +trait MakerTrait { + fn mk() -> Self; +} + +fn make_val() -> T { + MakerTrait::mk() +} + +impl<'t> MakerTrait for Box+'static> { + fn mk() -> Box+'static> { loop { } } +} + +pub fn main() { + let m : Box = make_val(); + assert_eq!(object_invoke1(&*m), (4,5)); + //~^ ERROR `*m` does not live long enough + + // the problem here is that the full type of `m` is + // + // Box+'static> + // + // Here `'m` must be exactly the lifetime of the variable `m`. + // This is because of two requirements: + // 1. First, the basic type rules require that the + // type of `m`'s value outlives the lifetime of `m`. This puts a lower + // bound `'m`. + // + // 2. Meanwhile, the signature of `object_invoke1` requires that + // we create a reference of type `&'d Trait<'d>` for some `'d`. + // `'d` cannot outlive `'m`, so that forces the lifetime to be `'m`. + // + // This then conflicts with the dropck rules, which require that + // the type of `m` *strictly outlives* `'m`. Hence we get an + // error. +} diff --git a/src/test/run-pass/regions-early-bound-trait-param.rs b/src/test/run-pass/regions-early-bound-trait-param.rs index 33889b27a87..72a214f4c9a 100644 --- a/src/test/run-pass/regions-early-bound-trait-param.rs +++ b/src/test/run-pass/regions-early-bound-trait-param.rs @@ -42,7 +42,7 @@ fn field_invoke1<'f, 'g>(x: &'g Struct1<'f>) -> (isize,isize) { (l,s) } -struct Struct2<'h, 'i> { +struct Struct2<'h, 'i:'h> { f: &'h (Trait<'i>+'h) } @@ -126,7 +126,10 @@ pub fn main() { assert_eq!(field_invoke2(&s2), 3); let m : Box = make_val(); - assert_eq!(object_invoke1(&*m), (4,5)); + // assert_eq!(object_invoke1(&*m), (4,5)); + // ~~~~~~~~~~~~~~~~~~~ + // this call yields a compilation error; see compile-fail/dropck-object-cycle.rs + // for details. assert_eq!(object_invoke2(&*m), 5); // The RefMakerTrait above is pretty strange (i.e. it is strange