warn for where/return-types that reference regions
This is a step towards fixing #32330. The full fix would be a breaking change, so we begin by issuing warnings for scenarios that will break.
This commit is contained in:
parent
97ca8d799c
commit
ccfb74e800
10 changed files with 449 additions and 15 deletions
|
|
@ -40,6 +40,17 @@ impl<'a> TheTrait<&'a isize> for UintStruct {
|
|||
}
|
||||
}
|
||||
|
||||
struct Tuple {
|
||||
}
|
||||
|
||||
impl<'a> TheTrait<(&'a isize, &'a isize)> for Tuple {
|
||||
type A = &'a isize;
|
||||
|
||||
fn get(&self, t: (&'a isize, &'a isize)) -> &'a isize {
|
||||
t.0
|
||||
}
|
||||
}
|
||||
|
||||
fn foo<T>()
|
||||
where T : for<'x> TheTrait<&'x isize, A = &'x isize>
|
||||
{
|
||||
|
|
@ -52,10 +63,28 @@ fn bar<T>()
|
|||
// ok for UintStruct, but not IntStruct
|
||||
}
|
||||
|
||||
fn baz<T>()
|
||||
where T : for<'x,'y> TheTrait<&'x isize, A = &'y isize>
|
||||
fn tuple_one<T>()
|
||||
where T : for<'x,'y> TheTrait<(&'x isize, &'y isize), A = &'x isize>
|
||||
{
|
||||
// not ok for either struct, due to the use of two lifetimes
|
||||
// not ok for tuple, two lifetimes and we pick first
|
||||
}
|
||||
|
||||
fn tuple_two<T>()
|
||||
where T : for<'x,'y> TheTrait<(&'x isize, &'y isize), A = &'y isize>
|
||||
{
|
||||
// not ok for tuple, two lifetimes and we pick second
|
||||
}
|
||||
|
||||
fn tuple_three<T>()
|
||||
where T : for<'x> TheTrait<(&'x isize, &'x isize), A = &'x isize>
|
||||
{
|
||||
// ok for tuple
|
||||
}
|
||||
|
||||
fn tuple_four<T>()
|
||||
where T : for<'x,'y> TheTrait<(&'x isize, &'y isize)>
|
||||
{
|
||||
// not ok for tuple, two lifetimes, and lifetime matching is invariant
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
|
|
@ -65,6 +94,16 @@ pub fn main() {
|
|||
bar::<IntStruct>(); //~ ERROR type mismatch
|
||||
bar::<UintStruct>();
|
||||
|
||||
baz::<IntStruct>(); //~ ERROR type mismatch
|
||||
baz::<UintStruct>(); //~ ERROR type mismatch
|
||||
tuple_one::<Tuple>();
|
||||
//~^ ERROR E0277
|
||||
//~| ERROR type mismatch
|
||||
|
||||
tuple_two::<Tuple>();
|
||||
//~^ ERROR E0277
|
||||
//~| ERROR type mismatch
|
||||
|
||||
tuple_three::<Tuple>();
|
||||
|
||||
tuple_four::<Tuple>();
|
||||
//~^ ERROR E0277
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,66 @@
|
|||
// Copyright 2012 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.
|
||||
|
||||
// revisions: func object clause
|
||||
|
||||
#![allow(dead_code)]
|
||||
#![feature(rustc_attrs)]
|
||||
#![feature(unboxed_closures)]
|
||||
#![deny(hr_lifetime_in_assoc_type)]
|
||||
|
||||
trait Foo<'a> {
|
||||
type Item;
|
||||
}
|
||||
|
||||
impl<'a> Foo<'a> for() {
|
||||
type Item = ();
|
||||
}
|
||||
|
||||
// Check that appearing in a projection input in the argument is not enough:
|
||||
#[cfg(func)]
|
||||
fn func1(_: for<'a> fn(<() as Foo<'a>>::Item) -> &'a i32) {
|
||||
//[func]~^ ERROR return type references lifetime `'a`
|
||||
//[func]~| WARNING previously accepted
|
||||
}
|
||||
|
||||
// Check that appearing in a projection input in the return still
|
||||
// causes an error:
|
||||
#[cfg(func)]
|
||||
fn func2(_: for<'a> fn() -> <() as Foo<'a>>::Item) {
|
||||
//[func]~^ ERROR return type references lifetime `'a`
|
||||
//[func]~| WARNING previously accepted
|
||||
}
|
||||
|
||||
#[cfg(object)]
|
||||
fn object1(_: Box<for<'a> Fn(<() as Foo<'a>>::Item) -> &'a i32>) {
|
||||
//[object]~^ ERROR `Output` references lifetime `'a`
|
||||
//[object]~| WARNING previously accepted
|
||||
}
|
||||
|
||||
#[cfg(object)]
|
||||
fn object2(_: Box<for<'a> Fn() -> <() as Foo<'a>>::Item>) {
|
||||
//[object]~^ ERROR `Output` references lifetime `'a`
|
||||
//[object]~| WARNING previously accepted
|
||||
}
|
||||
|
||||
#[cfg(clause)]
|
||||
fn clause1<T>() where T: for<'a> Fn(<() as Foo<'a>>::Item) -> &'a i32 {
|
||||
//[clause]~^ ERROR `Output` references lifetime `'a`
|
||||
//[clause]~| WARNING previously accepted
|
||||
}
|
||||
|
||||
#[cfg(clause)]
|
||||
fn clause2<T>() where T: for<'a> Fn() -> <() as Foo<'a>>::Item {
|
||||
//[clause]~^ ERROR `Output` references lifetime `'a`
|
||||
//[clause]~| WARNING previously accepted
|
||||
}
|
||||
|
||||
#[rustc_error]
|
||||
fn main() { } //[ok]~ ERROR compilation successful
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
// Copyright 2012 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.
|
||||
|
||||
// revisions: angle paren ok elision
|
||||
|
||||
#![allow(dead_code)]
|
||||
#![feature(rustc_attrs)]
|
||||
#![feature(unboxed_closures)]
|
||||
#![deny(hr_lifetime_in_assoc_type)]
|
||||
|
||||
trait Foo {
|
||||
type Item;
|
||||
}
|
||||
|
||||
#[cfg(angle)]
|
||||
fn angle<T: for<'a> Foo<Item=&'a i32>>() {
|
||||
//[angle]~^ ERROR binding for associated type `Item` references lifetime `'a`
|
||||
//[angle]~| WARNING previously accepted
|
||||
}
|
||||
|
||||
#[cfg(angle)]
|
||||
fn angle1<T>() where T: for<'a> Foo<Item=&'a i32> {
|
||||
//[angle]~^ ERROR binding for associated type `Item` references lifetime `'a`
|
||||
//[angle]~| WARNING previously accepted
|
||||
}
|
||||
|
||||
#[cfg(angle)]
|
||||
fn angle2<T>() where for<'a> T: Foo<Item=&'a i32> {
|
||||
//[angle]~^ ERROR binding for associated type `Item` references lifetime `'a`
|
||||
//[angle]~| WARNING previously accepted
|
||||
}
|
||||
|
||||
#[cfg(angle)]
|
||||
fn angle3(_: &for<'a> Foo<Item=&'a i32>) {
|
||||
//[angle]~^ ERROR binding for associated type `Item` references lifetime `'a`
|
||||
//[angle]~| WARNING previously accepted
|
||||
}
|
||||
|
||||
#[cfg(paren)]
|
||||
fn paren<T: for<'a> Fn() -> &'a i32>() {
|
||||
//[paren]~^ ERROR binding for associated type `Output` references lifetime `'a`
|
||||
//[paren]~| WARNING previously accepted
|
||||
}
|
||||
|
||||
#[cfg(paren)]
|
||||
fn paren1<T>() where T: for<'a> Fn() -> &'a i32 {
|
||||
//[paren]~^ ERROR binding for associated type `Output` references lifetime `'a`
|
||||
//[paren]~| WARNING previously accepted
|
||||
}
|
||||
|
||||
#[cfg(paren)]
|
||||
fn paren2<T>() where for<'a> T: Fn() -> &'a i32 {
|
||||
//[paren]~^ ERROR binding for associated type `Output` references lifetime `'a`
|
||||
//[paren]~| WARNING previously accepted
|
||||
}
|
||||
|
||||
#[cfg(paren)]
|
||||
fn paren3(_: &for<'a> Fn() -> &'a i32) {
|
||||
//[paren]~^ ERROR binding for associated type `Output` references lifetime `'a`
|
||||
//[paren]~| WARNING previously accepted
|
||||
}
|
||||
|
||||
#[cfg(elision)]
|
||||
fn elision<T: Fn() -> &i32>() {
|
||||
//[elision]~^ ERROR E0106
|
||||
}
|
||||
|
||||
struct Parameterized<'a> { x: &'a str }
|
||||
|
||||
#[cfg(ok)]
|
||||
fn ok1<T: for<'a> Fn(&Parameterized<'a>) -> &'a i32>() {
|
||||
}
|
||||
|
||||
#[cfg(ok)]
|
||||
fn ok2<T: for<'a,'b> Fn<(&'b Parameterized<'a>,), Output=&'a i32>>() {
|
||||
}
|
||||
|
||||
#[cfg(ok)]
|
||||
fn ok3<T>() where for<'a> Parameterized<'a>: Foo<Item=&'a i32> {
|
||||
}
|
||||
|
||||
#[rustc_error]
|
||||
fn main() { } //[ok]~ ERROR compilation successful
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
// Copyright 2012 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.
|
||||
|
||||
// revisions: sig local structure ok elision
|
||||
|
||||
#![allow(dead_code)]
|
||||
#![feature(rustc_attrs)]
|
||||
#![feature(unboxed_closures)]
|
||||
#![deny(hr_lifetime_in_assoc_type)]
|
||||
|
||||
trait Foo {
|
||||
type Item;
|
||||
}
|
||||
|
||||
#[cfg(sig)]
|
||||
fn sig1(_: for<'a> fn() -> &'a i32) {
|
||||
//[sig]~^ ERROR return type references lifetime `'a`
|
||||
//[sig]~| WARNING previously accepted
|
||||
}
|
||||
|
||||
#[cfg(sig)]
|
||||
fn sig2(_: for<'a, 'b> fn(&'b i32) -> &'a i32) {
|
||||
//[sig]~^ ERROR return type references lifetime `'a`
|
||||
//[sig]~| WARNING previously accepted
|
||||
}
|
||||
|
||||
#[cfg(local)]
|
||||
fn local1() {
|
||||
let _: for<'a> fn() -> &'a i32 = loop { };
|
||||
//[local]~^ ERROR return type references lifetime `'a`
|
||||
//[local]~| WARNING previously accepted
|
||||
}
|
||||
|
||||
#[cfg(structure)]
|
||||
struct Struct1 {
|
||||
x: for<'a> fn() -> &'a i32
|
||||
//[structure]~^ ERROR return type references lifetime `'a`
|
||||
//[structure]~| WARNING previously accepted
|
||||
}
|
||||
|
||||
#[cfg(elision)]
|
||||
fn elision(_: fn() -> &i32) {
|
||||
//[elision]~^ ERROR E0106
|
||||
}
|
||||
|
||||
struct Parameterized<'a> { x: &'a str }
|
||||
|
||||
#[cfg(ok)]
|
||||
fn ok1(_: &for<'a> Fn(&Parameterized<'a>) -> &'a i32) {
|
||||
}
|
||||
|
||||
#[cfg(ok)]
|
||||
fn ok2(_: &for<'a,'b> Fn<(&'b Parameterized<'a>,), Output=&'a i32>) {
|
||||
}
|
||||
|
||||
#[rustc_error]
|
||||
fn main() { } //[ok]~ ERROR compilation successful
|
||||
Loading…
Add table
Add a link
Reference in a new issue