Skip to content

[5.9] Fix for some wrong attribute fix its #1688

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/StringLiterals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ extension Parser {
// string literal.
guard currentToken.leadingTriviaText.isEmpty else { break }

if let stringSegment = self.consume(if: .stringSegment) {
if let stringSegment = self.consume(if: .stringSegment, TokenSpec(.identifier, remapping: .stringSegment)) {
segments.append(.stringSegment(RawStringSegmentSyntax(content: stringSegment, arena: self.arena)))
} else if let backslash = self.consume(if: .backslash) {
let (unexpectedBeforeDelimiter, delimiter) = self.parsePoundDelimiter(.rawStringDelimiter, matching: openDelimiter)
Expand Down
59 changes: 59 additions & 0 deletions Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,35 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
return handleMissingSyntax(node, additionalHandledNodes: [node.placeholder.id])
}

override open func visit(_ node: OriginallyDefinedInArgumentsSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
}
if let token = node.unexpectedBetweenModuleLabelAndColon?.onlyToken(where: { $0.tokenKind.isIdentifier }),
node.moduleLabel.presence == .missing
{
addDiagnostic(
node,
MissingNodesError(missingNodes: [Syntax(node.moduleLabel)]),
fixIts: [
FixIt(
message: ReplaceTokensFixIt(
replaceTokens: [token],
replacement: node.moduleLabel
),
changes: [
FixIt.MultiNodeChange.makeMissing(token),
FixIt.MultiNodeChange.makePresent(node.moduleLabel),
]
)
],
handledNodes: [node.moduleLabel.id, token.id]
)
}

return .visitChildren
}

public override func visit(_ node: OperatorDeclSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
Expand Down Expand Up @@ -1223,6 +1252,36 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
return .visitChildren
}

public override func visit(_ node: UnavailableFromAsyncArgumentsSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
}

if let token = node.unexpectedBetweenMessageLabelAndColon?.onlyToken(where: { $0.tokenKind.isIdentifier }),
node.messageLabel.presence == .missing
{
addDiagnostic(
node,
MissingNodesError(missingNodes: [Syntax(node.messageLabel)]),
fixIts: [
FixIt(
message: ReplaceTokensFixIt(
replaceTokens: [token],
replacement: node.messageLabel
),
changes: [
FixIt.MultiNodeChange.makeMissing(token),
FixIt.MultiNodeChange.makePresent(node.messageLabel),
]
)
],
handledNodes: [node.messageLabel.id, token.id]
)
}

return .visitChildren
}

public override func visit(_ node: UnresolvedTernaryExprSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
Expand Down
48 changes: 37 additions & 11 deletions Tests/SwiftParserTest/AttributeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -422,12 +422,23 @@ final class AttributeTests: XCTestCase {

assertParse(
"""
@_expose(Cxx, 1️⃣baz) func foo() {}
@_expose(Cxx, 1️⃣baz2️⃣) func foo() {}
""",
diagnostics: [
DiagnosticSpec(message: "expected string literal to end @_expose arguments"),
DiagnosticSpec(message: "unexpected code 'baz' in attribute"),
]
DiagnosticSpec(
locationMarker: "1️⃣",
message: #"expected '"' in string literal"#,
fixIts: [#"insert '"'"#]
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: #"expected '"' to end string literal"#,
fixIts: [#"insert '"'"#]
),
],
Comment on lines +428 to +438
Copy link
Contributor

Choose a reason for hiding this comment

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

How difficult is it to make this a single diagnostic? Seems a little weird as two to me, ie. I would expect a surround '...' in '"'

Copy link
Member

@ahoppen ahoppen May 22, 2023

Choose a reason for hiding this comment

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

Filed #1691. We can take care of this on main, it’s not necessary to do it for Swift 5.9.

fixedSource: """
@_expose(Cxx, "baz") func foo() {}
"""
)
}

Expand Down Expand Up @@ -475,9 +486,12 @@ final class AttributeTests: XCTestCase {
func foo() {}
""",
diagnostics: [
DiagnosticSpec(message: "expected 'message' in @_unavailableFromAsync argument"),
DiagnosticSpec(message: "unexpected code 'nope' before @_unavailableFromAsync argument"),
]
DiagnosticSpec(message: "expected 'message' in @_unavailableFromAsync argument", fixIts: ["replace 'nope' with 'message'"])
],
fixedSource: """
@_unavailableFromAsync(message: "abc")
func foo() {}
"""
)

assertParse(
Expand All @@ -493,13 +507,25 @@ final class AttributeTests: XCTestCase {

assertParse(
"""
@_unavailableFromAsync(message: 1️⃣abc)
@_unavailableFromAsync(message: 1️⃣abc2️⃣)
func foo() {}
""",
diagnostics: [
DiagnosticSpec(message: "expected string literal to end @_unavailableFromAsync argument"),
DiagnosticSpec(message: "unexpected code 'abc' in attribute"),
]
DiagnosticSpec(
locationMarker: "1️⃣",
message: #"expected '"' in string literal"#,
fixIts: [#"insert '"'"#]
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: #"expected '"' to end string literal"#,
fixIts: [#"insert '"'"#]
),
],
fixedSource: """
@_unavailableFromAsync(message: "abc")
func foo() {}
"""
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ final class OriginalDefinedInAttrTests: XCTestCase {
public func foo1() {}
"""#,
diagnostics: [
DiagnosticSpec(message: "expected 'module' in @_originallyDefinedIn arguments"),
DiagnosticSpec(message: "unexpected code 'modulename' before @_originallyDefinedIn arguments"),
]
DiagnosticSpec(
message: "expected 'module' in @_originallyDefinedIn arguments",
fixIts: ["replace 'modulename' with 'module'"]
)
],
fixedSource: #"""
@_originallyDefinedIn(module: "foo", OSX 13.13)
public func foo1() {}
"""#
)
}

Expand All @@ -53,20 +59,51 @@ final class OriginalDefinedInAttrTests: XCTestCase {
public class ToplevelClass1 {}
"""#,
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "expected ',' and version list in @_originallyDefinedIn arguments")
DiagnosticSpec(
message: "expected ',' and version list in @_originallyDefinedIn arguments",
fixIts: ["insert ',' and version list"]
)
]
)
}

func testOriginalDefinedInAttr5() {
assertParse(
"""
@_originallyDefinedIn(1️⃣OSX 13.13.3)
@_originallyDefinedIn(1️⃣OSX 2️⃣13.13.3)
public class ToplevelClass2 {}
""",
diagnostics: [
DiagnosticSpec(message: "expected 'module:', string literal, and ',' in @_originallyDefinedIn arguments")
]
DiagnosticSpec(
locationMarker: "1️⃣",
message: "expected 'module:' in @_originallyDefinedIn arguments",
fixIts: ["insert 'module:'"]
),
DiagnosticSpec(
locationMarker: "1️⃣",
message: #"expected '"' in string literal"#,
fixIts: [#"insert '"'"#]
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: #"expected '"' to end string literal"#,
fixIts: [#"insert '"'"#]
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "expected ',' in @_originallyDefinedIn arguments",
fixIts: ["insert ','"]
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "expected platform in version restriction",
fixIts: ["insert platform"]
),
],
fixedSource: """
@_originallyDefinedIn(module: "OSX", <#identifier#> 13.13.3)
public class ToplevelClass2 {}
"""
)
}

Expand Down Expand Up @@ -172,5 +209,4 @@ final class OriginalDefinedInAttrTests: XCTestCase {
"""
)
}

}