Skip to content

Commit f6b2e2e

Browse files
authored
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
1 parent c22649e commit f6b2e2e

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
@@ -4748,19 +4748,20 @@ static void diagUnqualifiedAccessToMethodNamedSelf(const Expr *E,
47484748
if (auto typeContext = DC->getInnermostTypeContext()) {
47494749
// self() is not easily confusable
47504750
if (!isa<CallExpr>(Parent.getAsExpr())) {
4751-
47524751
auto baseType = typeContext->getDeclaredInterfaceType();
4753-
auto baseTypeString = baseType.getString();
4752+
if (!baseType->getEnumOrBoundGenericEnum()) {
4753+
auto baseTypeString = baseType.getString();
47544754

4755-
Ctx.Diags.diagnose(E->getLoc(), diag::self_refers_to_method,
4756-
baseTypeString);
4755+
Ctx.Diags.diagnose(E->getLoc(), diag::self_refers_to_method,
4756+
baseTypeString);
47574757

4758-
Ctx.Diags
4758+
Ctx.Diags
47594759
.diagnose(E->getLoc(),
4760-
diag::fix_unqualified_access_member_named_self,
4761-
baseTypeString)
4760+
diag::fix_unqualified_access_member_named_self,
4761+
baseTypeString)
47624762
.fixItInsert(E->getLoc(), diag::insert_type_qualification,
4763-
baseType);
4763+
baseType);
4764+
}
47644765
}
47654766
}
47664767
}

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)