3 ms·
Well then, don't look too deeply at the nastiness in Go, Rust, and Swift.
by ktRolster 11y ago
Well then, don't look too deeply at the nastiness in Go, Rust, and Swift.
- dsfuoi 11y agoCan you elaborate? Hopefully they don't have straight UB do they?
- ktRolster 11y agoHopefully they don't have straight UB do they? Yes, most languages do, unless they are formally defined (ML is formally defined, but most other languages are not. In this book http://www.amazon.com/Masterminds-Programming-Conversations-Creators-Languages/dp/0596515170 http://www.amazon.com/Masterminds-Programming-Conversations-... several of the language creators say that fully defining the language formally is not worth the effort).
- zokier 11y agoWell, Rust for example does not have a formal well-defined behavior to start with, so its kinda fuzzy about UB too. And of course `unsafe` is another story altogether.
- mastax 11y agoCan you elaborate? The rust docs [1] seem to disagree: Unlike C, Undefined Behavior is pretty limited in scope in Rust. All the core language cares about is preventing the following things: - Dereferencing null or dangling pointers - Reading uninitialized memory - Breaking the pointer aliasing rules - Producing invalid primitive values: - dangling/null references - a bool that isn't 0 or 1 - an undefined enum discriminant - a char outside the ranges [0x0, 0xD7FF] and [0xE000, 0x10FFFF] - A non-utf8 str - Unwinding into another language - Causing a data race And all of those things require `unsafe`, so safe rust cannot do any of them (barring compiler or unsafe library or OS bugs). Edit: And I must admit I don't know much about language theory or formal definition, but there is also a self-described formal grammar [2]. [1]: https://doc.rust-lang.org/nomicon/races.html https://doc.rust-lang.org/nomicon/races.html [2]: https://doc.rust-lang.org/grammar.html https://doc.rust-lang.org/grammar.html
- ArkyBeagle 11y agoI think the best way to approach this question is the see CAR Tony Hoare's "Billion Dollar Mistake" talk. A fellow stands up at the end and says, in detail, something that can be summarized as "it's all golden until you invoke the I/O monad."
- marcosdumay 11y agoWhat do those languages return when you calculate MAX_INT + 1?
- steveklabnik 11y agoIn Rust: let x = std::u32::MAX + 1; This line will generate a warning upon build, and in a debug build, will panic. In a release build, it will overflow.
- drewcrawford 11y agoIn Swift: let a = Int.max + 1 Produces a compile error as the overflow is within Swift's conception of a "literal expression". If we step out to a case where the overflow is not trivially a literal expression: func foo(a: Int) -> Int { return Int.max + a } foo(1) This causes a runtime assertion failure in optimized or unoptimized builds. There is a (rarely-used) "Ounchecked" optimization level for which this program produces UB.