Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix record type #1732

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 11 additions & 21 deletions lib/rbs/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -515,34 +515,24 @@ def with_nonreturn_void?
end

class Record
attr_reader :all_fields
attr_reader :all_fields, :fields, :optional_fields
attr_reader :location

def initialize(all_fields: nil, fields: nil, location:)
if (all_fields && fields) || (all_fields.nil? && fields.nil?)
raise ArgumentError, "only one of `:fields` or `:all_fields` is requireds"
end

if fields
case
when fields && all_fields.nil?
@all_fields = fields.map { |k, v| [k, [v, true]] }.to_h
@fields = fields
else
@optional_fields = {}
when all_fields && fields.nil?
@all_fields = all_fields
@fields = nil
@fields = all_fields.filter_map { |k, (v, required)| [k, v] if required }.to_h
@optional_fields = all_fields.filter_map { |k, (v, required)| [k, v] unless required }.to_h
else
raise ArgumentError, "only one of `:fields` or `:all_fields` is requireds"
end

@location = location
@optional_fields = nil
end

def fields
@fields ||= all_fields.filter_map { |k, (v, required)| [k, v] if required }.to_h
end

def optional_fields
return if all_fields.size == fields.size

@optional_fields ||= all_fields.filter_map { |k, (v, required)| [k, v] unless required }.to_h
end

def ==(other)
Expand All @@ -560,7 +550,7 @@ def free_variables(set = Set.new)
fields.each_value do |type|
type.free_variables set
end
optional_fields&.each_value do |type|
optional_fields.each_value do |type|
type.free_variables set
end
end
Expand Down Expand Up @@ -596,7 +586,7 @@ def to_s(level = 0)
def each_type(&block)
if block
fields.each_value(&block)
optional_fields&.each_value(&block)
optional_fields.each_value(&block)
else
enum_for :each_type
end
Expand Down
8 changes: 4 additions & 4 deletions sig/types.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ module RBS
class Record
attr_reader all_fields: Hash[Symbol, [t, bool]]

attr_reader fields: Hash[Symbol, t]

attr_reader optional_fields: Hash[Symbol, t]

type loc = Location[bot, bot]

def initialize: (fields: Hash[Symbol, t], location: loc?) -> void
Expand All @@ -312,10 +316,6 @@ module RBS

attr_reader location: loc?

def fields: () -> Hash[Symbol, t]

def optional_fields: () -> Hash[Symbol, t]?

def map_type: () { (t) -> t } -> Record
| () -> Enumerator[t, Record]
end
Expand Down