Auto merge of #28669 - arielb1:well-formed-methods, r=nikomatsakis
By RFC1214:
> Before calling a fn, we check that its argument and return types are WF.
The previous code only checked the trait-ref, which was not enough
in several cases.
As this is a soundness fix, it is a [breaking-change]. Some new annotations are needed, which I think are because of #18653 and the imperfection of `projection_must_outlive` (that can probably be worked around by moving the wf obligation later).
Fixes #28609
r? @nikomatsakis
This commit is contained in:
commit
130851e030
10 changed files with 206 additions and 10 deletions
|
|
@ -22,6 +22,7 @@ fn foo(b: &Bar) {
|
|||
b.foo(&0)
|
||||
//~^ ERROR the trait `Foo` is not implemented for the type `Bar`
|
||||
//~| ERROR E0038
|
||||
//~| WARNING E0038
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ struct SExpr<'x> {
|
|||
impl<'x> PartialEq for SExpr<'x> {
|
||||
fn eq(&self, other:&SExpr<'x>) -> bool {
|
||||
println!("L1: {} L2: {}", self.elements.len(), other.elements.len());
|
||||
|
||||
let result = self.elements.len() == other.elements.len();
|
||||
|
||||
println!("Got compare {}", result);
|
||||
|
|
|
|||
|
|
@ -21,4 +21,5 @@ fn main() {
|
|||
//~^ ERROR E0038
|
||||
//~| ERROR E0038
|
||||
//~| ERROR E0277
|
||||
//~| WARNING E0038
|
||||
}
|
||||
|
|
|
|||
33
src/test/compile-fail/wf-method-late-bound-regions.rs
Normal file
33
src/test/compile-fail/wf-method-late-bound-regions.rs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
// 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.
|
||||
|
||||
// A method's receiver must be well-formed, even if it has late-bound regions.
|
||||
// Because of this, a method's substs being well-formed does not imply that
|
||||
// the method's implied bounds are met.
|
||||
|
||||
struct Foo<'b>(Option<&'b ()>);
|
||||
|
||||
trait Bar<'b> {
|
||||
fn xmute<'a>(&'a self, u: &'b u32) -> &'a u32;
|
||||
}
|
||||
|
||||
impl<'b> Bar<'b> for Foo<'b> {
|
||||
fn xmute<'a>(&'a self, u: &'b u32) -> &'a u32 { u }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let f = Foo(None);
|
||||
let f2 = f;
|
||||
let dangling = {
|
||||
let pointer = Box::new(42);
|
||||
f2.xmute(&pointer) //~ ERROR `pointer` does not live long enough
|
||||
};
|
||||
println!("{}", dangling);
|
||||
}
|
||||
84
src/test/compile-fail/wf-misc-methods-issue-28609.rs
Normal file
84
src/test/compile-fail/wf-misc-methods-issue-28609.rs
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
// 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.
|
||||
|
||||
// check that misc. method calls are well-formed
|
||||
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::{Deref, Shl};
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
struct S<'a, 'b: 'a> {
|
||||
marker: PhantomData<&'a &'b ()>,
|
||||
bomb: Option<&'b u32>
|
||||
}
|
||||
|
||||
type S2<'a> = S<'a, 'a>;
|
||||
|
||||
impl<'a, 'b> S<'a, 'b> {
|
||||
fn transmute_inherent(&self, a: &'b u32) -> &'a u32 {
|
||||
a
|
||||
}
|
||||
}
|
||||
|
||||
fn return_dangling_pointer_inherent(s: S2) -> &u32 {
|
||||
let s = s;
|
||||
s.transmute_inherent(&mut 42) //~ ERROR does not live long enough
|
||||
}
|
||||
|
||||
impl<'a, 'b> Deref for S<'a, 'b> {
|
||||
type Target = &'a u32;
|
||||
fn deref(&self) -> &&'a u32 {
|
||||
self.bomb.as_ref().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
fn return_dangling_pointer_coerce(s: S2) -> &u32 {
|
||||
let four = 4;
|
||||
let mut s = s;
|
||||
s.bomb = Some(&four); //~ ERROR does not live long enough
|
||||
&s
|
||||
}
|
||||
|
||||
fn return_dangling_pointer_unary_op(s: S2) -> &u32 {
|
||||
let four = 4;
|
||||
let mut s = s;
|
||||
s.bomb = Some(&four); //~ ERROR does not live long enough
|
||||
&*s
|
||||
}
|
||||
|
||||
impl<'a, 'b> Shl<&'b u32> for S<'a, 'b> {
|
||||
type Output = &'a u32;
|
||||
fn shl(self, t: &'b u32) -> &'a u32 { t }
|
||||
}
|
||||
|
||||
fn return_dangling_pointer_binary_op(s: S2) -> &u32 {
|
||||
let s = s;
|
||||
s << &mut 3 //~ ERROR does not live long enough
|
||||
}
|
||||
|
||||
fn return_dangling_pointer_method(s: S2) -> &u32 {
|
||||
let s = s;
|
||||
s.shl(&mut 3) //~ ERROR does not live long enough
|
||||
}
|
||||
|
||||
fn return_dangling_pointer_ufcs(s: S2) -> &u32 {
|
||||
let s = s;
|
||||
S2::shl(s, &mut 3) //~ ERROR does not live long enough
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let s = S { marker: PhantomData, bomb: None };
|
||||
let _inherent_dp = return_dangling_pointer_inherent(s);
|
||||
let _coerce_dp = return_dangling_pointer_coerce(s);
|
||||
let _unary_dp = return_dangling_pointer_unary_op(s);
|
||||
let _binary_dp = return_dangling_pointer_binary_op(s);
|
||||
let _method_dp = return_dangling_pointer_method(s);
|
||||
let _ufcs_dp = return_dangling_pointer_ufcs(s);
|
||||
}
|
||||
64
src/test/compile-fail/wf-static-method.rs
Normal file
64
src/test/compile-fail/wf-static-method.rs
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
// 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.
|
||||
|
||||
// check that static methods don't get to assume their trait-ref
|
||||
// is well-formed.
|
||||
// FIXME(#27579): this is just a bug. However, our checking with
|
||||
// static inherent methods isn't quite working - need to
|
||||
// fix that before removing the check.
|
||||
|
||||
trait Foo<'a, 'b, T>: Sized {
|
||||
fn make_me() -> Self { loop {} }
|
||||
fn static_evil(u: &'b u32) -> &'a u32;
|
||||
}
|
||||
|
||||
struct Evil<'a, 'b: 'a>(Option<&'a &'b ()>);
|
||||
|
||||
impl<'a, 'b> Foo<'a, 'b, Evil<'a, 'b>> for () {
|
||||
fn make_me() -> Self { }
|
||||
fn static_evil(u: &'b u32) -> &'a u32 {
|
||||
u //~ ERROR cannot infer an appropriate lifetime
|
||||
}
|
||||
}
|
||||
|
||||
struct IndirectEvil<'a, 'b: 'a>(Option<&'a &'b ()>);
|
||||
|
||||
impl<'a, 'b> Foo<'a, 'b, ()> for IndirectEvil<'a, 'b> {
|
||||
fn make_me() -> Self { IndirectEvil(None) }
|
||||
fn static_evil(u: &'b u32) -> &'a u32 {
|
||||
let me = Self::make_me(); //~ ERROR lifetime bound not satisfied
|
||||
loop {} // (`me` could be used for the lifetime transmute).
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b> Evil<'a, 'b> {
|
||||
fn inherent_evil(u: &'b u32) -> &'a u32 {
|
||||
u //~ ERROR cannot infer an appropriate lifetime
|
||||
}
|
||||
}
|
||||
|
||||
// while static methods don't get to *assume* this, we still
|
||||
// *check* that they hold.
|
||||
|
||||
fn evil<'a, 'b>(b: &'b u32) -> &'a u32 {
|
||||
<()>::static_evil(b) //~ ERROR cannot infer an appropriate lifetime
|
||||
}
|
||||
|
||||
fn indirect_evil<'a, 'b>(b: &'b u32) -> &'a u32 {
|
||||
<IndirectEvil>::static_evil(b)
|
||||
//~^ ERROR cannot infer an appropriate lifetime
|
||||
}
|
||||
|
||||
fn inherent_evil<'a, 'b>(b: &'b u32) -> &'a u32 {
|
||||
<Evil>::inherent_evil(b) // bug? shouldn't this be an error
|
||||
}
|
||||
|
||||
|
||||
fn main() {}
|
||||
Loading…
Add table
Add a link
Reference in a new issue