Skip to content

Commit 232fe59

Browse files
committed
[Sema] Don't issue warnings when accessing enum elements as instance members
This removes the logic which issued warnings when accessing enum elements as instance members (SE-0036), making room for a new implementation that will issue errors instead. This reverts commit ae1058a.
1 parent bc09af3 commit 232fe59

File tree

11 files changed

+15
-207
lines changed

11 files changed

+15
-207
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,6 @@ 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-
WARNING(could_not_use_enum_element_on_instance,none,
97-
"referencing enum element %0 as instance member is deprecated and will "
98-
"be removed in Swift 3",
99-
(DeclName))
10096
ERROR(could_not_use_type_member_on_existential,none,
10197
"static member %1 cannot be used on protocol metatype %0",
10298
(Type, DeclName))

include/swift/AST/Expr.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,13 +1031,6 @@ class TypeExpr : public Expr {
10311031
TypeLoc Info;
10321032
TypeExpr(Type Ty);
10331033
public:
1034-
/// Whether this type reference actually references an instance but has been
1035-
/// promoted to a type reference to access an enum element
1036-
///
1037-
/// This is purely transitional and will be removed when referencing enum
1038-
/// elements on instance members becomes an error
1039-
bool IsPromotedInstanceRef = false;
1040-
10411034
// Create a TypeExpr with location information.
10421035
TypeExpr(TypeLoc Ty);
10431036

@@ -1058,8 +1051,7 @@ class TypeExpr : public Expr {
10581051

10591052
/// Return a TypeExpr for a TypeDecl and the specified location.
10601053
static TypeExpr *createForDecl(SourceLoc Loc, TypeDecl *D,
1061-
bool isImplicit,
1062-
bool isPromotedInstanceRef = false);
1054+
bool isImplicit);
10631055
static TypeExpr *createForSpecializedDecl(SourceLoc Loc, TypeDecl *D,
10641056
ArrayRef<TypeRepr*> args,
10651057
SourceRange angleLocs);

include/swift/AST/NameLookup.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,6 @@ struct UnqualifiedLookupResult {
4141
ValueDecl *Value;
4242

4343
public:
44-
/// Whether this should actually reference an instance but has been promoted
45-
/// to a type reference to access an enum element
46-
///
47-
/// This is purely transitional and will be removed when referencing enum
48-
/// elements on instance members becomes an error
49-
bool IsPromotedInstanceRef = false;
50-
5144
UnqualifiedLookupResult(ValueDecl *value) : Base(nullptr), Value(value) { }
5245

5346
UnqualifiedLookupResult(ValueDecl *base, ValueDecl *value)

lib/AST/Expr.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,14 +1345,12 @@ Type TypeExpr::getInstanceType() const {
13451345

13461346
/// Return a TypeExpr for a simple identifier and the specified location.
13471347
TypeExpr *TypeExpr::createForDecl(SourceLoc Loc, TypeDecl *Decl,
1348-
bool isImplicit,
1349-
bool isPromotedInstanceRef) {
1348+
bool isImplicit) {
13501349
ASTContext &C = Decl->getASTContext();
13511350
assert(Loc.isValid());
13521351
auto *Repr = new (C) SimpleIdentTypeRepr(Loc, Decl->getName());
13531352
Repr->setValue(Decl);
13541353
auto result = new (C) TypeExpr(TypeLoc(Repr, Type()));
1355-
result->IsPromotedInstanceRef = isPromotedInstanceRef;
13561354
if (isImplicit)
13571355
result->setImplicit();
13581356
return result;

lib/AST/NameLookup.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -563,11 +563,7 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
563563
if (FD->isStatic() && !isMetatypeType)
564564
continue;
565565
} else if (isa<EnumElementDecl>(Result)) {
566-
auto lookupRes = UnqualifiedLookupResult(MetaBaseDecl, Result);
567-
if (!BaseDecl->getType()->is<MetatypeType>()) {
568-
lookupRes.IsPromotedInstanceRef = true;
569-
}
570-
Results.push_back(lookupRes);
566+
Results.push_back(UnqualifiedLookupResult(MetaBaseDecl, Result));
571567
continue;
572568
}
573569

lib/Sema/CSApply.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2545,24 +2545,6 @@ namespace {
25452545

25462546
public:
25472547
Expr *visitUnresolvedDotExpr(UnresolvedDotExpr *expr) {
2548-
if (auto ty = dyn_cast<TypeExpr>(expr->getBase())) {
2549-
if (ty->IsPromotedInstanceRef) {
2550-
// An enum element was looked up on an instance. Issue a warning
2551-
auto enumMetatype = ty->getType()->castTo<AnyMetatypeType>();
2552-
auto enumType = enumMetatype->getInstanceType()->castTo<EnumType>();
2553-
2554-
SmallString<32> enumTypeName;
2555-
llvm::raw_svector_ostream typeNameStream(enumTypeName);
2556-
typeNameStream << enumType->getDecl()->getName();
2557-
typeNameStream << ".";
2558-
2559-
TypeChecker &tc = cs.getTypeChecker();
2560-
tc.diagnose(expr->getLoc(),
2561-
diag::could_not_use_enum_element_on_instance,
2562-
expr->getName())
2563-
.fixItInsert(expr->getLoc(), typeNameStream.str());
2564-
}
2565-
}
25662548
return applyMemberRefExpr(expr, expr->getBase(), expr->getDotLoc(),
25672549
expr->getNameLoc(), expr->isImplicit());
25682550
}

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,6 @@ resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *DC) {
551551

552552
ResultValues.clear();
553553
bool AllMemberRefs = true;
554-
bool PromotedInstanceRef = false;
555554
ValueDecl *Base = 0;
556555
for (auto Result : Lookup) {
557556
// Track the base for member declarations.
@@ -562,9 +561,6 @@ resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *DC) {
562561
break;
563562
}
564563

565-
if (Result.IsPromotedInstanceRef) {
566-
PromotedInstanceRef = true;
567-
}
568564
Base = Result.Base;
569565
continue;
570566
}
@@ -576,8 +572,7 @@ resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *DC) {
576572
if (AllMemberRefs) {
577573
Expr *BaseExpr;
578574
if (auto NTD = dyn_cast<NominalTypeDecl>(Base)) {
579-
BaseExpr = TypeExpr::createForDecl(Loc, NTD, /*implicit=*/true,
580-
PromotedInstanceRef);
575+
BaseExpr = TypeExpr::createForDecl(Loc, NTD, /*implicit=*/true);
581576
} else {
582577
BaseExpr = new (Context) DeclRefExpr(Base, UDRE->getNameLoc(),
583578
/*implicit=*/true);

lib/Sema/TypeCheckNameLookup.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,7 @@ namespace {
103103
///
104104
/// \param foundInType The type through which we found the
105105
/// declaration.
106-
///
107-
/// \param promotedInstanceRef true if the lookup result to be added was
108-
/// actually looked up on an instance but promted to a type to look up an
109-
/// enum element
110-
void add(ValueDecl *found, ValueDecl *base, Type foundInType,
111-
bool promotedInstanceRef = false) {
106+
void add(ValueDecl *found, ValueDecl *base, Type foundInType) {
112107
// If we only want types, AST name lookup should not yield anything else.
113108
assert(!Options.contains(NameLookupFlags::OnlyTypes) ||
114109
isa<TypeDecl>(found));
@@ -144,7 +139,7 @@ namespace {
144139
isa<GenericTypeParamDecl>(found) ||
145140
(isa<FuncDecl>(found) && cast<FuncDecl>(found)->isOperator())) {
146141
if (Known.insert({{found, base}, false}).second) {
147-
Result.add({found, base, promotedInstanceRef});
142+
Result.add({found, base});
148143
FoundDecls.push_back(found);
149144
}
150145
return;
@@ -177,7 +172,7 @@ namespace {
177172
// default implementations in protocols.
178173
if (witness && !isa<ProtocolDecl>(witness->getDeclContext())) {
179174
if (Known.insert({{witness, base}, false}).second) {
180-
Result.add({witness, base, promotedInstanceRef});
175+
Result.add({witness, base});
181176
FoundDecls.push_back(witness);
182177
}
183178
}
@@ -234,8 +229,7 @@ LookupResult TypeChecker::lookupUnqualified(DeclContext *dc, DeclName name,
234229
assert(foundInType && "bogus base declaration?");
235230
}
236231

237-
builder.add(found.getValueDecl(), found.getBaseDecl(), foundInType,
238-
found.IsPromotedInstanceRef);
232+
builder.add(found.getValueDecl(), found.getBaseDecl(), foundInType);
239233
}
240234
return result;
241235
}
@@ -572,7 +566,7 @@ void TypeChecker::performTypoCorrection(DeclContext *DC, DeclRefKind refKind,
572566
entries.filterMaxScoreRange(MaxCallEditDistanceFromBestCandidate);
573567

574568
for (auto &entry : entries)
575-
result.add({ entry.Value, nullptr, false });
569+
result.add({ entry.Value, nullptr });
576570
}
577571

578572
static InFlightDiagnostic

lib/Sema/TypeCheckPattern.cpp

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,13 @@ extractEnumElement(TypeChecker &TC, DeclContext *DC, SourceLoc UseLoc,
7272
///
7373
/// If there are no enum elements but there are properties, attempts to map
7474
/// an arbitrary property to an enum element using extractEnumElement.
75-
///
76-
/// \param isPromoted If set to anything but the \c nullptr, this will be set to
77-
/// \c true if the found enum element is referenced as on an instance
78-
/// but the lookup has been promoted to be on the type instead
79-
/// This is purely transitional and will be removed when referencing enum
80-
/// elements on instance members becomes an error
81-
8275
static EnumElementDecl *
8376
filterForEnumElement(TypeChecker &TC, DeclContext *DC, SourceLoc UseLoc,
84-
LookupResult foundElements, bool *isPromoted = nullptr) {
77+
LookupResult foundElements) {
8578
EnumElementDecl *foundElement = nullptr;
8679
VarDecl *foundConstant = nullptr;
8780

88-
for (LookupResult::Result result : foundElements) {
89-
ValueDecl *e = result.Decl;
81+
for (ValueDecl *e : foundElements) {
9082
assert(e);
9183
if (e->isInvalid()) {
9284
continue;
@@ -96,9 +88,6 @@ filterForEnumElement(TypeChecker &TC, DeclContext *DC, SourceLoc UseLoc,
9688
// Ambiguities should be ruled out by parsing.
9789
assert(!foundElement && "ambiguity in enum case name lookup?!");
9890
foundElement = oe;
99-
if (isPromoted != nullptr) {
100-
*isPromoted = result.IsPromotedInstanceRef;
101-
}
10291
continue;
10392
}
10493

@@ -115,20 +104,13 @@ filterForEnumElement(TypeChecker &TC, DeclContext *DC, SourceLoc UseLoc,
115104
}
116105

117106
/// Find an unqualified enum element.
118-
///
119-
/// \param IsPromoted If set to anything but the \c nullptr, this will be set to
120-
/// \c true if the found enum element is referenced as on an instance
121-
/// but the lookup has been promoted to be on the type instead
122-
/// This is purely transitional and will be removed when referencing enum
123-
/// elements on instance members becomes an error
124107
static EnumElementDecl *
125108
lookupUnqualifiedEnumMemberElement(TypeChecker &TC, DeclContext *DC,
126-
Identifier name, SourceLoc UseLoc,
127-
bool *IsPromoted = nullptr) {
109+
Identifier name, SourceLoc UseLoc) {
128110
auto lookupOptions = defaultUnqualifiedLookupOptions;
129111
lookupOptions |= NameLookupFlags::KnownPrivate;
130112
auto lookup = TC.lookupUnqualified(DC, name, SourceLoc(), lookupOptions);
131-
return filterForEnumElement(TC, DC, UseLoc, lookup, IsPromoted);
113+
return filterForEnumElement(TC, DC, UseLoc, lookup);
132114
}
133115

134116
/// Find an enum element in an enum type.
@@ -502,22 +484,10 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
502484
// rdar://20879992 is addressed.
503485
//
504486
// Try looking up an enum element in context.
505-
506-
// Check if the enum element was actually accessed on an instance and was
507-
// promoted to be looked up on a type. If so, provide a warning
508-
bool isPromotedInstance = false;
509-
510487
if (EnumElementDecl *referencedElement
511488
= lookupUnqualifiedEnumMemberElement(TC, DC,
512489
ude->getName().getBaseName(),
513-
ude->getLoc(),
514-
&isPromotedInstance)) {
515-
if (isPromotedInstance) {
516-
TC.diagnose(ude->getLoc(), diag::could_not_use_enum_element_on_instance,
517-
ude->getName())
518-
.fixItInsert(ude->getLoc(), ".");
519-
}
520-
490+
ude->getLoc())) {
521491
auto *enumDecl = referencedElement->getParentEnum();
522492
auto enumTy = enumDecl->getDeclaredTypeInContext();
523493
TypeLoc loc = TypeLoc::withoutLoc(enumTy);
@@ -600,21 +570,9 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
600570
// If we had a single component, try looking up an enum element in context.
601571
if (auto compId = dyn_cast<ComponentIdentTypeRepr>(repr)) {
602572
// Try looking up an enum element in context.
603-
604-
// Check if the enum element was actually accessed on an instance and was
605-
// promoted to be looked up on a type. If so, provide a warning
606-
bool isPromoted = false;
607573
EnumElementDecl *referencedElement
608574
= lookupUnqualifiedEnumMemberElement(TC, DC, compId->getIdentifier(),
609-
repr->getLoc(), &isPromoted);
610-
611-
if (isPromoted) {
612-
TC.diagnose(compId->getLoc(),
613-
diag::could_not_use_enum_element_on_instance,
614-
compId->getIdentifier())
615-
.fixItInsert(compId->getLoc(), ".");
616-
}
617-
575+
repr->getLoc());
618576

619577
if (!referencedElement)
620578
return nullptr;

lib/Sema/TypeChecker.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,6 @@ class LookupResult {
6363
/// The base declaration through which we found the declaration.
6464
ValueDecl *Base;
6565

66-
/// Whether this should actually reference an instance but has been promoted
67-
/// to a type reference to access an enum element
68-
///
69-
/// This is purely transitional and will be removed when referencing enum
70-
/// elements on instance members becomes an error
71-
bool IsPromotedInstanceRef;
72-
7366
operator ValueDecl*() const { return Decl; }
7467
ValueDecl *operator->() const { return Decl; }
7568
};

test/Parse/enum.swift

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -433,92 +433,3 @@ enum RawValueBTest: Double, RawValueB {
433433
enum foo : String {
434434
case bar = nil // expected-error {{cannot convert nil to raw type 'String'}}
435435
}
436-
437-
// Static member lookup from instance methods
438-
439-
struct EmptyStruct {}
440-
441-
enum EnumWithStaticMember {
442-
static let staticVar = EmptyStruct()
443-
444-
func foo() {
445-
let _ = staticVar // expected-error {{static member 'staticVar' cannot be used on instance of type 'EnumWithStaticMember'}}
446-
}
447-
}
448-
449-
// SE-0036:
450-
451-
struct SE0036_Auxiliary {}
452-
453-
enum SE0036 {
454-
case A
455-
case B(SE0036_Auxiliary)
456-
457-
static func staticReference() {
458-
_ = A
459-
_ = SE0036.A
460-
}
461-
462-
func staticReferencInInstanceMethod() {
463-
_ = A // expected-warning {{referencing enum element 'A' as instance member is deprecated and will be removed in Swift 3}} {{9-9=SE0036.}}
464-
_ = SE0036.A
465-
}
466-
467-
static func staticReferencInSwitchInStaticMethod() {
468-
switch SE0036.A {
469-
case A: break
470-
case B(_): break
471-
}
472-
}
473-
474-
func staticReferencInSwitchInInstanceMethod() {
475-
switch self {
476-
case A: break // expected-warning {{referencing enum element 'A' as instance member is deprecated and will be removed in Swift 3}} {{10-10=.}}
477-
case B(_): break // expected-warning {{referencing enum element 'B' as instance member is deprecated and will be removed in Swift 3}} {{10-10=.}}
478-
}
479-
}
480-
481-
func explicitReferencInSwitch() {
482-
switch SE0036.A {
483-
case SE0036.A: break
484-
case SE0036.B(_): break
485-
}
486-
}
487-
488-
func dotReferencInSwitchInInstanceMethod() {
489-
switch self {
490-
case .A: break
491-
case .B(_): break
492-
}
493-
}
494-
495-
static func dotReferencInSwitchInStaticMethod() {
496-
switch SE0036.A {
497-
case .A: break
498-
case .B(_): break
499-
}
500-
}
501-
502-
init() {
503-
self = .A
504-
self = A // expected-warning {{referencing enum element 'A' as instance member is deprecated and will be removed in Swift 3}} {{12-12=SE0036.}}
505-
self = SE0036.A
506-
self = .B(SE0036_Auxiliary())
507-
self = B(SE0036_Auxiliary()) // expected-warning {{referencing enum element 'B' as instance member is deprecated and will be removed in Swift 3}} {{12-12=SE0036.}}
508-
self = SE0036.B(SE0036_Auxiliary())
509-
}
510-
}
511-
512-
enum SE0036_Generic<T> {
513-
case A(x: T)
514-
515-
func foo() {
516-
switch self {
517-
case A(_): break // expected-warning {{referencing enum element 'A' as instance member is deprecated and will be removed in Swift 3}} {{10-10=.}}
518-
}
519-
520-
switch self {
521-
case SE0036_Generic.A(let a): print(a)
522-
}
523-
}
524-
}

0 commit comments

Comments
 (0)