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

inconsistent and confusing error message about first argument of assert! #122159

Open
jendrikw opened this issue Mar 7, 2024 · 0 comments · May be fixed by #122661
Open

inconsistent and confusing error message about first argument of assert! #122159

jendrikw opened this issue Mar 7, 2024 · 0 comments · May be fixed by #122661
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jendrikw
Copy link
Contributor

jendrikw commented Mar 7, 2024

Code

fn f(x: Result<i8, Box<dyn std::error::Error>>) {
    assert!(x.unwrap(), "expected Ok");      // expected `bool`, found `i8`
    assert!(x.unwrap_err(), "expected Err"); // cannot apply unary operator `!`
}

Current output

error[E0308]: mismatched types
 --> src/lib.rs:2:5
  |
2 |     assert!(x.unwrap(), "expected Ok");      // expected `bool`, found `i8`
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `i8`

error[E0600]: cannot apply unary operator `!` to type `Box<dyn std::error::Error>`
 --> src/lib.rs:3:5
  |
3 |     assert!(x.unwrap_err(), "expected Err"); // cannot apply unary operator `!` to type `Box<dyn std::error::Error>`
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot apply unary operator `!`
  |
  = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)

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

Desired output

No response

Rationale and extra context

The macro assert! is documented as taking a bool as its first parameter. Therefore I suggest the error message be expected `bool`, found `i8` in both cases.

The very different error messages are very confusing, because Result::unwrap and Result::unwrap_err are basically the same. While I understand both error messages, I do not understand why they are different.

Edit: I just realized it's probably because i8 has an implementation of std::ops::Not. Still weird.

Other cases

No response

Rust Version

rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-unknown-linux-gnu
release: 1.76.0
LLVM version: 17.0.6

Anything else?

No response

@jendrikw jendrikw added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 7, 2024
@jieyouxu jieyouxu added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label Mar 7, 2024
@estebank estebank self-assigned this Mar 17, 2024
estebank added a commit to estebank/rust that referenced this issue Mar 17, 2024
In the desugaring of `assert!`, assign the condition expression to a `bool`
biding in order to provide better type errors when passed the wrong thing.

The span will point only at the expression, and not the whole `assert!`
invokation.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091.rs:2:13
   |
LL |     assert!(1,1);
   |             ^
   |             |
   |             expected `bool`, found integer
   |             expected due to this
```

We no longer mention the expression needing to implement the `Not` trait.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091-2.rs:15:13
   |
LL |     assert!(x, x);
   |             ^
   |             |
   |             expected `bool`, found `BytePos`
   |             expected due to this
```

Fix rust-lang#122159.
estebank added a commit to estebank/rust that referenced this issue Mar 17, 2024
In the desugaring of `assert!`, assign the condition expression to a `bool`
biding in order to provide better type errors when passed the wrong thing.

The span will point only at the expression, and not the whole `assert!`
invokation.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091.rs:2:13
   |
LL |     assert!(1,1);
   |             ^
   |             |
   |             expected `bool`, found integer
   |             expected due to this
```

We no longer mention the expression needing to implement the `Not` trait.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091-2.rs:15:13
   |
LL |     assert!(x, x);
   |             ^
   |             |
   |             expected `bool`, found `BytePos`
   |             expected due to this
```

Fix rust-lang#122159.
estebank added a commit to estebank/rust that referenced this issue Mar 18, 2024
In the desugaring of `assert!`, assign the condition expression to a `bool`
biding in order to provide better type errors when passed the wrong thing.

The span will point only at the expression, and not the whole `assert!`
invokation.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091.rs:2:13
   |
LL |     assert!(1,1);
   |             ^
   |             |
   |             expected `bool`, found integer
   |             expected due to this
```

We no longer mention the expression needing to implement the `Not` trait.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091-2.rs:15:13
   |
LL |     assert!(x, x);
   |             ^
   |             |
   |             expected `bool`, found `BytePos`
   |             expected due to this
```

Fix rust-lang#122159.
estebank added a commit to estebank/rust that referenced this issue May 7, 2024
In the desugaring of `assert!`, assign the condition expression to a `bool`
biding in order to provide better type errors when passed the wrong thing.

The span will point only at the expression, and not the whole `assert!`
invokation.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091.rs:2:13
   |
LL |     assert!(1,1);
   |             ^
   |             |
   |             expected `bool`, found integer
   |             expected due to this
```

We no longer mention the expression needing to implement the `Not` trait.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091-2.rs:15:13
   |
LL |     assert!(x, x);
   |             ^
   |             |
   |             expected `bool`, found `BytePos`
   |             expected due to this
```

