Skip to content

Commit

Permalink
Merge pull request #4345 from E-anae/master
Browse files Browse the repository at this point in the history
Fix and/or function argument to compile check bool expression
  • Loading branch information
weiznich authored Nov 15, 2024
2 parents 90d6b8d + e97ffd9 commit e659b01
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
4 changes: 2 additions & 2 deletions diesel/src/expression_methods/bool_expression_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub trait BoolExpressionMethods: Expression + Sized {
fn and<T, ST>(self, other: T) -> dsl::And<Self, T, ST>
where
Self::SqlType: SqlType,
ST: SqlType + TypedExpressionType,
ST: SqlType + TypedExpressionType + BoolOrNullableBool,
T: AsExpression<ST>,
And<Self, T::Expression>: Expression,
{
Expand Down Expand Up @@ -89,7 +89,7 @@ pub trait BoolExpressionMethods: Expression + Sized {
fn or<T, ST>(self, other: T) -> dsl::Or<Self, T, ST>
where
Self::SqlType: SqlType,
ST: SqlType + TypedExpressionType,
ST: SqlType + TypedExpressionType + BoolOrNullableBool,
T: AsExpression<ST>,
Or<Self, T::Expression>: Expression,
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extern crate diesel;

use diesel::prelude::*;

table! {
users {
id -> Integer,
name -> VarChar,
}
}

fn main() {
let conn = &mut PgConnection::establish("…").unwrap();
users::table
.filter(users::id.eq(1).and(users::id).or(users::id))
.select(users::id)
.execute(conn)
.unwrap();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
error[E0277]: `diesel::sql_types::Integer` is neither `diesel::sql_types::Bool` nor `diesel::sql_types::Nullable<Bool>`
--> tests/fail/and_or_functions_must_take_boolean_expr_as_attributes.rs:15:33
|
15 | .filter(users::id.eq(1).and(users::id).or(users::id))
| ^^^ the trait `BoolOrNullableBool` is not implemented for `diesel::sql_types::Integer`
|
= note: try to provide an expression that produces one of the expected sql types
= help: the following other types implement trait `BoolOrNullableBool`:
Bool
Nullable<Bool>
note: required by a bound in `diesel::BoolExpressionMethods::and`
--> $DIESEL/src/expression_methods/bool_expression_methods.rs
|
| fn and<T, ST>(self, other: T) -> dsl::And<Self, T, ST>
| --- required by a bound in this associated function
...
| ST: SqlType + TypedExpressionType + BoolOrNullableBool,
| ^^^^^^^^^^^^^^^^^^ required by this bound in `BoolExpressionMethods::and`

error[E0277]: `diesel::sql_types::Integer` is neither `diesel::sql_types::Bool` nor `diesel::sql_types::Nullable<Bool>`
--> tests/fail/and_or_functions_must_take_boolean_expr_as_attributes.rs:15:48
|
15 | .filter(users::id.eq(1).and(users::id).or(users::id))
| ^^ the trait `BoolOrNullableBool` is not implemented for `diesel::sql_types::Integer`
|
= note: try to provide an expression that produces one of the expected sql types
= help: the following other types implement trait `BoolOrNullableBool`:
Bool
Nullable<Bool>
note: required by a bound in `diesel::BoolExpressionMethods::or`
--> $DIESEL/src/expression_methods/bool_expression_methods.rs
|
| fn or<T, ST>(self, other: T) -> dsl::Or<Self, T, ST>
| -- required by a bound in this associated function
...
| ST: SqlType + TypedExpressionType + BoolOrNullableBool,
| ^^^^^^^^^^^^^^^^^^ required by this bound in `BoolExpressionMethods::or`

0 comments on commit e659b01

Please sign in to comment.