Live data from Hacker News

Corrode: C to Rust translator written in Haskell

github.com

41–50 of 127 posts

Re: Corrode: C to Rust translator written in Haskell

#41

Earlier quoted context omitted.

I guess the next stage would involve translating common non-idiomatic patterns into idiomatic Rust. Looks like this could be a job for a community-managed database!

On the rust subreddit someone tongue-in-cheek suggested `cargo clippy | rustfix` to be used in conjunction with this tool for better rust code. But that actually could work! Clippy has a ton of lints that make your code more idiomatic, and rustfix basically takes diagnostic output and applies suggestions (still WIP). Clippy is geared towards making human-written unidiomatic code better, so it might not catch some sil…

I haven't used nightly much, what all does Clippy do?

Re: Corrode: C to Rust translator written in Haskell

#42
post #30

It's too bad this is written in Haskell. I don't have anything against Haskell, it is just not as popular a language as others.[1] Any ANTLR target language would have been a solid choice.[2] This way more of the community could contribute. This is an invaluable tool if we're truly going to see a shift from C (or C++) to Rust. [1] http://pypl.github.io/PYPL.html [2] http://www.antlr.org/download.html

The author told me that one of the reasons he chose Haskell was that there was already great tooling around working with C source code: https://hackage.haskell.org/package/language-c

Re: Corrode: C to Rust translator written in Haskell

#43
post #35

Earlier quoted context omitted.

Here you go. It didn't like my stdio.h. Apparently enums and unions aren't supported, but: extern int printf(char *, ...); int main(int argc, char argv[]) { printf("Hello, world!\n"); return 0; } Was turned into: extern { fn printf(arg1 : *mut u8, ...) -> i32; } #[no_mangle] pub unsafe fn main(mut argc : i32, mut argv : *mut u8) -> i32 { printf(b"Hello, world!\n\0".as_ptr() as (*mut u8)); 0i32 } edit: Also worth noti…

Shouldn't a C `int` be converted to Rust's `isize`. I think that captures the spirit better.

I'm in no way connected to the project. Perhaps you should file an issue.

Re: Corrode: C to Rust translator written in Haskell

#44
post #32

I was curious about how this worked so I looked into the source a little (even though Haskell isn't exactly my cup of tea), and WOW... This is just amazing. The most important part of the source is highly educative literate haskell: https://github.com/jameysharp/corrode/blob/master/src/Langua...

That's an incredibly brilliant idea, there's some serious craftsmanship going on in that source file. Kinda taking Rust's doc unit tests format to a whole new level.

Rust should have a literate source format - with how much the community focuses on documentation, this would be an excellent addition.

Re: Corrode: C to Rust translator written in Haskell

#45

Earlier quoted context omitted.

On the rust subreddit someone tongue-in-cheek suggested `cargo clippy | rustfix` to be used in conjunction with this tool for better rust code. But that actually could work! Clippy has a ton of lints that make your code more idiomatic, and rustfix basically takes diagnostic output and applies suggestions (still WIP). Clippy is geared towards making human-written unidiomatic code better, so it might not catch some sil…

I haven't used nightly much, what all does Clippy do?

It tells you about places where you can improve your code. Possible pitfalls, style issues, documentation issues, unidiomatic code, everything.

Its a developer tool so you can use rustup to switch to nightly to run clippy (and use stable otherwise) and not impose nightly on the rest of the people who use the project. We have plans for making clippy a tool that you can fetch via rustup without requiring nightly.

Re: Corrode: C to Rust translator written in Haskell

#46

Earlier quoted context omitted.

That's an incredibly brilliant idea, there's some serious craftsmanship going on in that source file. Kinda taking Rust's doc unit tests format to a whole new level.

Rust should have a literate source format - with how much the community focuses on documentation, this would be an excellent addition.

https://github.com/rust-lang/rust/issues/26097

Re: Corrode: C to Rust translator written in Haskell

#47
> Partial automation for migrating legacy code that was implemented in C. (This tool does not fully automate the job because its output is only as safe as the input was; you should clean up the output afterward to use Rust features and idioms where appropriate.)

This was my immediate concern. Is there any chance this tool can produce anything close to clean, safe, idiomatic, rust code?

Re: Corrode: C to Rust translator written in Haskell

#48

Earlier quoted context omitted.

Rust should have a literate source format - with how much the community focuses on documentation, this would be an excellent addition.

https://github.com/rust-lang/rust/issues/26097

With the literate style, code is prefixed and comments are raw. So, instead of:

    -- foo is a function
    foo :: String -> String
It's

    foo is a function
    > foo :: String -> String

Re: Corrode: C to Rust translator written in Haskell

#49

Earlier quoted context omitted.

https://github.com/rust-lang/rust/issues/26097

With the literate style, code is prefixed and comments are raw. So, instead of: -- foo is a function foo :: String -> String It's foo is a function > foo :: String -> String

You could probably hack something together with a build.rs and a preprocessor, but that would probably not be very nice long term.

Re: Corrode: C to Rust translator written in Haskell

#50
post #35

Earlier quoted context omitted.

Here you go. It didn't like my stdio.h. Apparently enums and unions aren't supported, but: extern int printf(char *, ...); int main(int argc, char argv[]) { printf("Hello, world!\n"); return 0; } Was turned into: extern { fn printf(arg1 : *mut u8, ...) -> i32; } #[no_mangle] pub unsafe fn main(mut argc : i32, mut argv : *mut u8) -> i32 { printf(b"Hello, world!\n\0".as_ptr() as (*mut u8)); 0i32 } edit: Also worth noti…

Shouldn't a C `int` be converted to Rust's `isize`. I think that captures the spirit better.

They're different types. isize is ssize_t (well, intptr_t), in that it is tied to the size of the address space, while C's int is not constrained. In fact, it is usually 32 bits, even on 64-bit architectures, where isize is 64 bits.
Post reply on HN