by hyperhello · 2y ago · 0 comments
• They're opt-in instead of opt-out (meaning none of your system libraries or 3rd party dependencies have bothered using them.)
• What's been implemented in practice frequently fails to find bugs in relatively simple toy examples (I often confuse myself wondering why I've failed to turn some new bit of static analysis on, when in fact I have, and it's simply failed to catch even my demonstration bug.)
• The effective tools, on the other hand, tend to have "false" positives as well.
The conclusion I've come to is that to fully solve these problems in C or C++, you'd effectively transform it into a new language. And at that point, you might as well stop pretending that's not exactly what you're doing, and actually create said new language.
---
That said, when stuck using C or C++, static analysis and the compiler-specific half measures that are available are worth using, IMO.
• https://awesomekling.github.io/Catching-use-after-move-bugs-...
• https://clang.llvm.org/docs/ThreadSafetyAnalysis.html
• https://clang.llvm.org/docs/AttributeReference.html#lifetime...
• https://github.com/MaulingMonkey/notes/blob/master/programmi...
• `printf` type checking annotations exist, but don't necessairly work on MSVC. Often sanest to make your legacy logging macros call `printf` in a dead branch to leverage any hardcoded type checking of `printf`
Basically everything is a warning that you might consider cranking up to an error, of course.
That’s true for rust, too, with the difference being that rust won’t compile false positives. I haven’t used rust or recent C++ much, but it wouldn’t surprise me if the false positives that C++ gives had significant overlap with cases where the equivalent rust code would not compile.
- no uninitialized memory
- clone vs copy semantics as part of the type system
- Send, Sync, "fearless concurrency"
- no unchecked indexing
- type safety (you can't just cast things like in C)
I do think these are clear complements to a borrow checker, since it's all in the interest of preventing undefined behavior. Yet, there are even more nice modern features of Rust that C lacks: - hygienic macros
- namespaces
- pattern matching
- modern build/package systemRust is really high-level when you use it correctly.
And I rarely need lifetime annotations. Which is especially easy if you write in a somewhat functional style.
If you changed the language to support a Rust-like borrow checker, you’d lose back compat which defeats the purpose of stick with C semantics in the first place. There’s the related problem that the borrow checker isn’t automatic. There are places it does good inference, but any structure that has ownership needs to have that owner ship explicitly defined (and often in functions too). That is hard to do when you have to deal with a lot of legacy code that doesn’t have it - while that part can be solved through annotations, things inexpressible in the Rust borrow checker are perfectly legal C so you’d somehow also have to change that code. Additionally the Rust borrow checker can still fail spectacularly on very reasonable code. That’s being addressed in Polonius if that project ever lands, but even then it’s just a step function improvement to solve specific what should be valid code - it doesn’t fix all issues with the borrow checker.
C++ is exploring gradual safeness to provide for optionally strengthening the safety but it’s unclear that approach actually makes sense other than as a balwark to c++ dieing completely (ie the switching cost would be weighed against the cost of fortifying the safety of the code and relying on a poor estimate of the risks involved since there’s too many unknowns)
TLDR: structural, cultural, and back compat issues will prevent anything like the borrow checker.
Maybe in your industry, but not in general.
Dying doesn’t mean that there’s not going to be developers. It just means that it’s no longer growing and the ability to hire developers that have significant c/c++ experience will get harder and harder. It’s still very early days and c/c++ has a lot more legacy code still running than COBOL ever did and COBOL is still alive and kicking in a sense. I would still classify cobol as a largely dead language even though technically people still know it and the code is running.
As for industry, could be. I imagine you think finance but I have a hard time seeing them stick to c/c++ if the rest of tech proper has switched. For new projects, maintenance will be cheaper because of things like cargo crates and other ecosystem niceties that c/c++ will never have. And rust performance is on par with c/C++. So what would be the case for starting any new project in c/C++?
I’m not predicting a timeline. I’m just describing a clear trend. It could easily take 50+ years. Depends on investments by corporations + grass roots + how effective the c++ committee is at stopping the bleeding + whether the rust community does an own goal like trying to standardize the language through ISO which is a huge contributor for why c++ and c will be unable to adjust. But right now rust has a clear velocity and acceleration advantage and has achieved “escape” velocity for when a language will likely take over for another.
It’s not just inertia. Games have very specific latency and timing requirements that required tightly integrated language and compiler features developed for this very purpose. The financial industry too has similar needs and solutions. Rust isn’t a fit for those use cases. Not without a mountain of work that no one is doing, nor would be likely to be accepted as it conflicts with the design goals of Rust.
When XBox, Office and Windows business units really do approach Rust support the same way as they embrace C++ over anything else, then Rust is really in the house.
Yes I am aware of Rust/WinRT and the GDI regions kernel module rewrite.
Rust/WinRT is done by the same guys that botched C++/WinRT, no big hopes with that one.