Skip to content

[Macros] Unpack DiagnosticsError into separate diagnostics #64745

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
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
90 changes: 38 additions & 52 deletions lib/ASTGen/Sources/ASTGen/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -310,23 +310,35 @@ func expandFreestandingMacroInProcess(
discriminator: discriminator
)

guard let parentExpansion = macroSyntax.asProtocol(
FreestandingMacroExpansionSyntax.self
) else {
print("not on a macro expansion node: \(macroSyntax.recursiveDescription)")
return nil
}

let macroName = parentExpansion.macro.text

// Make sure we emit all of the diagnostics from the context.
defer {
// Emit diagnostics accumulated in the context.
for diag in context.diagnostics {
sourceManager.diagnose(
diagnostic: diag,
messageSuffix: " (from macro '\(macroName)')"
)
}

context.diagnostics = []
}

let macroPtr = macroPtr.bindMemory(to: ExportedMacro.self, capacity: 1)

let macroName: String
let evaluatedSyntax: Syntax
do {
switch macroPtr.pointee.macro {
// Handle expression macro.
case let exprMacro as ExpressionMacro.Type:
guard let parentExpansion = macroSyntax.asProtocol(
FreestandingMacroExpansionSyntax.self
) else {
print("not on a macro expansion node: \(macroSyntax.recursiveDescription)")
return nil
}

macroName = parentExpansion.macro.text

func expandExpressionMacro<Node: FreestandingMacroExpansionSyntax>(
_ node: Node
) throws -> ExprSyntax {
Expand All @@ -346,14 +358,6 @@ func expandFreestandingMacroInProcess(
// Handle declaration macro. The resulting decls are wrapped in a
// `CodeBlockItemListSyntax`.
case let declMacro as DeclarationMacro.Type:
guard let parentExpansion = macroSyntax.asProtocol(
FreestandingMacroExpansionSyntax.self
) else {
print("not on a macro expansion decl: \(macroSyntax.recursiveDescription)")
return nil
}
macroName = parentExpansion.macro.text

func expandDeclarationMacro<Node: FreestandingMacroExpansionSyntax>(
_ node: Node
) throws -> [DeclSyntax] {
Expand All @@ -374,25 +378,10 @@ func expandFreestandingMacroInProcess(
return nil
}
} catch {
// Record the error
sourceManager.diagnose(
diagnostic: Diagnostic(
node: macroSyntax,
message: ThrownErrorDiagnostic(message: String(describing: error))
),
messageSuffix: " (from macro '\(macroName)')"
)
context.addDiagnostics(from: error, node: macroSyntax)
return nil
}

// Emit diagnostics accumulated in the context.
for diag in context.diagnostics {
sourceManager.diagnose(
diagnostic: diag,
messageSuffix: " (from macro '\(macroName)')"
)
}

return evaluatedSyntax.trimmedDescription
}

Expand Down Expand Up @@ -670,6 +659,20 @@ func expandAttachedMacroInProcess(
)

let macroName = customAttrNode.attributeName.trimmedDescription

// Emit all of the accumulated diagnostics before we exit.
defer {
// Emit diagnostics accumulated in the context.
for diag in context.diagnostics {
sourceManager.diagnose(
diagnostic: diag,
messageSuffix: " (from macro '\(macroName)')"
)
}

context.diagnostics = []
}

var expandedSources: [String]
do {
switch (macro, macroRole) {
Expand Down Expand Up @@ -806,27 +809,10 @@ func expandAttachedMacroInProcess(
return nil
}
} catch {
// Record the error
// FIXME: Need to decide where to diagnose the error:
sourceManager.diagnose(
diagnostic: Diagnostic(
node: Syntax(declarationNode),
message: ThrownErrorDiagnostic(message: String(describing: error))
),
messageSuffix: " (from macro '\(macroName)')"
)

context.addDiagnostics(from: error, node: declarationNode)
return nil
}

// Emit diagnostics accumulated in the context.
for diag in context.diagnostics {
sourceManager.diagnose(
diagnostic: diag,
messageSuffix: " (from macro '\(macroName)')"
)
}

return expandedSources
}

Expand Down
45 changes: 45 additions & 0 deletions test/Macros/Inputs/syntax_macro_definitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,51 @@ public struct WarningMacro: ExpressionMacro {
}
}

public struct ErrorMacro: ExpressionMacro {
public static func expansion(
of macro: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) throws -> ExprSyntax {
guard let firstElement = macro.argumentList.first,
let stringLiteral = firstElement.expression.as(StringLiteralExprSyntax.self),
stringLiteral.segments.count == 1,
case let .stringSegment(messageString)? = stringLiteral.segments.first
else {
let errorNode: Syntax
if let firstElement = macro.argumentList.first {
errorNode = Syntax(firstElement)
} else {
errorNode = Syntax(macro)
}

let messageID = MessageID(domain: "silly", id: "error")
let diag = Diagnostic(
node: errorNode,
message: SimpleDiagnosticMessage(
message: "#myError macro requires a string literal",
diagnosticID: messageID,
severity: .error
)
)

throw DiagnosticsError(diagnostics: [diag])
}

context.diagnose(
Diagnostic(
node: Syntax(macro),
message: SimpleDiagnosticMessage(
message: messageString.content.description,
diagnosticID: MessageID(domain: "test", id: "error"),
severity: .error
)
)
)

return "()"
}
}

public struct PropertyWrapperMacro {}

extension PropertyWrapperMacro: AccessorMacro, Macro {
Expand Down
15 changes: 14 additions & 1 deletion test/Macros/macro_expand_throwing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: %target-build-swift -swift-version 5 -I %swift-host-lib-dir -L %swift-host-lib-dir -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath -swift-version 5

// Make sure the diagnostic comes through...
// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -module-name MacroUser
// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -module-name MacroUser -DTEST_DIAGNOSTICS

// Make sure the diagnostic doesn't crash in SILGen
// RUN: not %target-swift-frontend -swift-version 5 -emit-sil -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir %s -module-name MacroUser -o - -g
Expand All @@ -12,9 +12,22 @@

@freestanding(expression) macro myWarning(_ message: String) = #externalMacro(module: "MacroDefinition", type: "WarningMacro")

@freestanding(expression) macro myError(_ message: String) = #externalMacro(module: "MacroDefinition", type: "ErrorMacro")

func testThrownError() {
let name = "hello"
#myWarning (name) // expected-error{{#myWarning macro requires a string literal (from macro 'myWarning')}}

#myWarning("experimental features ahead") // expected-warning{{experimental features ahead}}
}

#if TEST_DIAGNOSTICS
func testThrownErrors() {
let name = "hello"
#myError(
name
) // expected-error@-1{{macro requires a string literal (from macro 'myError')}}

#myError("experimental features ahead") // expected-error{{experimental features ahead}}
}
#endif