Skip to content

Commit

Permalink
Add basic test cases for closure bounds. (rust-lang#3569)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblum committed Jun 13, 2013
1 parent 7f0916c commit efc3b1b
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/test/compile-fail/closure-bounds-cant-promote-superkind-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.
//

This comment has been minimized.

Copy link
@nikomatsakis

nikomatsakis Jun 14, 2013

This test is not needed, see closure-bounds-subtype.rs

// 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.

fn bar(_blk: &fn:Copy()) {
}

fn foo(blk: &fn()) {
bar(blk); //~ ERROR expected bounds `Copy` but found no bounds
}

fn main() {
}
19 changes: 19 additions & 0 deletions src/test/compile-fail/closure-bounds-cant-promote-superkind-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.

fn bar(_blk: &fn:Const+Owned()) {
}

fn foo(blk: &fn:Copy+Owned()) {
bar(blk); //~ ERROR expected bounds `Owned+Const` but found bounds `Copy+Owned`

This comment has been minimized.

Copy link
@nikomatsakis

nikomatsakis Jun 14, 2013

Maybe add this case to closure-bounds-subtype.rs

}

fn main() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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.

struct X {
field: @fn:Copy(),
}

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

fn main() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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.

use std::comm;

fn foo(blk: ~fn:Copy()) {
blk();
}

fn main() {
let (p,c) = comm::stream();
do foo { // shouldn't be legal
c.send(()); //~ ERROR cannot capture variable of type `std::comm::Chan<()>`, which does not fulfill `Copy`, in a bounded closure
//~^ NOTE this closure's environment must satisfy `Copy`

This comment has been minimized.

Copy link
@nikomatsakis

nikomatsakis Jun 14, 2013

NIT: I've found over time it's annoying to include NOTEs in tests. The text changes too often. Similarly it's often better to just keep the error message brief, like "//~ ERROR does not fulfill Copy"

}
p.recv();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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.

fn bar(blk: &fn:'static()) {
}

fn foo(x: &()) {
do bar {
let _ = x; //~ ERROR cannot capture variable of type `&()`, which does not fulfill `'static`, in a bounded closure

This comment has been minimized.

Copy link
@nikomatsakis

nikomatsakis Jun 14, 2013

NIT: Ditto

//~^ NOTE this closure's environment must satisfy `'static`
}
}

fn main() {
}
23 changes: 23 additions & 0 deletions src/test/run-pass/closure-bounds-can-capture-chan.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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.

use std::comm;

fn foo(blk: ~fn:Owned()) {
blk();
}

fn main() {
let (p,c) = comm::stream();
do foo { // shouldn't be legal

This comment has been minimized.

Copy link
@nikomatsakis

nikomatsakis Jun 14, 2013

NIT: Bad comment

c.send(());
}
p.recv();
}
19 changes: 19 additions & 0 deletions src/test/run-pass/closure-bounds-promote-subkind-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.

fn bar(_blk: &fn()) {
}

fn foo(blk: &fn:Copy()) {
bar(blk);
}

fn main() {

This comment has been minimized.

Copy link
@nikomatsakis

nikomatsakis Jun 14, 2013

NIT: It's generally better to make run-pass tests actually do something. Maybe have the closures return a value and assert that it holds? Otherwise, this test is subsumed by compile-fail/closure-bounds-subtype.rs

}
19 changes: 19 additions & 0 deletions src/test/run-pass/closure-bounds-promote-subkind-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.

fn bar(_blk: &fn:Const+Owned()) {
}

fn foo(blk: &fn:Const+Copy+Owned()) {
bar(blk);

This comment has been minimized.

Copy link
@nikomatsakis

nikomatsakis Jun 14, 2013

NIT: Ditto

}

fn main() {
}
20 changes: 20 additions & 0 deletions src/test/run-pass/closure-bounds-promote-subkind-in-struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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.

struct X {
field: @fn(),
}

fn foo(blk: @fn:Copy()) -> X {
return X { field: blk };

This comment has been minimized.

Copy link
@nikomatsakis

nikomatsakis Jun 14, 2013

NIT: Ditto.

}

fn main() {
}

0 comments on commit efc3b1b

Please sign in to comment.