Skip to content

Commit 5b18765

Browse files
committed
Fix invalid warning for enum cases named self (#41520)
#37992 introduced a warning when you were likely to confuse `self` with `TypeName.self`, this also applied to enum cases that were named `self`, these cases should not be easily confused at call sites since their use requires prefixing them with a `.`. There was also no way to avoid this warning since other syntax such as `TypeName.self`, which produces the enum type instead, or `` TypeName.`self` `` which produced the same warning again. Fixes https://bugs.swift.org/browse/SR-15691 (cherry picked from commit f6b2e2e)
1 parent f90355b commit 5b18765

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4723,19 +4723,20 @@ static void diagUnqualifiedAccessToMethodNamedSelf(const Expr *E,
47234723
if (auto typeContext = DC->getInnermostTypeContext()) {
47244724
// self() is not easily confusable
47254725
if (!isa<CallExpr>(Parent.getAsExpr())) {
4726-
47274726
auto baseType = typeContext->getDeclaredInterfaceType();
4728-
auto baseTypeString = baseType.getString();
4727+
if (!baseType->getEnumOrBoundGenericEnum()) {
4728+
auto baseTypeString = baseType.getString();
47294729

4730-
Ctx.Diags.diagnose(E->getLoc(), diag::self_refers_to_method,
4731-
baseTypeString);
4730+
Ctx.Diags.diagnose(E->getLoc(), diag::self_refers_to_method,
4731+
baseTypeString);
47324732

4733-
Ctx.Diags
4733+
Ctx.Diags
47344734
.diagnose(E->getLoc(),
4735-
diag::fix_unqualified_access_member_named_self,
4736-
baseTypeString)
4735+
diag::fix_unqualified_access_member_named_self,
4736+
baseTypeString)
47374737
.fixItInsert(E->getLoc(), diag::insert_type_qualification,
4738-
baseType);
4738+
baseType);
4739+
}
47394740
}
47404741
}
47414742
}

test/Parse/self_rebinding.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,13 @@ struct TypeWithSelfProperty {
116116

117117
let `self`: () = ()
118118
}
119+
120+
enum EnumCaseNamedSelf {
121+
case `self`
122+
123+
init() {
124+
self = .self // OK
125+
self = .`self` // OK
126+
self = EnumCaseNamedSelf.`self` // OK
127+
}
128+
}

0 commit comments

Comments
 (0)