10000 Cherry-pick some post-4.2 changes into swift-4.2-branch by allevato · Pull Request #6 · swiftlang/swift-syntax · GitHub
[go: up one dir, main page]

Skip to content

Cherry-pick some post-4.2 changes into swift-4.2-branch #6

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 cl 10000 icking “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 25 commits into from
Sep 12, 2018

Conversation

allevato
Copy link
Member
@allevato allevato commented Aug 31, 2018

These source-location-related APIs are generally useful and safe to cherry-pick into swift-4.2-branch because they do not depend on any syntax schema changes that have occurred since the branch cut. (They are also required by the formatter, and cannot be easily added as extensions in our code base because they need access to private properties.)

The final commit is a manual graft of tests from the original set, which couldn't be applied as a simple patch because the tests had been rewritten to use XCTest instead of StdlibUnittest.

This PR was tested by rebasing the changes on top of swift-4.2-DEVELOPMENT-SNAPSHOT-2018-08-25-a and running build-script.py -r -t. All tests pass.

cc @harlanhaskins

@allevato allevato requested a review from ahoppen August 31, 2018 15:00
Copy link
Member
@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Thanks for picking those over. Some initial comments from a first glance over where I think we should also cherry-pick some more commits from master to not get the two branches to diverge even more.


/// An absolute position in a source file as text - the absolute utf8Offset from
/// the start, line, and column.
public final class AbsolutePosition {
Copy link
Member

Choose a reason for hiding this comment

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

I think it would make sense to also migrate AbsolutePosition to the state on master that is a struct and interacts with SourceLength, i.e. cherry-pick fc313fc and maybe 86be68f

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@@ -251,6 +251,13 @@ extension RawSyntax {
}
}

