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

A tiny followup for #1641’s oopsies #1672

Merged
merged 4 commits into from
Dec 13, 2023
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
3 changes: 2 additions & 1 deletion core/string.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,7 @@ class String
# modification made, `self` otherwise.
#
def chomp!: (nil) -> nil
# | (?string separator) -> self? # https://github.com/ruby/rbs/pull/1672#discussion_r1423324796
| (?string? separator) -> self?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case is necessary.
Passing a node of type String? will be a type error without the ?.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a problem with the type checkers unable to break string and nil from string? to different paths and then merge them back to self? | nil i.e. self?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be true. Type checkers can do more about this.
But at least for now, RBS is for static type checking and having string? is more common for static type checking (I believe.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But at least for now

How about this: I will revert it for now, but we should also remind type checker maintainers about these sort of cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


# <!--
Expand Down Expand Up @@ -2741,7 +2742,7 @@ class String
# "\x81"
# "\x81"
#
def scrub: (?string replacement) -> String
def scrub: (?string? replacement) -> String
ParadoxV5 marked this conversation as resolved.
Show resolved Hide resolved
| (?nil) { (String bytes) -> string } -> String

# <!--
Expand Down
10 changes: 10 additions & 0 deletions test/stdlib/String_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,11 @@ def test_scrub
assert_send_type '() -> String',
invalid, :scrub

assert_send_type '(nil) -> String',
valid, :scrub, nil
assert_send_type '(nil) -> String',
invalid, :scrub, nil

with_string '&' do |replacement|
assert_send_type '(string) -> String',
valid, :scrub, replacement
Expand Down Expand Up @@ -1310,6 +1315,11 @@ def test_scrub!
assert_send_type '() -> String',
invalid.dup, :scrub!

assert_send_type '(nil) -> String',
valid.dup, :scrub!, nil
assert_send_type '(nil) -> String',
invalid.dup, :scrub!, nil

with_string '&' do |replacement|
assert_send_type '(string) -> String',
valid.dup, :scrub!, replacement
Expand Down