Skip to content

Commit 14a7334

Browse files
committed
Merge pull request #2224: SE-0036: Requiring Leading Dot Prefixes for Enum Instance Member Implementations
from @ahoppen
2 parents 60a594f + 0e8c69c commit 14a7334

File tree

17 files changed

+238
-127
lines changed

17 files changed

+238
-127
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ Swift 3.0
5454
`NS[Mutable]Dictionary` are still imported as nongeneric classes for
5555
the time being.
5656

57+
* [SE-0036](https://github.com/apple/swift-evolution/blob/master/proposals/0036-enum-dot.md):
58+
Enum elements can no longer be accessed like instance members in instance methods.
5759
* As part of the changes for SE-0055 (see below), the *pointee* types of
5860
imported pointers (e.g. the `id` in `id *`) are no longer assumed to always
5961
be `_Nullable` even if annotated otherwise. However, an implicit or explicit

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ ERROR(could_not_use_type_member,none,
9393
ERROR(could_not_use_type_member_on_instance,none,
9494
"static member %1 cannot be used on instance of type %0",
9595
(Type, DeclName))
96+
ERROR(could_not_use_enum_element_on_instance,none,
97+
"enum element %0 cannot be referenced as an instance member",
98+
(DeclName))
9699
ERROR(could_not_use_type_member_on_existential,none,
97100
"static member %1 cannot be used on protocol metatype %0",
98101
(Type, DeclName))

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1733,7 +1733,7 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
17331733
OS << '\n';
17341734
printRec(E->getArgument());
17351735
}
1736-
OS << "')";
1736+
OS << ")";
17371737
}
17381738
void visitDotSelfExpr(DotSelfExpr *E) {
17391739
printCommon(E, "dot_self_expr");

lib/AST/NameLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
557557
if (FD->isStatic() && !isMetatypeType)
558558
continue;
559559
} else if (isa<EnumElementDecl>(Result)) {
560-
Results.push_back(UnqualifiedLookupResult(MetaBaseDecl, Result));
560+
Results.push_back(UnqualifiedLookupResult(BaseDecl, Result));
561561
continue;
562562
}
563563

lib/Sema/CSDiag.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,6 +2278,22 @@ diagnoseUnviableLookupResults(MemberLookupResult &result, Type baseObjTy,
22782278
} else {
22792279
// Otherwise the static member lookup was invalid because it was
22802280
// called on an instance
2281+
2282+
// Handle enum element lookup on instance type
2283+
auto lookThroughBaseObjTy = baseObjTy->lookThroughAllAnyOptionalTypes();
2284+
if (lookThroughBaseObjTy->is<EnumType>()
2285+
|| lookThroughBaseObjTy->is<BoundGenericEnumType>()) {
2286+
for (auto cand : result.UnviableCandidates) {
2287+
ValueDecl *decl = cand.first;
2288+
if (isa<EnumElementDecl>(decl)) {
2289+
diagnose(loc, diag::could_not_use_enum_element_on_instance,
2290+
memberName);
2291+
return;
2292+
}
2293+
}
2294+
}
2295+
2296+
// Provide diagnostic other static member lookups on instance type
22812297
diagnose(loc, diag::could_not_use_type_member_on_instance,
22822298
baseObjTy, memberName)
22832299
.highlight(baseRange).highlight(nameLoc.getSourceRange());

lib/Sema/TypeCheckPattern.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,16 @@ filterForEnumElement(LookupResult foundElements) {
6565
EnumElementDecl *foundElement = nullptr;
6666
VarDecl *foundConstant = nullptr;
6767

68-
for (ValueDecl *e : foundElements) {
68+
for (swift::LookupResult::Result result : foundElements) {
69+
ValueDecl *e = result.Decl;
6970
assert(e);
7071
if (e->isInvalid()) {
7172
continue;
7273
}
74+
// Skip if the enum element was referenced as an instance member
75+
if (!result.Base || !result.Base->getType()->is<MetatypeType>()) {
76+
continue;
77+
}
7378

7479
if (auto *oe = dyn_cast<EnumElementDecl>(e)) {
7580
// Ambiguities should be ruled out by parsing.

lib/Sema/TypeCheckStmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
332332
template<typename StmtTy>
333333
bool typeCheckStmt(StmtTy *&S) {
334334
StmtTy *S2 = cast_or_null<StmtTy>(visit(S));
335-
if (S2 == 0) return true;
335+
if (S2 == nullptr) return true;
336336
S = S2;
337337
performStmtDiagnostics(TC, S);
338338
return false;

stdlib/private/StdlibCollectionUnittest/MinimalCollections.swift.gyb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,23 +158,23 @@ internal enum _CollectionOperation : Equatable {
158158
var newElementsIds = elementsLastMutatedStateIds
159159
var newEndIndexId = endIndexLastMutatedStateId
160160
switch self {
161-
case reserveCapacity:
161+
case .reserveCapacity:
162162
let invalidIndices = newElementsIds.indices
163163
newElementsIds.replaceSubrange(
164164
Range(invalidIndices),
165165
with: repeatElement(nextStateId, count: invalidIndices.count))
166166
newEndIndexId = nextStateId
167167

168-
case append:
168+
case .append:
169169
newElementsIds.append(nextStateId)
170170
newEndIndexId = nextStateId
171171

172-
case appendContentsOf(let count):
172+
case .appendContentsOf(let count):
173173
newElementsIds.append(contentsOf:
174174
repeatElement(nextStateId, count: count))
175175
newEndIndexId = nextStateId
176176

177-
case replaceRange(let subRange, let replacementCount):
177+
case .replaceRange(let subRange, let replacementCount):
178178
newElementsIds.replaceSubrange(
179179
subRange,
180180
with: repeatElement(nextStateId, count: replacementCount))
@@ -185,7 +185,7 @@ internal enum _CollectionOperation : Equatable {
185185
with: repeatElement(nextStateId, count: invalidIndices.count))
186186
newEndIndexId = nextStateId
187187

188-
case insert(let atIndex):
188+
case .insert(let atIndex):
189189
newElementsIds.insert(nextStateId, at: atIndex)
190190

191191
let invalidIndices = atIndex..<newElementsIds.endIndex
@@ -194,7 +194,7 @@ internal enum _CollectionOperation : Equatable {
194194
with: repeatElement(nextStateId, count: invalidIndices.count))
195195
newEndIndexId = nextStateId
196196

197-
case insertContentsOf(let atIndex, let count):
197+
case .insertContentsOf(let atIndex, let count):
198198
newElementsIds.insert(
199199
contentsOf: repeatElement(nextStateId, count: count),
200200
at: atIndex)
@@ -205,7 +205,7 @@ internal enum _CollectionOperation : Equatable {
205205
with: repeatElement(nextStateId, count: invalidIndices.count))
206206
newEndIndexId = nextStateId
207207

208-
case removeAtIndex(let index):
208+
case .removeAtIndex(let index):
209209
newElementsIds.remove(at: index)
210210

211211
let invalidIndices = index..<newElementsIds.endIndex
@@ -214,11 +214,11 @@ internal enum _CollectionOperation : Equatable {
214214
with: repeatElement(nextStateId, count: invalidIndices.count))
215215
newEndIndexId = nextStateId
216216

217-
case removeLast:
217+
case .removeLast:
218218
newElementsIds.removeLast()
219219
newEndIndexId = nextStateId
220220

221-
case removeRange(let subRange):
221+
case .removeRange(let subRange):
222222
newElementsIds.removeSubrange(subRange)
223223

224224
let invalidIndices = subRange.lowerBound..<newElementsIds.endIndex
@@ -227,7 +227,7 @@ internal enum _CollectionOperation : Equatable {
227227
with: repeatElement(nextStateId, count: invalidIndices.count))
228228
newEndIndexId = nextStateId
229229

230-
case removeAll(let keepCapacity):
230+
case .removeAll(let keepCapacity):
231231
newElementsIds.removeAll(keepingCapacity: keepCapacity)
232232
newEndIndexId = nextStateId
233233
}

0 commit comments

Comments
 (0)