Commit e75d601
authored
Remove extraneous separator from paths with arguments (#1340)
`compute_size_of_ty()` constructs a path using
`path_segment_with_args()`, so that e.g. `sizeof(int)` gets translated
to `::core::mem::size_of::<libc::c_int>()`.
But this code in ast-builder was adding an extra separator token to the
path when a path segment has an argument. The resulting token stream for
the call example given before thus corresponded to
`::core::mem::size_of::<libc::c_int> :: ()`. You can verify this by
adding a debug print log to show the converted value of a conditional
and then translating code like `(sizeof(int) > 0) ? 0 : 0)`. I haven't
dug into why, but (unless I'm mistaken about what's doing what here)
`prettyplease` masks this bug by not including the syntactically
incorrect trailing separator in its string output.
The incorrect syntax can be exposed when converting the invalid path to
a token stream. Or, put another way, passing such expressions to a macro
exposes the bad syntax.
Without this patch, translating
```c
#include <stddef.h>
#include <stdio.h>
struct s { int p[512]; };
int foo(unsigned char x) {
return offsetof(struct s, p[sizeof(int) + x]);
}
int main(int argc, char** argv) {
printf("%d\n", foo(argc));
return 0;
}
```
gives `foo` as
```rs
#[no_mangle]
pub unsafe extern "C" fn foo(mut x: libc::c_uchar) -> libc::c_int {
return offset_of!(
s, p[(::core::mem::size_of:: < libc::c_int > :: () as libc::c_ulong)
// ^^ this guy right here!
.wrapping_add(x as libc::c_ulong) as usize]
) as libc::c_ulong as libc::c_int;
}
```
Anyways, it appears that every use of `path_segment_with_args` in c2rust
comes at the end of a path. I don't know if the extra separator would be
needed for segments that are not at the end. Maybe there are other ways
of getting paths with args that I didn't look for? I tried running the
`c2rust-testsuite` but it seems to want a very specific machine config.
So testing in CI it is...File tree
2 files changed
+14
-7
lines changed- c2rust-ast-builder/src
- c2rust-transpile/tests
2 files changed
+14
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
315 | 315 | | |
316 | 316 | | |
317 | 317 | | |
318 | | - | |
319 | | - | |
320 | | - | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
| 318 | + | |
325 | 319 | | |
326 | 320 | | |
327 | 321 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
0 commit comments