rust/src/test/compile-fail/borrowck-borrow-overloaded-auto-deref.rs
Patrick Walton 6f99a27886 librustc: Implement lifetime elision.
This implements RFC 39. Omitted lifetimes in return values will now be
inferred to more useful defaults, and an error is reported if a lifetime
in a return type is omitted and one of the two lifetime elision rules
does not specify what it should be.

This primarily breaks two uncommon code patterns. The first is this:

    unsafe fn get_foo_out_of_thin_air() -> &Foo {
        ...
    }

This should be changed to:

    unsafe fn get_foo_out_of_thin_air() -> &'static Foo {
        ...
    }

The second pattern that needs to be changed is this:

    enum MaybeBorrowed<'a> {
        Borrowed(&'a str),
        Owned(String),
    }

    fn foo() -> MaybeBorrowed {
        Owned(format!("hello world"))
    }

Change code like this to:

    enum MaybeBorrowed<'a> {
        Borrowed(&'a str),
        Owned(String),
    }

    fn foo() -> MaybeBorrowed<'static> {
        Owned(format!("hello world"))
    }

Closes #15552.

[breaking-change]
2014-07-19 13:10:58 -07:00

123 lines
2.5 KiB
Rust

// 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 <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 how overloaded deref interacts with borrows when only
// Deref and not DerefMut is implemented.
use std::ops::Deref;
struct Rc<T> {
value: *const T
}
impl<T> Deref<T> for Rc<T> {
fn deref(&self) -> &T {
unsafe { &*self.value }
}
}
struct Point {
x: int,
y: int
}
impl Point {
fn get(&self) -> (int, int) {
(self.x, self.y)
}
fn set(&mut self, x: int, y: int) {
self.x = x;
self.y = y;
}
fn x_ref(&self) -> &int {
&self.x
}
fn y_mut(&mut self) -> &mut int {
&mut self.y
}
}
fn deref_imm_field(x: Rc<Point>) {
let _i = &x.y;
}
fn deref_mut_field1(x: Rc<Point>) {
let _i = &mut x.y; //~ ERROR cannot borrow
}
fn deref_mut_field2(mut x: Rc<Point>) {
let _i = &mut x.y; //~ ERROR cannot borrow
}
fn deref_extend_field(x: &Rc<Point>) -> &int {
&x.y
}
fn deref_extend_mut_field1(x: &Rc<Point>) -> &mut int {
&mut x.y //~ ERROR cannot borrow
}
fn deref_extend_mut_field2(x: &mut Rc<Point>) -> &mut int {
&mut x.y //~ ERROR cannot borrow
}
fn assign_field1<'a>(x: Rc<Point>) {
x.y = 3; //~ ERROR cannot assign
}
fn assign_field2<'a>(x: &'a Rc<Point>) {
x.y = 3; //~ ERROR cannot assign
}
fn assign_field3<'a>(x: &'a mut Rc<Point>) {
x.y = 3; //~ ERROR cannot assign
}
fn deref_imm_method(x: Rc<Point>) {
let _i = x.get();
}
fn deref_mut_method1(x: Rc<Point>) {
x.set(0, 0); //~ ERROR cannot borrow
}
fn deref_mut_method2(mut x: Rc<Point>) {
x.set(0, 0); //~ ERROR cannot borrow
}
fn deref_extend_method(x: &Rc<Point>) -> &int {
x.x_ref()
}
fn deref_extend_mut_method1(x: &Rc<Point>) -> &mut int {
x.y_mut() //~ ERROR cannot borrow
}
fn deref_extend_mut_method2(x: &mut Rc<Point>) -> &mut int {
x.y_mut() //~ ERROR cannot borrow
}
fn assign_method1<'a>(x: Rc<Point>) {
*x.y_mut() = 3; //~ ERROR cannot borrow
}
fn assign_method2<'a>(x: &'a Rc<Point>) {
*x.y_mut() = 3; //~ ERROR cannot borrow
}
fn assign_method3<'a>(x: &'a mut Rc<Point>) {
*x.y_mut() = 3; //~ ERROR cannot borrow
}
pub fn main() {}