Skip to content

Fix a crash when string interpolation creates a MissingStmtSyntax #1291

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

ahoppen
Copy link
Member

@ahoppen ahoppen commented Jan 28, 2023

We were crashing here because string interpolation created a MissingStmtSyntax, which doesn’t have any children. It then tried to assign the remaining tokens as unexpected tokens to the last child of the MissingStmtSyntax, which failed (because there are no children).

The fix here is to just add an unexpected child to the missing nodes. I’ve got a gut feeling that that might be useful anyway.

@ahoppen ahoppen requested a review from bnbarham January 28, 2023 14:42
String(describing: error),
"""

1 │ return 1
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer not testing the formatted string here. That's going to be a pain if we ever change it 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

We already have tests that check the diagnostic format in SwiftDiagnosticTests and I really want to check that we produce a nice-looking error message here.

So: I would-refer to keep it.

@@ -19,8 +19,10 @@ import SwiftParser
import SwiftParserDiagnostics

extension SyntaxParseable {
public init(stringInterpolationOrThrow stringInterpolation: SyntaxStringInterpolation) throws {
self = try performParse(source: stringInterpolation.sourceText, parse: { parser in
public typealias StringInterpolation = SyntaxStringInterpolation
Copy link
Contributor

Choose a reason for hiding this comment

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

Why wasn't this needed before?

Copy link
Member Author

Choose a reason for hiding this comment

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

I honestly don’t know and gave up trying to figure out why we need it now after a couple of minutes.

self = trivia
if pieces.contains(where: { $0.isUnexpected }) {
var diagnostics: [Diagnostic] = []
let tree = SourceFileSyntax(statements: [], eofToken: .eof(leadingTrivia: self))
Copy link
Contributor

Choose a reason for hiding this comment

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

Little bit weird, but I suppose this is what we were doing before.

Copy link
Member Author

Choose a reason for hiding this comment

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

It does the job of presenting a nice diagnostic so 🤷

We were crashing here because string interpolation created a `MissingStmtSyntax`, which doesn’t have any children. It then tried to assign the remaining tokens as unexpected tokens to the last child of the `MissingStmtSyntax`, which failed (because there are no children).

The fix here is to just add an `unexpected` child to the missing nodes. I’ve got a gut feeling that that might be useful anyway.
@ahoppen ahoppen changed the title Fix a crash when string interpolation creates a MissingStmtSyntax 🚥 #1290 Fix a crash when string interpolation creates a MissingStmtSyntax Jan 31, 2023
@ahoppen ahoppen force-pushed the ahoppen/invalid-string-interpolation-crash branch from 806a42f to b6bfbb2 Compare January 31, 2023 10:55
@ahoppen
Copy link
Member Author

ahoppen commented Jan 31, 2023

@swift-ci Please test

@ahoppen ahoppen merged commit dfb8deb into swiftlang:main Jan 31, 2023
@ahoppen ahoppen deleted the ahoppen/invalid-string-interpolation-crash branch January 31, 2023 17:47
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