Auto merge of #27045 - nikomatsakis:better-object-defaults-error, r=pnkfelix
Transition to the new object lifetime defaults, replacing the old defaults completely.
r? @pnkfelix
This is a [breaking-change] as specified by [RFC 1156][1156] (though all cases that would break should have been receiving warnings starting in Rust 1.2). Types like `&'a Box<Trait>` (or `&'a Rc<Trait>`, etc) will change from being interpreted as `&'a Box<Trait+'a>` to `&'a Box<Trait+'static>`. To restore the old behavior, write the `+'a` explicitly. For example, the function:
```rust
trait Trait { }
fn foo(x: &Box<Trait>) { ... }
```
would be rewritten as:
```rust
trait Trait { }
fn foo(x: &'a Box<Trait+'a>) { ... }
```
if one wanted to preserve the current typing.
[1156]: https://github.com/rust-lang/rfcs/blob/master/text/1156-adjust-default-object-bounds.md
This commit is contained in:
commit
e05ac3938b
28 changed files with 97 additions and 240 deletions
|
|
@ -10,8 +10,8 @@
|
|||
|
||||
// aux-build:lifetime_bound_will_change_warning_lib.rs
|
||||
|
||||
// Test that we get suitable warnings when lifetime bound change will
|
||||
// cause breakage.
|
||||
// Test that various corner cases cause an error. These are tests
|
||||
// that used to pass before we tweaked object defaults.
|
||||
|
||||
#![allow(dead_code)]
|
||||
#![allow(unused_variables)]
|
||||
|
|
@ -41,12 +41,12 @@ fn test1cc<'a>(x: &'a Box<Fn()+'a>) {
|
|||
|
||||
fn test2<'a>(x: &'a Box<Fn()+'a>) {
|
||||
// but ref_obj will not, so warn.
|
||||
ref_obj(x) //~ WARNING this code may fail to compile in Rust 1.3
|
||||
ref_obj(x) //~ ERROR mismatched types
|
||||
}
|
||||
|
||||
fn test2cc<'a>(x: &'a Box<Fn()+'a>) {
|
||||
// same as test2, but cross crate
|
||||
lib::ref_obj(x) //~ WARNING this code may fail to compile in Rust 1.3
|
||||
lib::ref_obj(x) //~ ERROR mismatched types
|
||||
}
|
||||
|
||||
fn test3<'a>(x: &'a Box<Fn()+'static>) {
|
||||
|
|
@ -60,5 +60,5 @@ fn test3cc<'a>(x: &'a Box<Fn()+'static>) {
|
|||
}
|
||||
|
||||
#[rustc_error]
|
||||
fn main() { //~ ERROR compilation successful
|
||||
fn main() {
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,10 +47,7 @@ fn d(t: Ref2<Ref1<Test>>) {
|
|||
}
|
||||
|
||||
fn e(t: Ref2<Ref0<Test>>) {
|
||||
//~^ ERROR lifetime bound for this object type cannot be deduced from context
|
||||
//
|
||||
// In this case, Ref2 is ambiguous, and Ref0 inherits the
|
||||
// ambiguity.
|
||||
// In this case, Ref2 is ambiguous, but Ref0 overrides with 'static.
|
||||
}
|
||||
|
||||
fn f(t: &Ref2<Test>) {
|
||||
|
|
|
|||
|
|
@ -34,13 +34,11 @@ fn load0<'a>(ss: &'a Box<SomeTrait>) -> Box<SomeTrait> {
|
|||
//
|
||||
// Under new rules the result is:
|
||||
//
|
||||
// for<'a> fn(&'a Box<SomeTrait+'a>) -> Box<SomeTrait+'static>
|
||||
// for<'a> fn(&'a Box<SomeTrait+'static>) -> Box<SomeTrait+'static>
|
||||
//
|
||||
// Therefore, we get a type error attempting to return `deref(ss)`
|
||||
// since `SomeTrait+'a <: SomeTrait+'static` does not hold.
|
||||
// Therefore, no type error.
|
||||
|
||||
deref(ss)
|
||||
//~^ ERROR cannot infer
|
||||
}
|
||||
|
||||
fn load1(ss: &SomeTrait) -> &SomeTrait {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,31 @@
|
|||
// Copyright 2015 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 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// Test that the lifetime from the enclosing `&` is "inherited"
|
||||
// through the `Box` struct.
|
||||
|
||||
// pretty-expanded FIXME #23616
|
||||
|
||||
#![allow(dead_code)]
|
||||
|
||||
trait Test {
|
||||
fn foo(&self) { }
|
||||
}
|
||||
|
||||
struct SomeStruct<'a> {
|
||||
t: &'a Box<Test>,
|
||||
}
|
||||
|
||||
fn c<'a>(t: &'a Box<Test+'a>, mut ss: SomeStruct<'a>) {
|
||||
ss.t = t; //~ ERROR mismatched types
|
||||
}
|
||||
|
||||
fn main() {
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
// Copyright 2015 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 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// Test that the lifetime from the enclosing `&` is "inherited"
|
||||
// through the `MyBox` struct.
|
||||
|
||||
// pretty-expanded FIXME #23616
|
||||
|
||||
#![allow(dead_code)]
|
||||
#![feature(rustc_error)]
|
||||
|
||||
trait Test {
|
||||
fn foo(&self) { }
|
||||
}
|
||||
|
||||
struct SomeStruct<'a> {
|
||||
t: &'a MyBox<Test>,
|
||||
u: &'a MyBox<Test+'a>,
|
||||
}
|
||||
|
||||
struct MyBox<T:?Sized> {
|
||||
b: Box<T>
|
||||
}
|
||||
|
||||
fn c<'a>(t: &'a MyBox<Test+'a>, mut ss: SomeStruct<'a>) {
|
||||
ss.t = t; //~ ERROR mismatched types
|
||||
}
|
||||
|
||||
fn main() {
|
||||
}
|
||||
|
|
@ -27,7 +27,7 @@ fn deref<T>(ss: &T) -> T {
|
|||
}
|
||||
|
||||
fn load0(ss: &MyBox<SomeTrait>) -> MyBox<SomeTrait> {
|
||||
deref(ss) //~ ERROR cannot infer
|
||||
deref(ss)
|
||||
}
|
||||
|
||||
fn load1<'a,'b>(a: &'a MyBox<SomeTrait>,
|
||||
|
|
@ -36,11 +36,10 @@ fn load1<'a,'b>(a: &'a MyBox<SomeTrait>,
|
|||
{
|
||||
a
|
||||
//~^ ERROR cannot infer
|
||||
//~| ERROR mismatched types
|
||||
}
|
||||
|
||||
fn load2<'a>(ss: &MyBox<SomeTrait+'a>) -> MyBox<SomeTrait+'a> {
|
||||
load0(ss) //~ WARNING E0398
|
||||
load0(ss) //~ ERROR mismatched types
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
|||
|
|
@ -1,22 +0,0 @@
|
|||
// Copyright 2014-2015 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 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// aux-build:lang-item-public.rs
|
||||
// ignore-android
|
||||
|
||||
#![feature(start, no_std)]
|
||||
#![no_std]
|
||||
|
||||
extern crate lang_item_public as lang_lib;
|
||||
|
||||
#[start]
|
||||
fn main(_: isize, _: *const *const u8) -> isize {
|
||||
1_isize % 1_isize
|
||||
}
|
||||
|
|
@ -32,9 +32,7 @@ fn b<'a>(t: &'a Box<Test>, mut ss: SomeStruct<'a>) {
|
|||
ss.u = t;
|
||||
}
|
||||
|
||||
fn c<'a>(t: &'a Box<Test+'a>, mut ss: SomeStruct<'a>) {
|
||||
ss.t = t;
|
||||
}
|
||||
// see also compile-fail/object-lifetime-default-from-rptr-box-error.rs
|
||||
|
||||
fn d<'a>(t: &'a Box<Test+'a>, mut ss: SomeStruct<'a>) {
|
||||
ss.u = t;
|
||||
|
|
|
|||
|
|
@ -36,9 +36,7 @@ fn b<'a>(t: &'a MyBox<Test>, mut ss: SomeStruct<'a>) {
|
|||
ss.u = t;
|
||||
}
|
||||
|
||||
fn c<'a>(t: &'a MyBox<Test+'a>, mut ss: SomeStruct<'a>) {
|
||||
ss.t = t;
|
||||
}
|
||||
// see also compile-fail/object-lifetime-default-from-rptr-box-error.rs
|
||||
|
||||
fn d<'a>(t: &'a MyBox<Test+'a>, mut ss: SomeStruct<'a>) {
|
||||
ss.u = t;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue