Skip to content

Add representation and parser support for the @isolated type attribute #2472

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
Feb 7, 2024
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
54 changes: 45 additions & 9 deletions Sources/SwiftParser/Attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ extension Parser {
parseArguments: (inout Parser) -> RawAttributeSyntax.Arguments
) -> RawAttributeListSyntax.Element {
let (unexpectedBeforeAtSign, atSign) = self.expect(.atSign)
let attributeName = self.parseType()
let attributeName = self.parseAttributeName()
let shouldParseArgument: Bool
switch argumentMode {
case .required:
Expand Down Expand Up @@ -358,21 +358,40 @@ extension Parser {
}
}

extension Parser {
mutating func parseMacroRoleArguments() -> [RawLabeledExprSyntax] {
let (unexpectedBeforeRole, role) = self.expect(.identifier, TokenSpec(.extension, remapping: .identifier), default: .identifier)
let roleTrailingComma = self.consume(if: .comma)
let roleElement = RawLabeledExprSyntax(
extension RawLabeledExprSyntax {
fileprivate init(
_ unexpectedBeforeIdentifier: RawUnexpectedNodesSyntax? = nil,
identifier: RawTokenSyntax,
_ unexpectedBetweenIdentifierAndTrailingComma: RawUnexpectedNodesSyntax? = nil,
trailingComma: RawTokenSyntax? = nil,
arena: __shared SyntaxArena
) {
self.init(
label: nil,
colon: nil,
expression: RawExprSyntax(
RawDeclReferenceExprSyntax(
unexpectedBeforeRole,
baseName: role,
unexpectedBeforeIdentifier,
baseName: identifier,
argumentNames: nil,
arena: self.arena
arena: arena
)
),
unexpectedBetweenIdentifierAndTrailingComma,
trailingComma: trailingComma,
arena: arena
)
}
}

extension Parser {
mutating func parseMacroRoleArguments() -> [RawLabeledExprSyntax] {
let (unexpectedBeforeRole, role) = self.expect(.identifier, TokenSpec(.extension, remapping: .identifier), default: .identifier)
let roleTrailingComma = self.consume(if: .comma)

let roleElement = RawLabeledExprSyntax(
unexpectedBeforeRole,
identifier: role,
trailingComma: roleTrailingComma,
arena: self.arena
)
Expand Down Expand Up @@ -848,6 +867,23 @@ extension Parser {
}
}

extension Parser {
mutating func parseIsolatedAttributeArguments() -> RawLabeledExprListSyntax {
let (unexpectedBeforeIsolationKind, isolationKind) =
self.expectIdentifier(allowKeywordsAsIdentifier: true)
let isolationKindElement = RawLabeledExprSyntax(
unexpectedBeforeIsolationKind,
identifier: isolationKind,
arena: self.arena
)

return RawLabeledExprListSyntax(
elements: [isolationKindElement],
arena: self.arena
)
}
}

extension Parser {
mutating func parseBackDeployedAttributeArguments() -> RawBackDeployedAttributeArgumentsSyntax {
let (unexpectedBeforeLabel, label) = self.expect(.keyword(.before))
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftParser/Lookahead.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ extension Parser.Lookahead {
// Ok, it is a valid attribute, eat it, and then process it.
self.eat(handle)
switch attr {
case .convention:
case .convention, .isolated:
self.skipSingle()
default:
break
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftParser/TokenPrecedence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ enum TokenPrecedence: Comparable {
.Sendable,
.retroactive,
.unchecked:
// Note that .isolated is preferred as a decl keyword
self = .exprKeyword

case // `DeclarationAttributeWithSpecialSyntax`
Expand Down
3 changes: 3 additions & 0 deletions Sources/SwiftParser/TokenSpecSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ enum TypeAttribute: TokenSpecSet {
case retroactive
case Sendable
case unchecked
case isolated

init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
switch PrepareForKeywordMatch(lexeme) {
Expand All @@ -660,6 +661,7 @@ enum TypeAttribute: TokenSpecSet {
case TokenSpec(.Sendable): self = .Sendable
case TokenSpec(.retroactive): self = .retroactive
case TokenSpec(.unchecked): self = .unchecked
case TokenSpec(.isolated): self = .isolated
default: return nil
}
}
Expand All @@ -680,6 +682,7 @@ enum TypeAttribute: TokenSpecSet {
case .retroactive: return .keyword(.retroactive)
case .Sendable: return .keyword(.Sendable)
case .unchecked: return .keyword(.unchecked)
case .isolated: return .keyword(.isolated)
}
}
}
Expand Down
17 changes: 16 additions & 1 deletion Sources/SwiftParser/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,15 @@ extension Parser {
}
}

/// Parse the subset of types that we allow in attribute names.
mutating func parseAttributeName() -> RawTypeSyntax {
return parseSimpleType(forAttributeName: true)
}

/// Parse a "simple" type
mutating func parseSimpleType(
stopAtFirstPeriod: Bool = false
stopAtFirstPeriod: Bool = false,
forAttributeName: Bool = false
) -> RawTypeSyntax {
enum TypeBaseStart: TokenSpecSet {
case `Self`
Expand Down Expand Up @@ -303,6 +309,11 @@ extension Parser {
continue
}

// Do not allow ? or ! suffixes when parsing attribute names.
if forAttributeName {
break
}

if self.at(TokenSpec(.postfixQuestionMark, allowAtStartOfLine: false)) {
base = RawTypeSyntax(self.parseOptionalType(base))
continue
Expand Down Expand Up @@ -937,6 +948,10 @@ extension Parser {
return parseAttribute(argumentMode: .required) { parser in
return .opaqueReturnTypeOfAttributeArguments(parser.parseOpaqueReturnTypeOfAttributeArguments())
}
case .isolated:
return parseAttribute(argumentMode: .required) { parser in
return .argumentList(parser.parseIsolatedAttributeArguments())
}
case nil: // Custom attribute
return parseAttribute(argumentMode: .customAttribute) { parser in
let arguments = parser.parseArgumentListElements(pattern: .none)
Expand Down
40 changes: 40 additions & 0 deletions Tests/SwiftParserTest/AttributeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -907,4 +907,44 @@ final class AttributeTests: ParserTestCase {
)
)
}

func testIsolatedTypeAttribute() {
assertParse(
"""
var fn: @isolated(any) () -> ()
"""
)

// We don't validate the kind in the parser
assertParse(
"""
var fn: @isolated(sdfhsdfi) () -> ()
"""
)

// Check that this combines correctly with other attributs.
// This is not a valid combination, but we don't validate that here.
assertParse(
"""
var fn: @isolated(any) @convention(swift) () -> ()
"""
)
assertParse(
"""
var fn: @convention(swift) @isolated(any) () -> ()
"""
)

// Test that lookahead correctly skips the argument clause.
assertParse(
"""
var array = [@isolated(any) @convention(swift) () -> ()]()
"""
)
assertParse(
"""
var array = [@convention(swift) @isolated(any) () -> ()]()
"""
)
}
}