Skip to content

Commit c75c227

Browse files
Suyash SrijanSuyash Srijan
authored andcommitted
[typechecker] use mapSignatureFunctionType in getOverloadSignatureType() to compute the type for the enum element decl, etc
1 parent 8a6a25d commit c75c227

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

include/swift/AST/Decl.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,6 @@ struct OverloadSignature {
213213
/// Whether this is a function.
214214
unsigned IsFunction : 1;
215215

216-
/// Whether this is a static function.
217-
unsigned IsStaticFunction : 1;
218-
219216
/// Whether this is a enum element.
220217
unsigned IsEnumElement : 1;
221218

lib/AST/Decl.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,12 +2090,15 @@ bool swift::conflicting(ASTContext &ctx,
20902090
if (!conflicting(sig1, sig2, skipProtocolExtensionCheck))
20912091
return false;
20922092

2093-
// Static functions do not conflict with enum element decls if the types
2094-
// are different
2095-
if (sig1.IsStaticFunction == sig2.IsEnumElement) {
2096-
if (sig1Type != sig2Type) {
2097-
return false;
2098-
}
2093+
// Two enum elements always conflict with each other. At this point, they
2094+
// have the same base name but different types.
2095+
if (sig1.IsEnumElement == sig2.IsEnumElement) {
2096+
return true;
2097+
}
2098+
2099+
// A function does not conflict with an enum element if their types differ.
2100+
if (sig1.IsFunction == sig2.IsEnumElement && sig1Type != sig2Type) {
2101+
return false;
20992102
}
21002103

21012104
// Functions always conflict with non-functions with the same signature.
@@ -2257,8 +2260,6 @@ OverloadSignature ValueDecl::getOverloadSignature() const {
22572260
signature.IsVariable = isa<VarDecl>(this);
22582261
signature.IsFunction = isa<AbstractFunctionDecl>(this);
22592262
signature.IsEnumElement = isa<EnumElementDecl>(this);
2260-
signature.IsStaticFunction =
2261-
isa<FuncDecl>(this) && cast<FuncDecl>(this)->isStatic();
22622263

22632264
// Unary operators also include prefix/postfix.
22642265
if (auto func = dyn_cast<FuncDecl>(this)) {
@@ -2305,7 +2306,10 @@ CanType ValueDecl::getOverloadSignatureType() const {
23052306
}
23062307

23072308
if (isa<EnumElementDecl>(this)) {
2308-
return this->getInterfaceType()->getCanonicalType();
2309+
auto mappedType = mapSignatureFunctionType(
2310+
getASTContext(), getInterfaceType(), /*topLevelFunction=*/false,
2311+
/*isMethod=*/false, /*isInitializer=*/false, /*curryLevels=*/0);
2312+
return mappedType->getCanonicalType();
23092313
}
23102314

23112315
// Note: If you add more cases to this function, you should update the

test/decl/overload.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,4 +492,28 @@ enum SR_10084_E {
492492
static func foo(_ value: SR_10084_S) -> SR_10084_E { // expected-error {{invalid redeclaration of 'foo'}}
493493
return .foo(value)
494494
}
495-
}
495+
}
496+
497+
enum SR_10084_E_1 {
498+
static func foo(_ name: String) -> SR_10084_E_1 { // expected-note {{'foo' previously declared here}}
499+
return .foo(SR_10084_S(name: name))
500+
}
501+
502+
static func foo(_ value: SR_10084_S) -> SR_10084_E_1 { // expected-error {{invalid redeclaration of 'foo'}}
503+
return .foo(value)
504+
}
505+
506+
case foo(SR_10084_S)
507+
}
508+
509+
enum SR_10084_E_2 {
510+
case fn(() -> Void) // expected-note {{'fn' previously declared here}}
511+
512+
static func fn(_ x: @escaping () -> Void) -> SR_10084_E_2 { // expected-error {{invalid redeclaration of 'fn'}}
513+
fatalError()
514+
}
515+
516+
static func fn(_ x: @escaping () -> Int) -> SR_10084_E_2 { // Ok
517+
fatalError()
518+
}
519+
}

0 commit comments

Comments
 (0)