Report a malformed class instance variable as a syntax error - #3149
Open
Halvanhelv wants to merge 1 commit into
Open
Report a malformed class instance variable as a syntax error#3149Halvanhelv wants to merge 1 commit into
Halvanhelv wants to merge 1 commit into
Conversation
The `kSELF` branch of `parse_variable_member` reported a missing or invalid variable name by calling `rbs_parser_set_error` with `syntax_error` set to `false`. `raise_error` discards the location, token and message in that case and raises a bare `RuntimeError`, so `class M self.` escaped `rescue RBS::ParsingError` and the CLI printed a Ruby backtrace instead of a formatted syntax error. Every other malformed member in the same position, and the same construct under `interface`, already reported an `RBS::ParsingError`. Pass `syntax_error` and a specific message, as the other syntax error sites do. Set the error on `next_token`, which is the token the branch tests and therefore the offending one; `current_token` would point the caret at the dot.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3148.
class M self.raisesRuntimeError: Unexpected errorinstead ofRBS::ParsingError, so it escapesrescue RBS::ParsingErrorand the CLI prints a raw Ruby backtrace. Every other malformed member in the same position reports correctly, and so does the same construct underinterface:class M <<<RBS::ParsingErrorwith location and caretinterface _I self.RBS::ParsingErrorclass M self.RuntimeError: Unexpected errorclass M self.5RuntimeError: Unexpected errorCause
src/parser.c:2353, in thekSELFbranch ofparse_variable_member, passesfalsefor thebool syntax_errorparameter (include/rbs/parser.h:161).raise_errorinext/rbs_extension/main.cthen discards the location, token and message and raises a bareRuntimeError:49 other call sites in
src/parser.cpasstrueand produce properRBS::ParsingErrors.Fix
Pass
syntax_errorwith a specific message, in the wording used by the neighbouring sites.The error is also set on
next_tokenrather thancurrent_token: the branch testsparser->next_token.type, sonext_tokenis the offending token, andcurrent_tokenwould put the caret on the dot. Sibling sites that testnext_tokenset the error on it the same way (lines 178, 288, 316, 325, 555, 665, 688, 704, 981).Scope — why only this one site
src/parser.chas 12 othersyntax_error = falsesites. I checked them empirically rather than by eye: I tagged each with a unique marker, temporarily stoppedmain.cfrom discardingerror->message, rebuilt, and ran targeted probes plus 25,000 mutation-fuzz iterations over the bundled signatures. Only 2353 was reached from user input. The rest are defensivedefault:arms over already-exhausted enums — e.g. the switch at 2383 covers exactly the four token types its caller dispatches on — so they indicate a parser bug, andRuntimeErroris the correct semantic there. Left untouched.Result
CLI now prints a formatted error instead of a backtrace:
Valid signatures are unchanged —
self.@x,@xand@@xall still parse, and all 342 bundledcore/,stdlib/andsig/files parse with no failures. Re-running the 25,000-input fuzz against the fixed build gives 23,649RBS::ParsingErrorand 0RuntimeError.Tests
Added two tests to
test/rbs/errors_test.rb: one assertingRBS::ParsingErroracross the four malformed forms, one checking the exactdetailed_messageand caret position. Both fail withRuntimeError(Unexpected error)without the fix.Full suite before:
999 tests, 8198 assertions, 0 failures, 0 errors.Full suite after:
1001 tests, 8204 assertions, 0 failures, 0 errors.