Fix rust-lang#122159.
estebank added a commit to estebank/rust that referenced this issue Nov 7, 2024
In the desugaring of `assert!`, assign the condition expression to a `bool`
biding in order to provide better type errors when passed the wrong thing.

The span will point only at the expression, and not the whole `assert!`
invokation.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091.rs:2:13
   |
LL |     assert!(1,1);
   |             ^
   |             |
   |             expected `bool`, found integer
   |             expected due to this
```

We no longer mention the expression needing to implement the `Not` trait.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091-2.rs:15:13
   |
LL |     assert!(x, x);
   |             ^
   |             |
   |             expected `bool`, found `BytePos`
   |             expected due to this
```

Fix rust-lang#122159.
estebank added a commit to estebank/rust that referenced this issue Jan 17, 2025
In the desugaring of `assert!`, assign the condition expression to a `bool`
biding in order to provide better type errors when passed the wrong thing.

The span will point only at the expression, and not the whole `assert!`
invokation.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091.rs:2:13
   |
LL |     assert!(1,1);
   |             ^
   |             |
   |             expected `bool`, found integer
   |             expected due to this
```

We no longer mention the expression needing to implement the `Not` trait.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091-2.rs:15:13
   |
LL |     assert!(x, x);
   |             ^
   |             |
   |             expected `bool`, found `BytePos`
   |             expected due to this
```

Fix rust-lang#122159.
estebank added a commit to estebank/rust that referenced this issue Jan 22, 2025
In the desugaring of `assert!`, assign the condition expression to a `bool`
biding in order to provide better type errors when passed the wrong thing.

The span will point only at the expression, and not the whole `assert!`
invokation.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091.rs:2:13
   |
LL |     assert!(1,1);
   |             ^
   |             |
   |             expected `bool`, found integer
   |             expected due to this
```

We no longer mention the expression needing to implement the `Not` trait.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091-2.rs:15:13
   |
LL |     assert!(x, x);
   |             ^
   |             |
   |             expected `bool`, found `BytePos`
   |             expected due to this
```

Fix rust-lang#122159.
estebank added a commit to estebank/rust that referenced this issue Jan 23, 2025
In the desugaring of `assert!`, we now expand to a `match` expression
instead of `if !cond {..}`.

The span of incorrect conditions will point only at the expression, and not
the whole `assert!` invocation.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091.rs:2:13
   |
LL |     assert!(1,1);
   |             ^ expected `bool`, found integer
```

We no longer mention the expression needing to implement the `Not` trait.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091-2.rs:15:13
   |
LL |     assert!(x, x);
   |             ^ expected `bool`, found `BytePos`
```

`assert!(val)` now desugars to:

```rust
match val {
    true => {},
    _ => $crate::panic::panic_2021!(),
}
```

Fix rust-lang#122159.

We make some minor changes to some diagnostics to avoid span overlap on
type mismatch or inverted "expected"/"found" on type errors.

We remove some unnecessary parens from core, alloc and miri.
estebank added a commit to estebank/rust that referenced this issue Jan 28, 2025
In the desugaring of `assert!`, we now expand to a `match` expression
instead of `if !cond {..}`.

The span of incorrect conditions will point only at the expression, and not
the whole `assert!` invocation.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091.rs:2:13
   |
LL |     assert!(1,1);
   |             ^ expected `bool`, found integer
```

We no longer mention the expression needing to implement the `Not` trait.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091-2.rs:15:13
   |
LL |     assert!(x, x);
   |             ^ expected `bool`, found `BytePos`
```

`assert!(val)` now desugars to:

```rust
match val {
    true => {},
    _ => $crate::panic::panic_2021!(),
}
```

Fix rust-lang#122159.

We make some minor changes to some diagnostics to avoid span overlap on
type mismatch or inverted "expected"/"found" on type errors.

We remove some unnecessary parens from core, alloc and miri.
estebank added a commit to estebank/rust that referenced this issue Feb 2, 2025
In the desugaring of `assert!`, we now expand to a `match` expression
instead of `if !cond {..}`.

The span of incorrect conditions will point only at the expression, and not
the whole `assert!` invocation.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091.rs:2:13
   |
LL |     assert!(1,1);
   |             ^ expected `bool`, found integer
```

We no longer mention the expression needing to implement the `Not` trait.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091-2.rs:15:13
   |
LL |     assert!(x, x);
   |             ^ expected `bool`, found `BytePos`
```

`assert!(val)` now desugars to:

```rust
match val {
    true => {},
    _ => $crate::panic::panic_2021!(),
}
```

Fix rust-lang#122159.

We make some minor changes to some diagnostics to avoid span overlap on
type mismatch or inverted "expected"/"found" on type errors.

