Skip to content

Parse _borrowing x contextually as a pattern binding when .borrowingSwitch feature is enabled. #2487

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

Conversation

jckarter
Copy link
Contributor

@jckarter jckarter commented Feb 8, 2024

The existing parsing under .referenceBindings parses this (along with _mutating and _consuming) as unconditional keywords, which would be a source break we can't ship. Narrow the behavior so that _borrowing is only parsed immediately before an identifier in a pattern.

@jckarter
Copy link
Contributor Author

jckarter commented Feb 8, 2024

@swift-ci Please test

@jckarter jckarter force-pushed the borrowing-switch-contextual-keyword branch from 32f793f to 1a7ff21 Compare February 8, 2024 15:35
@jckarter
Copy link
Contributor Author

jckarter commented Feb 8, 2024

@swift-ci Please test

jckarter added a commit to jckarter/swift that referenced this pull request Feb 8, 2024
And remove the `-disable-experimental-parser-round-trip` flag from borrowing
switch tests now that the SwiftSyntax parser supports them with
swiftlang/swift-syntax#2487.
@jckarter jckarter force-pushed the borrowing-switch-contextual-keyword branch from 1a7ff21 to c0c9a19 Compare February 8, 2024 19:26
@jckarter
Copy link
Contributor Author

jckarter commented Feb 8, 2024

@swift-ci Please test

Comment on lines 21 to +22
public let experimentalFeature: ExperimentalFeature?
public let experimentalFeature2: ExperimentalFeature?
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 be nicer to make this an array of experimental features + update the doc comment to say that the keyword is enabled if any of the experimental features are enabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know this stuff is performance-sensitive so I wasn't sure about introducing a refcounted field into the structure just for a one-off case where we happen to need two features for the same keyword. I'm also hoping I can just take this out again once the .borrowingSwitch feature is enabled, which will hopefully occur in the nearish term.

Copy link
Member

Choose a reason for hiding this comment

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

This is just part of the code generation, so it’s not performance critical at all.

init(
_ name: String,
experimentalFeature: ExperimentalFeature,
or experimentalFeature2: ExperimentalFeature,
Copy link
Member

Choose a reason for hiding this comment

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

And then make the initializer take a variadic parameter: experimentalFeature: ExperimentalFeature…,

Comment on lines 235 to 236
var backtrack = lookahead()
if backtrack.shouldParsePatternBinding(introducer: introducer) {
Copy link
Member

Choose a reason for hiding this comment

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

We call it lookahead not backtrack in the new parser because the parser doesn’t actually backtrack. It clones itself on lookahead() and the clone looks ahead while the parser itself stays at the same position.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think all of these uses can be replaced by withLookahead, which I didn't know about. I used backtrack because it is used in several places to name a lookahead variable elsewhere in the codebase; are those other places needing updating?

% git grep 'backtrack =' 
Sources/SwiftParser/Expressions.swift:      var backtrack = self.lookahead()
Sources/SwiftParser/Expressions.swift:      var backtrack = self.lookahead()
Sources/SwiftParser/Expressions.swift:      let backtrack = self.lookahead()
Sources/SwiftParser/Expressions.swift:        var backtrack = lookahead()
Sources/SwiftParser/Expressions.swift:      var backtrack = self.lookahead()
Sources/SwiftParser/Expressions.swift:    var backtrack = self.lookahead()
Sources/SwiftParser/Expressions.swift:    var backtrack = self.lookahead()
Sources/SwiftParser/Lookahead.swift:        var backtrack = self.lookahead()
Sources/SwiftParser/Names.swift:    var backtrack = self.lookahead()
Sources/SwiftParser/Names.swift:      var backtrack = self.lookahead()
Sources/SwiftParser/Patterns.swift:      var backtrack = self.lookahead()
Sources/SwiftParser/Patterns.swift:      var backtrack = lookahead()
Sources/SwiftParser/Types.swift:        var backtrack = self.lookahead()
Sources/SwiftParser/Types.swift:        var backtrack = self.lookahead()

Copy link
Member

Choose a reason for hiding this comment

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

Huh, yeah, they should probably all be updated. Probably still a leftover from the migration from C++. Filed #2494

@jckarter jckarter force-pushed the borrowing-switch-contextual-keyword branch from c0c9a19 to 007c84e Compare February 9, 2024 01:34
@jckarter
Copy link
Contributor Author

jckarter commented Feb 9, 2024

@swift-ci Please test

@jckarter
Copy link
Contributor Author

jckarter commented Feb 9, 2024

@ahoppen Thanks for the tip about withLookahead; using that I was able to integrate the contextual shouldParsePatternBinding check directly into the existing switches/if lets and simplify the control flow quite a bit. I also added substructure checks to the new tests. How's this look now?

@jckarter jckarter force-pushed the borrowing-switch-contextual-keyword branch from 007c84e to 6377fc9 Compare February 9, 2024 03:09
@jckarter
Copy link
Contributor Author

jckarter commented Feb 9, 2024

@swift-ci Please test

1 similar comment
@jckarter
Copy link
Contributor Author

jckarter commented Feb 9, 2024

@swift-ci Please test

…ngSwitch` feature is enabled.

The existing parsing under `.referenceBindings` parses this (along with `_mutating` and `_consuming`)
as unconditional keywords, which would be a source break we can't ship. Narrow the behavior so
that `_borrowing` is only parsed immediately before an identifier in a pattern.
@jckarter jckarter force-pushed the borrowing-switch-contextual-keyword branch from 6377fc9 to 43c13ca Compare February 9, 2024 17:35
@jckarter
Copy link
Contributor Author

jckarter commented Feb 9, 2024

@swift-ci Please test

@jckarter
Copy link
Contributor Author

jckarter commented Feb 9, 2024

@swift-ci Please test Windows

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.

Looks good to me. Would be good to address #2487 and #2487 but that can be a follow-up PR.

@jckarter jckarter merged commit 38ff630 into swiftlang:main Feb 12, 2024
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