Live data from Hacker News

C2Rust: translate C into Rust code

c2rust.com

31–40 of 79 posts

Re: C2Rust: translate C into Rust code

#31

    #define a "xxxxxxxxxxx"
    #define b a a a a a a a
    #define c b b b b b b b
    #define d c c c c c c c
    #define e d d d d d d d
    #define f e e e e e e e
    #define g f f f f f f f
    #define h g g g g g g g
    #define i h h h h h h h
    #define j i i i i i i i
    main(){char*z=j;}
This fails. The error is entity not found.

Re: C2Rust: translate C into Rust code

#32
post #24

This is compiling C into very unsafe Rust as a target language: pub unsafe extern "C" fn insertion_sort(n: libc::c_int, p: *mut libc::c_int) -> () { let mut i: libc::c_int = 1i32; while i 0i32 && *p.offset((j - 1i32) as isize) > tmp { *p.offset(j as isize) = *p.offset((j - 1i32) as isize); j -= 1 } *p.offset(j as isize) = tmp; i += 1 }; } The output is unmaintainable, like the output from a compiler. This isn't trans…

Even though it's translating to unsafe Rust, it does still have some value. In particular, C libraries can be converted into Rust libraries that can be managed via Cargo, avoiding the need to deal with C tooling.

Is that benefit worth the potential for inaccurate translation? Or is it possible to be 100% certain that the Rust functions identically to the C?

Re: C2Rust: translate C into Rust code

#33

#define a "xxxxxxxxxxx" #define b a a a a a a a #define c b b b b b b b #define d c c c c c c c #define e d d d d d d d #define f e e e e e e e #define g f f f f f f f #define h g g g g g g g #define i h h h h h h h #define j i i i i i i i main(){char*z=j;} This fails. The error is entity not found.

There are definitely ways to overwhelm the translator web demo. I know I'm not clever enough to block all of them. I put the page up to give people a way to try out the translator without having to build it. Please don't kill it :-)

Re: C2Rust: translate C into Rust code

#34
post #21

I think the next step would be to have a tool that can convert C into safe Rust with a combination of static analysis and framework/program-specific user-written rules to translate specific C framework constructs into Rust equivalents. An eventual goal could be for instance to automatically translate the Linux kernel with the aid of a lot of custom rules to handle its constructs.

[deleted]

Re: C2Rust: translate C into Rust code

#35
post #24

This is compiling C into very unsafe Rust as a target language: pub unsafe extern "C" fn insertion_sort(n: libc::c_int, p: *mut libc::c_int) -> () { let mut i: libc::c_int = 1i32; while i 0i32 && *p.offset((j - 1i32) as isize) > tmp { *p.offset(j as isize) = *p.offset((j - 1i32) as isize); j -= 1 } *p.offset(j as isize) = tmp; i += 1 }; } The output is unmaintainable, like the output from a compiler. This isn't trans…

Even though it's translating to unsafe Rust, it does still have some value. In particular, C libraries can be converted into Rust libraries that can be managed via Cargo, avoiding the need to deal with C tooling.

As a user of a C library in Cargo, you don't need to deal with C tooling, just have it installed.

Re: C2Rust: translate C into Rust code

#36
post #21

I think the next step would be to have a tool that can convert C into safe Rust with a combination of static analysis and framework/program-specific user-written rules to translate specific C framework constructs into Rust equivalents. An eventual goal could be for instance to automatically translate the Linux kernel with the aid of a lot of custom rules to handle its constructs.

That’s almost certainly impossible, but I bet there would be some value in doing the conversion without wrapping anything in unsafe blocks and letting compiler warnings guide through making it safe.

Re: C2Rust: translate C into Rust code

#38
post #21

I think the next step would be to have a tool that can convert C into safe Rust with a combination of static analysis and framework/program-specific user-written rules to translate specific C framework constructs into Rust equivalents. An eventual goal could be for instance to automatically translate the Linux kernel with the aid of a lot of custom rules to handle its constructs.

There's a real sense in which, if this were true, we might not need Rust. If we could mechanically translate the Linux kernel to safe Rust, we could prove the Linux kernel safe. If we could prove the Linux kernel safe, that would be a strong argument against a need to translate it to Rust. Note that this point is independent of the question of whether rewriting the Linux kernel in Rust is actually good/feasible idea.…

The major difference would be that future versions would be in safe Rust as well. All code written after that point would be safe.

What you say is true for a single point in time, not for the long-term future.

But, I would say this is probably infeasible, so theorizing too much about it seems a little wasteful. If the Linux maintainers, Linus et al, suddennly decided to convert to Rust, it would probably done incrementally, module by module. But it’s unlikely to happen given past statements.

Re: C2Rust: translate C into Rust code

#39
post #24

This is compiling C into very unsafe Rust as a target language: pub unsafe extern "C" fn insertion_sort(n: libc::c_int, p: *mut libc::c_int) -> () { let mut i: libc::c_int = 1i32; while i 0i32 && *p.offset((j - 1i32) as isize) > tmp { *p.offset(j as isize) = *p.offset((j - 1i32) as isize); j -= 1 } *p.offset(j as isize) = tmp; i += 1 }; } The output is unmaintainable, like the output from a compiler. This isn't trans…

> This is compiling C into very unsafe Rust as a target language

It’s basically as safe as the original.

> This isn't translation into usable Rust

It’s slightly more usable, and easier, than FFI to raw C (see other comments about Cargo integration).

I’ve been thinking about language rewrites like this, and generally I’m really torn. If it’s the intention to get rid of your C, it might in fact be easier to use bindgen for the C header translation, and create decent Rustful interfaces that abstract the C. After that maybe use this tool as a starting point to get Rust or possibly just rewrite the section by hand. In either case you’ll be rewriting the logic into safe Rust at some point, otherwise I don’t see a huge point in doing this (other than the Cargo integration, though there are other options for that).

Re: C2Rust: translate C into Rust code

#40

Earlier quoted context omitted.

There's a real sense in which, if this were true, we might not need Rust. If we could mechanically translate the Linux kernel to safe Rust, we could prove the Linux kernel safe. If we could prove the Linux kernel safe, that would be a strong argument against a need to translate it to Rust. Note that this point is independent of the question of whether rewriting the Linux kernel in Rust is actually good/feasible idea.…

The major difference would be that future versions would be in safe Rust as well. All code written after that point would be safe. What you say is true for a single point in time, not for the long-term future. But, I would say this is probably infeasible, so theorizing too much about it seems a little wasteful. If the Linux maintainers, Linus et al, suddennly decided to convert to Rust, it would probably done increme…

If we had a standard for safe C that the linux kernel could feasibly meet, then it could be a condition of future changes that it continue to be safe.

As you say, it's pretty hypothetical on both fronts--we're not gonna be able to prove that about a C project like the kernel, and they're not gonna rewrite in Rust any time soon.

Post reply on HN