func accumulateTrailingTrivia(_ pos: AbsolutePosition) {
Copy link
Member

Choose a reason for hiding this comment

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

This has since been removed on master in favour of trailingTriviaLength so I don't think we should add it back here. Should be part of fc313fc

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

XCTAssertEqual(0, parsed.position.utf8Offset)
XCTAssertEqual(source.count,
parsed.eofToken.positionAfterSkippingLeadingTrivia.utf8Offset)
XCTAssertEqual(0, parsed.position.utf8Offset)
Copy link
Member

Choose a reason for hiding this comment

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

Can we write the above two XCTAssertEqual to 0 the other way round, i.e. 0 as the second parameter?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

XCTAssertEqual(parsed.byteSize, source.count)
XCTAssertEqual(source.count,
parsed.eofToken.positionAfterSkippingLeadingTrivia.utf8Offset)
XCTAssertEqual(0, parsed.position.utf8Offset)
Copy link
Member

Choose a reason for hiding this comment

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

Same here

XCTAssertEqual(parsed.position.utf8Offset, 0)

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

XCTAssertEqual(state.leadingTrivia!.byteSize + state.trailingTrivia!.byteSize
+ state.byteSizeAfterTrimmingTrivia, state.byteSize)
XCTAssertEqual(3, state.leadingTrivia!.count)
XCTAssertEqual(1, state.trailingTrivia!.count)
Copy link
Member

Choose a reason for hiding this comment

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

Again, expected value as the second parameter.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

allevato and others added 16 commits September 5, 2018 08:17
572a144 Rename byteOffset to utf8Offset and remote utf16
9323d08 [SwiftSyntax] Add accessors for source locations and test diagnostic emission (#16141)
Since absolute position is defined by accumulating proceeding nodes,
we should allow its access for nodes without SourceFileSyntax as root.
SourcePresence and ID are already shared between the two node kinds and
the node's length will soon be cached in RawSyntax as well. By making it
a struct, we will be able to compute the node's length when it is being
constructed in the initialiser.
This was originally "[swiftSyntax] Add test cases for the SyntaxClassifier"
(apple/swift-syntax e7c90be). We don't
actually pull in any SyntaxClassifier logic, but this commit does fix a
bug where compiler errors cause the entire parse to fail.
This way we will be able to avoid reclassifying these nodes for syntax
highlighting since we know they haven't changed.
AbsolutePosition being a mutable reference type easily leads to bugs
where an AbsolutePosition is modified. Making it immutable eliminates
this issue. Furthermore, the introduction of SourceLength should allow
easier manipulation of AbsolutePositions on the client side.

We still cannot make AbsolutePosition a value type since it is used
inside AtomicCache, but the immutability gives the same safety.
AbsolutePosition had value semantics anyway, the only reason it was a
reference type was so that we can use it in AtomicCache. But that can be
worked around by boxing it into a reference type.
The methods were never executed because DEBUG was never defined in
normal builds and the only way to create nodes is through generated
factory methods which provide the same safety `validate` was supposed to
ensure at the interface level.
This change was made post-4.2 in apple/swift's GYB support files.
We copy it here because it's generally useful.
@allevato allevato force-pushed the swift-4.2-cherrypicks branch from 62d9c34 to 5d9650b Compare September 7, 2018 07:58
@allevato
Copy link
Member Author
allevato commented Sep 7, 2018

Good call on the extra cherrypicks. I ended up pulling everything I was able to from master, which was just about everything except for byte-tree and classifier changes. We'll have to update the formatter a bit more to use the public API changes, but it will be better in the long-run because we'll have less churn going from 4.2 to 5.0.

The final commit is a workaround for the fact that I can't just cherrypick the change I want for list nodes because the change occurred in apple/swift.

All tests still pass locally. Please take a look.

Copy link
Member
@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

I guess cherry-picking almost everything is indeed the cleanest solution. I just want to quickly hear @nkcsgexi 's opinion on it, but should be fine.

Changes look good overall. I've got some minor comments.

Other than that, I'd like to wait until PR testing is set up for this repository before merging. Shouldn't take much longer though.

let result = try swiftcRunner.invoke()
return result.stdoutData
public var description: String {
switch self{
Copy link
Member

Choose a reason for hiding this comment

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

Space after self. If this is also badly formatted on master, it's my fault but we could still make it clean here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done. (Any style errors in these commits are also in master, since I did straight cherrypicks without modification except in rare cases, but I'm happy to fix them up here.)

# live in apple/swift, so we've simply copied the updated implementation
# here and updated the calls below.
# See: https://github.com/apple/swift/commit/a66931e7
def PATCHED_is_visitable(node):
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 say that the fact that is_visitable was only introduced post 4.2 is an implementation detail and would thus still name the function is_visitable. IMHO there is no need to prefix it with PATCHED.

Copy link
Member Author

Choose a reason for hiding this comment

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

is_visitable was present in/before 4.2, but its implementation has changed; that's why I gave it a different name, to make sure that it didn't collide with the same symbol from gyb_syntax_support/__init__.py and to make it stand out to future readers.

Copy link
Member

Choose a reason for hiding this comment

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

OK, sounds good.

}

public class DiagnosticsTestCase: XCTestCase {
public class DiagnosticTestCase: XCTestCase {
Copy link
Member

Choose a reason for hiding this comment

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

Sorry for spelling that test case wrong when I cherry-picked. Could you also rename the file? Also, we should change this on master.

Copy link
Member Author

Choose a reason for hiding this comment

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

Rename it to DiagnosticTestCase.swift?

Right now it looks like all of the test files and the test classes inside them are named inconsistently; it might be cleaner to do a separate PR that unifies them all (and do it on both branches) instead of trying to add it to this cherrypick.

Copy link
Member

Choose a reason for hiding this comment

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

That makes sense. Let's tackle it in another PR then.

let parsed = try SyntaxTreeParser.parse(getInput("nested-blocks.swift"))
let visitor = VisitCollections()
visitor.visit(parsed)
XCTAssertEqual(4, visitor.numberOfCodeBlockItems)
Copy link
Member

Choose a reason for hiding this comment

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

One more occurrence where I'd prefer to have the expected value on the right.

Copy link
Member Author

Choose a reason for hiding this comment

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

You and @harlanhaskins have different points of view about expected/actual argument order 🙂 (I don't have a preference either way, so I'll change it since I've already changed the others.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I'm with @ahoppen on this. The only reason I had them as expected-first is because that's the order StdlibUnittest expected them, which made me sad.

Copy link
Member
@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Looking good now.

@ahoppen
Copy link
Member
ahoppen commented Sep 12, 2018

@ahoppen ahoppen merged commit aa3f4bd into swiftlang:swift-4.2-branch Sep 12, 2018
@allevato allevato deleted the swift-4.2-cherrypicks branch September 17, 2018 18:46
CippoX added a commit to CippoX/swift-syntax that referenced this pull request Mar 21, 2023
# This is the 1st commit message:

fixed testAvailabilityQuery34 and testAvailabilityQueryUnavailability28

# This is the commit message swiftlang#2:

Update Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Co-authored-by: Kim de Vos <kimdevos12@hotmail.com>
# This is the commit message swiftlang#3:

added fixedSource in test case

# This is the commit message swiftlang#4:

minor changes

# This is the commit message swiftlang#5:

implemented recovery inside the parser

# This is the commit message swiftlang#6:

runned format.py

# This is the commit message swiftlang#7:

minor changes

# This is the commit message swiftlang#8:

minor changes
adevress pushed a commit to adevress/swift-syntax that referenced this pull request Jan 14, 2024
Fix an index-out-of-bounds error in DontRepeatTypeInStaticProperties.
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
0