Skip to content

Commit 7e788d4

Browse files
authored
Merge pull request #26684 from slavapestov/is-iuo-request
Introduce IsImplicitlyUnwrappedOptionalRequest
2 parents fddc5b1 + 70bcd74 commit 7e788d4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+407
-413
lines changed

include/swift/AST/Attr.def

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,7 @@ DECL_ATTR(_restatedObjCConformance, RestatedObjCConformance,
348348
LongAttribute | RejectByParser |
349349
NotSerialized, 70)
350350
// NOTE: 71 is unused
351-
SIMPLE_DECL_ATTR(_implicitly_unwrapped_optional, ImplicitlyUnwrappedOptional,
352-
OnFunc | OnAccessor | OnVar | OnParam | OnSubscript | OnConstructor |
353-
RejectByParser,
354-
72)
351+
// NOTE: 72 is unused
355352
DECL_ATTR(_optimize, Optimize,
356353
OnAbstractFunction | OnSubscript | OnVar |
357354
UserInaccessible,

include/swift/AST/Decl.h

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -456,16 +456,16 @@ class alignas(1 << DeclAlignInBits) Decl {
456456
IsTransparentComputed : 1
457457
);
458458

459-
SWIFT_INLINE_BITFIELD(ConstructorDecl, AbstractFunctionDecl, 3+2+1,
459+
SWIFT_INLINE_BITFIELD(ConstructorDecl, AbstractFunctionDecl, 3+1+1,
460460
/// The body initialization kind (+1), or zero if not yet computed.
461461
///
462462
/// This value is cached but is not serialized, because it is a property
463463
/// of the definition of the constructor that is useful only to semantic
464464
/// analysis and SIL generation.
465465
ComputedBodyInitKind : 3,
466466

467-
/// The failability of this initializer, which is an OptionalTypeKind.
468-
Failability : 2,
467+
/// Whether this constructor can fail, by building an Optional type.
468+
Failable : 1,
469469

470470
/// Whether this initializer is a stub placed into a subclass to
471471
/// catch invalid delegations to a designated initializer not
@@ -2412,12 +2412,20 @@ class ValueDecl : public Decl {
24122412
/// Whether this declaration is 'final'. A final class can't be subclassed,
24132413
/// a final class member can't be overriden.
24142414
unsigned isFinal : 1;
2415+
2416+
/// Whether the "isIUO" bit" has been computed yet.
2417+
unsigned isIUOComputed : 1;
2418+
2419+
/// Whether this declaration produces an implicitly unwrapped
2420+
/// optional result.
2421+
unsigned isIUO : 1;
24152422
} LazySemanticInfo = { };
24162423

24172424
friend class OverriddenDeclsRequest;
24182425
friend class IsObjCRequest;
24192426
friend class IsFinalRequest;
24202427
friend class IsDynamicRequest;
2428+
friend class IsImplicitlyUnwrappedOptionalRequest;
24212429

24222430
protected:
24232431
ValueDecl(DeclKind K,
@@ -2686,6 +2694,21 @@ class ValueDecl : public Decl {
26862694
/// Returns true if this decl can be found by id-style dynamic lookup.
26872695
bool canBeAccessedByDynamicLookup() const;
26882696

2697+
/// Returns true if this declaration has an implicitly unwrapped optional
2698+
/// result. The precise meaning depends on the declaration kind:
2699+
/// - for properties, the value is IUO
2700+
/// - for subscripts, the element type is IUO
2701+
/// - for functions, the result type is IUO
2702+
/// - for constructors, the failability kind is IUO
2703+
bool isImplicitlyUnwrappedOptional() const;
2704+
2705+
/// Should only be set on imported and deserialized declarations; parsed
2706+
/// declarations compute this lazily via a request.
2707+
void setImplicitlyUnwrappedOptional(bool isIUO) {
2708+
LazySemanticInfo.isIUOComputed = 1;
2709+
LazySemanticInfo.isIUO = isIUO;
2710+
}
2711+
26892712
/// Returns the protocol requirements that this decl conforms to.
26902713
ArrayRef<ValueDecl *>
26912714
getSatisfiedProtocolRequirements(bool Sorted = false) const;
@@ -3198,19 +3221,6 @@ class AssociatedTypeDecl : public AbstractTypeParamDecl {
31983221

31993222
class MemberLookupTable;
32003223
class ConformanceLookupTable;
3201-
3202-
/// Kinds of optional types.
3203-
enum OptionalTypeKind : unsigned {
3204-
/// The type is not an optional type.
3205-
OTK_None = 0,
3206-
3207-
/// The type is Optional<T>.
3208-
OTK_Optional,
3209-
3210-
/// The type is ImplicitlyUnwrappedOptional<T>.
3211-
OTK_ImplicitlyUnwrappedOptional
3212-
};
3213-
enum { NumOptionalTypeKinds = 2 };
32143224

32153225
// Kinds of pointer types.
32163226
enum PointerTypeKind : unsigned {
@@ -6495,7 +6505,7 @@ class ConstructorDecl : public AbstractFunctionDecl {
64956505

64966506
public:
64976507
ConstructorDecl(DeclName Name, SourceLoc ConstructorLoc,
6498-
OptionalTypeKind Failability, SourceLoc FailabilityLoc,
6508+
bool Failable, SourceLoc FailabilityLoc,
64996509
bool Throws, SourceLoc ThrowsLoc,
65006510
ParameterList *BodyParams,
65016511
GenericParamList *GenericParams,
@@ -6595,9 +6605,9 @@ class ConstructorDecl : public AbstractFunctionDecl {
65956605
llvm_unreachable("bad CtorInitializerKind");
65966606
}
65976607

6598-
/// Determine the failability of the initializer.
6599-
OptionalTypeKind getFailability() const {
6600-
return static_cast<OptionalTypeKind>(Bits.ConstructorDecl.Failability);
6608+
/// Determine if this is a failable initializer.
6609+
bool isFailable() const {
6610+
return Bits.ConstructorDecl.Failable;
66016611
}
66026612

66036613
/// Retrieve the location of the '!' or '?' in a failable initializer.

include/swift/AST/PrintOptions.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,7 @@ struct PrintOptions {
292292
/// List of attribute kinds that should not be printed.
293293
std::vector<AnyAttrKind> ExcludeAttrList = {DAK_Transparent, DAK_Effects,
294294
DAK_FixedLayout,
295-
DAK_ShowInInterface,
296-
DAK_ImplicitlyUnwrappedOptional};
295+
DAK_ShowInInterface};
297296

298297
/// List of attribute kinds that should be printed exclusively.
299298
/// Empty means allow all.

include/swift/AST/TypeCheckRequests.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,27 @@ class EmittedMembersRequest :
10311031
void cacheResult(DeclRange value) const;
10321032
};
10331033

1034+
class IsImplicitlyUnwrappedOptionalRequest :
1035+
public SimpleRequest<IsImplicitlyUnwrappedOptionalRequest,
1036+
bool(ValueDecl *),
1037+
CacheKind::SeparatelyCached> {
1038+
public:
1039+
using SimpleRequest::SimpleRequest;
1040+
1041+
private:
1042+
friend SimpleRequest;
1043+
1044+
// Evaluation.
1045+
llvm::Expected<bool>
1046+
evaluate(Evaluator &evaluator, ValueDecl *value) const;
1047+
1048+
public:
1049+
// Separate caching.
1050+
bool isCached() const { return true; }
1051+
Optional<bool> getCachedResult() const;
1052+
void cacheResult(bool value) const;
1053+
};
1054+
10341055
// Allow AnyValue to compare two Type values, even though Type doesn't
10351056
// support ==.
10361057
template<>

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,4 @@ SWIFT_TYPEID(RequiresOpaqueModifyCoroutineRequest)
5555
SWIFT_TYPEID(IsAccessorTransparentRequest)
5656
SWIFT_TYPEID(SynthesizeAccessorRequest)
5757
SWIFT_TYPEID(EmittedMembersRequest)
58+
SWIFT_TYPEID(IsImplicitlyUnwrappedOptionalRequest)

include/swift/ClangImporter/ClangImporter.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ class TypeDecl;
6060
class VisibleDeclConsumer;
6161
enum class SelectorSplitKind;
6262

63+
/// Kinds of optional types.
64+
enum OptionalTypeKind : unsigned {
65+
/// The type is not an optional type.
66+
OTK_None = 0,
67+
68+
/// The type is Optional<T>.
69+
OTK_Optional,
70+
71+
/// The type is ImplicitlyUnwrappedOptional<T>.
72+
OTK_ImplicitlyUnwrappedOptional
73+
};
74+
enum { NumOptionalTypeKinds = 2 };
75+
6376
/// This interface is implemented by LLDB to serve as a fallback when Clang
6477
/// modules can't be imported from source in the debugger.
6578
///

include/swift/Parse/Parser.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,6 @@ class Parser {
10191019
ParserStatus parseGetSet(ParseDeclOptions Flags,
10201020
GenericParamList *GenericParams,
10211021
ParameterList *Indices,
1022-
TypeLoc ElementTy,
10231022
ParsedAccessors &accessors,
10241023
AbstractStorageDecl *storage,
10251024
SourceLoc StaticLoc);

include/swift/Serialization/ModuleFormat.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5252
/// describe what change you made. The content of this comment isn't important;
5353
/// it just ensures a conflict if two people change the module format.
5454
/// Don't worry about adhering to the 80-column limit for this line.
55-
const uint16_t SWIFTMODULE_VERSION_MINOR = 509; // [dynamic_lifetime] sil flag
55+
const uint16_t SWIFTMODULE_VERSION_MINOR = 511; // ctor failability change
5656

5757
using DeclIDField = BCFixed<31>;
5858

@@ -409,15 +409,6 @@ enum class AccessLevel : uint8_t {
409409
};
410410
using AccessLevelField = BCFixed<3>;
411411

412-
// These IDs must \em not be renumbered or reordered without incrementing
413-
// the module version.
414-
enum class OptionalTypeKind : uint8_t {
415-
None,
416-
Optional,
417-
ImplicitlyUnwrappedOptional
418-
};
419-
using OptionalTypeKindField = BCFixed<2>;
420-
421412
// These IDs must \em not be renumbered or reordered without incrementing
422413
// the module version.
423414
enum class DeclNameKind: uint8_t {
@@ -1019,7 +1010,8 @@ namespace decls_block {
10191010
using ConstructorLayout = BCRecordLayout<
10201011
CONSTRUCTOR_DECL,
10211012
DeclContextIDField, // context decl
1022-
OptionalTypeKindField, // failability
1013+
BCFixed<1>, // failable?
1014+
BCFixed<1>, // IUO result?
10231015
BCFixed<1>, // implicit?
10241016
BCFixed<1>, // objc?
10251017
BCFixed<1>, // stub implementation?
@@ -1059,6 +1051,7 @@ namespace decls_block {
10591051
ReadWriteImplKindField, // read-write implementation
10601052
AccessorCountField, // number of accessors
10611053
TypeIDField, // interface type
1054+
BCFixed<1>, // IUO value?
10621055
DeclIDField, // overridden decl
10631056
AccessLevelField, // access level
10641057
AccessLevelField, // setter access, if applicable
@@ -1075,6 +1068,7 @@ namespace decls_block {
10751068
DeclContextIDField, // context decl
10761069
ParamDeclSpecifierField, // specifier
10771070
TypeIDField, // interface type
1071+
BCFixed<1>, // isIUO?
10781072
BCFixed<1>, // isVariadic?
10791073
BCFixed<1>, // isAutoClosure?
10801074
DefaultArgumentField, // default argument kind
@@ -1093,6 +1087,7 @@ namespace decls_block {
10931087
BCFixed<1>, // throws?
10941088
GenericEnvironmentIDField, // generic environment
10951089
TypeIDField, // result interface type
1090+
BCFixed<1>, // IUO result?
10961091
DeclIDField, // operator decl
10971092
DeclIDField, // overridden function
10981093
BCVBR<5>, // 0 for a simple name, otherwise the number of parameter name
@@ -1134,6 +1129,7 @@ namespace decls_block {
11341129
BCFixed<1>, // throws?
11351130
GenericEnvironmentIDField, // generic environment
11361131
TypeIDField, // result interface type
1132+
BCFixed<1>, // IUO result?
11371133
DeclIDField, // overridden function
11381134
DeclIDField, // AccessorStorageDecl
11391135
AccessorKindField, // accessor kind
@@ -1220,6 +1216,7 @@ namespace decls_block {
12201216
AccessorCountField, // number of accessors
12211217
GenericEnvironmentIDField, // generic environment
12221218
TypeIDField, // element interface type
1219+
BCFixed<1>, // IUO element?
12231220
DeclIDField, // overridden decl
12241221
AccessLevelField, // access level
12251222
AccessLevelField, // setter access, if applicable

lib/AST/ASTDumper.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -370,15 +370,6 @@ static StringRef getCtorInitializerKindString(CtorInitializerKind value) {
370370

371371
llvm_unreachable("Unhandled CtorInitializerKind in switch.");
372372
}
373-
static StringRef getOptionalTypeKindString(OptionalTypeKind value) {
374-
switch (value) {
375-
case OTK_None: return "none";
376-
case OTK_Optional: return "Optional";
377-
case OTK_ImplicitlyUnwrappedOptional: return "ImplicitlyUnwrappedOptional";
378-
}
379-
380-
llvm_unreachable("Unhandled OptionalTypeKind in switch.");
381-
}
382373
static StringRef getAssociativityString(Associativity value) {
383374
switch (value) {
384375
case Associativity::None: return "none";
@@ -1100,9 +1091,11 @@ namespace {
11001091
PrintWithColorRAII(OS, DeclModifierColor) << " required";
11011092
PrintWithColorRAII(OS, DeclModifierColor) << " "
11021093
<< getCtorInitializerKindString(CD->getInitKind());
1103-
if (CD->getFailability() != OTK_None)
1094+
if (CD->isFailable())
11041095
PrintWithColorRAII(OS, DeclModifierColor) << " failable="
1105-
<< getOptionalTypeKindString(CD->getFailability());
1096+
<< (CD->isImplicitlyUnwrappedOptional()
1097+
? "ImplicitlyUnwrappedOptional"
1098+
: "Optional");
11061099
printAbstractFunctionDecl(CD);
11071100
PrintWithColorRAII(OS, ParenthesisColor) << ')';
11081101
}

0 commit comments

Comments
 (0)