-
Notifications
You must be signed in to change notification settings - Fork 439
Prepend SyntaxStringInterpolationError
errors in macro expansion with Internal macro error:
#2176
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
ahoppen
merged 2 commits into
swiftlang:main
from
natikgadzhi:macro-expansion/syntax-interpolation-errors
Sep 17, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -161,23 +161,31 @@ where Self.StringInterpolation == SyntaxStringInterpolation { | |
init(stringInterpolation: SyntaxStringInterpolation) | ||
} | ||
|
||
enum SyntaxStringInterpolationError: Error, CustomStringConvertible { | ||
case producedInvalidNodeType(expectedType: SyntaxProtocol.Type, actualType: SyntaxProtocol.Type) | ||
case diagnostics([Diagnostic], tree: Syntax) | ||
/// Describes an error when building a syntax node with string interpolation resulted in an unexpected node type. | ||
public struct SyntaxStringInterpolationInvalidNodeTypeError: Error, CustomStringConvertible { | ||
let expectedType: SyntaxProtocol.Type | ||
let actualType: SyntaxProtocol.Type | ||
|
||
/// Initialize the invalid node type error providing an expected type, and the actual node that resulted. | ||
public init<S: SyntaxProtocol>(expectedType: SyntaxProtocol.Type, actualNode: S) { | ||
self.expectedType = expectedType | ||
self.actualType = type(of: actualNode) | ||
} | ||
|
||
static func producedInvalidNodeType<S: SyntaxProtocol>(expectedType: SyntaxProtocol.Type, actualNode: S) -> Self { | ||
return .producedInvalidNodeType(expectedType: expectedType, actualType: type(of: actualNode)) | ||
public var description: String { | ||
return "Parsing the code snippet was expected to produce a \(expectedType) but produced a \(actualType)" | ||
} | ||
} | ||
|
||
/// A string interpolation error based on a ``SwiftDiagnostics/Diagnostic``. | ||
struct SyntaxStringInterpolationDiagnosticError: Error, CustomStringConvertible { | ||
let diagnostics: [Diagnostic] | ||
let tree: Syntax | ||
|
||
var description: String { | ||
switch self { | ||
case .producedInvalidNodeType(expectedType: let expectedType, actualType: let actualType): | ||
return "Parsing the code snippet was expected to produce a \(expectedType) but produced a \(actualType)" | ||
case .diagnostics(let diagnostics, let tree): | ||
// Start the diagnostic on a new line so it isn't prefixed with the file, which messes up the | ||
// column-aligned message from ``DiagnosticsFormatter``. | ||
Comment on lines
-177
to
-178
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we keep this comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, added that comment back <3 |
||
return "\n" + DiagnosticsFormatter.annotatedSource(tree: tree, diags: diagnostics) | ||
} | ||
// Start the diagnostic on a new line so it isn't prefixed with the file, which messes up the | ||
// column-aligned message from ``DiagnosticsFormatter``. | ||
return "\n" + DiagnosticsFormatter.annotatedSource(tree: tree, diags: diagnostics) | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
Tests/SwiftSyntaxMacroExpansionTest/StringInterpolationErrorTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import SwiftDiagnostics | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
import SwiftSyntaxMacroExpansion | ||
import SwiftSyntaxMacros | ||
import SwiftSyntaxMacrosTestSupport | ||
import XCTest | ||
|
||
// An error that is not `SyntaxStringInterpolationError`, only used to verify | ||
// that other error types won't get prefixed with `Internal macro error:` when | ||
// passed to `MacroExpansionContext.addDiagnostics`. | ||
private struct DummyError: Error { | ||
static let diagnosticTestError = DummyError() | ||
} | ||
|
||
// An extension macro that will fail with | ||
// `SyntaxStringInterpolationError.producedInvalidNodeType` | ||
private struct DummyMacro: ExtensionMacro { | ||
static func expansion( | ||
of node: AttributeSyntax, | ||
attachedTo declaration: some DeclGroupSyntax, | ||
providingExtensionsOf type: some TypeSyntaxProtocol, | ||
conformingTo protocols: [TypeSyntax], | ||
in context: some MacroExpansionContext | ||
) throws -> [ExtensionDeclSyntax] { | ||
let ext = try ExtensionDeclSyntax("var x: Int") | ||
return [ext] | ||
} | ||
} | ||
|
||
final class StringInterpolationErrorTests: XCTestCase { | ||
|
||
func testMacroExpansionContextAddDiagnosticsAddsSwiftSyntaxInterpolationErrorsWithWrappingMessage() throws { | ||
let context = BasicMacroExpansionContext() | ||
let error = SyntaxStringInterpolationInvalidNodeTypeError(expectedType: DeclSyntax.self, actualNode: ExprSyntax("test")) | ||
|
||
// Since we only care about the error switch inside of addDagnostics, we don't care about the particular node we're passing in | ||
context.addDiagnostics(from: error, node: ExprSyntax("1")) | ||
XCTAssertEqual(context.diagnostics.count, 1) | ||
let diagnostic = try XCTUnwrap(context.diagnostics.first) | ||
XCTAssertTrue(diagnostic.message.starts(with: "Internal macro error:")) | ||
} | ||
|
||
// Verify that any other error messages do not get "Internal macro error:" prefix. | ||
func testMacroExpansionContextAddDiagnosticsUsesErrorDescriptionForDiagMessage() throws { | ||
let context = BasicMacroExpansionContext() | ||
let error = DummyError.diagnosticTestError | ||
|
||
context.addDiagnostics(from: error, node: ExprSyntax("1")) | ||
XCTAssertEqual(context.diagnostics.count, 1) | ||
let diagnostic = try XCTUnwrap(context.diagnostics.first) | ||
XCTAssertEqual(diagnostic.message, String(describing: error)) | ||
} | ||
|
||
func testMacroExpansionSyntaxInterpolationErrorGetsPrefixed() { | ||
let expectedDiagnostic = DiagnosticSpec( | ||
message: "Internal macro error: Parsing the code snippet was expected to produce a ExtensionDeclSyntax but produced a DeclSyntax", | ||
line: 1, | ||
column: 1 | ||
) | ||
|
||
assertMacroExpansion( | ||
"@dummy struct Foo {}", | ||
expandedSource: "struct Foo {}", | ||
diagnostics: [expectedDiagnostic], | ||
macros: ["dummy": DummyMacro.self] | ||
) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.