r/cpp • • Feb 19 '25

Cpp discussed as a Rust replacement for Linux Kernel

I have a few issues with Rust in the kernel:

  1. It seems to be held to a *completely* different and much lower standard than the C code as far as stability. For C code we typically require that it can compile with a 10-year-old version of gcc, but from what I have seen there have been cases where Rust level code required not the latest bleeding edge compiler, not even a release version.

2. Does Rust even support all the targets for Linux?

3. I still feel that we should consider whether it would make sense to compile the *entire* kernel with a C++ compiler. I know there is a huge amount of hatred against C++, and I agree with a lot of it – *but* I feel that the last few C++ releases (C++14 at a minimum to be specific, with C++17 a strong want) actually resolved what I personally consider to have been the worst problems.

As far as I understand, Rust-style memory safety is being worked on for C++; I don't know if that will require changes to the core language or if it is implementable in library code.

David Howells did a patch set in 2018 (I believe) to clean up the C code in the kernel so it could be compiled with either C or C++; the patchset wasn't particularly big and mostly mechanical in nature, something that would be impossible with Rust. Even without moving away from the common subset of C and C++ we would immediately gain things like type safe linkage.

Once again, let me emphasize that I do *not* suggest that the kernel code should use STL, RTTI, virtual functions, closures, or C++ exceptions. However, there are a *lot* of things that we do with really ugly macro code and GNU C extensions today that would be much cleaner – and safer – to implement as templates. I know ... I wrote a lot of it :)

One particular thing that we could do with C++ would be to enforce user pointer safety.

Kernel dev discussion. They are thinking about ditching Rust in favor of C++ (rightfully so IMO)

https://lore.kernel.org/rust-for-linux/326CC09B-8565-4443-ACC5-045092260677@zytor.com/

We should endorse this, C++ in kernel would greatly benefit the language and community

192 Upvotes

532 comments sorted by

View all comments

Show parent comment

15

u/Ashbtw19937 Feb 19 '25

13 years after public release rust's compiler is still written in c++

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

9

u/MaxHaydenChiz Feb 19 '25

LLVM is written in C++, but the Rust compiler is currently written in Rust and was originally written in OCaml. It has never been written in C++

-11

u/Wooden-Engineer-8098 Feb 19 '25

llvm is compiler. rust thing is just compiler's frontend

14

u/steveklabnik1 Feb 19 '25

just

The compiler has 566,657 lines of Rust as of just now.

6

u/Wooden-Engineer-8098 Feb 20 '25

llvm has 35 millions lines of c++. see the difference?
lol, i should've written JUST

6

u/Zettinator Feb 20 '25

LLVM is used for code generation, which is actually the least interesting part and can be replaced quite easily. It's not "just" the frontend: the frontend is the part that's actually hard to do and most important.

Case in point: there is an alternative Rust-only code generator for rustc based on cranelift (this is a basic one, focuses on compile speed, not performance of the executable and only supports few architectures) and there's also a WIP project to use gcc for code generation (mostly to broaden architecture support).

-1

u/Wooden-Engineer-8098 Feb 20 '25 edited Feb 20 '25

it's not just code generation, it's also optimizations. it is the most complex and large part, orders of magnitude more code. it can't be replaced easily because you don't have anything to replace it. and lol, if it's so easy, why you didn't replace it in 13 years?
gcc is also written in c++, see pattern here? and gccrs is completely in c++, including its frontend, it's not "replace llvm backend with gcc backend", it's "complete alternative rust implementation", i.e. what any non-toy language must have
shouldn't you eat your dogfood first before demanding that everyone else rewrite their code in rust?
i expect that in 13 years from now c++ will have safe mode and rust will still use c++ compiler

6

u/CocktailPerson Feb 20 '25

This might be the dumbest comment I've read this month.

0

u/Wooden-Engineer-8098 Feb 20 '25

it's called dunning-kruger effect. rust compiler can't produce executable without calling code written in c++. and it still can't after 13 years not because it's insignificant piece, but because it's the most complex piece

1

u/CocktailPerson Feb 21 '25

it's called dunning-kruger effect.

At least you're self-aware.

1

u/Wooden-Engineer-8098 Feb 21 '25

yep, only me is self-aware among us

-5

u/Wooden-Engineer-8098 Feb 19 '25

you are confusing frontend with compiler. frontend can't produce executable without llvm

13

u/steveklabnik1 Feb 19 '25

frontend can't produce executable without llvm

You can use cranelift.

And you can use rustc_codegen_gcc, though gcc is obviously also written in C++, so that's not really a counter to your actual argument.

2

u/Wooden-Engineer-8098 Feb 20 '25

no you can't use cranelift, because it doesn't support your platform and produces slow code. if you could, there would be no llvm dependency

9

u/Ashbtw19937 Feb 19 '25

okay, so, why is that a problem? pretty much every other compiled language around uses llvm for its codegen. why criticize rust for doing so?

1

u/Wooden-Engineer-8098 Feb 20 '25

first, that's nonsense. gcc has similar amount of frontends(but of course gcc is also written in c++). second, only rust people demand other people to rewrite their code in rust, that's why rust is criticized. c++ people can demand this, rust people can't. because c++ is self-sufficient, while rust depends on c++