Skip to content

Commit 0e8c69c

Browse files
committed
[SE-0036]: Requiring Leading Dot Prefixes for Enum Instance Member Implementations
1 parent b7abfd2 commit 0e8c69c

File tree

15 files changed

+236
-125
lines changed

15 files changed

+236
-125
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/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.

stdlib/private/StdlibCollectionUnittest/MinimalCollections.swift.gyb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,23 +180,23 @@ internal enum _CollectionOperation : Equatable {
180180
var newElementsIds = elementsLastMutatedStateIds
181181
var newEndIndexId = endIndexLastMutatedStateId
182182
switch self {
183-
case reserveCapacity:
183+
case .reserveCapacity:
184184
let invalidIndices = newElementsIds.indices
185185
newElementsIds.replaceSubrange(
186186
Range(invalidIndices),
187187
with: repeatElement(nextStateId, count: invalidIndices.count))
188188
newEndIndexId = nextStateId
189189

190-
case append:
190+
case .append:
191191
newElementsIds.append(nextStateId)
192192
newEndIndexId = nextStateId
193193

194-
case appendContentsOf(let count):
194+
case .appendContentsOf(let count):
195195
newElementsIds.append(contentsOf:
196196
repeatElement(nextStateId, count: count))
197197
newEndIndexId = nextStateId
198198

199-
case replaceRange(let subRange, let replacementCount):
199+
case .replaceRange(let subRange, let replacementCount):
200200
newElementsIds.replaceSubrange(
201201
subRange,
202202
with: repeatElement(nextStateId, count: replacementCount))
@@ -207,7 +207,7 @@ internal enum _CollectionOperation : Equatable {
207207
with: repeatElement(nextStateId, count: invalidIndices.count))
208208
newEndIndexId = nextStateId
209209

210-
case insert(let atIndex):
210+
case .insert(let atIndex):
211211
newElementsIds.insert(nextStateId, at: atIndex)
212212

213213
let invalidIndices = atIndex..<newElementsIds.endIndex
@@ -216,7 +216,7 @@ internal enum _CollectionOperation : Equatable {
216216
with: repeatElement(nextStateId, count: invalidIndices.count))
217217
newEndIndexId = nextStateId
218218

219-
case insertContentsOf(let atIndex, let count):
219+
case .insertContentsOf(let atIndex, let count):
220220
newElementsIds.insert(
221221
contentsOf: repeatElement(nextStateId, count: count),
222222
at: atIndex)
@@ -227,7 +227,7 @@ internal enum _CollectionOperation : Equatable {
227227
with: repeatElement(nextStateId, count: invalidIndices.count))
228228
newEndIndexId = nextStateId
229229

230-
case removeAtIndex(let index):
230+
case .removeAtIndex(let index):
231231
newElementsIds.remove(at: index)
232232

233233
let invalidIndices = index..<newElementsIds.endIndex
@@ -236,11 +236,11 @@ internal enum _CollectionOperation : Equatable {
236236
with: repeatElement(nextStateId, count: invalidIndices.count))
237237
newEndIndexId = nextStateId
238238

239-
case removeLast:
239+
case .removeLast:
240240
newElementsIds.removeLast()
241241
newEndIndexId = nextStateId
242242

243-
case removeRange(let subRange):
243+
case .removeRange(let subRange):
244244
newElementsIds.removeSubrange(subRange)
245245

246246
let invalidIndices = subRange.lowerBound..<newElementsIds.endIndex
@@ -249,7 +249,7 @@ internal enum _CollectionOperation : Equatable {
249249
with: repeatElement(nextStateId, count: invalidIndices.count))
250250
newEndIndexId = nextStateId
251251

252-
case removeAll(let keepCapacity):
252+
case .removeAll(let keepCapacity):
253253
newElementsIds.removeAll(keepingCapacity: keepCapacity)
254254
newEndIndexId = nextStateId
255255
}

0 commit comments

Comments
 (0)