forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#42443 - tommyip:better_closure_msg, r=nikomat…
…sakis Better closure error message Use tracked data introduced in rust-lang#42196 to provide a better closure error message by showing why a closure implements `FnOnce`. ``` error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnOnce` --> $DIR/issue_26046.rs:4:19 | 4 | let closure = move || { | ___________________^ 5 | | vec 6 | | }; | |_____^ | note: closure is `FnOnce` because it moves the variable `vec` out of its environment --> $DIR/issue_26046.rs:5:9 | 5 | vec | ^^^ error: aborting due to previous error(s) ``` Fixes rust-lang#26046 r? @nikomatsakis cc @doomrobo
- Loading branch information
Showing
7 changed files
with
116 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2017 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 foo() -> Box<Fn()> { | ||
let num = 5; | ||
|
||
let closure = || { | ||
num += 1; | ||
}; | ||
|
||
Box::new(closure) | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnMut` | ||
--> $DIR/issue-26046-fn-mut.rs:14:19 | ||
| | ||
14 | let closure = || { | ||
| ___________________^ | ||
15 | | num += 1; | ||
16 | | }; | ||
| |_____^ | ||
17 | | ||
18 | Box::new(closure) | ||
| ----------------- the requirement to implement `Fn` derives from here | ||
| | ||
note: closure is `FnMut` because it mutates the variable `num` here | ||
--> $DIR/issue-26046-fn-mut.rs:15:9 | ||
| | ||
15 | num += 1; | ||
| ^^^ | ||
|
||
error: aborting due to previous error(s) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2017 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 get_closure() -> Box<Fn() -> Vec<u8>> { | ||
let vec = vec![1u8, 2u8]; | ||
|
||
let closure = move || { | ||
vec | ||
}; | ||
|
||
Box::new(closure) | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnOnce` | ||
--> $DIR/issue-26046-fn-once.rs:14:19 | ||
| | ||
14 | let closure = move || { | ||
| ___________________^ | ||
15 | | vec | ||
16 | | }; | ||
| |_____^ | ||
17 | | ||
18 | Box::new(closure) | ||
| ----------------- the requirement to implement `Fn` derives from here | ||
| | ||
note: closure is `FnOnce` because it moves the variable `vec` out of its environment | ||
--> $DIR/issue-26046-fn-once.rs:15:9 | ||
| | ||
15 | vec | ||
| ^^^ | ||
|
||
error: aborting due to previous error(s) | ||
|
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
src/test/ui/fn_once-moved.stderr → ...est/ui/closure_context/issue-42065.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters