Skip to content

Commit d63bb3f

Browse files
committed
Remove most uses of OptionalTypeKind.
What remains are places where we are conflating optionality with either nullability or failability.
1 parent 94a35a1 commit d63bb3f

28 files changed

+178
-383
lines changed

include/swift/AST/ASTContext.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ namespace swift {
9090
class PrecedenceGroupDecl;
9191
class TupleTypeElt;
9292
class EnumElementDecl;
93-
enum OptionalTypeKind : unsigned;
9493
class ProtocolDecl;
9594
class SubstitutableType;
9695
class SourceManager;
@@ -410,18 +409,12 @@ class ASTContext {
410409
DECL_CLASS *get##NAME##Decl() const;
411410
#include "swift/AST/KnownStdlibTypes.def"
412411

413-
/// Retrieve the declaration of Swift.Optional.
414-
EnumDecl *getOptionalDecl(OptionalTypeKind kind) const;
415-
416412
/// Retrieve the declaration of Swift.Optional<T>.Some.
417413
EnumElementDecl *getOptionalSomeDecl() const;
418414

419415
/// Retrieve the declaration of Swift.Optional<T>.None.
420416
EnumElementDecl *getOptionalNoneDecl() const;
421417

422-
EnumElementDecl *getOptionalSomeDecl(OptionalTypeKind kind) const;
423-
EnumElementDecl *getOptionalNoneDecl(OptionalTypeKind kind) const;
424-
425418
/// Retrieve the declaration of the "pointee" property of a pointer type.
426419
VarDecl *getPointerPointeePropertyDecl(PointerTypeKind ptrKind) const;
427420

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -613,12 +613,6 @@ ERROR(invalid_redecl,none,"invalid redeclaration of %0", (DeclName))
613613
NOTE(invalid_redecl_prev,none,
614614
"%0 previously declared here", (DeclName))
615615

616-
WARNING(deprecated_redecl_by_optionality, none,
617-
"invalid redeclaration of %0 which differs only by the kind of optional passed as an inout argument (%1 vs. %2)",
618-
(DeclName, Type, Type))
619-
NOTE(deprecated_redecl_by_optionality_note, none,
620-
"overloading by kind of optional is deprecated and will be removed in a future release", ())
621-
622616
ERROR(ambiguous_type_base,none,
623617
"%0 is ambiguous for type lookup in this context", (Identifier))
624618
ERROR(invalid_member_type,none,

include/swift/AST/Type.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class ModuleDecl;
4343
class NominalTypeDecl;
4444
class GenericTypeDecl;
4545
class NormalProtocolConformance;
46-
enum OptionalTypeKind : unsigned;
4746
class ProtocolConformanceRef;
4847
class ProtocolDecl;
4948
class ProtocolType;
@@ -389,8 +388,7 @@ class CanType : public Type {
389388
static bool isExistentialTypeImpl(CanType type);
390389
static bool isAnyExistentialTypeImpl(CanType type);
391390
static bool isObjCExistentialTypeImpl(CanType type);
392-
static CanType getOptionalObjectTypeImpl(CanType type,
393-
OptionalTypeKind &kind);
391+
static CanType getOptionalObjectTypeImpl(CanType type, bool &isOptional);
394392
static CanType getReferenceStorageReferentImpl(CanType type);
395393
static CanType getWithoutSpecifierTypeImpl(CanType type);
396394

@@ -458,12 +456,12 @@ class CanType : public Type {
458456
GenericTypeDecl *getAnyGeneric() const;
459457

460458
CanType getOptionalObjectType() const {
461-
OptionalTypeKind kind;
462-
return getOptionalObjectTypeImpl(*this, kind);
459+
bool isOptional;
460+
return getOptionalObjectTypeImpl(*this, isOptional);
463461
}
464462

465-
CanType getOptionalObjectType(OptionalTypeKind &kind) const {
466-
return getOptionalObjectTypeImpl(*this, kind);
463+
CanType getOptionalObjectType(bool &isOptional) const {
464+
return getOptionalObjectTypeImpl(*this, isOptional);
467465
}
468466

469467
CanType getReferenceStorageReferent() const {

include/swift/AST/Types.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ namespace swift {
6969
class ModuleDecl;
7070
class ModuleType;
7171
class ProtocolConformance;
72-
enum OptionalTypeKind : unsigned;
7372
enum PointerTypeKind : unsigned;
7473
struct ValueOwnershipKind;
7574

@@ -1002,7 +1001,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {
10021001
/// Return T if this type is Optional<T>; otherwise, return the null
10031002
/// type. Set \p kind to OTK_Optional if it is an optional, OTK_None
10041003
/// otherwise.
1005-
Type getOptionalObjectType(OptionalTypeKind &kind);
1004+
Type getOptionalObjectType(bool &isOptional);
10061005

10071006
// Return type underlying type of a swift_newtype annotated imported struct;
10081007
// otherwise, return the null type.
@@ -4092,16 +4091,6 @@ class OptionalType : public UnarySyntaxSugarType {
40924091
/// Return a uniqued optional type with the specified base type.
40934092
static OptionalType *get(Type baseTy);
40944093

4095-
/// Build one of the optional type sugar kinds.
4096-
///
4097-
/// It's a bit unnatural to have this on OptionalType, but we don't
4098-
/// have an abstract common class, and polluting TypeBase with it
4099-
/// would be unfortunate. If we ever make an AnyOptionalType,
4100-
/// we can move it there.
4101-
///
4102-
/// \param kind - can't be OTK_None
4103-
static Type get(OptionalTypeKind kind, Type baseTy);
4104-
41054094
// Implement isa/cast/dyncast/etc.
41064095
static bool classof(const TypeBase *T) {
41074096
return T->getKind() == TypeKind::Optional;

lib/AST/ASTContext.cpp

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,6 @@ struct ASTContext::Implementation {
152152
/// The declaration of Swift.Optional<T>.None.
153153
EnumElementDecl *OptionalNoneDecl = nullptr;
154154

155-
/// The declaration of Swift.ImplicitlyUnwrappedOptional<T>.Some.
156-
EnumElementDecl *ImplicitlyUnwrappedOptionalSomeDecl = nullptr;
157-
158-
/// The declaration of Swift.ImplicitlyUnwrappedOptional<T>.None.
159-
EnumElementDecl *ImplicitlyUnwrappedOptionalNoneDecl = nullptr;
160-
161155
/// The declaration of Swift.UnsafeMutableRawPointer.memory.
162156
VarDecl *UnsafeMutableRawPointerMemoryDecl = nullptr;
163157

@@ -644,43 +638,6 @@ ProtocolDecl *ASTContext::getErrorDecl() const {
644638
return getProtocol(KnownProtocolKind::Error);
645639
}
646640

647-
EnumDecl *ASTContext::getOptionalDecl(OptionalTypeKind kind) const {
648-
switch (kind) {
649-
case OTK_None:
650-
llvm_unreachable("not optional");
651-
case OTK_ImplicitlyUnwrappedOptional:
652-
llvm_unreachable("Should no longer have IUOs");
653-
case OTK_Optional:
654-
return getOptionalDecl();
655-
}
656-
657-
llvm_unreachable("Unhandled OptionalTypeKind in switch.");
658-
}
659-
660-
EnumElementDecl *ASTContext::getOptionalSomeDecl(OptionalTypeKind kind) const {
661-
switch (kind) {
662-
case OTK_Optional:
663-
return getOptionalSomeDecl();
664-
case OTK_ImplicitlyUnwrappedOptional:
665-
llvm_unreachable("Should not have IUOs.");
666-
case OTK_None:
667-
llvm_unreachable("getting Some decl for non-optional type?");
668-
}
669-
llvm_unreachable("bad OTK");
670-
}
671-
672-
EnumElementDecl *ASTContext::getOptionalNoneDecl(OptionalTypeKind kind) const {
673-
switch (kind) {
674-
case OTK_Optional:
675-
return getOptionalNoneDecl();
676-
case OTK_ImplicitlyUnwrappedOptional:
677-
llvm_unreachable("Should not have IUOs.");
678-
case OTK_None:
679-
llvm_unreachable("getting None decl for non-optional type?");
680-
}
681-
llvm_unreachable("bad OTK");
682-
}
683-
684641
EnumElementDecl *ASTContext::getOptionalSomeDecl() const {
685642
if (!Impl.OptionalSomeDecl)
686643
Impl.OptionalSomeDecl = getOptionalDecl()->getUniqueElement(/*hasVal*/true);
@@ -4025,18 +3982,6 @@ DictionaryType *DictionaryType::get(Type keyType, Type valueType) {
40253982
properties);
40263983
}
40273984

4028-
Type OptionalType::get(OptionalTypeKind which, Type valueType) {
4029-
switch (which) {
4030-
// It wouldn't be unreasonable for this method to just ignore
4031-
// OTK_None if we made code more convenient to write.
4032-
case OTK_None: llvm_unreachable("building a non-optional type!");
4033-
case OTK_Optional: return OptionalType::get(valueType);
4034-
case OTK_ImplicitlyUnwrappedOptional:
4035-
llvm_unreachable("Should no longer have IUOs");
4036-
}
4037-
llvm_unreachable("bad optional type kind");
4038-
}
4039-
40403985
OptionalType *OptionalType::get(Type base) {
40413986
auto properties = base->getRecursiveProperties();
40423987
auto arena = getArena(properties);

lib/AST/ASTVerifier.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,12 +2687,11 @@ class Verifier : public ASTWalker {
26872687
if (!CD->isInvalid() &&
26882688
CD->getDeclContext()->getDeclaredInterfaceType()->getAnyNominal() !=
26892689
Ctx.getOptionalDecl()) {
2690-
OptionalTypeKind resultOptionality = OTK_None;
2691-
CD->getResultInterfaceType()->getOptionalObjectType(resultOptionality);
2692-
auto declOptionality = CD->getFailability();
2690+
bool resultIsOptional;
2691+
CD->getResultInterfaceType()->getOptionalObjectType(resultIsOptional);
2692+
auto declIsOptional = CD->getFailability() != OTK_None;
26932693

2694-
if ((resultOptionality != OTK_None || declOptionality != OTK_None) &&
2695-
(resultOptionality == OTK_None || declOptionality == OTK_None)) {
2694+
if (resultIsOptional != declIsOptional) {
26962695
Out << "Initializer has result optionality/failability mismatch\n";
26972696
CD->dump(llvm::errs());
26982697
abort();
@@ -2701,13 +2700,11 @@ class Verifier : public ASTWalker {
27012700
// Also check the interface type.
27022701
if (auto genericFn
27032702
= CD->getInterfaceType()->getAs<GenericFunctionType>()) {
2704-
resultOptionality = OTK_None;
27052703
genericFn->getResult()
27062704
->castTo<AnyFunctionType>()
27072705
->getResult()
2708-
->getOptionalObjectType(resultOptionality);
2709-
if ((resultOptionality != OTK_None || declOptionality != OTK_None) &&
2710-
(resultOptionality == OTK_None || declOptionality == OTK_None)) {
2706+
->getOptionalObjectType(resultIsOptional);
2707+
if (resultIsOptional != declIsOptional) {
27112708
Out << "Initializer has result optionality/failability mismatch\n";
27122709
CD->dump(llvm::errs());
27132710
abort();

lib/AST/Type.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,9 @@ static bool isLegalSILType(CanType type) {
439439
return true;
440440
}
441441

442-
// Optionals are legal if their object type is legal and they're Optional.
443-
OptionalTypeKind optKind;
444-
if (auto objectType = type.getOptionalObjectType(optKind)) {
445-
return (optKind == OTK_Optional && isLegalSILType(objectType));
442+
// Optionals are legal if their object type is legal.
443+
if (auto objectType = type.getOptionalObjectType()) {
444+
return isLegalSILType(objectType);
446445
}
447446

448447
// Reference storage types are legal if their object type is legal.
@@ -502,26 +501,25 @@ Type TypeBase::getOptionalObjectType() {
502501
return Type();
503502
}
504503

505-
Type TypeBase::getOptionalObjectType(OptionalTypeKind &kind) {
504+
Type TypeBase::getOptionalObjectType(bool &isOptional) {
506505
if (auto boundTy = getAs<BoundGenericEnumType>()) {
507506
if (boundTy->getDecl()->isOptionalDecl()) {
508-
kind = OTK_Optional;
507+
isOptional = true;
509508
return boundTy->getGenericArgs()[0];
510509
}
511510
}
512-
kind = OTK_None;
511+
isOptional = false;
513512
return Type();
514513
}
515514

516-
CanType CanType::getOptionalObjectTypeImpl(CanType type,
517-
OptionalTypeKind &kind) {
515+
CanType CanType::getOptionalObjectTypeImpl(CanType type, bool &isOptional) {
518516
if (auto boundTy = dyn_cast<BoundGenericEnumType>(type)) {
519517
if (boundTy->getDecl()->isOptionalDecl()) {
520-
kind = OTK_Optional;
518+
isOptional = true;
521519
return boundTy.getGenericArgs()[0];
522520
}
523521
}
524-
kind = OTK_None;
522+
isOptional = false;
525523
return CanType();
526524
}
527525

@@ -684,13 +682,10 @@ Type TypeBase::getWithoutImmediateLabel() {
684682
Type TypeBase::replaceCovariantResultType(Type newResultType,
685683
unsigned uncurryLevel) {
686684
if (uncurryLevel == 0) {
687-
OptionalTypeKind resultOTK;
688-
if (auto objectType = getOptionalObjectType(resultOTK)) {
685+
if (auto objectType = getOptionalObjectType()) {
689686
assert(!newResultType->getOptionalObjectType());
690687
return OptionalType::get(
691-
resultOTK,
692-
objectType->replaceCovariantResultType(
693-
newResultType, uncurryLevel));
688+
objectType->replaceCovariantResultType(newResultType, uncurryLevel));
694689
}
695690

696691
return newResultType;

lib/AST/TypeJoinMeet.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ struct TypeJoin : CanTypeVisitor<TypeJoin, CanType> {
6767

6868
// Until we handle all the combinations of joins, we need to make
6969
// sure we visit the optional side.
70-
OptionalTypeKind otk;
71-
if (second->getOptionalObjectType(otk))
70+
if (second->getOptionalObjectType())
7271
return TypeJoin(first).visit(second);
7372

7473
return TypeJoin(second).visit(first);
@@ -160,12 +159,17 @@ CanType TypeJoin::visitBoundGenericEnumType(CanType second) {
160159
if (First->getKind() != second->getKind())
161160
return First->getASTContext().TheAnyType;
162161

163-
OptionalTypeKind otk1, otk2;
164-
auto firstObject = First->getOptionalObjectType(otk1);
165-
auto secondObject = second->getOptionalObjectType(otk2);
166-
if (otk1 == OTK_Optional || otk2 == OTK_Optional) {
167-
auto canFirst = firstObject->getCanonicalType();
168-
auto canSecond = secondObject->getCanonicalType();
162+
bool firstIsOptional, secondIsOptional;
163+
auto firstObject = First->getOptionalObjectType(firstIsOptional);
164+
auto secondObject = second->getOptionalObjectType(secondIsOptional);
165+
if (firstIsOptional || secondIsOptional) {
166+
CanType canFirst;
167+
CanType canSecond;
168+
169+
if (firstObject)
170+
canFirst = firstObject->getCanonicalType();
171+
if (secondObject)
172+
canSecond = secondObject->getCanonicalType();
169173

170174
// Compute the join of the unwrapped type. If there is none, we're done.
171175
auto unwrappedJoin =

lib/ClangImporter/ImportDecl.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6106,18 +6106,21 @@ ConstructorDecl *SwiftDeclConverter::importConstructor(
61066106

61076107
// Determine the failability of this initializer.
61086108
auto oldFnType = type->castTo<AnyFunctionType>();
6109-
OptionalTypeKind failability;
6110-
(void)oldFnType->getResult()->getOptionalObjectType(failability);
6109+
bool resultIsOptional;
6110+
(void)oldFnType->getResult()->getOptionalObjectType(resultIsOptional);
61116111

61126112
// Update the failability appropriately based on the imported method type.
6113-
if (importedType.isImplicitlyUnwrapped()) {
6114-
assert(failability != OTK_None);
6115-
failability = OTK_ImplicitlyUnwrappedOptional;
6113+
assert(resultIsOptional || !importedType.isImplicitlyUnwrapped());
6114+
OptionalTypeKind failability = OTK_None;
6115+
if (resultIsOptional) {
6116+
failability = OTK_Optional;
6117+
if (importedType.isImplicitlyUnwrapped())
6118+
failability = OTK_ImplicitlyUnwrappedOptional;
61166119
}
61176120

61186121
// Rebuild the function type with the appropriate result type;
61196122
Type resultTy = selfTy;
6120-
if (failability != OTK_None)
6123+
if (resultIsOptional)
61216124
resultTy = OptionalType::get(resultTy);
61226125

61236126
type = FunctionType::get(oldFnType->getInput(), resultTy,

lib/ClangImporter/ImportType.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,8 +1238,8 @@ static ImportedType adjustTypeForConcreteImport(
12381238
if (!elementType || PTK != PTK_UnsafeMutablePointer)
12391239
return Type();
12401240

1241-
OptionalTypeKind OTK;
1242-
auto insideOptionalType = elementType->getOptionalObjectType(OTK);
1241+
bool isOptional;
1242+
auto insideOptionalType = elementType->getOptionalObjectType(isOptional);
12431243
if (!insideOptionalType)
12441244
insideOptionalType = elementType;
12451245

@@ -1255,10 +1255,8 @@ static ImportedType adjustTypeForConcreteImport(
12551255
"signature of Unmanaged has changed");
12561256

12571257
auto resultTy = boundGenericTy->getGenericArgs().front();
1258-
if (OTK != OTK_None) {
1259-
assert(OTK != OTK_ImplicitlyUnwrappedOptional);
1258+
if (isOptional)
12601259
resultTy = OptionalType::get(resultTy);
1261-
}
12621260

12631261
StringRef pointerName;
12641262
if (importKind == ImportTypeKind::CFRetainedOutParameter)
@@ -1509,8 +1507,7 @@ ImportedType ClangImporter::Implementation::importPropertyType(
15091507
/// Apply the @noescape attribute
15101508
static Type applyNoEscape(Type type) {
15111509
// Recurse into optional types.
1512-
OptionalTypeKind optKind;
1513-
if (Type objectType = type->getOptionalObjectType(optKind)) {
1510+
if (Type objectType = type->getOptionalObjectType()) {
15141511
return OptionalType::get(applyNoEscape(objectType));
15151512
}
15161513

@@ -1951,8 +1948,8 @@ ImportedType ClangImporter::Implementation::importMethodType(
19511948
clangDecl->getMethodFamily() == clang::OMF_performSelector) {
19521949
// performSelector methods that return 'id' should be imported into Swift
19531950
// as returning Unmanaged<AnyObject>.
1954-
Type nonOptionalTy =
1955-
swiftResultTy->getOptionalObjectType(OptionalityOfReturn);
1951+
bool resultIsOptional;
1952+
Type nonOptionalTy = swiftResultTy->getOptionalObjectType(resultIsOptional);
19561953
if (!nonOptionalTy)
19571954
nonOptionalTy = swiftResultTy;
19581955

@@ -1962,7 +1959,7 @@ ImportedType ClangImporter::Implementation::importMethodType(
19621959

19631960
if (nonOptionalTy->isAnyClassReferenceType()) {
19641961
swiftResultTy = getUnmanagedType(*this, nonOptionalTy);
1965-
if (OptionalityOfReturn != OTK_None)
1962+
if (resultIsOptional)
19661963
swiftResultTy = OptionalType::get(swiftResultTy);
19671964
}
19681965
}

0 commit comments

Comments
 (0)