Skip to content

Commit 8254c36

Browse files
authored
transpile: Always use absolute paths when referring to crates (#1408)
- Fixes #447.
2 parents e07fb3c + 8cb4d97 commit 8254c36

39 files changed

+707
-652
lines changed

c2rust-ast-builder/src/builder.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,14 @@ impl Builder {
12431243
}))
12441244
}
12451245

1246+
pub fn abs_path_ty<Pa>(self, path: Pa) -> Box<Type>
1247+
where
1248+
Pa: Make<Path>,
1249+
{
1250+
let path = mk().abs_path(path);
1251+
self.path_ty(path)
1252+
}
1253+
12461254
pub fn path_ty<Pa>(self, path: Pa) -> Box<Type>
12471255
where
12481256
Pa: Make<Path>,

c2rust-transpile/src/convert_type.rs

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ impl TypeConverter {
284284
// in the case of pointers.
285285
CTypeKind::Void => Ok(mk()
286286
.set_mutbl(mutbl)
287-
.ptr_ty(mk().path_ty(vec!["core", "ffi", "c_void"]))),
287+
.ptr_ty(mk().abs_path_ty(vec!["core", "ffi", "c_void"]))),
288288

289289
CTypeKind::VariableArray(mut elt, _len) => {
290290
while let CTypeKind::VariableArray(elt_, _) = ctxt.resolve_type(elt).kind {
@@ -317,59 +317,58 @@ impl TypeConverter {
317317
ctype: CTypeId,
318318
) -> TranslationResult<Box<Type>> {
319319
if self.translate_valist && ctxt.is_va_list(ctype) {
320-
let path = vec!["core", "ffi", "VaList"];
321-
let ty = mk().path_ty(mk().abs_path(path));
320+
let ty = mk().abs_path_ty(vec!["core", "ffi", "VaList"]);
322321
return Ok(ty);
323322
}
324323

325324
match ctxt.index(ctype).kind {
326325
CTypeKind::Void => Ok(mk().tuple_ty(vec![])),
327-
CTypeKind::Bool => Ok(mk().path_ty(mk().path(vec!["bool"]))),
328-
CTypeKind::Short => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_short"]))),
329-
CTypeKind::Int => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_int"]))),
330-
CTypeKind::Long => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_long"]))),
331-
CTypeKind::LongLong => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_longlong"]))),
332-
CTypeKind::UShort => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_ushort"]))),
333-
CTypeKind::UInt => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_uint"]))),
334-
CTypeKind::ULong => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_ulong"]))),
335-
CTypeKind::ULongLong => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_ulonglong"]))),
336-
CTypeKind::SChar => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_schar"]))),
337-
CTypeKind::UChar => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_uchar"]))),
338-
CTypeKind::Char => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_char"]))),
339-
CTypeKind::Double => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_double"]))),
326+
CTypeKind::Bool => Ok(mk().path_ty(vec!["bool"])),
327+
CTypeKind::Short => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_short"])),
328+
CTypeKind::Int => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_int"])),
329+
CTypeKind::Long => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_long"])),
330+
CTypeKind::LongLong => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_longlong"])),
331+
CTypeKind::UShort => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_ushort"])),
332+
CTypeKind::UInt => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_uint"])),
333+
CTypeKind::ULong => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_ulong"])),
334+
CTypeKind::ULongLong => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_ulonglong"])),
335+
CTypeKind::SChar => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_schar"])),
336+
CTypeKind::UChar => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_uchar"])),
337+
CTypeKind::Char => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_char"])),
338+
CTypeKind::Double => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_double"])),
340339
CTypeKind::LongDouble | CTypeKind::Float128 => {
341340
self.use_crate(ExternCrate::F128);
342-
Ok(mk().path_ty(mk().path(vec!["f128", "f128"])))
341+
Ok(mk().abs_path_ty(vec!["f128", "f128"]))
343342
}
344-
CTypeKind::Float => Ok(mk().path_ty(mk().path(vec!["core", "ffi", "c_float"]))),
345-
CTypeKind::Int128 => Ok(mk().path_ty(mk().path(vec!["i128"]))),
346-
CTypeKind::UInt128 => Ok(mk().path_ty(mk().path(vec!["u128"]))),
347-
CTypeKind::BFloat16 => Ok(mk().path_ty(mk().path(vec!["bf16"]))),
348-
349-
CTypeKind::Int8 => Ok(mk().path_ty(mk().path(vec!["i8"]))),
350-
CTypeKind::Int16 => Ok(mk().path_ty(mk().path(vec!["i16"]))),
351-
CTypeKind::Int32 => Ok(mk().path_ty(mk().path(vec!["i32"]))),
352-
CTypeKind::Int64 => Ok(mk().path_ty(mk().path(vec!["i64"]))),
353-
CTypeKind::IntPtr => Ok(mk().path_ty(mk().path(vec!["isize"]))),
354-
CTypeKind::UInt8 => Ok(mk().path_ty(mk().path(vec!["u8"]))),
355-
CTypeKind::UInt16 => Ok(mk().path_ty(mk().path(vec!["u16"]))),
356-
CTypeKind::UInt32 => Ok(mk().path_ty(mk().path(vec!["u32"]))),
357-
CTypeKind::UInt64 => Ok(mk().path_ty(mk().path(vec!["u64"]))),
358-
CTypeKind::UIntPtr => Ok(mk().path_ty(mk().path(vec!["usize"]))),
343+
CTypeKind::Float => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_float"])),
344+
CTypeKind::Int128 => Ok(mk().path_ty(vec!["i128"])),
345+
CTypeKind::UInt128 => Ok(mk().path_ty(vec!["u128"])),
346+
CTypeKind::BFloat16 => Ok(mk().path_ty(vec!["bf16"])),
347+
348+
CTypeKind::Int8 => Ok(mk().path_ty(vec!["i8"])),
349+
CTypeKind::Int16 => Ok(mk().path_ty(vec!["i16"])),
350+
CTypeKind::Int32 => Ok(mk().path_ty(vec!["i32"])),
351+
CTypeKind::Int64 => Ok(mk().path_ty(vec!["i64"])),
352+
CTypeKind::IntPtr => Ok(mk().path_ty(vec!["isize"])),
353+
CTypeKind::UInt8 => Ok(mk().path_ty(vec!["u8"])),
354+
CTypeKind::UInt16 => Ok(mk().path_ty(vec!["u16"])),
355+
CTypeKind::UInt32 => Ok(mk().path_ty(vec!["u32"])),
356+
CTypeKind::UInt64 => Ok(mk().path_ty(vec!["u64"])),
357+
CTypeKind::UIntPtr => Ok(mk().path_ty(vec!["usize"])),
359358
CTypeKind::IntMax => {
360359
self.use_crate(ExternCrate::Libc);
361-
Ok(mk().path_ty(mk().path(vec!["libc", "intmax_t"])))
360+
Ok(mk().abs_path_ty(vec!["libc", "intmax_t"]))
362361
}
363362
CTypeKind::UIntMax => {
364363
self.use_crate(ExternCrate::Libc);
365-
Ok(mk().path_ty(mk().path(vec!["libc", "uintmax_t"])))
364+
Ok(mk().abs_path_ty(vec!["libc", "uintmax_t"]))
366365
}
367-
CTypeKind::Size => Ok(mk().path_ty(mk().path(vec!["usize"]))),
368-
CTypeKind::SSize => Ok(mk().path_ty(mk().path(vec!["isize"]))),
369-
CTypeKind::PtrDiff => Ok(mk().path_ty(mk().path(vec!["isize"]))),
366+
CTypeKind::Size => Ok(mk().path_ty(vec!["usize"])),
367+
CTypeKind::SSize => Ok(mk().path_ty(vec!["isize"])),
368+
CTypeKind::PtrDiff => Ok(mk().path_ty(vec!["isize"])),
370369
CTypeKind::WChar => {
371370
self.use_crate(ExternCrate::Libc);
372-
Ok(mk().path_ty(mk().path(vec!["libc", "wchar_t"])))
371+
Ok(mk().abs_path_ty(vec!["libc", "wchar_t"]))
373372
}
374373

375374
CTypeKind::Pointer(qtype) => self.convert_pointer(ctxt, qtype),
@@ -382,22 +381,22 @@ impl TypeConverter {
382381
let new_name = self
383382
.resolve_decl_name(decl_id)
384383
.ok_or_else(|| format_err!("Unknown decl id {:?}", decl_id))?;
385-
Ok(mk().path_ty(mk().path(vec![new_name])))
384+
Ok(mk().path_ty(vec![new_name]))
386385
}
387386

388387
CTypeKind::Union(decl_id) => {
389388
let new_name = self.resolve_decl_name(decl_id).unwrap();
390-
Ok(mk().path_ty(mk().path(vec![new_name])))
389+
Ok(mk().path_ty(vec![new_name]))
391390
}
392391

393392
CTypeKind::Enum(decl_id) => {
394393
let new_name = self.resolve_decl_name(decl_id).unwrap();
395-
Ok(mk().path_ty(mk().path(vec![new_name])))
394+
Ok(mk().path_ty(vec![new_name]))
396395
}
397396

398397
CTypeKind::Typedef(decl_id) => {
399398
let new_name = self.resolve_decl_name(decl_id).unwrap();
400-
Ok(mk().path_ty(mk().path(vec![new_name])))
399+
Ok(mk().path_ty(vec![new_name]))
401400
}
402401

403402
CTypeKind::ConstantArray(element, count) => {

c2rust-transpile/src/rust_ast/item_store.rs

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,47 @@ impl MultiImport {
3636
}
3737

3838
#[derive(Debug, Default)]
39-
pub struct PathedMultiImports(IndexMap<Vec<String>, MultiImport>);
39+
pub struct PathedMultiImports(IndexMap<(bool, Vec<String>), MultiImport>);
4040

4141
impl PathedMultiImports {
4242
pub fn new() -> Self {
4343
Self::default()
4444
}
4545

46-
pub fn get_mut(&mut self, path: Vec<String>) -> &mut MultiImport {
47-
self.0.entry(path).or_insert(MultiImport::new())
46+
pub fn get_mut(&mut self, is_absolute: bool, path: Vec<String>) -> &mut MultiImport {
47+
self.0
48+
.entry((is_absolute, path))
49+
.or_insert(MultiImport::new())
4850
}
4951

5052
pub fn into_items(self) -> Vec<Box<Item>> {
51-
fn build_items((mut path, imports): (Vec<String>, MultiImport)) -> Box<Item> {
52-
let mut leaves = imports.leaves;
53-
let attrs = imports.attrs.unwrap_or_else(mk);
54-
55-
if leaves.len() == 1 {
56-
path.push(leaves.pop().unwrap());
57-
58-
attrs.use_simple_item(path, None as Option<Ident>)
59-
} else {
60-
attrs.use_multiple_item(path, leaves.into_iter())
61-
}
62-
}
63-
64-
self.0.into_iter().map(build_items).collect()
53+
self.0
54+
.into_iter()
55+
.map(|((is_absolute, mut path), imports)| {
56+
let mut leaves = imports.leaves;
57+
let attrs = imports.attrs.unwrap_or_else(mk);
58+
59+
if leaves.len() == 1 {
60+
path.push(leaves.pop().unwrap());
61+
62+
let path = if is_absolute {
63+
mk().abs_path(path)
64+
} else {
65+
mk().path(path)
66+
};
67+
68+
attrs.use_simple_item(path, None as Option<Ident>)
69+
} else {
70+
let path = if is_absolute {
71+
mk().abs_path(path)
72+
} else {
73+
mk().path(path)
74+
};
75+
76+
attrs.use_multiple_item(path, leaves.into_iter())
77+
}
78+
})
79+
.collect()
6580
}
6681
}
6782

@@ -87,12 +102,20 @@ impl ItemStore {
87102
self.foreign_items.push(item);
88103
}
89104

90-
pub fn add_use(&mut self, path: Vec<String>, ident: &str) {
91-
self.uses.get_mut(path).insert(ident)
105+
pub fn add_use(&mut self, is_absolute: bool, path: Vec<String>, ident: &str) {
106+
self.uses.get_mut(is_absolute, path).insert(ident)
92107
}
93108

94-
pub fn add_use_with_attr(&mut self, path: Vec<String>, ident: &str, attrs: Builder) {
95-
self.uses.get_mut(path).insert_with_attr(ident, attrs)
109+
pub fn add_use_with_attr(
110+
&mut self,
111+
is_absolute: bool,
112+
path: Vec<String>,
113+
ident: &str,
114+
attrs: Builder,
115+
) {
116+
self.uses
117+
.get_mut(is_absolute, path)
118+
.insert_with_attr(ident, attrs)
96119
}
97120

98121
pub fn drain(&mut self) -> (Vec<Box<Item>>, Vec<ForeignItem>, PathedMultiImports) {

c2rust-transpile/src/translator/assembly.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ impl<'c> Translation<'c> {
931931

932932
// Import the trait into scope
933933
self.with_cur_file_item_store(|item_store| {
934-
item_store.add_use(vec!["c2rust_asm_casts".into()], "AsmCastTrait");
934+
item_store.add_use(true, vec!["c2rust_asm_casts".into()], "AsmCastTrait");
935935
});
936936

937937
let (output_name, inner_name) = operand_renames.get(tied_operand).unwrap();
@@ -1059,7 +1059,7 @@ impl<'c> Translation<'c> {
10591059
}
10601060

10611061
self.with_cur_file_item_store(|item_store| {
1062-
item_store.add_use(vec!["core".into(), "arch".into()], "asm");
1062+
item_store.add_use(true, vec!["core".into(), "arch".into()], "asm");
10631063
});
10641064

10651065
let mac = mk().mac(

c2rust-transpile/src/translator/builtins.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ impl<'c> Translation<'c> {
9090
self.use_crate(ExternCrate::F128);
9191

9292
Ok(WithStmts::new_val(
93-
mk().path_expr(vec!["f128", "f128", "NAN"]),
93+
mk().abs_path_expr(vec!["f128", "f128", "NAN"]),
9494
))
9595
}
9696
"__builtin_signbit" | "__builtin_signbitf" | "__builtin_signbitl" => {
9797
// Long doubles require the Float trait from num_traits to call this method
9898
if builtin_name == "__builtin_signbitl" {
9999
self.with_cur_file_item_store(|item_store| {
100-
item_store.add_use(vec!["num_traits".into()], "Float");
100+
item_store.add_use(true, vec!["num_traits".into()], "Float");
101101
});
102102
}
103103

@@ -106,7 +106,7 @@ impl<'c> Translation<'c> {
106106
Ok(val.map(|v| {
107107
let val = mk().method_call_expr(v, "is_sign_negative", vec![]);
108108

109-
mk().cast_expr(val, mk().path_ty(vec!["core", "ffi", "c_int"]))
109+
mk().cast_expr(val, mk().abs_path_ty(vec!["core", "ffi", "c_int"]))
110110
}))
111111
}
112112
"__builtin_ffs" | "__builtin_ffsl" | "__builtin_ffsll" => {
@@ -309,7 +309,7 @@ impl<'c> Translation<'c> {
309309
Some(mk().lit_expr(mk().int_lit(0, "isize"))),
310310
);
311311
self.use_crate(ExternCrate::Libc);
312-
let size_t = mk().path_ty(vec!["libc", "size_t"]);
312+
let size_t = mk().abs_path_ty(vec!["libc", "size_t"]);
313313
mk().cast_expr(if_expr, size_t)
314314
}))
315315
})
@@ -772,7 +772,7 @@ impl<'c> Translation<'c> {
772772
) -> TranslationResult<WithStmts<Box<Expr>>> {
773773
let name = &builtin_name[10..];
774774
self.use_crate(ExternCrate::Libc);
775-
let mem = mk().path_expr(vec!["libc", name]);
775+
let mem = mk().abs_path_expr(vec!["libc", name]);
776776
let args = self.convert_exprs(ctx.used(), args, None)?;
777777
args.and_then(|args| {
778778
if args.len() != arg_types.len() {
@@ -791,7 +791,7 @@ impl<'c> Translation<'c> {
791791
.map(|(arg, &ty)| {
792792
if ty == LibcFnArgType::Size {
793793
self.use_crate(ExternCrate::Libc);
794-
mk().cast_expr(arg, mk().path_ty(vec!["libc", "size_t"]))
794+
mk().cast_expr(arg, mk().abs_path_ty(vec!["libc", "size_t"]))
795795
} else {
796796
arg
797797
}

c2rust-transpile/src/translator/literals.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl<'c> Translation<'c> {
139139

140140
self.use_crate(ExternCrate::F128);
141141

142-
let fn_path = mk().path_expr(vec!["f128", "f128", "new"]);
142+
let fn_path = mk().abs_path_expr(vec!["f128", "f128", "new"]);
143143
let args = vec![mk().lit_expr(mk().float_unsuffixed_lit(&str))];
144144

145145
mk().call_expr(fn_path, args)

c2rust-transpile/src/translator/main_function.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<'c> Translation<'c> {
5858
Some(mk().path_ty(vec![mk().path_segment_with_args(
5959
"Vec",
6060
mk().angle_bracketed_args(vec![
61-
mk().mutbl().ptr_ty(mk().path_ty(vec!["core", "ffi", "c_char"])),
61+
mk().mutbl().ptr_ty(mk().abs_path_ty(vec!["core", "ffi", "c_char"])),
6262
]),
6363
)])),
6464
Some(mk().call_expr(mk().path_expr(vec!["Vec", "new"]), vec![])),
@@ -126,7 +126,7 @@ impl<'c> Translation<'c> {
126126
Some(mk().path_ty(vec![mk().path_segment_with_args(
127127
"Vec",
128128
mk().angle_bracketed_args(vec![
129-
mk().mutbl().ptr_ty(mk().path_ty(vec!["core", "ffi", "c_char"])),
129+
mk().mutbl().ptr_ty(mk().abs_path_ty(vec!["core", "ffi", "c_char"])),
130130
]),
131131
)])),
132132
Some(mk().call_expr(mk().path_expr(vec!["Vec", "new"]), vec![])),

0 commit comments

Comments
 (0)