Skip to content

Commit f658648

Browse files
committed
Sema: Build TypeRefinementContexts correctly for multi-element enum cases.
Nodes in the TypeRefinementContext tree should be introduced for enum cases, rather than enum elements, since its the cases that carry availability annotations. Previously, enum cases in source that contained more than one element would result in a malformed TRC tree that had overlapping sibling nodes for each of the elements in a case declaration.
1 parent 4a51cfa commit f658648

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

lib/AST/TypeRefinementContext.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,10 @@ void TypeRefinementContext::print(raw_ostream &OS, SourceManager &SrcMgr,
381381
if (auto VD = PBD->getAnchoringVarDecl(0)) {
382382
OS << VD->getName();
383383
}
384+
} else if (auto ECD = dyn_cast<EnumCaseDecl>(D)) {
385+
if (auto EED = ECD->getFirstElement()) {
386+
OS << EED->getName();
387+
}
384388
}
385389
}
386390

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@ class TypeRefinementContextBuilder : private ASTWalker {
640640
/// if no new context should be introduced.
641641
TypeRefinementContext *getNewContextForSignatureOfDecl(Decl *D) {
642642
if (!isa<ValueDecl>(D) &&
643+
!isa<EnumCaseDecl>(D) &&
643644
!isa<ExtensionDecl>(D) &&
644645
!isa<MacroExpansionDecl>(D) &&
645646
!isa<PatternBindingDecl>(D))
@@ -658,6 +659,11 @@ class TypeRefinementContextBuilder : private ASTWalker {
658659
return nullptr;
659660
}
660661

662+
// Don't introduce for enum element declarations. All the relevant
663+
// information is on the enum case declaration.
664+
if (isa<EnumElementDecl>(D))
665+
return nullptr;
666+
661667
// Declarations with an explicit availability attribute always get a TRC.
662668
if (hasActiveAvailableAttribute(D, Context)) {
663669
AvailabilityRange DeclaredAvailability =

test/Sema/availability_refinement_contexts.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,16 @@ struct SomeStruct {
273273
@available(OSX 52, *)
274274
func someMethodAvailable52() -> Int { return 52 }
275275
}
276+
277+
// CHECK-NEXT: {{^}} (decl version=51 decl=SomeEnum
278+
// CHECK-NEXT: {{^}} (decl version=52 decl=a
279+
// CHECK-NEXT: {{^}} (decl version=53 decl=b
280+
281+
@available(OSX 51, *)
282+
enum SomeEnum {
283+
@available(OSX 52, *)
284+
case a
285+
286+
@available(OSX 53, *)
287+
case b, c
288+
}

0 commit comments

Comments
 (0)