Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improve diagnostics for macro annotated members #64

Merged
merged 1 commit into from
Jan 10, 2024
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/MMIO/Register.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/// A container type referencing of a region of memory whose layout is defined
/// by another type.
public struct Register<Value> where Value: RegisterValue {
public let unsafeAddress: UInt
public var unsafeAddress: UInt

#if FEATURE_INTERPOSABLE
public var interposer: (any MMIOInterposer)?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,4 @@ extension ErrorDiagnostic {
static func internalError() -> Self {
.init("'\(Macro.signature)' internal error. \(Self.internalErrorSuffix)")
}

// Declaration Member Errors
static func onlyMemberVarDecls() -> Self {
.init("'\(Macro.signature)' type can only contain properties")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ where Macro: ParsableMacro, Context: MacroExpansionContext {
fixIts: fixIts))
return ExpansionError()
}

func error(
at node: some SyntaxProtocol,
message: ErrorDiagnostic<Macro>,
highlights: [Syntax]? = nil,
notes: [Note] = [],
fixIts: [FixIt]
) -> ExpansionError {
self.context.diagnose(
.init(
node: node,
position: nil,
message: message,
highlights: highlights,
notes: notes,
fixIts: fixIts))
return ExpansionError()
}

}

extension MacroContext where Context == SuppressionContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,37 @@ extension WithAttributesSyntax {
matches.append(.init(attribute: attribute, macroType: macroType))
}
}
guard matches.count == 1 else {
switch macros.count {
case 1:
throw context.error(
at: self,
message: .expectedMemberAnnotatedWithMacro(macros),
fixIts: .insertMacro(node: self, macros[0]))
default:
throw context.error(
at: self,
message: .expectedMemberAnnotatedWithMacro(macros))
}

switch matches.count {
case 0:
throw context.error(
at: self,
message: .expectedMemberAnnotatedWithMacro(macros),
fixIts: macros.map { .insertMacro(node: self, $0) })
case 1:
return matches[0]
default:
throw context.error(
at: self,
message: .expectedMemberAnnotatedWithMacro(macros))
}
return matches[0]
}
}

extension ErrorDiagnostic {
static func expectedMemberAnnotatedWithMacro(
_ macros: [any (ParsableMacro.Type)]
) -> Self {
guard macros.count == 1 else {
switch macros.count {
case 0:
preconditionFailure("Expected at at least one macro")
case 1:
return .init(
"""
'\(Macro.signature)' type member must be annotated with \
'\(macros[0].signature)'
""")
default:
guard let last = macros.last else { fatalError() }
let optionsPrefix =
macros
Expand All @@ -75,15 +84,10 @@ extension ErrorDiagnostic {

return .init(
"""
'\(Macro.signature)' type member must be annotated with exactly one \
macro of \(options)
'\(Macro.signature)' type member must be annotated with exactly one of \
\(options)
""")
}
return .init(
"""
'\(Macro.signature)' type member must be annotated with \
'\(macros[0].signature)' macro
""")
}
}

Expand Down
24 changes: 21 additions & 3 deletions Tests/MMIOMacrosTests/Macros/RegisterMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,35 @@ final class RegisterMacroTests: XCTestCase {
message: ErrorDiagnostic.expectedMemberAnnotatedWithMacro(bitFieldMacros).message,
line: 3,
column: 3,
highlight: "var v1: Int"),
highlight: "var v1: Int",
fixIts: [
.init(message: "Insert '@Reserved(bits:)' macro"),
.init(message: "Insert '@ReadWrite(bits:as:)' macro"),
.init(message: "Insert '@ReadOnly(bits:as:)' macro"),
.init(message: "Insert '@WriteOnly(bits:as:)' macro"),
]),
.init(
message: ErrorDiagnostic.expectedMemberAnnotatedWithMacro(bitFieldMacros).message,
line: 4,
column: 3,
highlight: "@OtherAttribute var v2: Int"),
highlight: "@OtherAttribute var v2: Int",
fixIts: [
.init(message: "Insert '@Reserved(bits:)' macro"),
.init(message: "Insert '@ReadWrite(bits:as:)' macro"),
.init(message: "Insert '@ReadOnly(bits:as:)' macro"),
.init(message: "Insert '@WriteOnly(bits:as:)' macro"),
]),
.init(
message: ErrorDiagnostic.expectedMemberAnnotatedWithMacro(bitFieldMacros).message,
line: 5,
column: 3,
highlight: "var v3: Int { willSet {} }"),
highlight: "var v3: Int { willSet {} }",
fixIts: [
.init(message: "Insert '@Reserved(bits:)' macro"),
.init(message: "Insert '@ReadWrite(bits:as:)' macro"),
.init(message: "Insert '@ReadOnly(bits:as:)' macro"),
.init(message: "Insert '@WriteOnly(bits:as:)' macro"),
]),
],
macros: Self.macros,
indentationWidth: Self.indentationWidth)
Expand Down