News for hackers. Stuff that matters.
Re:The Value of Undefined Behavior by cperciva on Saturday July 21, 2018 @05:20PM (#17583913)
Rust has it right here: don't allow undefined behavior and make the programmer specify the behavior they want.

If they want wrapping overflow, fine. If they don't, great.

What if I know overflow can't happen (e.g. I have an integer counting from 1 to 10)? How do I say "it doesn't matter how you handle overflow; just do whatever is fastest"?

Re:The Value of Undefined Behavior by steveklabnik on Saturday July 21, 2018 @06:01PM (#17584079)
The parent spoke a bit too broadly. Here’s the rules:

* overflow is a “program error”, not UB

* compilers must either check for overflow and panic, or two’s compliment wrap

* when debug_assertions are on, implementations are required to panic on overflow

This means release mode wraps today, but if it’s cheap enough someday, we could make it panic.

If you want specific semantics on overflow, then you should use the various wrappers and/or methods that let you do them directly, rather than relying on any of the above. But you don’t have to specify; by default, the above is the semantics.

Re:The Value of Undefined Behavior by Paul-ish on Saturday July 21, 2018 @08:12PM (#17584567)
What if rust got "unchecked" (unsafe) versions of arithmetic operations that have C arithmetic semantics. Kind of the opposite of "checked_add".
Re:The Value of Undefined Behavior by steveklabnik on Sunday July 22, 2018 @04:17AM (#17585811)
It’s possible! Nobody has ever proposed it...
Re:The Value of Undefined Behavior by tolmasky on Saturday July 21, 2018 @05:57PM (#17584068)
In a language with a sufficiently advanced type system you can use Peano numbers to specify a type specifying an integer between 0 and 10. These type systems usually are smart enough to have optimized internal representation of said types.
Re:The Value of Undefined Behavior by steveklabnik on Saturday July 21, 2018 @06:02PM (#17584086)
You can do that in rust, there’s even a package for it. It will destroy your compile times though.

In the future we’ll have const generics that let you express it directly without that shenanigans.

Re:The Value of Undefined Behavior by tolmasky on Saturday July 21, 2018 @06:05PM (#17584092)
I would love to have something that would translate a more human readable version that looks like a constraint or range that internally translates to the same functionality of the peano solution. If that's what you're saying Rust intends to have then I'm very excited and continue to be impressed by the progress there.
Re:The Value of Undefined Behavior by steveklabnik on Saturday July 21, 2018 @06:36PM (#17584204)
https://github.com/rust-lang/rfcs/blob/master/text/2000-cons... is the current accepted design; we plan to go further in the future, but wanted something minimal at first. This is likely to land in nightly by the end of the year and be stable sometime early next year.
Re:The Value of Undefined Behavior by jgibson on Saturday July 21, 2018 @06:14PM (#17584134)
Doesn't Ada have this ability where you define numeric types with a range?
Re:The Value of Undefined Behavior by MaxLeiter on Saturday July 21, 2018 @06:21PM (#17584146)
Yes, like so

  type jump_by_tenth is delta 0.1 range 1.0 .. 99.0;
Re:The Value of Undefined Behavior by adrianN on Sunday July 22, 2018 @12:54AM (#17585349)
Yes but afaik it translates to runtime checks.
Re:The Value of Undefined Behavior by red75prime on Sunday July 22, 2018 @03:09AM (#17585621)
It cannot be done in compile-time in general case. Rice's theorem forbids. Every non-trivial property of a program (like whether result of this function lies in this range for all inputs in that range) is undecidable.

ETA: I'm not completely correct. Ada could have used something akin to 2-complement overflow handling, that is forcing every operation on a range to have a result in the same range, but it introduces runtime overhead too.