Skip to content

Commit

Permalink
Merge pull request #366 from dtolnay/derefbool
Browse files Browse the repository at this point in the history
Add ensure test with a Deref to bool
  • Loading branch information
dtolnay committed May 17, 2024
2 parents 52a46eb + 85f0a69 commit 3f9c220
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 15 deletions.
21 changes: 18 additions & 3 deletions tests/ui/ensure-nonbool.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
use anyhow::{ensure, Result};
use std::ops::Deref;

struct Bool(bool);

struct DerefBool(bool);

impl Deref for DerefBool {
type Target = bool;
fn deref(&self) -> &Self::Target {
&self.0
}
}

fn main() -> Result<()> {
ensure!("...");

struct Struct(bool);
let mut s = Struct(true);
let mut s = Bool(true);
match &mut s {
Struct(cond) => ensure!(cond),
Bool(cond) => ensure!(cond),
}

let db = DerefBool(true);
ensure!(db);
ensure!(&db);

Ok(())
}
50 changes: 38 additions & 12 deletions tests/ui/ensure-nonbool.stderr
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@
error[E0600]: cannot apply unary operator `!` to type `&'static str`
--> tests/ui/ensure-nonbool.rs:4:5
|
4 | ensure!("...");
| ^^^^^^^^^^^^^^ cannot apply unary operator `!`
|
= note: this error originates in the macro `$crate::__fallback_ensure` which comes from the expansion of the macro `ensure` (in Nightly builds, run with -Z macro-backtrace for more info)
--> tests/ui/ensure-nonbool.rs:16:5
|
16 | ensure!("...");
| ^^^^^^^^^^^^^^ cannot apply unary operator `!`
|
= note: this error originates in the macro `$crate::__fallback_ensure` which comes from the expansion of the macro `ensure` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0600]: cannot apply unary operator `!` to type `&mut bool`
--> tests/ui/ensure-nonbool.rs:9:25
|
9 | Struct(cond) => ensure!(cond),
| ^^^^^^^^^^^^^ cannot apply unary operator `!`
|
= note: this error originates in the macro `$crate::__fallback_ensure` which comes from the expansion of the macro `ensure` (in Nightly builds, run with -Z macro-backtrace for more info)
--> tests/ui/ensure-nonbool.rs:20:23
|
20 | Bool(cond) => ensure!(cond),
| ^^^^^^^^^^^^^ cannot apply unary operator `!`
|
= note: this error originates in the macro `$crate::__fallback_ensure` which comes from the expansion of the macro `ensure` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0600]: cannot apply unary operator `!` to type `DerefBool`
--> tests/ui/ensure-nonbool.rs:24:5
|
24 | ensure!(db);
| ^^^^^^^^^^^ cannot apply unary operator `!`
|
note: an implementation of `Not` might be missing for `DerefBool`
--> tests/ui/ensure-nonbool.rs:6:1
|
6 | struct DerefBool(bool);
| ^^^^^^^^^^^^^^^^ must implement `Not`
note: the trait `Not` must be implemented
--> $RUST/core/src/ops/bit.rs
|
| pub trait Not {
| ^^^^^^^^^^^^^
= note: this error originates in the macro `$crate::__fallback_ensure` which comes from the expansion of the macro `ensure` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0600]: cannot apply unary operator `!` to type `&DerefBool`
--> tests/ui/ensure-nonbool.rs:25:5
|
25 | ensure!(&db);
| ^^^^^^^^^^^^ cannot apply unary operator `!`
|
= note: this error originates in the macro `$crate::__fallback_ensure` which comes from the expansion of the macro `ensure` (in Nightly builds, run with -Z macro-backtrace for more info)

0 comments on commit 3f9c220

Please sign in to comment.