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 fatal bug in is(_:) #142

Merged
merged 3 commits into from
Jan 10, 2024
Merged

Fix fatal bug in is(_:) #142

merged 3 commits into from
Jan 10, 2024

Conversation

Ryu0118
Copy link
Contributor

@Ryu0118 Ryu0118 commented Jan 10, 2024

Hi there, I found a bug in is(_:).

In version 1.2.0, the handling of optional associated values has been improved. Specifically, the subscript(case:) now correctly returns a value without unnecessarily wrapping it in an additional Optional layer when the associated value is already an Optional. However, this improvement has introduced a significant bug.

Consider the following enum as an example:

enum Foo {
  case c1
  case c2(String?)
}

When using the is(_:) method with c2's associated value set to nil, like so:

Foo.c2(nil).is(\.c2) // false

The expected result would be true, but it incorrectly returns false. This is a direct consequence of the changes in version 1.2.0. Prior to this change, the value of self[case: keyPath] used in the implementation of is(_:) would be Optional(nil), bug which is not nil, thus functioning correctly. However, post-change, the value of self[case: keyPath] becomes nil, leading to incorrect behavior even when the case matches.

On the other hand, with a non-nil associated value such as:

Foo.c2("foo").is(\.c2) // true

The method works correctly because self[case: keyPath] returns "foo".

This pull request introduces a code change to prevent the execution of the line at 52 in Optional+CasePathable that was added in 1.2.0:

public subscript<Member>(
  dynamicMember keyPath: KeyPath<Value.AllCasePaths, AnyCasePath<Value, Member?>>
) -> Case<Member>
where Value: CasePathable {
  self[dynamicMember: keyPath].some
}

Copy link
Member

@stephencelis stephencelis left a comment

Choose a reason for hiding this comment

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

Thanks! I guess this is a gotcha to know about when introducing new APIs, but you can always use explicit \.some or \.none to work around any issue.

@stephencelis stephencelis merged commit 76d7791 into pointfreeco:main Jan 10, 2024
5 checks passed
@Ryu0118 Ryu0118 deleted the fix-is-bug branch January 11, 2024 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants