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

[Diagnostics] fix trivia merging and transfer #2848

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

AppAppWorks
Copy link
Contributor

@AppAppWorks AppAppWorks commented Sep 12, 2024

This PR will deprecate the incorrectly implemented methods Trivia.merging(_:) and Trivia.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.

func test() -> throws(any Error) Int 
// after Fix-it
func test() throws(any Error) ->  Int 
// expected
func test() throws(any Error) -> Int 

Even worse, sometimes the trivia of the moved node will be destroyed,

func test() -> throws(any Error)/**/ Int 
// after Fix-it
func test() throws(any Error) ->  Int 
// expected
func test() throws(any Error) -> /**/ Int 

Making use of the newly introduced methodsTrivia.mergingCommonPrefix and Trivia.mergingCommonSuffix, this PR will also fix these issues.

@AppAppWorks AppAppWorks marked this pull request as draft September 12, 2024 10:17
`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.
@AppAppWorks AppAppWorks force-pushed the fix-trivia-merging-and-transfer branch from da0c744 to cd125a0 Compare September 13, 2024 00:10
Sources/SwiftParserDiagnostics/DiagnosticExtensions.swift Outdated Show resolved Hide resolved
Copy link
Member

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 {
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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.

Sources/SwiftParserDiagnostics/DiagnosticExtensions.swift Outdated Show resolved Hide resolved
@AppAppWorks AppAppWorks force-pushed the fix-trivia-merging-and-transfer branch from cd125a0 to 5429b5c Compare September 16, 2024 20:55
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.
@AppAppWorks AppAppWorks force-pushed the fix-trivia-merging-and-transfer branch from 5429b5c to cc339ce Compare September 16, 2024 22:08
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.

2 participants