Skip to content
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

Closed
OleStrohm opened this issue Mar 3, 2022 · 2 comments
Closed

Unary operators do not add bounds to closure, instead just errors #94543

OleStrohm opened this issue Mar 3, 2022 · 2 comments
Labels
C-bug Category: This is a bug.

Comments

@OleStrohm
Copy link
Contributor

OleStrohm commented Mar 3, 2022

I tried this code:

(|a| -a)(42)

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.

fn closure_unary<T>() -> impl Fn(T) -> T {
    |a| -a
}

fn closure_binary<T>() -> impl Fn(T) -> T {
    |a| a+a
}

I expected to see this happen:
I expected both of these to have errors like closure_binary:

error[E0369]: cannot add `T` to `T`
 --> src/main.rs:6:10
  |
6 |     |a| a+a
  |         -^- T
  |         |
  |         T
  |
help: consider restricting type parameter `T`
  |
5 | fn closure_binary<T: std::ops::Add<Output = T>>() -> impl Fn(T) -> T {
  |                    +++++++++++++++++++++++++++

Instead, this happened:
Only closure_binary has such an error. Instead closure_unary has:

error[E0600]: cannot apply unary operator `-` to type `T`
 --> src/main.rs:2:9
  |
2 |     |a| -a
  |         ^^ cannot apply unary operator `-`

Meta

rustc --version --verbose:

rustc 1.59.0 (9d1b2106e 2022-02-23)
binary: rustc
commit-hash: 9d1b2106e23b1abd32fce1f17267604a5102f57a
commit-date: 2022-02-23
host: x86_64-unknown-linux-gnu
release: 1.59.0
LLVM version: 13.0.0
@OleStrohm OleStrohm added the C-bug Category: This is a bug. label Mar 3, 2022
@OleStrohm OleStrohm changed the title Unary minus does not add bounds to closure, instead just errors Unary operators do not add bounds to closure, instead just errors Mar 3, 2022
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.
@frank-king
Copy link
Contributor

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
}
error[E0600]: cannot apply unary operator `-` to type `T`
 --> src/lib.rs:2:9
  |
2 |     |a| -a
  |         ^^ cannot apply unary operator `-`
  |
help: consider restricting type parameter `T`
  |
1 | fn closure_unary<T: std::ops::Neg<Output = T>>() -> impl Fn(T) -> T {
  |                   +++++++++++++++++++++++++++

error[E0369]: cannot add `T` to `T`
 --> src/lib.rs:6:10
  |
6 |     |a| a+a
  |         -^- T
  |         |
  |         T
  |
help: consider restricting type parameter `T`
  |
5 | fn closure_binary<T: std::ops::Add<Output = T>>() -> impl Fn(T) -> T {
  |                    +++++++++++++++++++++++++++

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

@OleStrohm
Copy link
Contributor Author

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
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants