Skip to content

Fix parsing attributes at the start of an accessor #756

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 1 commit into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions Sources/SwiftParser/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1519,33 +1519,37 @@ extension Parser {
struct AccessorIntroducer {
var attributes: RawAttributeListSyntax?
var modifier: RawDeclModifierSyntax?
var introducer: (AccessorKind, RawTokenSyntax)?
var kind: AccessorKind
var token: RawTokenSyntax
}

mutating func parseAccessorIntroducer() -> AccessorIntroducer {
mutating func parseAccessorIntroducer() -> AccessorIntroducer? {
// Check there is an identifier before consuming
var look = self.lookahead()
let _ = look.consumeAttributeList()
let hasModifier = look.consume(ifAny: [], contextualKeywords: ["mutating", "nonmutating", "__consuming"]) != nil
guard let (kind, handle) = look.at(anyIn: AccessorKind.self) else {
return nil
}

let attrs = self.parseAttributeList()

// Parse the contextual keywords for 'mutating' and 'nonmutating' before
// get and set.
let modifier: RawDeclModifierSyntax?
if let name = self.consume(ifAny: [], contextualKeywords: ["mutating", "nonmutating", "__consuming"]) {
if hasModifier {
modifier = RawDeclModifierSyntax(
name: name,
name: self.consumeAnyToken(),
detail: nil,
arena: self.arena
)
} else {
modifier = nil
}

guard let (kind, handle) = self.at(anyIn: AccessorKind.self) else {
return AccessorIntroducer(
attributes: attrs, modifier: modifier, introducer: nil)
}

let introducer = self.eat(handle)
return AccessorIntroducer(
attributes: attrs, modifier: modifier, introducer: (kind, introducer))
attributes: attrs, modifier: modifier, kind: kind, token: introducer)
}

@_spi(RawSyntax)
Expand Down Expand Up @@ -1612,8 +1616,7 @@ extension Parser {
do {
var loopProgress = LoopProgressCondition()
while !self.at(any: [.eof, .rightBrace]) && loopProgress.evaluate(currentToken) {
let introducer = self.parseAccessorIntroducer()
guard let (kind, kindToken) = introducer.introducer else {
guard let introducer = self.parseAccessorIntroducer() else {
// There can only be an implicit getter if no other accessors were
// seen before this one.
guard elements.isEmpty else {
Expand Down Expand Up @@ -1649,7 +1652,7 @@ extension Parser {
//
// set-name ::= '(' identifier ')'
let parameter: RawAccessorParameterSyntax?
if [ AccessorKind.set, .willSet, .didSet ].contains(kind), let lparen = self.consume(if: .leftParen) {
if [ AccessorKind.set, .willSet, .didSet ].contains(introducer.kind), let lparen = self.consume(if: .leftParen) {
let (unexpectedBeforeName, name) = self.expectIdentifier()
let (unexpectedBeforeRParen, rparen) = self.expect(.rightParen)
parameter = RawAccessorParameterSyntax(
Expand Down Expand Up @@ -1681,7 +1684,7 @@ extension Parser {
elements.append(RawAccessorDeclSyntax(
attributes: introducer.attributes,
modifier: introducer.modifier,
accessorKind: kindToken,
accessorKind: introducer.token,
parameter: parameter,
asyncKeyword: asyncKeyword,
throwsKeyword: throwsKeyword,
Expand Down
22 changes: 22 additions & 0 deletions Tests/SwiftParserTest/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,28 @@ final class DeclarationTests: XCTestCase {
async let theVeryLastPhotoWeWant = fetch("4.jpg")
"""
)

AssertParse(
"""
var foo: Int {
@available(swift 5.0)
func myFun() -> Int {
return 42
}
return myFun()
}
"""
)

AssertParse(
"""
var foo: Int {
mutating set {
test += 1
}
}
"""
)
}

func testTypealias() {
Expand Down