Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b2ab006

Browse files
authoredJun 25, 2025··
Fix impl Ord for Ident (#1893)
1 parent b9365b3 commit b2ab006

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed
 

‎src/ast/mod.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use helpers::{
2828
stmt_data_loading::{FileStagingCommand, StageLoadSelectItemKind},
2929
};
3030

31+
use core::cmp::Ordering;
3132
use core::ops::Deref;
3233
use core::{
3334
fmt::{self, Display},
@@ -172,7 +173,7 @@ fn format_statement_list(f: &mut fmt::Formatter, statements: &[Statement]) -> fm
172173
}
173174

174175
/// An identifier, decomposed into its value or character data and the quote style.
175-
#[derive(Debug, Clone, PartialOrd, Ord)]
176+
#[derive(Debug, Clone)]
176177
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
177178
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
178179
pub struct Ident {
@@ -214,6 +215,35 @@ impl core::hash::Hash for Ident {
214215

215216
impl Eq for Ident {}
216217

218+
impl PartialOrd for Ident {
219+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
220+
Some(self.cmp(other))
221+
}
222+
}
223+
224+
impl Ord for Ident {
225+
fn cmp(&self, other: &Self) -> Ordering {
226+
let Ident {
227+
value,
228+
quote_style,
229+
// exhaustiveness check; we ignore spans in ordering
230+
span: _,
231+
} = self;
232+
233+
let Ident {
234+
value: other_value,
235+
quote_style: other_quote_style,
236+
// exhaustiveness check; we ignore spans in ordering
237+
span: _,
238+
} = other;
239+
240+
// First compare by value, then by quote_style
241+
value
242+
.cmp(other_value)
243+
.then_with(|| quote_style.cmp(other_quote_style))
244+
}
245+
}
246+
217247
impl Ident {
218248
/// Create a new identifier with the given value and no quotes and an empty span.
219249
pub fn new<S>(value: S) -> Self
@@ -4214,7 +4244,7 @@ pub enum Statement {
42144244
/// ```sql
42154245
/// NOTIFY channel [ , payload ]
42164246
/// ```
4217-
/// send a notification event together with an optional payload string to channel
4247+
/// send a notification event together with an optional "payload" string to channel
42184248
///
42194249
/// See Postgres <https://www.postgresql.org/docs/current/sql-notify.html>
42204250
NOTIFY {
@@ -9771,6 +9801,8 @@ impl fmt::Display for NullInclusion {
97719801

97729802
#[cfg(test)]
97739803
mod tests {
9804+
use crate::tokenizer::Location;
9805+
97749806
use super::*;
97759807

97769808
#[test]
@@ -10066,4 +10098,16 @@ mod tests {
1006610098
test_steps(OneOrManyWithParens::Many(vec![2]), vec![2], 3);
1006710099
test_steps(OneOrManyWithParens::Many(vec![3, 4]), vec![3, 4], 4);
1006810100
}
10101+
10102+
// Tests that the position in the code of an `Ident` does not affect its
10103+
// ordering.
10104+
#[test]
10105+
fn test_ident_ord() {
10106+
let mut a = Ident::with_span(Span::new(Location::new(1, 1), Location::new(1, 1)), "a");
10107+
let mut b = Ident::with_span(Span::new(Location::new(2, 2), Location::new(2, 2)), "b");
10108+
10109+
assert!(a < b);
10110+
std::mem::swap(&mut a.span, &mut b.span);
10111+
assert!(a < b);
10112+
}
1006910113
}

0 commit comments

Comments
 (0)
Please sign in to comment.