Skip to content

Commit a4a052b

Browse files
committed
transpile: improve error message for SrcLoc total ordering test
1 parent 0872f48 commit a4a052b

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

c2rust-ast-exporter/src/clang_ast.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use serde_bytes::ByteBuf;
22
use serde_cbor::error;
3-
use std;
43
use std::collections::{HashMap, VecDeque};
54
use std::convert::TryInto;
5+
use std::fmt::{Display, Formatter};
66
use std::path::{Path, PathBuf};
7+
use std::{self, fmt};
78

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

@@ -31,6 +32,17 @@ pub struct SrcLoc {
3132
pub column: u64,
3233
}
3334

35+
impl Display for SrcLoc {
36+
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
37+
let Self {
38+
fileid,
39+
line,
40+
column,
41+
} = *self;
42+
write!(f, "file_{fileid}:{line}:{column}")
43+
}
44+
}
45+
3446
#[derive(Copy, Debug, Clone, PartialOrd, PartialEq, Ord, Eq)]
3547
pub struct SrcSpan {
3648
pub fileid: u64,
@@ -40,6 +52,22 @@ pub struct SrcSpan {
4052
pub end_column: u64,
4153
}
4254

55+
impl Display for SrcSpan {
56+
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
57+
let Self {
58+
fileid,
59+
begin_line,
60+
begin_column,
61+
end_line,
62+
end_column,
63+
} = *self;
64+
write!(
65+
f,
66+
"file_{fileid}:{begin_line}:{begin_column}-{end_line}:{end_column}"
67+
)
68+
}
69+
}
70+
4371
impl From<SrcLoc> for SrcSpan {
4472
fn from(loc: SrcLoc) -> Self {
4573
Self {

c2rust-transpile/src/c_ast/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -2101,7 +2101,12 @@ mod tests {
21012101
let bc = ctx.compare_src_locs(&b, &c);
21022102
let ac = ctx.compare_src_locs(&a, &c);
21032103
if ab == bc {
2104-
assert_eq!(ab, ac, "Total order (transitivity) has been violated");
2104+
let [ab, bc, ac] = [ab, bc, ac].map(|ord| match ord {
2105+
Ordering::Less => "<",
2106+
Ordering::Equal => "==",
2107+
Ordering::Greater => ">",
2108+
});
2109+
assert_eq!(ab, ac, "Total order (transitivity) has been violated: {a} {ab} {b} and {b} {bc} {c}, but {a} {ac} {c}");
21052110
}
21062111
}
21072112
}

0 commit comments

Comments
 (0)