sord icon indicating copy to clipboard operation
sord copied to clipboard

Elem constant generation

Open connorshea opened this issue 6 years ago • 3 comments

Is your feature request related to a problem? Please describe. A lot of the remaining errors returned by rake examples:typecheck are to do with classes descending from Array or Struct, and thus needing to have the Elem constant redeclared.

sord_examples/yard.rbi:4248: Type Elem declared by parent Array must be re-declared in YARD::Parser::Ruby::Legacy::TokenList https://srb.help/5036
    4248 |        class TokenList < Array
                  ^^^^^^^^^^^^^^^^^^^^^^^
    https://github.com/sorbet/sorbet/tree/7afa6f30e4d5f6e6e49815ff130d6c98563df809/rbi/core/array.rbi#L7: Elem declared in parent here
     7 |  Elem = type_member(:out)
          ^^^^^^^^^^^^^^^^^^^^^^^^

sord_examples/yard.rbi:4296: Type Elem declared by parent Array must be re-declared in YARD::Parser::Ruby::Legacy::StatementList https://srb.help/5036
    4296 |        class StatementList < Array
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    https://github.com/sorbet/sorbet/tree/7afa6f30e4d5f6e6e49815ff130d6c98563df809/rbi/core/array.rbi#L7: Elem declared in parent here
     7 |  Elem = type_member(:out)
          ^^^^^^^^^^^^^^^^^^^^^^^^

sord_examples/yard.rbi:4361: Type Elem declared by parent Array must be re-declared in YARD::Parser::Ruby::AstNode https://srb.help/5036
    4361 |      class AstNode < Array
                ^^^^^^^^^^^^^^^^^^^^^
    https://github.com/sorbet/sorbet/tree/7afa6f30e4d5f6e6e49815ff130d6c98563df809/rbi/core/array.rbi#L7: Elem declared in parent here
     7 |  Elem = type_member(:out)
          ^^^^^^^^^^^^^^^^^^^^^^^^

sord_examples/yard.rbi:4501: Type Elem declared by parent YARD::Parser::Ruby::AstNode must be re-declared in YARD::Parser::Ruby::ReferenceNode https://srb.help/5036
    4501 |      class ReferenceNode < AstNode
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    sord_examples/yard.rbi:4361: Elem declared in parent here
    4361 |      class AstNode < Array
                ^^^^^^^^^^^^^^^^^^^^^

Describe the solution you'd like The simplest solution would be to just define the necessary constants for each parent class that needs this (Array, Struct, etc.), but that's somewhat fragile. I'm not sure if there's a way for us to determine what Sorbet wants us to add when generating the rbi file?

I suppose these constants wouldn't change very often for core classes like this, so it may be fine to just handle it on a case-by-case basis in Sord.

Describe alternatives you've considered I'd prefer if Sorbet magically fixed this itself, but I don't think it can :(

connorshea avatar Jul 20 '19 16:07 connorshea

This is certainly a tricky one. I'm not too familiar with how type_member works, but my understanding is that, for an array, Elem should be set to the type of each element in the array. Is there any way this could be determined through YARD, or would we just use T.untyped?

AaronC81 avatar Jul 20 '19 17:07 AaronC81

Probably just T.untyped.

connorshea avatar Jul 20 '19 17:07 connorshea

T.untyped should work as a stop-gap solution here. Elem = type_member(fixed: T.untyped)

DarkDimius avatar Jul 21 '19 17:07 DarkDimius