Auto merge of #24461 - nikomatsakis:issue-22077-unused-lifetimes, r=aturon

This makes it illegal to have unconstrained lifetimes that appear in an associated type definition. Arguably, we should prohibit all unconstrained lifetimes -- but it would break various macros. It'd be good to evaluate how large a break change it would be. But this seems like the minimal change we need to do to establish soundness, so we should land it regardless. Another variant would be to prohibit all lifetimes that appear in any impl item, not just associated types. I don't think that's necessary for soundness -- associated types are different because they can be projected -- but it would feel a bit more consistent and "obviously" safe. I'll experiment with that in the meantime.

r? @aturon 

Fixes #22077.
This commit is contained in:
bors 2015-04-17 20:38:18 +00:00
commit f305579e49
16 changed files with 302 additions and 99 deletions

View file

@ -0,0 +1,28 @@
// 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 lifetime parameters must be constrained if they appear in
// an associated type def'n. Issue #22077.
trait Fun {
type Output;
fn call<'x>(&'x self) -> Self::Output;
}
struct Holder { x: String }
impl<'a> Fun for Holder { //~ ERROR E0207
type Output = &'a str;
fn call<'b>(&'b self) -> &'b str {
&self.x[..]
}
}
fn main() { }

View file

@ -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.
// Regression test for #22886.
fn crash_please() {
let mut iter = Newtype(Some(Box::new(0)));
let saved = iter.next().unwrap();
println!("{}", saved);
iter.0 = None;
println!("{}", saved);
}
struct Newtype(Option<Box<usize>>);
impl<'a> Iterator for Newtype { //~ ERROR E0207
type Item = &'a Box<usize>;
fn next(&mut self) -> Option<&Box<usize>> {
self.0.as_ref()
}
}
fn main() { }