-
Notifications
You must be signed in to change notification settings - Fork 171
Make error messages consistent #678
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
base: main
Are you sure you want to change the base?
Conversation
| } else { | ||
| std::string rtype = ASRUtils::type_to_str_python(right_type); | ||
| throw SemanticError("Casting " + rtype + " to complex is not Implemented", | ||
| right->base.loc); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change makes LPython return the correct message.
One thing that worries me is that this branch of the if statement structure in this function does not do anything after this change. Is that a good design of cast_helper?
It seems that cast_helper gives an error message right away if things cannot be cast. In which case, maybe we should just reformulate what this error message says. Instead of saying "not implemented", we should say "cannot cast c32 to string".
I do like the current error message (in this PR, as seen in the tests). Do we effectively have two places in LPython that return the error? In cast_helper and then later as well?
Should we just have one place, say cast_helper, that checks for errors, and then later we assume they are correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well IMO,
Is that a good design of
cast_helper?
cast_helper is not abstract enough. Instead of a long if-else-if ladder I would prefer a simple rule map based with a tuple of type as keys and the desired cast as the value. Just pass in the types to the rule map and get your cast type. But anyways doesn't matter much now as its already implemented so let's keep using it.
Instead of saying "not implemented", we should say "cannot cast c32 to string".
Agreed. The second message is more transparent and helps in debugging.
Should we just have one place, say cast_helper, that checks for errors,
In an abstract sense, yes there should be only one place which checks for type compatibilities and return errors. If no errors then assume correctness in rest of your implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@faze-geek Why have you removed this SemanticError anyways?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current SemanticError is inconsistent (as pointed in #673) so first I was changing the error inside the else itself . But then by --show-stacktrace I realised that by removing these lines , the type mismatch errors for 1. str + int, 2. str + float and 3. str + complex were getting generated from the same lines in the code ( which I thought is better than error generated from different lines)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought this would be straightforward ,I was unaware of cast_helper.
| semantic error: Type mismatch in binary operator; the types must be compatible | ||
| --> tests/errors/test_str_mismatch.py:5:9 | ||
| | | ||
| 5 | s = "abc" + c | ||
| | ^^^^^ ^ type mismatch (str and c32) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We get this message using the main:
$ lpython examples/expr2.py
semantic error: Type mismatch in binary operator; the types must be compatible
--> examples/expr2.py:5:9
|
5 | s = "abc" + c
| ^^^^^ ^ type mismatch (str and c64)
Note: if any of the above error or warning messages are not clear or are lacking
context please report it to us (we consider that a bug that needs to be fixed).I think this is fixed.
@czgdp1807
Remove unnecessary lines to fix #673