ruby / ruby/rbs

Parse empty type params

Open
#2,993 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
2.2k
Forks
256
Avg merge
6d 17h
Merged PRs (30d)
37

Description

The two examples below fail to parse.

I think we should loosen the parsing requirements to allow empty type parameters, and then let the library user (Steep, Sorbet) decide how to handle that.

These cases are reachable if you're editing a file and want to rename a type parameter name. You would delete what's already there (leaving the [] empty), before you start typing the new name.

If we choose to leave this as an error in the RBS parser, it would be nice to at least have a more human-readable error messages.

Examples from Sorbet
Empty type parameters on a type (Sorbet.run):
#: []
#  ^^ ❌ expected a token `pLBRACKET`
class NotActuallyGeneric
end

https://github.com/ruby/rbs/blob/e1373f0182fdfdc30d78be9b591ac188b8af7603/src/parser.c#L3341-L3345

Empty type parameters on a method (Sorbet.run):
#: [] (T) -> T
#  ^^ ❌ expected a token `pARROW`
def identity(x) = x

Minimal repro

require "rbs"

RBS::Parser.parse_type_params "[]"
# /.../gems/rbs-4.0.2/lib/rbs/parser_aux.rb:43:in 'RBS::Parser._parse_type_params': a.rbs:1:0...1:2: # Syntax error: expected a token `pLBRACKET`, token=`[]` (pAREF_OPR) (RBS::ParsingError)
#
#  []
#  ^^

RBS::Parser.parse_method_type("[] () -> void")
# /.../gems/rbs-4.0.2/lib/rbs/parser_aux.rb:17:in 'RBS::Parser._parse_method_type': a.rbs:1:0...1:2: Syntax error: expected a token `pARROW`, token=`[]` (pAREF_OPR) (RBS::ParsingError)
#
#  [] () -> void
#  ^

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/parser.c at the linked type-parameter parsing code, then inspect lib/rbs/parser_aux.rb and the parse_type_params and parse_method_type entry points from the repro. Confirm whether [] should parse for both examples or only receive clearer errors, and verify that the chosen behavior is covered by the parser tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, ruby
Domain
compilers
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
56/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.