Improve error messages for invalid binary strings#16688
Improve error messages for invalid binary strings#16688sholderbach merged 5 commits intonushell:mainfrom
Conversation
63f6b30 to
a06b11e
Compare
|
Sorry if I had to forced-push 😓. I had to resolve merge conflicts from Rust 1.88.0 PR. |
sholderbach
left a comment
There was a problem hiding this comment.
Thanks for tackling this, looks good to me for the most part.
Welcome a second set of eyes regarding the integration into the chain of which erorrs are raised or fall back into a different parse.
| } else if !matches!( | ||
| working_set.parse_errors.last(), | ||
| Some(ParseError::Expected(_, _)) | ||
| ) { |
There was a problem hiding this comment.
By looking at just the diff I can't immediately tell why this (negative) match is OK to pass with error suppresion.
Can you explain that briefly? Maybe it is worth adding a comment or a different match here.
There was a problem hiding this comment.
This is similar to how parse_number matches (except without error supression). Before this change, the parser actually does not stop parsing and treats invalid binary strings as GlobPattern which is why I have to catch if the error is unexpected if that makes sense.
nushell/crates/nu-parser/src/parser.rs
Lines 1796 to 1808 in 529f6ec
There was a problem hiding this comment.
The section you quoted is a horrible double negation with that empty else if ! branch.
I was confused for a second why the truncation in your code was fine but it makes sense in the is_math_expression_like hack where we don't want to leave errors.
If I understand the codepaths correctly the !matches on Expected filters down to just your newly created variant for InvalidBinaryString? Then I think it would make sense to use that and maybe add a comment why we choose to deviate here from the rest of the is_math_expression_like chain.
There was a problem hiding this comment.
Whoops my bad, I genuinely thought that empty else if ! branch had a purpose (which was to not clear unexpected parsing integer error and fall through float parsing).
Yeah only that InvalidBinaryString variant should hit that codepath, or alternatively I can modify it to just strictly check if it matches InvalidBinaryString.
I'll add comments for this so people wouldn't get confused.
crates/nu-parser/src/parser.rs
Outdated
| 2 => "binary strings may contain only 0 or 1.", | ||
| 8 => "octal strings must have a length that is a multiple of three and contain values between 0o000 and 0o377.", | ||
| 16 => "hexadecimal strings may contain only the characters 0–9 and A–F.", | ||
| _ => "", // radix other than 2, 8, or 16 is an invalid radix |
There was a problem hiding this comment.
Thanks for elaborating in the messages.
Defensively you can throw the text of the comment with something like INTERNAL ERROR: into the fall through arm. If there is ever something going wrong being loud about it can be helpful even if it is vanishingly likely to break.
| #[error("Invalid binary string.")] | ||
| #[diagnostic(code(nu::parser::invalid_binary_string), help("{1}"))] | ||
| InvalidBinaryString(#[label("invalid binary string")] Span, String), |
There was a problem hiding this comment.
It's a bit of a stylistic question if we need a completely separate variant for that or if we can cover it with our existing variants and some additional text (cc @cptpiepmatz )
This `else if` branch being empty makes no sense if there is no comment. Can be collapsed by removing the negation on the `matches!` Stumbled over this while reading nushell#16688 (comment)
sholderbach
left a comment
There was a problem hiding this comment.
After you can take a stab at the comment, clear to land from my side
Fixes #9566. Based on the decision from this PR #16678, instead of dealing with out-of-range octal strings, throwing a specific error is better.
Let me know if these error messages are too detailed and prefer just having a single "out-of-range error".
Release notes summary - What our users need to know
Improved error messages for binary literals
Previously, a generic shell error is thrown for invalid binary strings. Now, an improve error message is thrown for invalid binary, hexadecimal, and octal strings.
Tasks after submitting