Skip to content

Parse empty type params #2993

Description

@amomchilov

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

rbs/src/parser.c

Lines 3341 to 3345 in e1373f0

bool rbs_parse_type_params(rbs_parser_t *parser, bool module_type_params, rbs_node_list_t **params) {
if (parser->next_token.type != pLBRACKET) {
rbs_parser_set_error(parser, parser->next_token, true, "expected a token `pLBRACKET`");
return false;
}

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
#  ^

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions