Skip to content

Commit

Permalink
transpile: fix curl transpile breakage while maintaining a total `S…
Browse files Browse the repository at this point in the history
…rcLoc` order

#1128 fixed the non-total (non-transitive) `SrcLoc` ordering,
but accidentally broke the `curl` transpilation test in `c2rust-testsuites`,
which is tested in CI, but was broken for external contributors PRs (fixed now in #1159).

In the `curl` transpilation, a couple of `use` imports (`__sigset_t` and `C2RustUnnamed_4`)
were missing, cause the build to fail afterward.

I'm still not exactly sure why this fixes the issue while maintaining a transitive, total order,
but it passes the total order test and transpiles `curl` correctly now.
Hopefully this is a complete fix, and I didn't just fix a one-off error in `curl`.
  • Loading branch information
kkysen committed Nov 18, 2024
1 parent a4a052b commit 9679a3b
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions c2rust-transpile/src/c_ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,14 @@ impl TypedAstContext {

/// Compare two [`SrcLoc`]s based on their import path
pub fn compare_src_locs(&self, a: &SrcLoc, b: &SrcLoc) -> Ordering {
/// Compare without regard to `fileid`.
fn cmp_pos(a: &SrcLoc, b: &SrcLoc) -> Ordering {
(a.line, a.column).cmp(&(b.line, b.column))
}

use Ordering::*;
let path_a = &self.include_map[self.file_map[a.fileid as usize]];
let path_b = &self.include_map[self.file_map[b.fileid as usize]];
let path_a = &self.include_map[self.file_map[a.fileid as usize]][..];
let path_b = &self.include_map[self.file_map[b.fileid as usize]][..];

// Find the first include that does not match between the two
let common_len = path_a.len().min(path_b.len());
Expand All @@ -221,22 +226,21 @@ impl TypedAstContext {

// Either all parent includes are the same, or the include paths are of different lengths
// because .zip() stops when one of the iterators is empty.
let (a, b) = match path_a.len().cmp(&path_b.len()) {
match path_a.len().cmp(&path_b.len()) {
Less => {
// a has the shorter path, which means b was included in a's file
// so extract that include and compare the position to a
let b = &path_b[path_a.len()];
(a, b)
cmp_pos(a, b)
}
Equal => (a, b), // a and b have the same include path and are thus in the same file
Equal => a.cmp(b), // a and b have the same include path and are thus in the same file
Greater => {
// b has the shorter path, which means a was included in b's file
// so extract that include and compare the position to b
let a = &path_a[path_b.len()];
(a, b)
cmp_pos(a, b)
}
};
a.cmp(b)
}
}

pub fn get_file_include_line_number(&self, file: FileId) -> Option<u64> {
Expand Down

0 comments on commit 9679a3b

Please sign in to comment.