Skip to content

Commit

Permalink
Fix tests after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewjasper committed Feb 14, 2020
1 parent edddb62 commit d863978
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 51 deletions.
3 changes: 2 additions & 1 deletion src/libstd/future.rs
Expand Up @@ -16,9 +16,10 @@ pub use core::future::*;
///
/// This function returns a `GenFuture` underneath, but hides it in `impl Trait` to give
/// better error messages (`impl Future` rather than `GenFuture<[closure.....]>`).
// This is `const` to avoid extra errors after we recover from `const async fn`
#[doc(hidden)]
#[unstable(feature = "gen_future", issue = "50547")]
pub fn from_generator<T: Generator<Yield = ()>>(x: T) -> impl Future<Output = T::Return> {
pub const fn from_generator<T: Generator<Yield = ()>>(x: T) -> impl Future<Output = T::Return> {
GenFuture(x)
}

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/async-await/no-const-async.rs
Expand Up @@ -3,3 +3,4 @@

pub const async fn x() {}
//~^ ERROR functions cannot be both `const` and `async`
//~| ERROR `impl Trait` in const fn is unstable
12 changes: 11 additions & 1 deletion src/test/ui/async-await/no-const-async.stderr
Expand Up @@ -7,5 +7,15 @@ LL | pub const async fn x() {}
| | `async` because of this
| `const` because of this

error: aborting due to previous error
error[E0723]: `impl Trait` in const fn is unstable
--> $DIR/no-const-async.rs:4:24
|
LL | pub const async fn x() {}
| ^
|
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
= help: add `#![feature(const_fn)]` to the crate attributes to enable

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0723`.
14 changes: 14 additions & 0 deletions src/test/ui/borrowck/borrow-immutable-upvar-mutation-impl-trait.rs
@@ -0,0 +1,14 @@
#![feature(unboxed_closures)]

// Tests that we can't assign to or mutably borrow upvars from `Fn`
// closures (issue #17780)

fn main() {}

fn bar() -> impl Fn() -> usize {
let mut x = 0;
move || {
x += 1; //~ ERROR cannot assign
x
}
}
@@ -0,0 +1,16 @@
error[E0594]: cannot assign to `x`, as it is a captured variable in a `Fn` closure
--> $DIR/borrow-immutable-upvar-mutation-impl-trait.rs:11:9
|
LL | fn bar() -> impl Fn() -> usize {
| --- ------------------ change this to return `FnMut` instead of `Fn`
LL | let mut x = 0;
LL | / move || {
LL | | x += 1;
| | ^^^^^^ cannot assign
LL | | x
LL | | }
| |_____- in this closure

error: aborting due to previous error

For more information about this error, try `rustc --explain E0594`.
26 changes: 14 additions & 12 deletions src/test/ui/borrowck/borrow-immutable-upvar-mutation.rs
Expand Up @@ -3,10 +3,16 @@
// Tests that we can't assign to or mutably borrow upvars from `Fn`
// closures (issue #17780)

fn set(x: &mut usize) { *x = 5; }
fn set(x: &mut usize) {
*x = 5;
}

fn to_fn<A,F:Fn<A>>(f: F) -> F { f }
fn to_fn_mut<A,F:FnMut<A>>(f: F) -> F { f }
fn to_fn<A, F: Fn<A>>(f: F) -> F {
f
}
fn to_fn_mut<A, F: FnMut<A>>(f: F) -> F {
f
}

fn main() {
// By-ref captures
Expand All @@ -33,7 +39,11 @@ fn main() {
let _g = to_fn(move || set(&mut y)); //~ ERROR cannot borrow

let mut z = 0;
let _h = to_fn_mut(move || { set(&mut z); to_fn(move || z = 42); }); //~ ERROR cannot assign
let _h = to_fn_mut(move || {
set(&mut z);
to_fn(move || z = 42);
//~^ ERROR cannot assign
});
}
}

Expand All @@ -44,11 +54,3 @@ fn foo() -> Box<dyn Fn() -> usize> {
x
})
}

fn bar() -> impl Fn() -> usize {
let mut x = 0;
move || {
x += 1; //~ ERROR cannot assign
x
}
}
61 changes: 24 additions & 37 deletions src/test/ui/borrowck/borrow-immutable-upvar-mutation.stderr
@@ -1,71 +1,71 @@
error[E0594]: cannot assign to `x`, as it is a captured variable in a `Fn` closure
--> $DIR/borrow-immutable-upvar-mutation.rs:15:27
--> $DIR/borrow-immutable-upvar-mutation.rs:21:27
|
LL | fn to_fn<A,F:Fn<A>>(f: F) -> F { f }
| - change this to accept `FnMut` instead of `Fn`
LL | fn to_fn<A, F: Fn<A>>(f: F) -> F {
| - change this to accept `FnMut` instead of `Fn`
...
LL | let _f = to_fn(|| x = 42);
| ----- ^^^^^^ cannot assign
| |
| expects `Fn` instead of `FnMut`

error[E0596]: cannot borrow `y` as mutable, as it is a captured variable in a `Fn` closure
--> $DIR/borrow-immutable-upvar-mutation.rs:18:31
--> $DIR/borrow-immutable-upvar-mutation.rs:24:31
|
LL | fn to_fn<A,F:Fn<A>>(f: F) -> F { f }
| - change this to accept `FnMut` instead of `Fn`
LL | fn to_fn<A, F: Fn<A>>(f: F) -> F {
| - change this to accept `FnMut` instead of `Fn`
...
LL | let _g = to_fn(|| set(&mut y));
| ----- ^^^^^^ cannot borrow as mutable
| |
| expects `Fn` instead of `FnMut`

error[E0594]: cannot assign to `z`, as it is a captured variable in a `Fn` closure
--> $DIR/borrow-immutable-upvar-mutation.rs:23:22
--> $DIR/borrow-immutable-upvar-mutation.rs:29:22
|
LL | fn to_fn<A,F:Fn<A>>(f: F) -> F { f }
| - change this to accept `FnMut` instead of `Fn`
LL | fn to_fn<A, F: Fn<A>>(f: F) -> F {
| - change this to accept `FnMut` instead of `Fn`
...
LL | to_fn(|| z = 42);
| ----- ^^^^^^ cannot assign
| |
| expects `Fn` instead of `FnMut`

error[E0594]: cannot assign to `x`, as it is a captured variable in a `Fn` closure
--> $DIR/borrow-immutable-upvar-mutation.rs:30:32
--> $DIR/borrow-immutable-upvar-mutation.rs:36:32
|
LL | fn to_fn<A,F:Fn<A>>(f: F) -> F { f }
| - change this to accept `FnMut` instead of `Fn`
LL | fn to_fn<A, F: Fn<A>>(f: F) -> F {
| - change this to accept `FnMut` instead of `Fn`
...
LL | let _f = to_fn(move || x = 42);
| ----- ^^^^^^ cannot assign
| |
| expects `Fn` instead of `FnMut`

error[E0596]: cannot borrow `y` as mutable, as it is a captured variable in a `Fn` closure
--> $DIR/borrow-immutable-upvar-mutation.rs:33:36
--> $DIR/borrow-immutable-upvar-mutation.rs:39:36
|
LL | fn to_fn<A,F:Fn<A>>(f: F) -> F { f }
| - change this to accept `FnMut` instead of `Fn`
LL | fn to_fn<A, F: Fn<A>>(f: F) -> F {
| - change this to accept `FnMut` instead of `Fn`
...
LL | let _g = to_fn(move || set(&mut y));
| ----- ^^^^^^ cannot borrow as mutable
| |
| expects `Fn` instead of `FnMut`

error[E0594]: cannot assign to `z`, as it is a captured variable in a `Fn` closure
--> $DIR/borrow-immutable-upvar-mutation.rs:36:65
--> $DIR/borrow-immutable-upvar-mutation.rs:44:27
|
LL | fn to_fn<A,F:Fn<A>>(f: F) -> F { f }
| - change this to accept `FnMut` instead of `Fn`
LL | fn to_fn<A, F: Fn<A>>(f: F) -> F {
| - change this to accept `FnMut` instead of `Fn`
...
LL | let _h = to_fn_mut(move || { set(&mut z); to_fn(move || z = 42); });
| ----- ^^^^^^ cannot assign
| |
| expects `Fn` instead of `FnMut`
LL | to_fn(move || z = 42);
| ----- ^^^^^^ cannot assign
| |
| expects `Fn` instead of `FnMut`

error[E0594]: cannot assign to `x`, as it is a captured variable in a `Fn` closure
--> $DIR/borrow-immutable-upvar-mutation.rs:43:9
--> $DIR/borrow-immutable-upvar-mutation.rs:53:9
|
LL | fn foo() -> Box<dyn Fn() -> usize> {
| --- ---------------------- change this to return `FnMut` instead of `Fn`
Expand All @@ -78,20 +78,7 @@ LL | | x
LL | | })
| |_____- in this closure

error[E0594]: cannot assign to `x`, as it is a captured variable in a `Fn` closure
--> $DIR/borrow-immutable-upvar-mutation.rs:51:9
|
LL | fn bar() -> impl Fn() -> usize {
| --- ------------------ change this to return `FnMut` instead of `Fn`
LL | let mut x = 0;
LL | / move || {
LL | | x += 1;
| | ^^^^^^ cannot assign
LL | | x
LL | | }
| |_____- in this closure

error: aborting due to 8 previous errors
error: aborting due to 7 previous errors

Some errors have detailed explanations: E0594, E0596.
For more information about an error, try `rustc --explain E0594`.

0 comments on commit d863978

Please sign in to comment.