Skip to content

Commit 88dd6fc

Browse files
committed
[Macros] Introduce an example where member operators aren't found
Operators introduced by member macros are not getting found by global operator lookup. This is technically a bug, but due to the potential cost of having to expand macros on/in types just to find operators, for now we're just going to document here that it doesn't work and reconsider the semantics.
1 parent ebe0b63 commit 88dd6fc

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,3 +1553,15 @@ public struct SelfAlwaysEqualOperator: DeclarationMacro {
15531553
]
15541554
}
15551555
}
1556+
1557+
extension SelfAlwaysEqualOperator: MemberMacro {
1558+
public static func expansion(
1559+
of node: AttributeSyntax,
1560+
providingMembersOf decl: some DeclGroupSyntax,
1561+
in context: some MacroExpansionContext
1562+
) throws -> [DeclSyntax] {
1563+
return [
1564+
"static func ==(lhs: Self, rhs: Bool) -> Bool { true }",
1565+
]
1566+
}
1567+
}

test/Macros/macro_expand.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,8 @@ func testHasAnExpandedStatic() {
446446

447447
@freestanding(declaration, names: named(==)) public macro addSelfEqualsOperator() = #externalMacro(module: "MacroDefinition", type: "SelfAlwaysEqualOperator")
448448
@freestanding(declaration, names: arbitrary) public macro addSelfEqualsOperatorArbitrary() = #externalMacro(module: "MacroDefinition", type: "SelfAlwaysEqualOperator")
449+
@attached(member, names: named(==)) public macro AddSelfEqualsMemberOperator() = #externalMacro(module: "MacroDefinition", type: "SelfAlwaysEqualOperator")
450+
@attached(member, names: arbitrary) public macro AddSelfEqualsMemberOperatorArbitrary() = #externalMacro(module: "MacroDefinition", type: "SelfAlwaysEqualOperator")
449451

450452
struct HasEqualsSelf {
451453
#addSelfEqualsOperator
@@ -455,7 +457,26 @@ struct HasEqualsSelf2 {
455457
#addSelfEqualsOperatorArbitrary
456458
}
457459

458-
func testHasEqualsSelf(x: HasEqualsSelf, y: HasEqualsSelf2) {
460+
@AddSelfEqualsMemberOperator
461+
struct HasEqualsSelf3 {
462+
}
463+
464+
@AddSelfEqualsMemberOperatorArbitrary
465+
struct HasEqualsSelf4 {
466+
}
467+
468+
func testHasEqualsSelf(
469+
x: HasEqualsSelf, y: HasEqualsSelf2, z: HasEqualsSelf3, w: HasEqualsSelf4
470+
) {
459471
_ = (x == true)
460472
_ = (y == true)
473+
#if TEST_DIAGNOSTICS
474+
// FIXME: This is technically a bug, because we should be able to find the
475+
// == operator introduced through a member operator. However, we might
476+
// want to change the rule rather than implement this.
477+
_ = (z == true) // expected-error{{binary operator '==' cannot be applied to operands}}
478+
// expected-note@-1{{overloads for '==' exist with these partially matching parameter lists}}
479+
_ = (w == true) // expected-error{{binary operator '==' cannot be applied to operands}}
480+
// expected-note@-1{{overloads for '==' exist with these partially matching parameter lists}}
481+
#endif
461482
}

0 commit comments

Comments
 (0)