-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Better closure error message #42443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Better closure error message #42443
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9d188e6
Better closure error message
tommyip 9cbb5f9
Add ui tests for issue 26046
tommyip 94c808c
Update closure errors to emit context for FnMut
tommyip 2c282b8
Add additional ui tests for issue 26046
tommyip b1b6490
Group closure context ui tests
tommyip 345b833
Cover all cases in closure errors
tommyip File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One minor point -- in this arm, we emit neither the new note nor the old one. Seems like the old one might still be helpful?
Actually, maybe we should always emit the old note -- but instead of a note, just make it a
span_label
? Seems like it's useful information either way. In other words, the old note tells you "why it has to beFn
", and the new note tells you why it cannot be...If you agree, maybe move the body of the
else
below out and above thisif
, and convertspan_note
tospan_label
(so that it prints within one code snippet)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I didn't think about that!