Skip to content

Commit 84fba88

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. (cherry picked from commit 88dd6fc)
1 parent e7321ac commit 84fba88

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
@@ -445,6 +445,8 @@ func testHasAnExpandedStatic() {
445445

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

449451
struct HasEqualsSelf {
450452
#addSelfEqualsOperator
@@ -454,7 +456,26 @@ struct HasEqualsSelf2 {
454456
#addSelfEqualsOperatorArbitrary
455457
}
456458

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

0 commit comments

Comments
 (0)