Skip to content

Commit 6bad02c

Browse files
committed
[Macros] Implement overloading rules
1 parent 36dd677 commit 6bad02c

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

include/swift/AST/Decl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ struct OverloadSignature {
255255
/// Whether this is a type alias.
256256
unsigned IsTypeAlias : 1;
257257

258+
/// Whether this is a macro.
259+
unsigned IsMacro : 1;
260+
258261
/// Whether this signature is part of a protocol extension.
259262
unsigned InProtocolExtension : 1;
260263

lib/AST/Decl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2785,6 +2785,10 @@ bool swift::conflicting(const OverloadSignature& sig1,
27852785
if (sig1.IsAsyncFunction != sig2.IsAsyncFunction)
27862786
return false;
27872787

2788+
// If one is a macro and the other is not, they can't conflict.
2789+
if (sig1.IsMacro != sig2.IsMacro)
2790+
return false;
2791+
27882792
// If one is a compound name and the other is not, they do not conflict
27892793
// if one is a property and the other is a non-nullary function.
27902794
if (sig1.Name.isCompoundName() != sig2.Name.isCompoundName()) {
@@ -3016,6 +3020,7 @@ OverloadSignature ValueDecl::getOverloadSignature() const {
30163020
signature.IsEnumElement = isa<EnumElementDecl>(this);
30173021
signature.IsNominal = isa<NominalTypeDecl>(this);
30183022
signature.IsTypeAlias = isa<TypeAliasDecl>(this);
3023+
signature.IsMacro = isa<MacroDecl>(this);
30193024
signature.HasOpaqueReturnType =
30203025
!signature.IsVariable && (bool)getOpaqueResultTypeDecl();
30213026

test/Macros/macros_diagnostics.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ struct ZZZ {
4141
@expression macro multiArgMacro(_: Any, second: Any) = MissingModule.MissingType
4242
// expected-note@-1{{'multiArgMacro(_:second:)' declared here}}
4343

44+
@expression macro overloaded1(_ p: P) = MissingModule.MissingType
45+
func overloaded1(_ p: Any) { }
46+
47+
@expression macro notOverloaded1(_ p: P) = MissingModule.MissingType // expected-note{{'notOverloaded1' previously declared here}}
48+
@expression macro notOverloaded1(_ p: P) = MissingModule.MissingOtherType // expected-error{{invalid redeclaration of 'notOverloaded1'}}
49+
4450
func testDiags(a: Int, b: Int) {
4551
// FIXME: Bad diagnostic.
4652
let s = #stringify<Int, Int>(a + b) // expected-error{{type of expression is ambiguous without more context}}
@@ -59,6 +65,9 @@ func testDiags(a: Int, b: Int) {
5965

6066
_ = stringify(a + b)
6167
// expected-error@-1{{expansion of macro 'stringify' requires leading '#'}}{{7-7=#}}
68+
69+
overloaded1(a) // okay, calls the function
70+
#overloaded1(a) // expected-error{{argument type 'Int' does not conform to expected type 'P'}}
6271
}
6372

6473
func shadow(a: Int, b: Int, stringify: Int) {

0 commit comments

Comments
 (0)