Skip to content

Commit cfeed31

Browse files
committed
Remove fallback for missing Struct#keyword_init?
Always present since Ruby 3.2
1 parent e2135da commit cfeed31

3 files changed

Lines changed: 50 additions & 135 deletions

File tree

lib/rbs/prototype/runtime/value_object_generator.rb

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ def self.generatable?(target)
9999

100100
private
101101

102-
CAN_CALL_KEYWORD_INIT_P = Struct.new(:tmp).respond_to?(:keyword_init?)
103-
104102
def build_super_class
105103
AST::Declarations::Class::Super.new(name: TypeName.parse("::Struct"), args: [untyped], location: nil)
106104
end
@@ -118,21 +116,16 @@ def build_s_new
118116
[:new, :[]].map do |name|
119117
new_overloads = [] #: Array[AST::Members::MethodDefinition::Overload]
120118

121-
if CAN_CALL_KEYWORD_INIT_P
122-
case @target_class.keyword_init?
123-
when false
124-
new_overloads << build_overload_for_positional_arguments
125-
when true
126-
new_overloads << build_overload_for_keyword_arguments
127-
when nil
128-
new_overloads << build_overload_for_positional_arguments
129-
new_overloads << build_overload_for_keyword_arguments
130-
else
131-
raise
132-
end
133-
else
119+
case @target_class.keyword_init?
120+
when false
121+
new_overloads << build_overload_for_positional_arguments
122+
when true
123+
new_overloads << build_overload_for_keyword_arguments
124+
when nil
134125
new_overloads << build_overload_for_positional_arguments
135126
new_overloads << build_overload_for_keyword_arguments
127+
else
128+
raise
136129
end
137130

138131
AST::Members::MethodDefinition.new(
@@ -178,8 +171,6 @@ def build_overload_for_keyword_arguments
178171

179172
# def self.keyword_init?: () -> bool?
180173
def build_s_keyword_init_p
181-
return [] unless CAN_CALL_KEYWORD_INIT_P
182-
183174
return_type = @target_class.keyword_init?.nil? \
184175
? Types::Bases::Nil.new(location: nil)
185176
: Types::Literal.new(literal: @target_class.keyword_init?, location: nil)

sig/prototype/runtime.rbs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ module RBS
6464
def build_s_new: () -> Array[AST::Members::MethodDefinition]
6565

6666
def build_super_class: () -> AST::Declarations::Class::Super
67-
68-
CAN_CALL_KEYWORD_INIT_P: bool
6967
end
7068

7169
class DataGenerator < ValueObjectBase

test/rbs/runtime_prototype_test.rb

Lines changed: 42 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -690,151 +690,77 @@ def test_struct
690690
SignatureManager.new do |manager|
691691
manager.build do |env|
692692
p = Runtime.new(patterns: ["RBS::RuntimePrototypeTest::StructInheritWithNil"], env: env, merge: false)
693-
if Runtime::StructGenerator::CAN_CALL_KEYWORD_INIT_P
694-
assert_write p.decls, <<~RBS
695-
module RBS
696-
class RuntimePrototypeTest < ::Test::Unit::TestCase
697-
class StructInheritWithNil < ::Struct[untyped]
698-
def self.new: (?untyped foo, ?untyped bar, ?untyped `baz?`) -> instance
699-
| (?foo: untyped, ?bar: untyped, ?baz?: untyped) -> instance
693+
assert_write p.decls, <<~RBS
694+
module RBS
695+
class RuntimePrototypeTest < ::Test::Unit::TestCase
696+
class StructInheritWithNil < ::Struct[untyped]
697+
def self.new: (?untyped foo, ?untyped bar, ?untyped `baz?`) -> instance
698+
| (?foo: untyped, ?bar: untyped, ?baz?: untyped) -> instance
700699
701-
def self.[]: (?untyped foo, ?untyped bar, ?untyped `baz?`) -> instance
702-
| (?foo: untyped, ?bar: untyped, ?baz?: untyped) -> instance
700+
def self.[]: (?untyped foo, ?untyped bar, ?untyped `baz?`) -> instance
701+
| (?foo: untyped, ?bar: untyped, ?baz?: untyped) -> instance
703702
704-
def self.keyword_init?: () -> nil
703+
def self.keyword_init?: () -> nil
705704
706-
def self.members: () -> [ :foo, :bar, :baz? ]
705+
def self.members: () -> [ :foo, :bar, :baz? ]
707706
708-
def members: () -> [ :foo, :bar, :baz? ]
707+
def members: () -> [ :foo, :bar, :baz? ]
709708
710-
attr_accessor foo: untyped
709+
attr_accessor foo: untyped
711710
712-
attr_accessor bar: untyped
711+
attr_accessor bar: untyped
713712
714-
attr_accessor baz?: untyped
715-
end
713+
attr_accessor baz?: untyped
716714
end
717715
end
718-
RBS
719-
else
720-
assert_write p.decls, <<~RBS
721-
module RBS
722-
class RuntimePrototypeTest < ::Test::Unit::TestCase
723-
class StructInheritWithNil < ::Struct[untyped]
724-
def self.new: (?untyped foo, ?untyped bar, ?untyped `baz?`) -> instance
725-
| (?foo: untyped, ?bar: untyped, ?baz?: untyped) -> instance
726-
727-
def self.[]: (?untyped foo, ?untyped bar, ?untyped `baz?`) -> instance
728-
| (?foo: untyped, ?bar: untyped, ?baz?: untyped) -> instance
729-
730-
def self.members: () -> [ :foo, :bar, :baz? ]
731-
732-
def members: () -> [ :foo, :bar, :baz? ]
733-
734-
attr_accessor foo: untyped
735-
736-
attr_accessor bar: untyped
737-
738-
attr_accessor baz?: untyped
739-
end
740-
end
741-
end
742-
RBS
743-
end
716+
end
717+
RBS
744718

745719
p = Runtime.new(patterns: ["RBS::RuntimePrototypeTest::StructKeywordInitTrue"], env: env, merge: false)
746-
if Runtime::StructGenerator::CAN_CALL_KEYWORD_INIT_P
747-
assert_write p.decls, <<~RBS
748-
module RBS
749-
class RuntimePrototypeTest < ::Test::Unit::TestCase
750-
class StructKeywordInitTrue < ::Struct[untyped]
751-
def self.new: (?foo: untyped, ?bar: untyped) -> instance
752-
753-
def self.[]: (?foo: untyped, ?bar: untyped) -> instance
754-
755-
def self.keyword_init?: () -> true
756-
757-
def self.members: () -> [ :foo, :bar ]
758-
759-
def members: () -> [ :foo, :bar ]
760-
761-
attr_accessor foo: untyped
720+
assert_write p.decls, <<~RBS
721+
module RBS
722+
class RuntimePrototypeTest < ::Test::Unit::TestCase
723+
class StructKeywordInitTrue < ::Struct[untyped]
724+
def self.new: (?foo: untyped, ?bar: untyped) -> instance
762725
763-
attr_accessor bar: untyped
764-
end
765-
end
766-
end
767-
RBS
768-
else
769-
assert_write p.decls, <<~RBS
770-
module RBS
771-
class RuntimePrototypeTest < ::Test::Unit::TestCase
772-
class StructKeywordInitTrue < ::Struct[untyped]
773-
def self.new: (?untyped foo, ?untyped bar) -> instance
774-
| (?foo: untyped, ?bar: untyped) -> instance
726+
def self.[]: (?foo: untyped, ?bar: untyped) -> instance
775727
776-
def self.[]: (?untyped foo, ?untyped bar) -> instance
777-
| (?foo: untyped, ?bar: untyped) -> instance
728+
def self.keyword_init?: () -> true
778729
779-
def self.members: () -> [ :foo, :bar ]
730+
def self.members: () -> [ :foo, :bar ]
780731
781-
def members: () -> [ :foo, :bar ]
732+
def members: () -> [ :foo, :bar ]
782733
783-
attr_accessor foo: untyped
734+
attr_accessor foo: untyped
784735
785-
attr_accessor bar: untyped
786-
end
736+
attr_accessor bar: untyped
787737
end
788738
end
789-
RBS
790-
end
739+
end
740+
RBS
791741

792742
p = Runtime.new(patterns: ["RBS::RuntimePrototypeTest::StructKeywordInitFalse"], env: env, merge: false)
793-
if Runtime::StructGenerator::CAN_CALL_KEYWORD_INIT_P
794-
assert_write p.decls, <<~RBS
795-
module RBS
796-
class RuntimePrototypeTest < ::Test::Unit::TestCase
797-
class StructKeywordInitFalse < ::Struct[untyped]
798-
def self.new: (?untyped foo, ?untyped bar) -> instance
799-
800-
def self.[]: (?untyped foo, ?untyped bar) -> instance
801-
802-
def self.keyword_init?: () -> false
803-
804-
def self.members: () -> [ :foo, :bar ]
805-
806-
def members: () -> [ :foo, :bar ]
807-
808-
attr_accessor foo: untyped
743+
assert_write p.decls, <<~RBS
744+
module RBS
745+
class RuntimePrototypeTest < ::Test::Unit::TestCase
746+
class StructKeywordInitFalse < ::Struct[untyped]
747+
def self.new: (?untyped foo, ?untyped bar) -> instance
809748
810-
attr_accessor bar: untyped
811-
end
812-
end
813-
end
814-
RBS
815-
else
816-
assert_write p.decls, <<~RBS
817-
module RBS
818-
class RuntimePrototypeTest < ::Test::Unit::TestCase
819-
class StructKeywordInitFalse < ::Struct[untyped]
820-
def self.new: (?untyped foo, ?untyped bar) -> instance
821-
| (?foo: untyped, ?bar: untyped) -> instance
749+
def self.[]: (?untyped foo, ?untyped bar) -> instance
822750
823-
def self.[]: (?untyped foo, ?untyped bar) -> instance
824-
| (?foo: untyped, ?bar: untyped) -> instance
751+
def self.keyword_init?: () -> false
825752
826-
def self.members: () -> [ :foo, :bar ]
753+
def self.members: () -> [ :foo, :bar ]
827754
828-
def members: () -> [ :foo, :bar ]
755+
def members: () -> [ :foo, :bar ]
829756
830-
attr_accessor foo: untyped
757+
attr_accessor foo: untyped
831758
832-
attr_accessor bar: untyped
833-
end
759+
attr_accessor bar: untyped
834760
end
835761
end
836-
RBS
837-
end
762+
end
763+
RBS
838764

839765
p = Runtime.new(patterns: ["RBS::RuntimePrototypeTest::StructDirectInherited"], env: env, merge: false)
840766
assert_write p.decls, <<~RBS

0 commit comments

Comments
 (0)