auto merge of #7354 : bblum/rust/trait-bounds, r=pcwalton

r? @nikomatsakis
This commit is contained in:
bors 2013-06-26 17:37:29 -07:00
commit 36d7f601d8
33 changed files with 361 additions and 144 deletions

View file

@ -12,7 +12,7 @@ struct X {
field: @fn:Copy(),
}
fn foo(blk: @fn()) -> X {
fn foo(blk: @fn:()) -> X {
return X { field: blk }; //~ ERROR expected bounds `Copy` but found no bounds
}

View file

@ -1,5 +1,5 @@
fn take_any(_: &fn()) {
fn take_any(_: &fn:()) {
}
fn take_copyable(_: &fn:Copy()) {
@ -11,7 +11,7 @@ fn take_copyable_owned(_: &fn:Copy+Owned()) {
fn take_const_owned(_: &fn:Const+Owned()) {
}
fn give_any(f: &fn()) {
fn give_any(f: &fn:()) {
take_any(f);
take_copyable(f); //~ ERROR expected bounds `Copy` but found no bounds
take_copyable_owned(f); //~ ERROR expected bounds `Copy+Owned` but found no bounds

View file

@ -12,7 +12,7 @@ fn foo(_x: @uint) {}
fn main() {
let x = @3u;
let _: ~fn() = || foo(x); //~ ERROR value has non-owned type `@uint`
let _: ~fn() = || foo(x); //~ ERROR value has non-owned type `@uint`
let _: ~fn() = || foo(x); //~ ERROR value has non-owned type `@uint`
let _: ~fn() = || foo(x); //~ ERROR does not fulfill `Owned`
let _: ~fn() = || foo(x); //~ ERROR does not fulfill `Owned`
let _: ~fn() = || foo(x); //~ ERROR does not fulfill `Owned`
}

View file

@ -11,7 +11,9 @@
trait foo { fn foo(&self); }
fn to_foo<T:Copy + foo>(t: T) -> @foo {
@t as @foo //~ ERROR value may contain borrowed pointers; add `'static` bound
@t as @foo
//~^ ERROR value may contain borrowed pointers; add `'static` bound
//~^^ ERROR cannot pack type
}
fn to_foo2<T:Copy + foo + 'static>(t: T) -> @foo {

View file

@ -9,8 +9,7 @@
// except according to those terms.
fn copy1<T:Copy>(t: T) -> @fn() -> T {
let result: @fn() -> T = || copy t;
//~^ ERROR value may contain borrowed pointers
let result: @fn() -> T = || copy t; //~ ERROR does not fulfill `'static`
result
}

View file

@ -32,7 +32,7 @@ fn main() {
let x = Cell::new(foo(Port(@())));
do task::spawn {
let y = x.take(); //~ ERROR value has non-owned type
let y = x.take(); //~ ERROR does not fulfill `Owned`
error!(y);
}
}

View file

@ -21,7 +21,7 @@ fn c(x: ~Foo:Const+Owned) {
b(x); //~ ERROR expected bounds `Copy+Owned`
}
fn d(x: ~Foo) {
fn d(x: ~Foo:) {
a(x); //~ ERROR found no bounds
}

View file

@ -0,0 +1,37 @@
// Copyright 2013 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.
// Tests for "default" bounds inferred for traits with no bounds list.
trait Foo {
}
fn a(_x: ~Foo) { // should be same as ~Foo:Owned
}
fn b(_x: @Foo) { // should be same as ~Foo:'static
}
fn c(_x: &'static Foo) { // should be same as &'static Foo:'static
}
fn d(x: ~Foo:Const) {
a(x); //~ ERROR expected bounds `Owned`
}
fn e(x: @Foo:Const) {
b(x); //~ ERROR expected bounds `'static`
}
fn f(x: &'static Foo:Const) {
c(x); //~ ERROR expected bounds `'static`
}
fn main() { }

View file

@ -8,11 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// xfail-pretty
// Tests correct copying of heap closures' environments.
fn foo(x: ~fn:Copy()) -> (~fn(), ~fn()) {
fn foo(x: ~fn:Copy()) -> (~fn:(), ~fn:()) {
(copy x, x)
}
fn main() {

View file

@ -8,14 +8,12 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// xfail-pretty
// Tests correct copying of heap closures' environments.
fn bar<T: Copy>(x: T) -> (T, T) {
(copy x, x)
}
fn foo(x: ~fn:Copy()) -> (~fn(), ~fn()) {
fn foo(x: ~fn:Copy()) -> (~fn:(), ~fn:()) {
bar(x)
}
fn main() {

View file

@ -14,9 +14,9 @@ impl<A:Copy> repeat<A> for @A {
fn get(&self) -> A { copy **self }
}
fn repeater<A:Copy>(v: @A) -> @repeat<A> {
fn repeater<A:Copy>(v: @A) -> @repeat:<A> {
// Note: owned kind is not necessary as A appears in the trait type
@v as @repeat<A> // No
@v as @repeat:<A> // No
}
pub fn main() {

View file

@ -22,7 +22,7 @@ impl<'self> get_ctxt<'self> for HasCtxt<'self> {
}
}
fn get_v(gc: @get_ctxt) -> uint {
fn get_v(gc: @get_ctxt:) -> uint {
gc.get_ctxt().v
}
@ -30,5 +30,5 @@ pub fn main() {
let ctxt = Ctxt { v: 22 };
let hc = HasCtxt { c: &ctxt };
assert_eq!(get_v(@hc as @get_ctxt), 22);
assert_eq!(get_v(@hc as @get_ctxt:), 22);
}

View file

@ -11,7 +11,7 @@
trait Foo {
}
fn a(_x: ~Foo) {
fn a(_x: ~Foo:) {
}
fn b(_x: ~Foo:Owned) {
@ -25,4 +25,8 @@ fn d(x: ~Foo:Owned+Copy) {
b(x);
}
fn e(x: ~Foo) { // sugar for ~Foo:Owned
b(x);
}
fn main() { }

View file

@ -0,0 +1,103 @@
// Copyright 2013 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.
// Tests that a heterogeneous list of existential types can be put inside an ARC
// and shared between tasks as long as all types fulfill Const+Owned.
// xfail-fast
extern mod extra;
use extra::arc;
use std::comm;
use std::task;
use std::cell;
trait Pet {
fn name(&self, blk: &fn(&str));
fn num_legs(&self) -> uint;
fn of_good_pedigree(&self) -> bool;
}
struct Catte {
num_whiskers: uint,
name: ~str,
}
struct Dogge {
bark_decibels: uint,
tricks_known: uint,
name: ~str,
}
struct Goldfyshe {
swim_speed: uint,
name: ~str,
}
impl Pet for Catte {
fn name(&self, blk: &fn(&str)) { blk(self.name) }
fn num_legs(&self) -> uint { 4 }
fn of_good_pedigree(&self) -> bool { self.num_whiskers >= 4 }
}
impl Pet for Dogge {
fn name(&self, blk: &fn(&str)) { blk(self.name) }
fn num_legs(&self) -> uint { 4 }
fn of_good_pedigree(&self) -> bool {
self.bark_decibels < 70 || self.tricks_known > 20
}
}
impl Pet for Goldfyshe {
fn name(&self, blk: &fn(&str)) { blk(self.name) }
fn num_legs(&self) -> uint { 0 }
fn of_good_pedigree(&self) -> bool { self.swim_speed >= 500 }
}
fn main() {
let catte = Catte { num_whiskers: 7, name: ~"alonzo_church" };
let dogge1 = Dogge { bark_decibels: 100, tricks_known: 42, name: ~"alan_turing" };
let dogge2 = Dogge { bark_decibels: 55, tricks_known: 11, name: ~"albert_einstein" };
let fishe = Goldfyshe { swim_speed: 998, name: ~"alec_guinness" };
let arc = arc::ARC(~[~catte as ~Pet:Const+Owned,
~dogge1 as ~Pet:Const+Owned,
~fishe as ~Pet:Const+Owned,
~dogge2 as ~Pet:Const+Owned]);
let (p1,c1) = comm::stream();
let arc1 = cell::Cell::new(arc.clone());
do task::spawn { check_legs(arc1.take()); c1.send(()); }
let (p2,c2) = comm::stream();
let arc2 = cell::Cell::new(arc.clone());
do task::spawn { check_names(arc2.take()); c2.send(()); }
let (p3,c3) = comm::stream();
let arc3 = cell::Cell::new(arc.clone());
do task::spawn { check_pedigree(arc3.take()); c3.send(()); }
p1.recv();
p2.recv();
p3.recv();
}
fn check_legs(arc: arc::ARC<~[~Pet:Const+Owned]>) {
let mut legs = 0;
for arc.get().iter().advance |pet| {
legs += pet.num_legs();
}
assert!(legs == 12);
}
fn check_names(arc: arc::ARC<~[~Pet:Const+Owned]>) {
for arc.get().iter().advance |pet| {
do pet.name |name| {
assert!(name[0] == 'a' as u8 && name[1] == 'l' as u8);
}
}
}
fn check_pedigree(arc: arc::ARC<~[~Pet:Const+Owned]>) {
for arc.get().iter().advance |pet| {
assert!(pet.of_good_pedigree());
}
}