Skip to content

Commit

Permalink
transpile: improve error message for SrcLoc total ordering test
Browse files Browse the repository at this point in the history
  • Loading branch information
kkysen committed Nov 18, 2024
1 parent 0872f48 commit a4a052b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
30 changes: 29 additions & 1 deletion c2rust-ast-exporter/src/clang_ast.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use serde_bytes::ByteBuf;
use serde_cbor::error;
use std;
use std::collections::{HashMap, VecDeque};
use std::convert::TryInto;
use std::fmt::{Display, Formatter};
use std::path::{Path, PathBuf};
use std::{self, fmt};

pub use serde_cbor::value::{from_value, Value};

Expand Down Expand Up @@ -31,6 +32,17 @@ pub struct SrcLoc {
pub column: u64,
}

impl Display for SrcLoc {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let Self {
fileid,
line,
column,
} = *self;
write!(f, "file_{fileid}:{line}:{column}")
}
}

#[derive(Copy, Debug, Clone, PartialOrd, PartialEq, Ord, Eq)]
pub struct SrcSpan {
pub fileid: u64,
Expand All @@ -40,6 +52,22 @@ pub struct SrcSpan {
pub end_column: u64,
}

impl Display for SrcSpan {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let Self {
fileid,
begin_line,
begin_column,
end_line,
end_column,
} = *self;
write!(
f,
"file_{fileid}:{begin_line}:{begin_column}-{end_line}:{end_column}"
)
}
}

impl From<SrcLoc> for SrcSpan {
fn from(loc: SrcLoc) -> Self {
Self {
Expand Down
7 changes: 6 additions & 1 deletion c2rust-transpile/src/c_ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2101,7 +2101,12 @@ mod tests {
let bc = ctx.compare_src_locs(&b, &c);
let ac = ctx.compare_src_locs(&a, &c);
if ab == bc {
assert_eq!(ab, ac, "Total order (transitivity) has been violated");
let [ab, bc, ac] = [ab, bc, ac].map(|ord| match ord {
Ordering::Less => "<",
Ordering::Equal => "==",
Ordering::Greater => ">",
});
assert_eq!(ab, ac, "Total order (transitivity) has been violated: {a} {ab} {b} and {b} {bc} {c}, but {a} {ac} {c}");
}
}
}
Expand Down

0 comments on commit a4a052b

Please sign in to comment.