News for hackers. Stuff that matters.
Re:Announcing Rust 1.9 by spion on Thursday May 26, 2016 @07:56PM (#11782741)
I don't know how this unexpected vs expected errors philosophy gets propagated, but to me it always looked suspicious. Take array bounds for example: what if you have an API that lets users send a list of image transformations, and the user requests a face-detect crop, followed by a fixed crop with a given x, y, w, h.

Clearly your code can get out of (2D) array bounds with the fixed crop (if the image is such that the face-detect crop ends up small enough). Suddenly the thing that was "unexpected" at the array level becomes very much expected at the higher API level.

So the API provider can't decide whether an error is expected or not. Only the API consumer can do that. Applying this further, a mid-level consumer cannot (always) make the decision either. Which is why exceptions work the way they do: bubble until a consumer makes a decision, otherwise consider the whole error unexpected. Mid-level consumers should use finally (or even better, defer!) to clean up any potentially bad state.

I think Swift got this right. What you care about isn't what exactly was thrown (checked, typed exceptions) but the critical thing is whether a call throws or not. This informs us whether to use finally/defer to clean up. The rest of error handling is easy: we handle errors we expect, we fix errors we forgot to expect, but either way we don't crash for clean up because finally/defer/destructors take care of that.

Re:Announcing Rust 1.9 by Manishearth on Thursday May 26, 2016 @11:03PM (#11783615)
This is why almost every panicky API has a non-panicky variant.

Non-panicky APIs can be used in a panicky way via unwrap() or expect(). There are very few panicky APIs, reserved for things where 99% of the time you want it to panic and it would be annoying to have to handle errors all the time. Array indexing is one of these cases, where it would be super annoying to handle errors on each indexing operation.

But if you're in a situation where you expect an index to fail, just use `get()` which returns an Option instead.

APIs do not make the decision for you. At most they decide if the panicky API is easier to use than the monadic error one. Which is where the fuzzy notion of "unexpected" and "expected" errors at the API level comes up, it's good enough to be able to determine what the default should be. Callers still have the power to do something else.

Re:Announcing Rust 1.9 by dikaiosune on Thursday May 26, 2016 @11:36PM (#11783731)
I would kill to have a monadic slicing API for Vec<T>, though. It seems like the one significant exception to the "use whichever works better for you" argument. Of course you can bounds check yourself first and return Err if there's a problem, but there's also no get_unchecked() for slices so you'd pay for the checks twice.
Re:Announcing Rust 1.9 by Sean1708 on Friday May 27, 2016 @04:28AM (#11784654)
> but there's also no get_unchecked() for slices so you'd pay for the checks twice.

Yeah there is[1], unless I'm being really stupid...

[1]: https://doc.rust-lang.org/std/primitive.slice.html#method.ge...

Re:Announcing Rust 1.9 by dikaiosune on Friday May 27, 2016 @12:31PM (#11787101)
Rather, there's no get_unchecked to get a slice from a Vec. Unlike index, get_unchecked can't take a range as input.
Re:Announcing Rust 1.9 by Sean1708 on Friday May 27, 2016 @02:51PM (#11788064)
Ah gotcha, sounds like it could be a good first RFC for someone.
Re:Announcing Rust 1.9 by dikaiosune on Saturday May 28, 2016 @01:58AM (#11790738)
I'll add it to my long list of rust projects I wish I had time to do :).
Re:Announcing Rust 1.9 by spion on Friday May 27, 2016 @03:27AM (#11784453)
But if you are a mid-level consumer, you don't know whether you can pick the panicky or the non-panicky API. Do you provide 2 APIs again?
Re:Announcing Rust 1.9 by Manishearth on Friday May 27, 2016 @04:48AM (#11784714)
No, you provide the non-panicky API, consumers can convert those into panics easily.

In Rust, if your API uses monadic error handling (Result or Option), this is easily chained with other errors and propagated up. Additionally, callers can choose to discard errors they feel should not happen by using unwrap/expect to convert them into panics.

If your API uses panics, this can't be cleanly turned into non-panicky things without using catch_unwind (you can, but you shouldn't). In the rare case you make the decision to have an ergonomic panicky API, try to provide a slightly less ergonomic non-panicky one.

Re:Announcing Rust 1.9 by spion on Friday May 27, 2016 @10:20AM (#11786199)
So ultimately the reason for providing a panicky API has nothing to do with expectedness, its actually all about performance. Otherwise we'd always go for a non-panicky one.

Also, in languages with finally and/or defer, catch isn't supposed to be a problem.

Re:Announcing Rust 1.9 by Manishearth on Friday May 27, 2016 @11:28AM (#11786617)
I never said that.

Not performance, ergonomics. `array[5]` and `array.get(5).unwrap()` do the same thing (with a different panic message) and have the same performance when optimized (since it should inline). It would be annoying to have to handle the error every time you index, and would kind of ruin the syntactic sugar of indexing. Thus, indexing panics, and you have `.get()` if you want to not panic and use monadic errors (but as you can see, you can make `.get()` panic too by unwrapping it).

The same goes for other panicky APIs, they're panicky because 99% of the time you don't want to deal with that error since it's an "unexpected" one which means something went horribly wrong and there's no sane way to continue. For the 1% of the time you do need to deal with that error, you can use the alternate Result/Option-based API.

Re:Announcing Rust 1.9 by spion on Friday May 27, 2016 @12:16PM (#11786977)
`try!` seems like sufficient sugar to deal with the ergonomics part.

> and there's no sane way to continue

Would like to hear more about this.

Re:Announcing Rust 1.9 by Manishearth on Saturday May 28, 2016 @03:09AM (#11790864)
Not really, you have to change the return type of all of your functions and introduce new error types.

There's no same way to continue in cases where an out of bounds access is a program bug. What should a program do when it knows it is in an inconsistent state? Crash, in many cases.

Re:Announcing Rust 1.9 by spion on Saturday May 28, 2016 @10:35PM (#11794509)
Why is the program in an inconsistent state when it attempts to access an array out of bounds and is prevented from it?
Re:Announcing Rust 1.9 by Manishearth on Sunday May 29, 2016 @06:36AM (#11795858)
Because it was designed assuming that the array would not go out of bounds. If an array did go out of bounds, clearly something has gone horribly wrong.

This is a question of how the software was designed and what assumptions were made.

In a case where the programmer feels that out of bounds may happen (or wishes to program defensively), of course the usual monadic error APIs can be used instead of indexing directly.

Re:Announcing Rust 1.9 by spion on Sunday May 29, 2016 @05:43PM (#11798366)
This "clearly something has gone horribly wrong" is where I think we actually get lazy.

Let me illustrate using my example. The mid-level consumer is a crop function that takes the image and coordinates. It has no reason to assume someone wont check the image bounds so it uses the panicky API to access the array, assuming "Ok, they'll check if they are out of image dimensions beforehand".

But another level up, we're passing user-generated data right to it, after the face detection crop. Clearly thats wrong. Unfortunately the mid-level consumer didn't provide a non-panicky API so we had to write our own wrapper.

The question is, is it crashy-wrong? If shared mutable state is cleaned up via RAII/defer/finally, there is nothing wrong with the "exception" being caught and the process can continue running. The reason why we don't know if something went horribly wrong is that we don't have the cleanup contract for handling these types of things in the first place.

So why do we want this extremely unsafe "cleanupless" contract to begin with? I can't think of any other reason than performance, because syntax sugar can always take care of the rest.

Re:Announcing Rust 1.9 by Manishearth on Sunday May 29, 2016 @07:54PM (#11798822)
> Ok, they'll check if they are out of image dimensions beforehand

that's a straw man example, its not the kind of API I'm talking about.

Let me repeat: this is for cases where the library developer has written code where it won't go out of bounds, and if it does it's a bug in their own library. Not the input. Rust APIs are not designed the exception-way where unexpected input is supposed to be caught like exceptions.

For example, I may allocate a buffer of size n, and index it willy nilly at various points I know to be less than n (perhaps, for example, values from another array which was constructed with values not exceeding n). If this doesn't work, it's a bug in your program and there no two ways about it. The assumptions you made about your own program when writing it were wrong. This can happen if you make a mistake when programming, or mess up a refactoring, or rely on a library that changes behavior (which itself shouldn't happen but the world isn't perfect).

Almost all of the indexing panics I've seen in Rust have been bugs in the code that was indexing; where continuing the program somehow would probably make things worse since the state is inconsistent (according to the assumptions the programmer made about their own code, not the input). People tend to use `.get()` when it's possible that the input may trigger an out of bounds error.

Its not a performance issue; the bounds check happens either way, please stop making it one. Its an ergonomics issue. Rust tries to offer both imperative and functional styles, and also tries to be palatable to people from C++. This leads to some C++-like defaults like the indexing syntax. If people had to unwrap/try on every index in rust half the community would probably revolt.

(note that indexing isn't that common; rust uses iterators and other safer, bounds-check-contained abstractions, but it was determined that when you want to use direct indexing in Rust you usually are designing your code such that the out of bounds is a bug)

Re:Announcing Rust 1.9 by blaisio on Thursday May 26, 2016 @09:47PM (#11783330)
Your example doesn't make sense. Keep in mind that an out of bounds array access can cause a lot of problems; in the best case, it would cause a segfault, and in the worst it would allow for code injection. So there isn't really a case where accessing an array out of bounds makes sense. That's where the idea of "expected vs unexpected errors" comes from.

Another way to think about it is that some errors can be handled and some can't. For example, most of the time you can't "fix" a segfault, so it makes sense for that to just crash the program.

Re:Announcing Rust 1.9 by nickm12 on Thursday May 26, 2016 @09:08PM (#11783151)
I don't understand your example. The API provider has to determine the contract and decide whether out-of-bounds crops are supported or not. If they are supported, then an out-of-bounds panic is a bug in the API. If they are not supported, then an out-of-bounds panic is a bug in the caller. In either case, the exception is unexpected and due to someone not following the contract.
Re:Announcing Rust 1.9 by oconnor663 on Thursday May 26, 2016 @09:56PM (#11783364)
In cases where the caller might want to handle an error, Rust almost always uses an Option/Result. For vectors, the `get` method lets you try to get an element out without panicking if your index is bad. (Or of course you can just check the index manually. This is guaranteed to be threadsafe, which is nice.) The try! macro is the standard way to say "I don't want to handle errors here -- you take care of it."