Normal compiler hoisting operations aren't allowed to do that. It violates normal execution order semantics. If you don't do that, you can't optimize checks fully. This is why generating assert statements in macros or templates and then trying to optimize them doesn't work. Asserts may need to be hoisted through statements with side effects. Compilers have to handle that at the flow graph level.
This works best if you're not allowed to capture such errors. If you're headed for an unavoidable crash, it's best to have it sooner, rather than later. Go takes a hard line on this - no exception mechanism. (Admittedly some people are abusing Go's "panic/recover".) Google's position is that in their kind of operation, it's better to fail the whole program. They can recover from a failed server, after all. Erlang also take a "fail early" position.
As for the need to eliminate errors, the recent history of computer language development has been in the direction of increased safety. All the major scripting languages are memory-safe. Newer hard-compiled languages such as D and Go are memory-safe, although they have to use GC to do it. A whole generation of programmers have never had to look for a buffer overflow or a dangling pointer.
Meanwhile, in the security world, the buffer overflow threat has become worse. It's not script kiddies any more. It's national intelligence agencies (ours, theirs, who knows who else?) with big budgets and competent people looking for exploits. There's a black market in zero-day exploits now. Rust has a fighting chance to fix that. Not just trim it back a little. Fix it. Please don't blow this chance.
Latest buffer overflow reported by CERT: November 14, 2014:
https://www.us-cert.gov/ncas/alerts/TA14-318B
The Microsoft Windows OLE OleAut32.dll library provides the SafeArrayRedim function that allows resizing of SAFEARRAY objects in memory.... In certain circumstances, this library does not properly check sizes of arrays when an error occurs. The improper size allows an attacker to manipulate memory in a way that can bypass the Internet Explorer Enhanced Protected Mode (EPM) sandbox as well as the Enhanced Mitigation Experience Toolkit (EMET).
This vulnerability can be exploited using a specially-crafted web page utilizing VBscript in Internet Explorer. However, it may impact other software that makes use of OleAut32.dll and VBscript. Exploit code is publicly available for this vulnerability. Arbitrary code can be run on the computer with user privileges.
That's why "carefully checked" "unsafe" code doesn't work. "SafeArrayRedim" - wasn't.
They all have the ability to write unsafe code in the same manner as Rust; but only Rust has it really built-in to the language so the compiler can reason about it. E.g.
- http://golang.org/pkg/unsafe/
- https://docs.python.org/3.4/library/ctypes.html
It just doesn't make sense to presuppose that it is any worse in Rust. In fact, quite the opposite, the type system is designed for ensuring memory safety without a GC, and in multithreaded environments. I believe this is not true for Go (it is vulnerable to data races, and requires a runtime tool to detect some subset of the ones can occur in any given program), but I don't know about D.
Rust cannot remove `unsafe` without being useless for its target, and useless as a language (it wouldn't even have FFI).
The low-level code has to exist somewhere, vectors need to be allocated, strings need to be printed, and people will want the performance. This code is either going to be in external libs called via FFI, as raw codegen in the compiler or in Rust. The last sounds like the one that is safest since the FFI code would have to be written in one of the unsafe systems languages, while writing in Rust gives all the type system advantages and high-level features (references, pattern matching), not to mention avoiding the (mental) overhead of correctly interacting between languages.
It seems to me that taking away the low-level power of opt-in `unsafe` is just myopically punting the problem along, rather than seeing it as giving Rust the ability to avoid having to write really unsafe C plugins (like Python etc.).
I like to think of Haskell as a parallel: It guarantees side effect-free code. But you still need an escape hatch in the form of unsafePerformIO in order to use the FFI. The language's semantics can easily be broken with that functionality, but it seems to be a necessary price to pay if you want to have any kind of FFI. (<- Well, I guess something like Proof Carrying Code could be an alternative..)
> All the major scripting languages are memory-safe.
> Newer hard-compiled languages such as D and Go are memory-safe
Rust's definition of safety actually surpasses most of these languages, as it extends to safety in the face of concurrent operations. Rust statically prevents data races, which AFAIK is a property that until now has only been exhibited by thoroughly immutable languages such as Erlang. That it manages all this without a GC is very exciting. > Meanwhile, in the security world,
> the buffer overflow threat has become worse.
The Rust developers agree with you. Rust is motivated by the fact that Mozilla is tired of Firefox getting pwned by dint of C++'s looseness, and despairing at the fact that further improvements to Firefox will entail tacking concurrency onto a multi-million-line C++ codebase dating back to 1997, which would be notoriously difficult even if security weren't a concern. They're writing a brand-new browser engine in Servo with a focus on massive concurrency and user safety.I guarantee you that Rust cares a great deal about safety. It goes to lengths that almost no other language is willing to go in pursuit of this goal. However, it is also a thoroughly pragmatic language. We won't make the world a better place by blindly preaching dogma. Rust also won't be the last language ever made. If nothing else, even baby steps toward a safer world are a measure of progress. :)