Skip to content

Commit

Permalink
Demand Eq for derive(PostgresEq)
Browse files Browse the repository at this point in the history
Previously, PartialEq was enough for a derivation of PostgresEq.
However, the way that we already annotate these operators implies
heavy usage of an assumption that there is an absolute equality.
Partially-ordered sets need not apply.
  • Loading branch information
workingjubilee committed Aug 16, 2023
1 parent fd497c1 commit 521e0a6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pgrx-macros/src/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ pub(crate) fn deriving_postgres_hash(ast: DeriveInput) -> syn::Result<proc_macro
pub fn derive_pg_eq(type_name: &Ident, type_path: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {
let pg_name = Ident::new(&format!("{}_eq", type_name).to_lowercase(), type_name.span());
quote! {
#[doc(hidden)]
impl ::pgrx::deriving::PostgresEqRequiresTotalEq for #type_name {}

#[allow(non_snake_case)]
#[::pgrx::pgrx_macros::pg_operator(immutable, parallel_safe)]
#[::pgrx::pgrx_macros::opname(=)]
Expand Down
8 changes: 8 additions & 0 deletions pgrx-tests/tests/ui/total_eq_for_postgres_eq.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use pgrx::prelude::*;

#[derive(PartialEq, PostgresEq)]
struct BrokenType {
int: i32,
}

fn main() {}
16 changes: 16 additions & 0 deletions pgrx-tests/tests/ui/total_eq_for_postgres_eq.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0277]: the trait bound `BrokenType: std::cmp::Eq` is not satisfied
--> tests/ui/total_eq_for_postgres_eq.rs:4:8
|
4 | struct BrokenType {
| ^^^^^^^^^^ the trait `std::cmp::Eq` is not implemented for `BrokenType`
|
note: required by a bound in `PostgresEqRequiresTotalEq`
--> $WORKSPACE/pgrx/src/deriving.rs
|
| pub trait PostgresEqRequiresTotalEq: Eq {}
| ^^ required by this bound in `PostgresEqRequiresTotalEq`
help: consider annotating `BrokenType` with `#[derive(Eq)]`
|
4 + #[derive(Eq)]
5 | struct BrokenType {
|
3 changes: 3 additions & 0 deletions pgrx/src/deriving.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#![doc(hidden)]

pub trait PostgresEqRequiresTotalEq: Eq {}
1 change: 1 addition & 0 deletions pgrx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub mod array;
pub mod atomics;
pub mod bgworkers;
pub mod callbacks;
pub mod deriving;
pub mod datum;
pub mod enum_helper;
pub mod fcinfo;
Expand Down

0 comments on commit 521e0a6

Please sign in to comment.