Skip to content

[6.0] Fix parsing issues related to suppressed conformances / noncopyable generics #2585

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
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
2 changes: 1 addition & 1 deletion Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ extension Parser {
)

case (.any, _)?:
if !atContextualExpressionModifier() {
if !atContextualExpressionModifier() && !self.peek().isContextualPunctuator("~") {
break EXPR_PREFIX
}

Expand Down
3 changes: 3 additions & 0 deletions Sources/SwiftParser/TokenConsumer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ extension TokenConsumer {
return false
}

case .prefixOperator where lexeme.isContextualPunctuator("~"):
return true

default:
return false
}
Expand Down
3 changes: 3 additions & 0 deletions Sources/SwiftParser/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,9 @@ extension Parser.Lookahead {
switch self.currentToken {
case TokenSpec(.Any):
self.consumeAnyToken()
case TokenSpec(.prefixOperator) where self.currentToken.tokenText == "~":
self.consumeAnyToken();
fallthrough
case TokenSpec(.Self), TokenSpec(.identifier):
guard self.canParseTypeIdentifier() else {
return false
Expand Down
103 changes: 103 additions & 0 deletions Tests/SwiftParserTest/TypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,109 @@ final class TypeTests: ParserTestCase {
)
}

func testInverseTypes() {
assertParse(
"[~Copyable]()"
)

assertParse(
"[any ~Copyable]()"
)

assertParse(
"[any P & ~Copyable]()"
)

assertParse(
"[P & ~Copyable]()"
)

assertParse(
"X<~Copyable>()"
)

assertParse(
"X<any ~Copyable>()"
)

assertParse(
"X<P & ~Copyable>()"
)

assertParse(
"X<any P & ~Copyable>()"
)
}

func testInverseTypesAsExpr() {
assertParse(
"(~Copyable).self"
)

assertParse(
"~Copyable.self"
)

assertParse(
"(any ~Copyable).self"
)
}

func testInverseTypesInParameter() {
assertParse(
"func f(_: borrowing ~Copyable) {}"
)

assertParse(
"func f(_: consuming ~Copyable) {}"
)

assertParse(
"func f(_: borrowing any ~Copyable) {}"
)

assertParse(
"func f(_: consuming any ~Copyable) {}"
)

assertParse(
"func f(_: ~Copyable) {}"
)

assertParse(
"typealias T = (~Copyable) -> Void"
)

assertParse(
"typealias T = (_ x: ~Copyable) -> Void"
)

assertParse(
"typealias T = (borrowing ~Copyable) -> Void"
)

assertParse(
"typealias T = (_ x: borrowing ~Copyable) -> Void"
)

assertParse(
"typealias T = (borrowing any ~Copyable) -> Void"
)

assertParse(
"typealias T = (_ x: borrowing any ~Copyable) -> Void"
)

assertParse(
"func f(_: any borrowing 1️⃣~Copyable) {}",
diagnostics: [
DiagnosticSpec(
message: "unexpected code '~Copyable' in parameter clause"
)
]
)
}

func testTypedThrows() {
assertParse(
"""
Expand Down