Live data from Hacker News

C2Rust: translate C into Rust code

c2rust.com

21–30 of 79 posts

Re: C2Rust: translate C into Rust code

#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.

Re: C2Rust: translate C into Rust code

#22
post #11
post #10

Earlier quoted context omitted.

Fortunately, once you start refactoring the Rust to take advantage of the functionality that doesn't exist in C (like slices, iterators, etc), things start to look much cleaner. Translating the code into ugly, unsafe Rust is intended to only be the first step. We're working on tools to help with that refactoring process, too.

Sounds like a case of "writing C in Rust" compared with "writing Rust". You're not really writing in a language until you're thinking in its idioms.

A translator like this is still really useful as the first step of transforming a C codebase into a Rust codebase. The important thing is to get working code first, and then you can modify the code to be more idiomatic.

Re: C2Rust: translate C into Rust code

#23
post #5

The insertion sort conversion example makes Rust look intimidating and verbose as compared to C :-).

It's unsafe code manipulating raw pointers, which is "intimidating and verbose" by design.

It's also explicitly typing everything, which increases verbosity.

The "clean" rust conversion would look something like this:

    fn insertion_sort(p: &mut [T]) {
        for i in 1..p.len() {
            let x = p[i];

            let mut j = i;
            while j > 0 && p[j-1] > x {
                p[j] = p[j-1];
                j -= 1;
            }
            p[j] = x;
        }
    }
Although unless it's really necessary I would expect the simpler version:

    fn insertion_sort(p: &mut [T]) {
        for i in 0..p.len() {
            for j in (0..i).rev() {
                if p[j] 

Re: C2Rust: translate C into Rust code

#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 translation into usable Rust.

A C++ to Rust translator would be a good thing, but it has to be much smarter than this. This doesn't even comprehend arrays in C.

A good translator might need some human help to deal with C's ambiguities. Like "Is this an array? (Y or N)" How big is it? (Enter expression). Then you get a program that's real Rust. If the user is wrong about the size, safe Rust will either allocate too much space or get a subscript error.

Re: C2Rust: translate C into Rust code

#25

What's the use case for this? Are native C binaries/libraries not invokable in rust?

Yes, you can call any C using the Rust FFI but then you need to create a safe wrapper around the unsafe Rust calls to C. This tool looks like it's for generating a first pass at porting existing C code to Rust. Once you have that C-to-Rust conversion, it becomes much easier because you can incrementally port select types instead of incrementally porting the API and dealing with the impedance mismatch between the Rust api and C api (you can work on function arguments instead of function calls).

Re: C2Rust: translate C into Rust code

#26
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…

The problem is the type noise, which may or may not be avoidable. Most of the mess you see there is probably better expressed as std::mem::* function calls which give proper names to the operations (you can see one of them, .offset, used here but it's a very general function). I don't think it's a good idea to try to convert C code like this to more complex std::mem calls automatically but once you have the ugly Rust code, it becomes relatively easy to start porting it to be more Rustic.

The bigger issue I see here is that this should be implemented as an iterator. I don't think there's any hope of automatically converting to those though - there's just no C equivalent except pointer arithmetic.

Re: C2Rust: translate C into Rust code

#27
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.

Re: C2Rust: translate C into Rust code

#28
post #10
post #5

The insertion sort conversion example makes Rust look intimidating and verbose as compared to C :-).

Fortunately, once you start refactoring the Rust to take advantage of the functionality that doesn't exist in C (like slices, iterators, etc), things start to look much cleaner. Translating the code into ugly, unsafe Rust is intended to only be the first step. We're working on tools to help with that refactoring process, too.

Yeah, I assumed that this was just converting word-for-word as a first step and one was expected to then go back to use Rust idioms to tweak the code if necessary. Definitely useful if you want to move code-bases to Rust entirely.

Re: C2Rust: translate C into Rust code

#29
post #13

So the demo does actually translate C into unsafe Rust. I'm guessing this translator is, thus, unaware of Rust borrowing/ownership system?

> So the demo does actually translate C into unsafe Rust. It's semantics-preserving, and C constructs are not expected to match safe Rust, so that makes sense, Corrode does more or less the same. Citrus generates "safe rust", it also doesn't generate working code.

The compilation target could be a Rust slab [0], much like Emscripten uses Typed Arrays [1]. In doing so, it would retain much of the safety properties of Rust, but it pushes the problem into maintaining "the heap" for the C code.

[0] https://github.com/carllerche/slab

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Type...

Re: C2Rust: translate C into Rust code

#30
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. I also think that Rust has other advantages over C that aren't just safety, so it's not a complete comparison--safety is just the biggest one.

Post reply on HN