-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add better error messages for dimension mismatches in promote_array #6698
Conversation
Great! This is a step in the right direction. |
Honestly, this was just something that annoyed me while I was working on a project, and I just want this fixed so that I don't run into it anymore when I'm not using my own patched Julia version. ;) |
Yeah, if you feel motivated enough to improve all error messages in the same vein, please go on! (I'm thinking of e.g. BoundsError... :-). It's terrific that Julia makes it so clean to generate helpful error messages. |
I suspect doing this for BoundsError would be a total disaster. Building the string or even just allocating an exception object requires a lot more code; too much to do for every array access. In this commit, it's not great that the string is repeated everywhere. It would be better to throw |
That was my initial thought as well, but then taking a look at the usage of
Checks that a matrix is square aren't really easily put into a generic message that |
That "matrix is not symmetric" error really doesn't strike me as a dimension mismatch. This is a pitfall of using a free-form string for everything. We might need another distinction, like |
@JeffBezanson Would calling |
I think it's related to the (possible) need for garbage collection… even if that branch isn't traversed. Jeff explained this nicely to me a while ago:
|
It has to do with (1) code size, (2) extra overhead for functions that might allocate memory vs. those that never do. |
Being a nonsquare matrix is a dimension mismatch of sorts, if one interprets it as a mismatch between two dimensions of the same matrix. Technically, passing a rectangular matrix to a function that expects a square matrix could also be a It would be nice if The Woodbury stuff (wikipedia) refers to the |
@JeffBezanson @mbauman OK, so there's no way out? Getting the details of an out of bounds error is often very useful -- but of course it cannot come at a cost for every array indexing operation. |
I think the best way forward would be to have the ability to intercept the error in a debugger, where you could inspect the stack and see the values of all variables. That would be even more general without requiring us to commit to a more heavyweight BoundsError. |
A debugger like that would be really awesome, but for pieces of code that On Thu, May 1, 2014 at 11:52 AM, Jeff Bezanson notifications@github.comwrote:
|
Agreed. Maybe there can be a mechanism which would work similar to a debugger, but would only generate an error message from a few variables. That sounds terribly similar to exceptions, though. It's sad to have to sacrifice good error reporting for the sake of performance... :-/ |
One of the most frustrating things in a new language, is when you don't understand the error message. Clear and helpful error messages is something that would make Julia really shine, compared to the competition. If we want to attract users that have not already tried the Is there any way we could have a nice syntax to do both? What about making throw behave like a macro that wraps the exception and string manipulation in a anonymous function? Wouldn't that defer the setup of the GC frame until there is actually an error? |
@staticfloat – this is entirely orthogonal to the ideas I mentioned about error handling. Figuring out how to throw more explicit exceptions without trashing performance is relevant regardless. @JeffBezanson – even if we can intercept issues in the debugger, what about logging errors in non-interactive situations? In such cases, you'd still want something more informative. An interesting option would be to be able to record part of the stack so that it could be explored later. Fundamentally, the issue here is that you want to be able to include the stack in an error object and reference local values in the top stack frame without having to do any real work. |
You don't need an anonymous function specifically, but we could have It would be great to be able to capture the stack and inspect it later. But, it is hard to translate that to something that can be usefully printed to a log file. And there would have to be an option to control this since it would make exceptions very expensive indeed. |
For the original example, on master/LLVM 3.3 I see:
It is not as terrible with the line information, though it could certainly be a bit nicer. Presumably @staticfloat is on LLVM 3.4 and this is partially a dupe of #6275. |
By the way, a backtrace is already saved on every Is it correct to say that we only really care about the local cost of throwing an exception? (ie, not what happens after we hit |
Isaiah is right, the backtrace problems aren't the issue here. Mainly I
|
Since there's always the possibility to use |
Assuming that I don't really want to make changes here that will propagate out and affect the entire codebase, what's the resolution on the way forward that makes everyone happy? I don't want to mess around with DimensionMismatch until we have a good consensus on what is and is not okay for Exceptions to do, but I think a change like what I'm proposing in this specific instance is a good thing to have, from a usability standpoint. |
bump |
I'm going to close this PR pending a more extensive overhaul of errors |
Before this change:
After this change: