|
|
Subscribe / Log in / New account

Progress toward a GCC-based Rust compiler

Progress toward a GCC-based Rust compiler

Posted Dec 19, 2023 16:37 UTC (Tue) by steveklabnik (guest, #114343)
In reply to: Progress toward a GCC-based Rust compiler by rrolls
Parent article: Progress toward a GCC-based Rust compiler

Borrow checking does not affect codegen in any way. As long as the code runs successfully under a borrow checker, a compiler without one can compile it with zero issues. As rustc has been checked by rustc's borrow checker, mrustc will (modulo other bugs, of course) not miscompile it.

At one point mrustc produced a byte-identical compiler to rustc, I am not sure if that's a one-time thing or if they always do that, though.


to post comments

Progress toward a GCC-based Rust compiler

Posted Dec 20, 2023 8:20 UTC (Wed) by rrolls (subscriber, #151126) [Link] (1 responses)

Aha, so basically, the solution "borrow-check it with rustc, then compile it with whatever" effectively _is_ the solution "write the borrow-checker in a lower-level language". (Since IIRC, each rustc is built with an older rustc, but at some point you'd get back to a version of rustc that wasn't written in Rust.)

One more complication, then. Is "safety" _purely_ a property of the source code, or does it depend on the environment it's being compiled in as well? For example, you use rustc to borrow-check your source code on a typical consumer-grade x86-64 linux platform, and now you compile the same code on some esoteric architecture and operating system. Presumably, the environment can affect, for example, how generics get instantiated, which might turn something that was deemed safe on the first platform into something that's not fine at all on the second? Or does Rust insist that code is only deemed safe if every possible instantiation is safe? I suppose you could request cross-compilation in your borrow-check step, which might address this, but it does add some complication.

Progress toward a GCC-based Rust compiler

Posted Dec 20, 2023 13:13 UTC (Wed) by farnz (subscriber, #17727) [Link]

The part of safety that the borrow checker is responsible for is purely a property of the source code; it confirms that (for example) that the shared XOR mutable invariant is maintained by safe Rust.

Anything which can't be machine-checked at the source level as maintaining the safety invariants is supposed to marked with unsafe, which indicates that the human programmer is responsible for checking that things are safe. This means that anything outside Rust source (such as machine-specific primitives) should be marked as unsafe, and it's on the humans writing a safe wrapper to ensure that the wrapper maintains invariants at all times.

Rust does insist that all instantiations are either safe, or marked appropriately with unsafe; additionally, the caller of an instantiation of a generic that's marked with unsafe must mark their calling block with unsafe to indicate that they're OK with this. You cannot have an unsafe instantiation of a safe generic in safe code - you need the markers to tell Rust that you've thought about this and are going to uphold the language invariants, even if the compiler can't check your working (which is true of platform interfaces, for example, where the compiler can't check the platform behaviour).


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds