-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Unary operators do not add bounds to closure, instead just errors #94543
Labels
C-bug
Category: This is a bug.
Comments
Dylan-DPC
added a commit
to Dylan-DPC/rust
that referenced
this issue
Mar 22, 2022
…rain, r=petrochenkov Suggest constraining param for unary ops when missing trait impl This PR adds a suggestion of constraining param for unary ops `-` and `!` when the corresponding trait implementation is missing. Fixs rust-lang#94543. BTW, this is my first time to touch rustc, please correct me if I did anything wrong.
Fixed in the latest nightly version, see https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=a10612cc878d632b58b427b742c95f42. Is this the expected behaivor? fn closure_unary<T>() -> impl Fn(T) -> T {
|a| -a
}
fn closure_binary<T>() -> impl Fn(T) -> T {
|a| a+a
}
|
I think that's the expected output, yes. Though I think there's a separate issue here, which I think I'll submit a new issue for |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried this code:
Howevery this unexpectedly fails, requiring the type to be specified in the closure definition.
(|a| a + a)(42)
on the other hand works just fine.Digging a little deeper I discovered this related problem. It seems that the unary operator doesn't add an implicit trait bound to the closure, and instead just gives an error immediately.
This seems to be the behaviour for all unary operators, not just the unary minus.
I expected to see this happen:
I expected both of these to have errors like closure_binary:
Instead, this happened:
Only closure_binary has such an error. Instead closure_unary has:
Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: