Skip to content

Commit a61588f

Browse files
authored
Merge pull request #2325 from ruby/define_method_parsing
Fix `define_method` method block self type
2 parents 74db910 + ebd0849 commit a61588f

File tree

5 files changed

+37
-15
lines changed

5 files changed

+37
-15
lines changed

core/module.rbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,8 +731,8 @@ class Module < Object
731731
# I'm Dino!
732732
# #<B:0x401b39e8>
733733
#
734-
def define_method: (interned symbol, ^() [self: self] -> untyped | Method | UnboundMethod method) -> Symbol
735-
| (interned symbol) { () [self: self] -> untyped } -> Symbol
734+
def define_method: (interned symbol, ^(?) [self: top] -> untyped | Method | UnboundMethod method) -> Symbol
735+
| (interned symbol) { (?) [self: top] -> untyped } -> Symbol
736736

737737
# <!--
738738
# rdoc-file=object.c

ext/rbs_extension/parser.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -661,16 +661,16 @@ static void parse_function(parserstate *state, VALUE *function, VALUE *block, VA
661661
parser_advance_assert(state, pRPAREN);
662662
}
663663

664-
// Untyped method parameter means it cannot have block
665-
if (rbs_is_untyped_params(&params)) {
666-
if (state->next_token.type != pARROW) {
667-
raise_syntax_error(state, state->next_token2, "A method type with untyped method parameter cannot have block");
668-
}
669-
}
670-
671664
// Passing NULL to function_self_type means the function itself doesn't accept self type binding. (== method type)
672665
if (function_self_type) {
673666
*function_self_type = parse_self_type_binding(state);
667+
} else {
668+
// Parsing method type. untyped_params means it cannot have a block
669+
if (rbs_is_untyped_params(&params)) {
670+
if (state->next_token.type != pARROW) {
671+
raise_syntax_error(state, state->next_token2, "A method type with untyped method parameter cannot have block");
672+
}
673+
}
674674
}
675675

676676
VALUE required = Qtrue;

test/rbs/parser_test.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -820,12 +820,6 @@ def test_proc__untyped_function
820820
end
821821
end
822822

823-
def test_proc__untyped_function_parse_error
824-
assert_raises(RBS::ParsingError) do
825-
RBS::Parser.parse_type("^(?) { (?) -> void } -> Integer")
826-
end
827-
end
828-
829823
def test__lex
830824
content = <<~RBS
831825
# LineComment

test/rbs/signature_parsing_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,4 +2268,15 @@ def test_constant__annotation
22682268
end
22692269
end
22702270
end
2271+
2272+
def test__method_type__untyped_function_and_block
2273+
assert_raises(RBS::ParsingError) do
2274+
Parser.parse_signature(<<~RBS).tap do |_, _, decls|
2275+
class Foo
2276+
def foo: (?) { () -> void } -> void
2277+
end
2278+
RBS
2279+
end
2280+
end
2281+
end
22712282
end

test/rbs/type_parsing_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,23 @@ def test_proc_with_self
489489
end
490490
end
491491

492+
def test_untyped_proc_with_self
493+
Parser.parse_type("^(?) -> void").yield_self do |type|
494+
assert_instance_of Types::Proc, type
495+
assert_instance_of Types::UntypedFunction, type.type
496+
assert_equal "^(?) -> void", type.location.source
497+
assert_nil type.self_type
498+
end
499+
500+
Parser.parse_type("^(?) [self: String] -> void").yield_self do |type|
501+
assert_instance_of Types::Proc, type
502+
assert_instance_of Types::UntypedFunction, type.type
503+
504+
assert_equal "^(?) [self: String] -> void", type.location.source
505+
assert_equal Parser.parse_type("String"), type.self_type
506+
end
507+
end
508+
492509
def test_optional
493510
Parser.parse_type("untyped?").yield_self do |type|
494511
assert_instance_of Types::Optional, type

0 commit comments

Comments
 (0)