diff --git a/Sources/TestingMacros/Support/AttributeDiscovery.swift b/Sources/TestingMacros/Support/AttributeDiscovery.swift index a61989aef..3d95df294 100644 --- a/Sources/TestingMacros/Support/AttributeDiscovery.swift +++ b/Sources/TestingMacros/Support/AttributeDiscovery.swift @@ -144,8 +144,9 @@ struct AttributeInfo { let rawIdentifier = namedDecl.name.rawIdentifier { if let displayName, let displayNameArgument { context.diagnose(.declaration(namedDecl, hasExtraneousDisplayName: displayName, fromArgument: displayNameArgument, using: attribute)) + } else { + displayName = StringLiteralExprSyntax(content: rawIdentifier) } - displayName = StringLiteralExprSyntax(content: rawIdentifier) } // Remove leading "Self." expressions from the arguments of the attribute. diff --git a/Sources/TestingMacros/Support/DiagnosticMessage.swift b/Sources/TestingMacros/Support/DiagnosticMessage.swift index b7103bcc6..9f155b63a 100644 --- a/Sources/TestingMacros/Support/DiagnosticMessage.swift +++ b/Sources/TestingMacros/Support/DiagnosticMessage.swift @@ -657,10 +657,16 @@ struct DiagnosticMessage: SwiftDiagnostics.DiagnosticMessage { fromArgument argumentContainingDisplayName: LabeledExprListSyntax.Element, using attribute: AttributeSyntax ) -> Self { - Self( + // If the name of the ambiguously-named symbol should be derived from a raw + // identifier, this situation is an error. If the name is not raw but is + // still surrounded by backticks (e.g. "func `foo`()" or "struct `if`") then + // lower the severity to a warning. That way, existing code structured this + // way doesn't suddenly fail to build. + let severity: DiagnosticSeverity = (decl.name.rawIdentifier != nil) ? .error : .warning + return Self( syntax: Syntax(decl), - message: "Attribute \(_macroName(attribute)) specifies display name '\(displayNameFromAttribute.representedLiteralValue!)' for \(_kindString(for: decl)) with implicit display name '\(decl.name.rawIdentifier!)'", - severity: .error, + message: "Attribute \(_macroName(attribute)) specifies display name '\(displayNameFromAttribute.representedLiteralValue!)' for \(_kindString(for: decl)) with implicit display name '\(decl.name.textWithoutBackticks)'", + severity: severity, fixIts: [ FixIt( message: MacroExpansionFixItMessage("Remove '\(displayNameFromAttribute.representedLiteralValue!)'"),