Skip to content

More IUO removal clean-up. #14383

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

Merged
merged 5 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2986,10 +2986,9 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {

void setConformanceLoader(LazyMemberLoader *resolver, uint64_t contextData);

/// classifyAsOptionalType - Decide whether this declaration is one
/// of the library-intrinsic Optional<T> or ImplicitlyUnwrappedOptional<T> types.
OptionalTypeKind classifyAsOptionalType() const;

/// Is this the decl for Optional<T>?
bool isOptionalDecl() const;

private:
/// Predicate used to filter StoredPropertyRange.
struct ToStoredProperty {
Expand Down
15 changes: 5 additions & 10 deletions include/swift/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,8 @@ class CanType : public Type {
static bool isExistentialTypeImpl(CanType type);
static bool isAnyExistentialTypeImpl(CanType type);
static bool isObjCExistentialTypeImpl(CanType type);
static CanType getAnyOptionalObjectTypeImpl(CanType type,
OptionalTypeKind &kind);
static CanType getOptionalObjectTypeImpl(CanType type);
static CanType getOptionalObjectTypeImpl(CanType type,
OptionalTypeKind &kind);
static CanType getReferenceStorageReferentImpl(CanType type);
static CanType getWithoutSpecifierTypeImpl(CanType type);

Expand Down Expand Up @@ -459,16 +458,12 @@ class CanType : public Type {
GenericTypeDecl *getAnyGeneric() const;

CanType getOptionalObjectType() const {
return getOptionalObjectTypeImpl(*this);
}

CanType getAnyOptionalObjectType() const {
OptionalTypeKind kind;
return getAnyOptionalObjectTypeImpl(*this, kind);
return getOptionalObjectTypeImpl(*this, kind);
}

CanType getAnyOptionalObjectType(OptionalTypeKind &kind) const {
return getAnyOptionalObjectTypeImpl(*this, kind);
CanType getOptionalObjectType(OptionalTypeKind &kind) const {
return getOptionalObjectTypeImpl(*this, kind);
}

CanType getReferenceStorageReferent() const {
Expand Down
3 changes: 1 addition & 2 deletions include/swift/AST/TypeNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ ABSTRACT_SUGARED_TYPE(Sugar, Type)
ABSTRACT_SUGARED_TYPE(UnarySyntaxSugar, SyntaxSugarType)
SUGARED_TYPE(ArraySlice, UnarySyntaxSugarType)
SUGARED_TYPE(Optional, UnarySyntaxSugarType)
SUGARED_TYPE(ImplicitlyUnwrappedOptional, UnarySyntaxSugarType)
TYPE_RANGE(UnarySyntaxSugar, ArraySlice, ImplicitlyUnwrappedOptional)
TYPE_RANGE(UnarySyntaxSugar, ArraySlice, Optional)
SUGARED_TYPE(Dictionary, SyntaxSugarType)
TYPE_RANGE(SyntaxSugar, ArraySlice, Dictionary)
TYPE_RANGE(Sugar, Paren, Dictionary)
Expand Down
43 changes: 10 additions & 33 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -993,25 +993,22 @@ class alignas(1 << TypeAlignInBits) TypeBase {
/// Return T if this type is Optional<T>; otherwise, return the null type.
Type getOptionalObjectType();

/// Return T if this type is Optional<T> or ImplicitlyUnwrappedOptional<T>;
/// otherwise, return the null type.
Type getAnyOptionalObjectType(OptionalTypeKind &kind);
Type getAnyOptionalObjectType() {
OptionalTypeKind ignored;
return getAnyOptionalObjectType(ignored);
}
/// Return T if this type is Optional<T>; otherwise, return the null
/// type. Set \p kind to OTK_Optional if it is an optional, OTK_None
/// otherwise.
Type getOptionalObjectType(OptionalTypeKind &kind);

// Return type underlying type of a swift_newtype annotated imported struct;
// otherwise, return the null type.
Type getSwiftNewtypeUnderlyingType();

/// Return the type T after looking through all of the optional or
/// implicitly-unwrapped optional types.
Type lookThroughAllAnyOptionalTypes();
/// Return the type T after looking through all of the optional
/// types.
Type lookThroughAllOptionalTypes();

/// Return the type T after looking through all of the optional or
/// implicitly-unwrapped optional types.
Type lookThroughAllAnyOptionalTypes(SmallVectorImpl<Type> &optionals);
/// Return the type T after looking through all of the optional
/// types.
Type lookThroughAllOptionalTypes(SmallVectorImpl<Type> &optionals);

/// Whether this is the AnyObject type.
bool isAnyObject();
Expand Down Expand Up @@ -4105,26 +4102,6 @@ class OptionalType : public UnarySyntaxSugarType {
}
};

/// The type T!, which is always sugar for a library type.
class ImplicitlyUnwrappedOptionalType : public UnarySyntaxSugarType {
ImplicitlyUnwrappedOptionalType(const ASTContext &ctx, Type base,
RecursiveTypeProperties properties)
: UnarySyntaxSugarType(TypeKind::ImplicitlyUnwrappedOptional, ctx, base,
properties) {
llvm_unreachable(
"ImplicitlyUnwrappedOptionalType::ImplicitlyUnwrappedOptionalType");
}

public:
/// Return a uniqued optional type with the specified base type.
static ImplicitlyUnwrappedOptionalType *get(Type baseTy);

// Implement isa/cast/dyncast/etc.
static bool classof(const TypeBase *T) {
return T->getKind() == TypeKind::ImplicitlyUnwrappedOptional;
}
};

/// The dictionary type [K : V], which is syntactic sugar for Dictionary<K, V>.
///
/// Example:
Expand Down
2 changes: 1 addition & 1 deletion include/swift/SIL/AbstractionPattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ class AbstractionPattern {

/// Given that the value being abstracted is optional, return the
/// abstraction pattern for its object type.
AbstractionPattern getAnyOptionalObjectType() const;
AbstractionPattern getOptionalObjectType() const;

/// If this pattern refers to a reference storage type, look through
/// it.
Expand Down
4 changes: 2 additions & 2 deletions include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -2481,8 +2481,8 @@ class KeyPathPatternComponent {
case Kind::OptionalForce:
break;
case Kind::OptionalWrap:
assert(ty->getAnyOptionalObjectType()
&& "optional wrap didn't form optional?!");
assert(ty->getOptionalObjectType() &&
"optional wrap didn't form optional?!");
break;
case Kind::StoredProperty:
case Kind::GettableProperty:
Expand Down
4 changes: 2 additions & 2 deletions include/swift/SIL/SILType.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ class SILType {
bool isBlockPointerCompatible() const {
// Look through one level of optionality.
SILType ty = *this;
if (auto optPayload = ty.getAnyOptionalObjectType()) {
if (auto optPayload = ty.getOptionalObjectType()) {
ty = optPayload;
}

Expand Down Expand Up @@ -481,7 +481,7 @@ class SILType {

/// Returns the lowered type for T if this type is Optional<T>;
/// otherwise, return the null type.
SILType getAnyOptionalObjectType() const;
SILType getOptionalObjectType() const;

/// Unwraps one level of optional type.
/// Returns the lowered T if the given type is Optional<T>.
Expand Down
19 changes: 3 additions & 16 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ FOR_KNOWN_FOUNDATION_TYPES(CACHE_FOUNDATION_DECL)
llvm::DenseMap<Type, ArraySliceType*> ArraySliceTypes;
llvm::DenseMap<std::pair<Type, Type>, DictionaryType *> DictionaryTypes;
llvm::DenseMap<Type, OptionalType*> OptionalTypes;
llvm::DenseMap<Type, ImplicitlyUnwrappedOptionalType*> ImplicitlyUnwrappedOptionalTypes;
llvm::DenseMap<std::pair<Type, unsigned>, ParenType*> ParenTypes;
llvm::DenseMap<uintptr_t, ReferenceStorageType*> ReferenceStorageTypes;
llvm::DenseMap<Type, LValueType*> LValueTypes;
Expand Down Expand Up @@ -2120,7 +2119,6 @@ size_t ASTContext::Implementation::Arena::getTotalMemory() const {
llvm::capacity_in_bytes(ArraySliceTypes) +
llvm::capacity_in_bytes(DictionaryTypes) +
llvm::capacity_in_bytes(OptionalTypes) +
llvm::capacity_in_bytes(ImplicitlyUnwrappedOptionalTypes) +
llvm::capacity_in_bytes(ParenTypes) +
llvm::capacity_in_bytes(ReferenceStorageTypes) +
llvm::capacity_in_bytes(LValueTypes) +
Expand Down Expand Up @@ -3440,7 +3438,7 @@ ReferenceStorageType *ReferenceStorageType::get(Type T, Ownership ownership,
return entry = new (C, arena) UnownedStorageType(
T, T->isCanonical() ? &C : nullptr, properties);
case Ownership::Weak:
assert(T->getAnyOptionalObjectType() &&
assert(T->getOptionalObjectType() &&
"object of weak storage type is not optional");
return entry = new (C, arena)
WeakStorageType(T, T->isCanonical() ? &C : nullptr, properties);
Expand Down Expand Up @@ -4033,7 +4031,8 @@ Type OptionalType::get(OptionalTypeKind which, Type valueType) {
// OTK_None if we made code more convenient to write.
case OTK_None: llvm_unreachable("building a non-optional type!");
case OTK_Optional: return OptionalType::get(valueType);
case OTK_ImplicitlyUnwrappedOptional: return ImplicitlyUnwrappedOptionalType::get(valueType);
case OTK_ImplicitlyUnwrappedOptional:
llvm_unreachable("Should no longer have IUOs");
}
llvm_unreachable("bad optional type kind");
}
Expand All @@ -4050,18 +4049,6 @@ OptionalType *OptionalType::get(Type base) {
return entry = new (C, arena) OptionalType(C, base, properties);
}

ImplicitlyUnwrappedOptionalType *ImplicitlyUnwrappedOptionalType::get(Type base) {
auto properties = base->getRecursiveProperties();
auto arena = getArena(properties);

const ASTContext &C = base->getASTContext();

auto *&entry = C.Impl.getArena(arena).ImplicitlyUnwrappedOptionalTypes[base];
if (entry) return entry;

return entry = new (C, arena) ImplicitlyUnwrappedOptionalType(C, base, properties);
}

ProtocolType *ProtocolType::get(ProtocolDecl *D, Type Parent,
const ASTContext &C) {
llvm::FoldingSetNodeID id;
Expand Down
7 changes: 0 additions & 7 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3222,13 +3222,6 @@ namespace {
OS << ")";
}

void visitImplicitlyUnwrappedOptionalType(
ImplicitlyUnwrappedOptionalType *T, StringRef label) {
printCommon(label, "implicitly_unwrapped_optional_type");
printRec(T->getBaseType());
OS << ")";
}

void visitDictionaryType(DictionaryType *T, StringRef label) {
printCommon(label, "dictionary_type");
printRec("key", T->getKeyType());
Expand Down
4 changes: 0 additions & 4 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,10 +754,6 @@ void ASTMangler::appendType(Type type) {
case TypeKind::Dictionary:
return appendSugaredType<SyntaxSugarType>(type);

case TypeKind::ImplicitlyUnwrappedOptional: {
llvm_unreachable("Should no longer have IUOs");
}

case TypeKind::ExistentialMetatype: {
ExistentialMetatypeType *EMT = cast<ExistentialMetatypeType>(tybase);
appendType(EMT->getInstanceType());
Expand Down
14 changes: 0 additions & 14 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3785,20 +3785,6 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
Printer << "?";
}

void visitImplicitlyUnwrappedOptionalType(ImplicitlyUnwrappedOptionalType *T) {
auto printAsIUO = Options.PrintOptionalAsImplicitlyUnwrapped;

// Printing optionals with a trailing '!' applies only to
// top-level optionals, not to any nested within.
const_cast<PrintOptions &>(Options).PrintOptionalAsImplicitlyUnwrapped =
false;
printWithParensIfNotSimple(T->getBaseType());
const_cast<PrintOptions &>(Options).PrintOptionalAsImplicitlyUnwrapped =
printAsIUO;

Printer << "!";
}

void visitProtocolType(ProtocolType *T) {
printTypeDeclName(T);
}
Expand Down
22 changes: 12 additions & 10 deletions lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1686,10 +1686,10 @@ class Verifier : public ASTWalker {
Expr *E) {
auto optionalRVType = optionalType->getRValueType();
auto objectRVType = objectType->getRValueType();
checkSameType(objectRVType, optionalRVType->getAnyOptionalObjectType(),

checkSameType(objectRVType, optionalRVType->getOptionalObjectType(),
"optional object type");

if (objectType->is<LValueType>() != optionalType->is<LValueType>()) {
Out << "optional operation must preserve lvalue-ness of base\n";
E->print(Out);
Expand Down Expand Up @@ -1856,7 +1856,7 @@ class Verifier : public ASTWalker {
PrettyStackTraceExpr debugStack(Ctx, "verifying InjectIntoOptionalExpr",
E);

auto valueType = E->getType()->getAnyOptionalObjectType();
auto valueType = E->getType()->getOptionalObjectType();
if (!valueType) {
Out << "InjectIntoOptionalExpr is not of Optional type";
abort();
Expand Down Expand Up @@ -2320,7 +2320,7 @@ class Verifier : public ASTWalker {

// FIXME: Update to look for plain Optional once
// ImplicitlyUnwrappedOptional is removed
if (!varTy->getAnyOptionalObjectType()) {
if (!varTy->getOptionalObjectType()) {
Out << "implicitly unwrapped optional attribute should only be set on VarDecl "
"with optional type\n";
abort();
Expand Down Expand Up @@ -2688,7 +2688,7 @@ class Verifier : public ASTWalker {
CD->getDeclContext()->getDeclaredInterfaceType()->getAnyNominal() !=
Ctx.getOptionalDecl()) {
OptionalTypeKind resultOptionality = OTK_None;
CD->getResultInterfaceType()->getAnyOptionalObjectType(resultOptionality);
CD->getResultInterfaceType()->getOptionalObjectType(resultOptionality);
auto declOptionality = CD->getFailability();

if ((resultOptionality != OTK_None || declOptionality != OTK_None) &&
Expand All @@ -2702,8 +2702,10 @@ class Verifier : public ASTWalker {
if (auto genericFn
= CD->getInterfaceType()->getAs<GenericFunctionType>()) {
resultOptionality = OTK_None;
genericFn->getResult()->castTo<AnyFunctionType>()->getResult()
->getAnyOptionalObjectType(resultOptionality);
genericFn->getResult()
->castTo<AnyFunctionType>()
->getResult()
->getOptionalObjectType(resultOptionality);
if ((resultOptionality != OTK_None || declOptionality != OTK_None) &&
(resultOptionality == OTK_None || declOptionality == OTK_None)) {
Out << "Initializer has result optionality/failability mismatch\n";
Expand Down Expand Up @@ -2732,7 +2734,7 @@ class Verifier : public ASTWalker {

// FIXME: Update to look for plain Optional once
// ImplicitlyUnwrappedOptional is removed
if (!resultTy->getAnyOptionalObjectType()) {
if (!resultTy->getOptionalObjectType()) {
Out << "implicitly unwrapped optional attribute should only be set "
"on constructors with optional return types\n";
CD->dump(llvm::errs());
Expand Down Expand Up @@ -2900,7 +2902,7 @@ class Verifier : public ASTWalker {

// FIXME: Update to look for plain Optional once
// ImplicitlyUnwrappedOptional is removed
if (!resultTy->getAnyOptionalObjectType()) {
if (!resultTy->getOptionalObjectType()) {
Out << "implicitly unwrapped optional attribute should only be set "
"on functions with optional return types\n";
abort();
Expand Down
19 changes: 7 additions & 12 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,7 @@ bool PatternBindingDecl::isDefaultInitializable(unsigned i) const {
if (const auto *varDecl = typedPattern->getSingleVar())
// Lazy storage is never user accessible.
if (!varDecl->isUserAccessible())
if (typedPattern->getTypeLoc().getType()->getAnyOptionalObjectType())
if (typedPattern->getTypeLoc().getType()->getOptionalObjectType())
return true;
}
}
Expand Down Expand Up @@ -1741,10 +1741,10 @@ static Type mapSignatureFunctionType(ASTContext &ctx, Type type,
if (isInitializer) {
if (auto inOutTy = type->getAs<InOutType>()) {
if (auto objectType =
inOutTy->getObjectType()->getAnyOptionalObjectType()) {
inOutTy->getObjectType()->getOptionalObjectType()) {
type = InOutType::get(objectType);
}
} else if (auto objectType = type->getAnyOptionalObjectType()) {
} else if (auto objectType = type->getOptionalObjectType()) {
type = objectType;
}
}
Expand Down Expand Up @@ -2478,13 +2478,8 @@ void NominalTypeDecl::addExtension(ExtensionDecl *extension) {
LastExtension = extension;
}

OptionalTypeKind NominalTypeDecl::classifyAsOptionalType() const {
const ASTContext &ctx = getASTContext();
if (this == ctx.getOptionalDecl()) {
return OTK_Optional;
} else {
return OTK_None;
}
bool NominalTypeDecl::isOptionalDecl() const {
return this == getASTContext().getOptionalDecl();
}

GenericTypeDecl::GenericTypeDecl(DeclKind K, DeclContext *DC,
Expand Down Expand Up @@ -3273,7 +3268,7 @@ findProtocolSelfReferences(const ProtocolDecl *proto, Type type,
}

// Optionals preserve variance.
if (auto optType = type->getAnyOptionalObjectType()) {
if (auto optType = type->getOptionalObjectType()) {
return findProtocolSelfReferences(proto, optType,
skipAssocTypes);
}
Expand Down Expand Up @@ -4559,7 +4554,7 @@ ObjCSubscriptKind SubscriptDecl::getObjCSubscriptKind(

// If the index type is an object type in Objective-C, we have a
// keyed subscript.
if (Type objectTy = indexTy->getAnyOptionalObjectType())
if (Type objectTy = indexTy->getOptionalObjectType())
indexTy = objectTy;

return ObjCSubscriptKind::Keyed;
Expand Down
Loading