Skip to content

Commit bb1dbab

Browse files
eliaperantoniayman-sigma
authored andcommitted
Fix impl Ord for Ident (apache#1893)
1 parent a954449 commit bb1dbab

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
@@ -4243,7 +4273,7 @@ pub enum Statement {
42434273
/// ```sql
42444274
/// NOTIFY channel [ , payload ]
42454275
/// ```
4246-
/// send a notification event together with an optional payload string to channel
4276+
/// send a notification event together with an optional "payload" string to channel
42474277
///
42484278
/// See Postgres <https://www.postgresql.org/docs/current/sql-notify.html>
42494279
NOTIFY {
@@ -9800,6 +9830,8 @@ impl fmt::Display for NullInclusion {
98009830

98019831
#[cfg(test)]
98029832
mod tests {
9833+
use crate::tokenizer::Location;
9834+
98039835
use super::*;
98049836

98059837
#[test]
@@ -10095,4 +10127,16 @@ mod tests {
1009510127
test_steps(OneOrManyWithParens::Many(vec![2]), vec![2], 3);
1009610128
test_steps(OneOrManyWithParens::Many(vec![3, 4]), vec![3, 4], 4);
1009710129
}
10130+
10131+
// Tests that the position in the code of an `Ident` does not affect its
10132+
// ordering.
10133+
#[test]
10134+
fn test_ident_ord() {
10135+
let mut a = Ident::with_span(Span::new(Location::new(1, 1), Location::new(1, 1)), "a");
10136+
let mut b = Ident::with_span(Span::new(Location::new(2, 2), Location::new(2, 2)), "b");
10137+
10138+
assert!(a < b);
10139+
std::mem::swap(&mut a.span, &mut b.span);
10140+
assert!(a < b);
10141+
}
1009810142
}

0 commit comments

Comments
 (0)