diff --git a/lib/rbs/ast/ruby/comment_block.rb b/lib/rbs/ast/ruby/comment_block.rb index 1eaaa4a399..14de35ecb7 100644 --- a/lib/rbs/ast/ruby/comment_block.rb +++ b/lib/rbs/ast/ruby/comment_block.rb @@ -199,7 +199,7 @@ def parse_annotation_lines(start_line, end_line, variables) def trailing_annotation(variables) if trailing? comment = comments[0] or raise - if comment.location.slice.start_with?(/#[:\[]/) + if comment.location.slice.start_with?("#:", "#[") begin Parser.parse_inline_trailing_annotation(comment_buffer, 0...comment_buffer.last_position, variables: variables) rescue ParsingError => error diff --git a/test/rbs/inline_parser_test.rb b/test/rbs/inline_parser_test.rb index befd885f32..a6de530c46 100644 --- a/test/rbs/inline_parser_test.rb +++ b/test/rbs/inline_parser_test.rb @@ -1459,6 +1459,18 @@ class Foo end end + def test_error__attr_type_with_invalid_utf8 + omit_on_truffle_ruby! "The C extension does not raise `RBS::ParsingError` for invalid UTF-8" + omit_on_jruby! "`Prism::Location#start_line_slice` returns `nil` on JRuby's prism" + + ["\xDD", "\xFF", "\xC3", "\xE2\x82"].each do |invalid_bytes| + source = "class Foo\n attr_reader :value #: Integer # #{invalid_bytes}\nend\n".dup.force_encoding(Encoding::UTF_8) + result = parse(source) + + assert_instance_of RBS::InlineParser::Diagnostic::AnnotationSyntaxError, result.diagnostics.fetch(0) + end + end + def test_parse__class_with_super_class result = parse(<<~RUBY) class Child < Parent