-
Notifications
You must be signed in to change notification settings - Fork 419
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
[Diagnostics] fix trivia merging and transfer #2848
base: main
Are you sure you want to change the base?
[Diagnostics] fix trivia merging and transfer #2848
Conversation
`Trivia.merging(_:)` doesn't behave as its doc comment advertises as it merges by neither common prefixes nor common suffixes. We supersede it with `Trivia.mergingCommonPrefix(_:)` and `Trivia.mergingCommonSuffix(_:)`. `Trivia.merging(triviaOf:)` is also superseded with `Trivia.mergingCommonPrefix(triviaOf:)` and `Trivia.mergingCommonSuffix(triviaOf:)`. Convenience extension methods to `SyntaxProtocol` are also introduced to ease creation of merged trivia.
da0c744
to
cd125a0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add an entry in Release Notes/601.md
for the public API changes?
@@ -69,6 +69,7 @@ public struct Trivia: Sendable { | |||
|
|||
/// Creates a new ``Trivia`` by merging in the given trivia. Only includes one | |||
/// copy of a common prefix of `self` and `trivia`. | |||
@available(*, deprecated, message: "Use mergingCommonPrefix(trivia) or mergingCommonSuffix(trivia) instead") | |||
public func merging(_ trivia: Trivia?) -> Trivia { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of continuing to have a single merging
function that merges both a common prefix and a common suffix? I would expect that that’s what most callers would prefer. I doubt that anyone is actively thinking about whether they want to merge the prefix or the suffix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue here is merging
doesn't actually merge by common prefix or common suffix despite its documentation, changing its behavior may break client code that depends on it, that's why I suggested to deprecate it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would consider that a bug fix and thus an acceptable behavior change.
cd125a0
to
5429b5c
Compare
Fixed the erratic behaviors of `transferTriviaAtSides(from:)` by adopting the new trivia merging API. As a result, the issue of incorrect transfer of trivia after applying Fix-it has also been solved. Cleaned up the `FixIt.MultiNodeChange.makeMissing` methods.
5429b5c
to
cc339ce
Compare
This PR will deprecate the incorrectly implemented methods
Trivia.merging(_:)
andTrivia.merging(triviaOf:)
which aren't behaving in the way as advertised by their documentation.The unexpected behaviors of
Trivia.merging(_:)
are causing subtle issues in trivia transfers. For example, applying the Fix-it that moves a misplaced throw clause from after the arrow to before the arrow will result in an extraneous whitespace before the return type.Even worse, sometimes the trivia of the moved node will be destroyed,
Making use of the newly introduced methods
Trivia.mergingCommonPrefix
andTrivia.mergingCommonSuffix
, this PR will also fix these issues.