We remove some unnecessary parens from core, alloc and miri.
estebank added a commit to estebank/rust that referenced this issue Feb 5, 2025
In the desugaring of `assert!`, we now expand to a `match` expression
instead of `if !cond {..}`.

The span of incorrect conditions will point only at the expression, and not
the whole `assert!` invocation.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091.rs:2:13
   |
LL |     assert!(1,1);
   |             ^ expected `bool`, found integer
```

We no longer mention the expression needing to implement the `Not` trait.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091-2.rs:15:13
   |
LL |     assert!(x, x);
   |             ^ expected `bool`, found `BytePos`
```

`assert!(val)` now desugars to:

```rust
match val {
    true => {},
    _ => $crate::panic::panic_2021!(),
}
```

Fix rust-lang#122159.

We make some minor changes to some diagnostics to avoid span overlap on
type mismatch or inverted "expected"/"found" on type errors.

We remove some unnecessary parens from core, alloc and miri.
estebank added a commit to estebank/rust that referenced this issue Feb 6, 2025
In the desugaring of `assert!`, we now expand to a `match` expression
instead of `if !cond {..}`.

The span of incorrect conditions will point only at the expression, and not
the whole `assert!` invocation.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091.rs:2:13
   |
LL |     assert!(1,1);
   |             ^ expected `bool`, found integer
```

We no longer mention the expression needing to implement the `Not` trait.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091-2.rs:15:13
   |
LL |     assert!(x, x);
   |             ^ expected `bool`, found `BytePos`
```

`assert!(val)` now desugars to:

```rust
match val {
    true => {},
    _ => $crate::panic::panic_2021!(),
}
```

Fix rust-lang#122159.

We make some minor changes to some diagnostics to avoid span overlap on
type mismatch or inverted "expected"/"found" on type errors.

We remove some unnecessary parens from core, alloc and miri.
estebank added a commit to estebank/rust that referenced this issue Feb 10, 2025
In the desugaring of `assert!`, we now expand to a `match` expression
instead of `if !cond {..}`.

The span of incorrect conditions will point only at the expression, and not
the whole `assert!` invocation.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091.rs:2:13
   |
LL |     assert!(1,1);
   |             ^ expected `bool`, found integer
```

We no longer mention the expression needing to implement the `Not` trait.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091-2.rs:15:13
   |
LL |     assert!(x, x);
   |             ^ expected `bool`, found `BytePos`
```

`assert!(val)` now desugars to:

```rust
match val {
    true => {},
    _ => $crate::panic::panic_2021!(),
}
```

Fix rust-lang#122159.

We make some minor changes to some diagnostics to avoid span overlap on
type mismatch or inverted "expected"/"found" on type errors.

We remove some unnecessary parens from core, alloc and miri.
estebank added a commit to estebank/rust that referenced this issue Feb 14, 2025
In the desugaring of `assert!`, we now expand to a `match` expression
instead of `if !cond {..}`.

The span of incorrect conditions will point only at the expression, and not
the whole `assert!` invocation.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091.rs:2:13
   |
LL |     assert!(1,1);
   |             ^ expected `bool`, found integer
```

We no longer mention the expression needing to implement the `Not` trait.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091-2.rs:15:13
   |
LL |     assert!(x, x);
   |             ^ expected `bool`, found `BytePos`
```

`assert!(val)` now desugars to:

```rust
match val {
    true => {},
    _ => $crate::panic::panic_2021!(),
}
```

Fix rust-lang#122159.

We make some minor changes to some diagnostics to avoid span overlap on
type mismatch or inverted "expected"/"found" on type errors.

We remove some unnecessary parens from core, alloc and miri.
estebank added a commit to estebank/rust that referenced this issue Feb 14, 2025
In the desugaring of `assert!`, we now expand to a `match` expression
instead of `if !cond {..}`.

The span of incorrect conditions will point only at the expression, and not
the whole `assert!` invocation.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091.rs:2:13
   |
LL |     assert!(1,1);
   |             ^ expected `bool`, found integer
```

We no longer mention the expression needing to implement the `Not` trait.

```
error[E0308]: mismatched types
  --> $DIR/issue-14091-2.rs:15:13
   |
LL |     assert!(x, x);
   |             ^ expected `bool`, found `BytePos`
```

`assert!(val)` now desugars to:

```rust
match val {
    true => {},
    _ => $crate::panic::panic_2021!(),
}
```

Fix rust-lang#122159.

We make some minor changes to some diagnostics to avoid span overlap on
type mismatch or inverted "expected"/"found" on type errors.

We remove some unnecessary parens from core, alloc and miri.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants