-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Poor Trailing Semicolon Error in -> impl Trait Function #54771
Comments
Note: there's a similar issue in closure return types: trait Bar {}
impl Bar for u8 {}
fn bar<R: Bar>(_: impl Fn() -> R) {}
fn main() {
bar(|| { 5u8; })
} gives error[E0277]: the trait bound `(): Bar` is not satisfied
--> src/main.rs:6:5
|
6 | bar(|| { 5u8; })
| ^^^ the trait `Bar` is not implemented for `()`
|
note: required by `bar`
--> src/main.rs:4:1
|
4 | fn bar<R: Bar>(_: impl Fn() -> R) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error |
rust/src/librustc_typeck/check/mod.rs Lines 4917 to 4919 in 4cf1176
DEBUG LOG:
as it shows the checking should move to lint_remove_semi before the 2nd time of calling |
Disregard this, we actually do the right thing for this case:
|
On return type `impl Trait` for block with no expr point at last semi Partial solution, doesn't actually validate that the last statement in the function body can satisfy the trait bound, but it's a good incremental improvement over the status quo. ``` error[E0277]: the trait bound `(): Bar` is not satisfied --> $DIR/impl-trait-return-trailing-semicolon.rs:3:13 | LL | fn foo() -> impl Bar { | ^^^^^^^^ the trait `Bar` is not implemented for `()` LL | 5; | - consider removing this semicolon | = note: the return type of a function must have a statically known size ``` Partially addresses rust-lang#54771.
On return type `impl Trait` for block with no expr point at last semi Partial solution, doesn't actually validate that the last statement in the function body can satisfy the trait bound, but it's a good incremental improvement over the status quo. ``` error[E0277]: the trait bound `(): Bar` is not satisfied --> $DIR/impl-trait-return-trailing-semicolon.rs:3:13 | LL | fn foo() -> impl Bar { | ^^^^^^^^ the trait `Bar` is not implemented for `()` LL | 5; | - consider removing this semicolon | = note: the return type of a function must have a statically known size ``` Partially addresses rust-lang#54771.
It still needs checking that the last statement's expr can actually conform to the trait, but the naïve behavior is there. |
This opaque Ideally, the error message in this case should be something like: ...
socket
.for_each(|message| {
println!("recieved: {:?}", message) // error: mismatched types: expected `IntoFuture` but found `()`
})
.map_err(|e| println!("read error: {:?}", e))
... But is this possible at all? |
I also found this error message confusing and referred here via stackoverflow question Setting aside whether it is possible, it would have been really helpful to see an error like:
That's kind of wordy, but thought another suggestion might help someone to come up with an improvement here. The key thing is issue for me is that the annotation directs my attention to the top of the function (which is good since I would need to look that up in the docs), but I didn't have the experience to recognize that it was referring to an incorrect type of the return value and had forgotten that a function that ends with println! is actually returning |
@ultrasaurus thanks for the suggestion! This is something we certainly want to improve but because reasons (gesticulates wildly) we are limited in what we can do today. We have some planned work that will help tremendously, I think and the minimized case in your question is very useful. @oli-obk another case of the "pointing at call instead of argument making things confusing" problem that might get a great improvement by keeping the right |
Upvoting for frustration. |
Don't suggest it if the last statement doesn't have a semicolon Fixes rust-lang#81098 See also rust-lang#54771 for why this suggestion was added
Refine "remove semicolon" suggestion in trait selection Don't suggest it if the last statement doesn't have a semicolon Fixes rust-lang#81098 See also rust-lang#54771 for why this suggestion was added
Don't suggest it if the last statement doesn't have a semicolon Fixes rust-lang#81098 See also rust-lang#54771 for why this suggestion was added
…bank Only suggest removing semicolon when expression is compatible with `impl Trait` rust-lang#54771 (comment) > It still needs checking that the last statement's expr can actually conform to the trait, but the naïve behavior is there. Only suggest removing a semicolon when the type behind the semicolon actually implements the trait in an RPIT `-> impl Trait`. Also upgrade the label that suggests removing the semicolon to a suggestion (should it be verbose?). cc rust-lang#54771
…bank Only suggest removing semicolon when expression is compatible with `impl Trait` rust-lang#54771 (comment) > It still needs checking that the last statement's expr can actually conform to the trait, but the naïve behavior is there. Only suggest removing a semicolon when the type behind the semicolon actually implements the trait in an RPIT `-> impl Trait`. Also upgrade the label that suggests removing the semicolon to a suggestion (should it be verbose?). cc rust-lang#54771
…bank Only suggest removing semicolon when expression is compatible with `impl Trait` rust-lang#54771 (comment) > It still needs checking that the last statement's expr can actually conform to the trait, but the naïve behavior is there. Only suggest removing a semicolon when the type behind the semicolon actually implements the trait in an RPIT `-> impl Trait`. Also upgrade the label that suggests removing the semicolon to a suggestion (should it be verbose?). cc rust-lang#54771
…bank Only suggest removing semicolon when expression is compatible with `impl Trait` rust-lang#54771 (comment) > It still needs checking that the last statement's expr can actually conform to the trait, but the naïve behavior is there. Only suggest removing a semicolon when the type behind the semicolon actually implements the trait in an RPIT `-> impl Trait`. Also upgrade the label that suggests removing the semicolon to a suggestion (should it be verbose?). cc rust-lang#54771
Only suggest removing semicolon when expression is compatible with `impl Trait` rust-lang#54771 (comment) > It still needs checking that the last statement's expr can actually conform to the trait, but the naïve behavior is there. Only suggest removing a semicolon when the type behind the semicolon actually implements the trait in an RPIT `-> impl Trait`. Also upgrade the label that suggests removing the semicolon to a suggestion (should it be verbose?). cc rust-lang#54771
Current output:
|
The diagnostics that check whether the concrete return type of an
-> impl Trait
function meets the: Trait
bound should special-case the error when the concrete type is()
to suggest removing the semicolon. Without this, the errors for a misplaced semicolon are misleading:Without
impl Trait
:With
impl Trait
:The text was updated successfully, but these errors were encountered: