Skip to content

Fix Parsers.Parser.||| (by adding equality comparison for Position objects) #507

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

Merged
merged 2 commits into from
Apr 5, 2023

Conversation

peteraldous
Copy link

Parsers.Parser.||| was not working as expected. I looked into it and discovered that Position objects were using pointer equality, not structural equality. I added this equality function and it fixed the issue.

I won't be offended if there are changes that need to be made to make this more idiomatic or whatever.

@SethTisue
Copy link
Member

SethTisue commented Feb 27, 2023

hi! welcome to the repo

Parsers.Parser.||| was not working as expected

could you add a test case to demonstrate the issue that is now fixed, and to prevent it from regressing in the future?

@SethTisue
Copy link
Member

It's a bit puzzling to me that this would have fixed anything, given that the only two subtypes of Position that I see are NoPosition and OffsetPosition, and the former is a singleton for which reference equality should be fine, and the latter is a case class which should have an automatically generated equals. Though in the latter it's comparing offset instead of line and column, as in your change — is the latter somehow more correct?

@peteraldous
Copy link
Author

The issue is from an assumption in this pattern: https://github.com/scala/scala-parser-combinators/blob/main/shared/src/main/scala/scala/util/parsing/combinator/Parsers.scala#L422 - it seems obvious that if next2.pos < next1.pos and next2.pos == next1.pos are both false that the only remaining possibility is that next2.pos > next1.pos. However, next2.pos == next1.pos seems to have been checking for pointer equality. As such, the parser acts as if q matched more of the input than p did whenever they match the same amount of the input. Adding an equals method fixes this logic.

@peteraldous
Copy link
Author

I'm not seeing the same behavior when I run sbt test on my machine. Is there a way I can configure my machine so it produces the same error as the workflow above?

@Philippus
Copy link
Member

I'm not seeing the same behavior when I run sbt test on my machine. Is there a way I can configure my machine so it produces the same error as the workflow above?

I'm also having trouble with triggering the same error locally.

@peteraldous
Copy link
Author

The puzzle might be explained by the fact that https://github.com/scala/scala-parser-combinators/blob/main/shared/src/main/scala/scala/util/parsing/input/StreamReader.scala#L69 creates an anonymous subclass of Position.

@SethTisue
Copy link
Member

Ah, that seems to makes sense, though to be really sure, I'd have to dig deeper than I have time to do.

We are... understaffed in this repo for knowledgeable PR review.

As a result, I'm not sure how to proceed; optimistically merge, or ask you to further defend the change. wdyt? How sure are you this is now right?

(Anyone else watching the repo available to weigh in?)

@peteraldous
Copy link
Author

Thanks for the response. I have plenty on my plate, as well, so I understand.

I am happy to further defend the change but thought I'd answered all of your questions: the tests I added demonstrate correct behavior and if you comment out the equals method I added, the last of the three breaks. I also added a comment with a pointer to code that makes an anonymous subclass of Position that doesn't have an equals method.

I would much rather that you wait until you're comfortable merging. Is there anything else I can answer or tweak? (Happy to have comments from others on the repo, too.)

@SethTisue SethTisue merged commit 4f020d5 into scala:main Apr 5, 2023
@SethTisue
Copy link
Member

Thank you!

@SethTisue SethTisue changed the title added equality comparison for Position objects Fix Parsers.Parser.||| (by adding equality comparison for Position objects) Apr 5, 2023
HyukjinKwon pushed a commit to apache/spark that referenced this pull request Jul 4, 2023
### What changes were proposed in this pull request?
This pr aims to upgrade `scala-parser-combinators` from 2.2.0 to 2.3.0

### Why are the changes needed?
The new version [dropped support for Scala 2.11](scala/scala-parser-combinators#504) and bring a bug fix:
- scala/scala-parser-combinators#507

The full release notes as follows:
- https://github.com/scala/scala-parser-combinators/releases/tag/v2.3.0

### Does this PR introduce _any_ user-facing change?
No

### How was this patch tested?
Pass Github Actions

Closes #41848 from LuciferYang/scala-parser-combinators-23.

Authored-by: yangjie01 <[email protected]>
Signed-off-by: Hyukjin Kwon <[email protected]>
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.

3 participants