Rust panics under the hood, and implementing them in .NET

submited by
Style Pass
2024-09-23 05:30:02

I am currently working on a Rust to .NET compiler, rustc_codegen_clr. To get it to work, I need to implement many Rust features using .NET APIs. One of such features is panicking and unwinding.

In this part, I will look at unwinding (the compiler side of panicking), and in the next one, I will explain the Linux GNU std implementation of panicking.

Before I talk about the project, I should probably explain what it is. It is a "rust compiler backend" - but what does that mean?

You can imagine it as a compiler plugin, which replaces the very last step of compilation (code generation). Instead of using LLVM to generate native code, my project turns the internal Rust representation called MIR, into .NET Common Intermediate Language. CIL is then stored inside .NET assemblies, which allows the .NET runtime to load, and execute the compiled Rust code easily.

From the perspective of the Runtime, the compiled Rust looks identical to unsafe C#. So, the Rust code can easily call .NET functions and create .NET objects. In theory, there is nothing you can do in C# that can't be done in Rust too.

Leave a Comment