@@ -32,7 +32,10 @@ using namespace swift;
32
32
// / This requires the getter's body to have a certain syntactic form. It should
33
33
// / be kept in sync with importEnumCaseAlias in the ClangImporter library.
34
34
static EnumElementDecl *
35
- extractEnumElement (const VarDecl *constant) {
35
+ extractEnumElement (TypeChecker &TC, DeclContext *DC, SourceLoc UseLoc,
36
+ const VarDecl *constant) {
37
+ TC.diagnoseExplicitUnavailability (constant, UseLoc, DC, nullptr );
38
+
36
39
const FuncDecl *getter = constant->getGetter ();
37
40
if (!getter)
38
41
return nullptr ;
@@ -62,7 +65,8 @@ extractEnumElement(const VarDecl *constant) {
62
65
// / If there are no enum elements but there are properties, attempts to map
63
66
// / an arbitrary property to an enum element using extractEnumElement.
64
67
static EnumElementDecl *
65
- filterForEnumElement (LookupResult foundElements) {
68
+ filterForEnumElement (TypeChecker &TC, DeclContext *DC, SourceLoc UseLoc,
69
+ LookupResult foundElements) {
66
70
EnumElementDecl *foundElement = nullptr ;
67
71
VarDecl *foundConstant = nullptr ;
68
72
@@ -86,32 +90,32 @@ filterForEnumElement(LookupResult foundElements) {
86
90
}
87
91
88
92
if (!foundElement && foundConstant && foundConstant->hasClangNode ())
89
- foundElement = extractEnumElement (foundConstant);
93
+ foundElement = extractEnumElement (TC, DC, UseLoc, foundConstant);
90
94
91
95
return foundElement;
92
96
}
93
97
94
98
// / Find an unqualified enum element.
95
99
static EnumElementDecl *
96
100
lookupUnqualifiedEnumMemberElement (TypeChecker &TC, DeclContext *DC,
97
- Identifier name) {
101
+ Identifier name, SourceLoc UseLoc ) {
98
102
auto lookupOptions = defaultUnqualifiedLookupOptions;
99
103
lookupOptions |= NameLookupFlags::KnownPrivate;
100
104
auto lookup = TC.lookupUnqualified (DC, name, SourceLoc (), lookupOptions);
101
- return filterForEnumElement (lookup);
105
+ return filterForEnumElement (TC, DC, UseLoc, lookup);
102
106
}
103
107
104
108
// / Find an enum element in an enum type.
105
109
static EnumElementDecl *
106
110
lookupEnumMemberElement (TypeChecker &TC, DeclContext *DC, Type ty,
107
- Identifier name) {
111
+ Identifier name, SourceLoc UseLoc ) {
108
112
assert (ty->getAnyNominal ());
109
113
// Look up the case inside the enum.
110
114
// FIXME: We should be able to tell if this is a private lookup.
111
115
NameLookupOptions lookupOptions
112
116
= defaultMemberLookupOptions - NameLookupFlags::DynamicLookup;
113
117
LookupResult foundElements = TC.lookupMember (DC, ty, name, lookupOptions);
114
- return filterForEnumElement (foundElements);
118
+ return filterForEnumElement (TC, DC, UseLoc, foundElements);
115
119
}
116
120
117
121
namespace {
@@ -436,7 +440,8 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
436
440
437
441
// FIXME: Argument labels?
438
442
EnumElementDecl *referencedElement
439
- = lookupEnumMemberElement (TC, DC, ty, ude->getName ().getBaseName ());
443
+ = lookupEnumMemberElement (TC, DC, ty, ude->getName ().getBaseName (),
444
+ ude->getLoc ());
440
445
441
446
// Build a TypeRepr from the head of the full path.
442
447
// FIXME: Compound names.
@@ -474,7 +479,8 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
474
479
// Try looking up an enum element in context.
475
480
if (EnumElementDecl *referencedElement
476
481
= lookupUnqualifiedEnumMemberElement (TC, DC,
477
- ude->getName ().getBaseName ())) {
482
+ ude->getName ().getBaseName (),
483
+ ude->getLoc ())) {
478
484
auto *enumDecl = referencedElement->getParentEnum ();
479
485
auto enumTy = enumDecl->getDeclaredTypeInContext ();
480
486
TypeLoc loc = TypeLoc::withoutLoc (enumTy);
@@ -558,7 +564,8 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
558
564
if (auto compId = dyn_cast<ComponentIdentTypeRepr>(repr)) {
559
565
// Try looking up an enum element in context.
560
566
EnumElementDecl *referencedElement
561
- = lookupUnqualifiedEnumMemberElement (TC, DC, compId->getIdentifier ());
567
+ = lookupUnqualifiedEnumMemberElement (TC, DC, compId->getIdentifier (),
568
+ repr->getLoc ());
562
569
563
570
if (!referencedElement)
564
571
return nullptr ;
@@ -596,7 +603,8 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
596
603
auto tailComponent = compoundR->Components .back ();
597
604
598
605
EnumElementDecl *referencedElement
599
- = lookupEnumMemberElement (TC, DC, enumTy, tailComponent->getIdentifier ());
606
+ = lookupEnumMemberElement (TC, DC, enumTy, tailComponent->getIdentifier (),
607
+ tailComponent->getLoc ());
600
608
if (!referencedElement)
601
609
return nullptr ;
602
610
@@ -1382,8 +1390,10 @@ bool TypeChecker::coercePatternToType(Pattern *&P, DeclContext *dc, Type type,
1382
1390
1383
1391
Type enumTy;
1384
1392
if (!elt) {
1385
- if (type->getAnyNominal ())
1386
- elt = lookupEnumMemberElement (*this , dc, type, EEP->getName ());
1393
+ if (type->getAnyNominal ()) {
1394
+ elt = lookupEnumMemberElement (*this , dc, type, EEP->getName (),
1395
+ EEP->getLoc ());
1396
+ }
1387
1397
if (!elt) {
1388
1398
if (!type->is <ErrorType>()) {
1389
1399
// Lowercasing of Swift.Optional's cases is handled in the
@@ -1513,7 +1523,8 @@ bool TypeChecker::coercePatternToType(Pattern *&P, DeclContext *dc, Type type,
1513
1523
// If the element decl was not resolved (because it was spelled without a
1514
1524
// type as `.Foo`), resolve it now that we have a type.
1515
1525
if (!OP->getElementDecl ()) {
1516
- auto *element = lookupEnumMemberElement (*this , dc, type, Context.Id_some );
1526
+ auto *element = lookupEnumMemberElement (*this , dc, type, Context.Id_some ,
1527
+ OP->getLoc ());
1517
1528
if (!element) {
1518
1529
diagnose (OP->getLoc (), diag::enum_element_pattern_member_not_found,
1519
1530
" Some" , type);
0 commit comments