Skip to content

Commit 3ee5ec0

Browse files
authored
Lower the "ambiguous display name" diagnostic to a warning for some names. (#1175)
This PR modifies the behaviour of this compile-time macro diagnostic: > 🛑 "Attribute 'Test' specifies display name 'foo' for function with implicit display name 'bar' If `bar` (in the above context) is _not_ considered a raw identifier by the language, we emit a warning instead of an error. This will allow us to adjust display-name-from-backticked-name inference (see #1174) without introducing a source-breaking change for a declaration such as: ```swift @test("subscript([K]) operator") func `subscript`() ``` (The above is a real-world test function in our own package that would be impacted.) Note that we don't actually have a code path that triggers this warning yet. #1174, if approved and merged, would introduce such a code path. Here's an example of what that would look like: <img width="774" alt="Screenshot showing the warning diagnostic presented for func subscript()" src="https://github.com/user-attachments/assets/ee8ff23c-8cbb-4335-af36-24a54deac6cc" /> ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent a208154 commit 3ee5ec0

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

Sources/TestingMacros/Support/AttributeDiscovery.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,9 @@ struct AttributeInfo {
144144
let rawIdentifier = namedDecl.name.rawIdentifier {
145145
if let displayName, let displayNameArgument {
146146
context.diagnose(.declaration(namedDecl, hasExtraneousDisplayName: displayName, fromArgument: displayNameArgument, using: attribute))
147+
} else {
148+
displayName = StringLiteralExprSyntax(content: rawIdentifier)
147149
}
148-
displayName = StringLiteralExprSyntax(content: rawIdentifier)
149150
}
150151

151152
// Remove leading "Self." expressions from the arguments of the attribute.

Sources/TestingMacros/Support/DiagnosticMessage.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,10 +657,16 @@ struct DiagnosticMessage: SwiftDiagnostics.DiagnosticMessage {
657657
fromArgument argumentContainingDisplayName: LabeledExprListSyntax.Element,
658658
using attribute: AttributeSyntax
659659
) -> Self {
660-
Self(
660+
// If the name of the ambiguously-named symbol should be derived from a raw
661+
// identifier, this situation is an error. If the name is not raw but is
662+
// still surrounded by backticks (e.g. "func `foo`()" or "struct `if`") then
663+
// lower the severity to a warning. That way, existing code structured this
664+
// way doesn't suddenly fail to build.
665+
let severity: DiagnosticSeverity = (decl.name.rawIdentifier != nil) ? .error : .warning
666+
return Self(
661667
syntax: Syntax(decl),
662-
message: "Attribute \(_macroName(attribute)) specifies display name '\(displayNameFromAttribute.representedLiteralValue!)' for \(_kindString(for: decl)) with implicit display name '\(decl.name.rawIdentifier!)'",
663-
severity: .error,
668+
message: "Attribute \(_macroName(attribute)) specifies display name '\(displayNameFromAttribute.representedLiteralValue!)' for \(_kindString(for: decl)) with implicit display name '\(decl.name.textWithoutBackticks)'",
669+
severity: severity,
664670
fixIts: [
665671
FixIt(
666672
message: MacroExpansionFixItMessage("Remove '\(displayNameFromAttribute.representedLiteralValue!)'"),

0 commit comments

Comments
 (0)