-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Implementation for /most/ of SE-0192 (frozen and non-frozen enums) #14945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5e9b547
b4274b6
7c60f1c
00361df
7e3aaff
9034ba6
52af3f3
6ea47bd
1b1a4cb
05293f2
3866535
348f966
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2286,9 +2286,11 @@ bool NominalTypeDecl::isFormallyResilient() const { | |
/*respectVersionedAttr=*/true).isPublic()) | ||
return false; | ||
|
||
// Check for an explicit @_fixed_layout attribute. | ||
if (getAttrs().hasAttribute<FixedLayoutAttr>()) | ||
// Check for an explicit @_fixed_layout or @_frozen attribute. | ||
if (getAttrs().hasAttribute<FixedLayoutAttr>() || | ||
getAttrs().hasAttribute<FrozenAttr>()) { | ||
return false; | ||
} | ||
|
||
// Structs and enums imported from C *always* have a fixed layout. | ||
// We know their size, and pass them as values in SIL and IRGen. | ||
|
@@ -3088,6 +3090,53 @@ bool EnumDecl::hasOnlyCasesWithoutAssociatedValues() const { | |
return true; | ||
} | ||
|
||
bool EnumDecl::isExhaustive(const DeclContext *useDC) const { | ||
// Enums explicitly marked frozen are exhaustive. | ||
if (getAttrs().hasAttribute<FrozenAttr>()) | ||
return true; | ||
|
||
// Objective-C enums /not/ marked frozen are /not/ exhaustive. | ||
// Note: This implicitly holds @objc enums defined in Swift to a higher | ||
// standard! | ||
if (hasClangNode()) | ||
return false; | ||
|
||
// Non-imported enums in non-resilient modules are exhaustive. | ||
const ModuleDecl *containingModule = getModuleContext(); | ||
switch (containingModule->getResilienceStrategy()) { | ||
case ResilienceStrategy::Default: | ||
return true; | ||
case ResilienceStrategy::Resilient: | ||
break; | ||
} | ||
|
||
// Non-public, non-versioned enums are always exhaustive. | ||
AccessScope accessScope = getFormalAccessScope(/*useDC*/nullptr, | ||
/*respectVersioned*/true); | ||
if (!accessScope.isPublic()) | ||
return true; | ||
|
||
// All other checks are use-site specific; with no further information, the | ||
// enum must be treated non-exhaustively. | ||
if (!useDC) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we ever need to check exhaustiveness in SILGen or anything else that runs after Sema? Because we won't have a useDC anymore There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we'll have a DC up through SILGen, but you do have me worried about optimizations. Unfortunately we don't actually the right API for that ("AvailabilityContext"); fortunately, it'll err on the side of "non-exhaustive". (That is, passing |
||
return false; | ||
|
||
// Enums in the same module as the use site are exhaustive /unless/ the use | ||
// site is inlinable. | ||
if (useDC->getParentModule() == containingModule) | ||
if (useDC->getResilienceExpansion() == ResilienceExpansion::Maximal) | ||
return true; | ||
|
||
// Testably imported enums are exhaustive, on the grounds that only the author | ||
// of the original library can import it testably. | ||
if (auto *useSF = dyn_cast<SourceFile>(useDC->getModuleScopeContext())) | ||
if (useSF->hasTestableImport(containingModule)) | ||
return true; | ||
|
||
// Otherwise, the enum is non-exhaustive. | ||
return false; | ||
} | ||
|
||
ProtocolDecl::ProtocolDecl(DeclContext *DC, SourceLoc ProtocolLoc, | ||
SourceLoc NameLoc, Identifier Name, | ||
MutableArrayRef<TypeLoc> Inherited, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for staging while we settle on the "unknown case" spelling, right? It'll go away then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's mainly so I can land this part before SE-0192's review period ends, but yes.