Fix impl Ord for Ident (#1893)
Some checks are pending
license / Release Audit Tool (RAT) (push) Waiting to run
Rust / codestyle (push) Waiting to run
Rust / lint (push) Waiting to run
Rust / benchmark-lint (push) Waiting to run
Rust / compile (push) Waiting to run
Rust / docs (push) Waiting to run
Rust / compile-no-std (push) Waiting to run
Rust / test (beta) (push) Waiting to run
Rust / test (nightly) (push) Waiting to run
Rust / test (stable) (push) Waiting to run

This commit is contained in:
Elia Perantoni 2025-06-25 12:21:59 +02:00 committed by GitHub
parent b9365b3853
commit b2ab0061c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -28,6 +28,7 @@ use helpers::{
stmt_data_loading::{FileStagingCommand, StageLoadSelectItemKind}, stmt_data_loading::{FileStagingCommand, StageLoadSelectItemKind},
}; };
use core::cmp::Ordering;
use core::ops::Deref; use core::ops::Deref;
use core::{ use core::{
fmt::{self, Display}, fmt::{self, Display},
@ -172,7 +173,7 @@ fn format_statement_list(f: &mut fmt::Formatter, statements: &[Statement]) -> fm
} }
/// An identifier, decomposed into its value or character data and the quote style. /// An identifier, decomposed into its value or character data and the quote style.
#[derive(Debug, Clone, PartialOrd, Ord)] #[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] #[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct Ident { pub struct Ident {
@ -214,6 +215,35 @@ impl core::hash::Hash for Ident {
impl Eq for Ident {} impl Eq for Ident {}
impl PartialOrd for Ident {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Ident {
fn cmp(&self, other: &Self) -> Ordering {
let Ident {
value,
quote_style,
// exhaustiveness check; we ignore spans in ordering
span: _,
} = self;
let Ident {
value: other_value,
quote_style: other_quote_style,
// exhaustiveness check; we ignore spans in ordering
span: _,
} = other;
// First compare by value, then by quote_style
value
.cmp(other_value)
.then_with(|| quote_style.cmp(other_quote_style))
}
}
impl Ident { impl Ident {
/// Create a new identifier with the given value and no quotes and an empty span. /// Create a new identifier with the given value and no quotes and an empty span.
pub fn new<S>(value: S) -> Self pub fn new<S>(value: S) -> Self
@ -4214,7 +4244,7 @@ pub enum Statement {
/// ```sql /// ```sql
/// NOTIFY channel [ , payload ] /// NOTIFY channel [ , payload ]
/// ``` /// ```
/// send a notification event together with an optional “payload” string to channel /// send a notification event together with an optional "payload" string to channel
/// ///
/// See Postgres <https://www.postgresql.org/docs/current/sql-notify.html> /// See Postgres <https://www.postgresql.org/docs/current/sql-notify.html>
NOTIFY { NOTIFY {
@ -9771,6 +9801,8 @@ impl fmt::Display for NullInclusion {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::tokenizer::Location;
use super::*; use super::*;
#[test] #[test]
@ -10066,4 +10098,16 @@ mod tests {
test_steps(OneOrManyWithParens::Many(vec![2]), vec![2], 3); test_steps(OneOrManyWithParens::Many(vec![2]), vec![2], 3);
test_steps(OneOrManyWithParens::Many(vec![3, 4]), vec![3, 4], 4); test_steps(OneOrManyWithParens::Many(vec![3, 4]), vec![3, 4], 4);
} }
// Tests that the position in the code of an `Ident` does not affect its
// ordering.
#[test]
fn test_ident_ord() {
let mut a = Ident::with_span(Span::new(Location::new(1, 1), Location::new(1, 1)), "a");
let mut b = Ident::with_span(Span::new(Location::new(2, 2), Location::new(2, 2)), "b");
assert!(a < b);
std::mem::swap(&mut a.span, &mut b.span);
assert!(a < b);
}
} }