Skip to content

Make SourceLocation and related types Hashable #254

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
Jan 15, 2021
Merged

Make SourceLocation and related types Hashable #254

merged 2 commits into from
Jan 15, 2021

Conversation

MaxDesiatov
Copy link
Contributor

@MaxDesiatov MaxDesiatov commented Jan 15, 2021

It seems natural to be able to compare source locations for equality, or to add locations to a Set or use them as keys in dictionaries. Currently this requires declaring manual Hashable conformance in libraries that use SwiftSyntax, which is error-prone.

It seems natural to be able to compare ranges for equality. Currently this requires declaring manual `Equatable` conformance in libraries that use SwiftSyntax, which is error-prone.
@MaxDesiatov

This comment has been minimized.

@MaxDesiatov MaxDesiatov changed the title Make SourceLocation and related types Equatable Make SourceLocation and related types Hashable Jan 15, 2021
@MaxDesiatov

This comment has been minimized.

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 9aaae85

@MaxDesiatov
Copy link
Contributor Author

@swift-ci please test

@MaxDesiatov
Copy link
Contributor Author

Please feel free to merge when you see fit, I don't have commit access here.

@akyrtzi akyrtzi merged commit 669acb1 into swiftlang:main Jan 15, 2021
@@ -43,7 +43,7 @@ struct ComputedLocation: Codable, CustomDebugStringConvertible {
}

/// Represents a source location in a Swift file.
public struct SourceLocation: Codable, CustomDebugStringConvertible {
public struct SourceLocation: Hashable, Codable, CustomDebugStringConvertible {
Copy link
Member

Choose a reason for hiding this comment

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

Just to be clear: Should two SourceLocations be different if one has a ComputedLocation and the other does not? After all, they still refer to the same location in the source code.

I’m not 100% sure on this, but I’m leaning towards making the identity of SourceLocation be purely based on the value of offset. What do you think, @MaxDesiatov?

CC: @akyrtzi

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In my opinion they shouldn't be equal since line, column, and file will return different results for them. The one without a ComputedLocation could refer to a different file altogether, as file is inferred from compLoc.

Copy link
Member

@ahoppen ahoppen Jan 18, 2021

Choose a reason for hiding this comment

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

That’s a good point. I didn’t consider file. With that in mind, I’m fine with keeping the implementation as-is.

@MaxDesiatov MaxDesiatov deleted the patch-2 branch January 18, 2021 09:33
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.

4 participants