Skip to content

Commit

Permalink
Rollup merge of rust-lang#72446 - dtolnay:ord, r=petrochenkov
Browse files Browse the repository at this point in the history
Impl Ord for proc_macro::LineColumn

```rust
impl Ord for LineColumn {...}
impl PartialOrd for LineColumn {...}
```

for https://doc.rust-lang.org/nightly/proc_macro/struct.LineColumn.html.

The ordering is the natural one you would get by writing one line after another, where we compare line first, then compare columns within the same line.
  • Loading branch information
Dylan-DPC authored May 23, 2020
2 parents 7f5588f + 5a4bf44 commit ec110c9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/libproc_macro/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ mod diagnostic;
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
pub use diagnostic::{Diagnostic, Level, MultiSpan};

use std::cmp::Ordering;
use std::ops::{Bound, RangeBounds};
use std::path::PathBuf;
use std::str::FromStr;
Expand Down Expand Up @@ -420,6 +421,20 @@ impl !Send for LineColumn {}
#[unstable(feature = "proc_macro_span", issue = "54725")]
impl !Sync for LineColumn {}

#[unstable(feature = "proc_macro_span", issue = "54725")]
impl Ord for LineColumn {
fn cmp(&self, other: &Self) -> Ordering {
self.line.cmp(&other.line).then(self.column.cmp(&other.column))
}
}

#[unstable(feature = "proc_macro_span", issue = "54725")]
impl PartialOrd for LineColumn {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

/// The source file of a given `Span`.
#[unstable(feature = "proc_macro_span", issue = "54725")]
#[derive(Clone)]
Expand Down
12 changes: 12 additions & 0 deletions src/libproc_macro/tests/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![feature(proc_macro_span)]

use proc_macro::LineColumn;

#[test]
fn test_line_column_ord() {
let line0_column0 = LineColumn { line: 0, column: 0 };
let line0_column1 = LineColumn { line: 0, column: 1 };
let line1_column0 = LineColumn { line: 1, column: 0 };
assert!(line0_column0 < line0_column1);
assert!(line0_column1 < line1_column0);
}

0 comments on commit ec110c9

Please sign in to comment.