Skip to content

IUO: Generate Optional<T> rather than ImplicitlyUnwrappedOptional<T> #14299

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 3 commits into from
Feb 2, 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
14 changes: 12 additions & 2 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ class alignas(8) Expr {
NumVariadicArgs : 16
);

SWIFT_INLINE_BITFIELD(ForceValueExpr, Expr, 1,
ForcedIUO : 1
);

SWIFT_INLINE_BITFIELD(InOutToPointerExpr, ImplicitConversionExpr, 1,
IsNonAccessing : 1
);
Expand Down Expand Up @@ -2507,9 +2511,11 @@ class ForceValueExpr : public Expr {
SourceLoc ExclaimLoc;

public:
ForceValueExpr(Expr *subExpr, SourceLoc exclaimLoc)
ForceValueExpr(Expr *subExpr, SourceLoc exclaimLoc, bool forcedIUO = false)
: Expr(ExprKind::ForceValue, /*Implicit=*/exclaimLoc.isInvalid(), Type()),
SubExpr(subExpr), ExclaimLoc(exclaimLoc) {}
SubExpr(subExpr), ExclaimLoc(exclaimLoc) {
Bits.ForceValueExpr.ForcedIUO = forcedIUO;
}

SourceRange getSourceRange() const {
if (ExclaimLoc.isInvalid())
Expand All @@ -2534,6 +2540,10 @@ class ForceValueExpr : public Expr {
Expr *getSubExpr() const { return SubExpr; }
void setSubExpr(Expr *expr) { SubExpr = expr; }

bool isForceOfImplicitlyUnwrappedOptional() const {
return Bits.ForceValueExpr.ForcedIUO;
}

static bool classof(const Expr *E) {
return E->getKind() == ExprKind::ForceValue;
}
Expand Down
13 changes: 12 additions & 1 deletion include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,15 @@ class alignas(1 << TypeAlignInBits) TypeBase {
/// \p matchOptions.
bool matches(Type other, TypeMatchOptions matchOptions);

bool matchesParameter(Type other, TypeMatchOptions matchMode);

/// \brief Determines whether this function type is similar to \p
/// other as defined by \p matchOptions and the callback \p
/// paramsAndResultMatch which determines in a client-specific way
/// whether the parameters and result of the types match.
bool matchesFunctionType(Type other, TypeMatchOptions matchOptions,
std::function<bool()> paramsAndResultMatch);

/// \brief Determines whether this type has a retainable pointer
/// representation, i.e. whether it is representable as a single,
/// possibly nil pointer that can be unknown-retained and
Expand Down Expand Up @@ -4105,7 +4114,9 @@ class ImplicitlyUnwrappedOptionalType : public UnarySyntaxSugarType {
ImplicitlyUnwrappedOptionalType(const ASTContext &ctx, Type base,
RecursiveTypeProperties properties)
: UnarySyntaxSugarType(TypeKind::ImplicitlyUnwrappedOptional, ctx, base,
properties) {}
properties) {
//llvm_unreachable("ImplicitlyUnwrappedOptionalType::ImplicitlyUnwrappedOptionalType");
}

public:
/// Return a uniqued optional type with the specified base type.
Expand Down
13 changes: 2 additions & 11 deletions lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1069,17 +1069,8 @@ class Verifier : public ASTWalker {
Type srcObj = checkLValue(E->getSubExpr()->getType(),
"result of InOutExpr");
auto DestTy = E->getType()->castTo<InOutType>()->getObjectType();

// HACK: Allow differences in optionality of the source and
// result types. When IUO is gone from the type system we'll no
// longer need this.
auto srcOptObjTy = srcObj->getAnyOptionalObjectType();
auto dstOptObjTy = DestTy->getAnyOptionalObjectType();
if (srcOptObjTy && dstOptObjTy) {
checkSameType(srcOptObjTy, dstOptObjTy, "object types for InOutExpr");
} else {
checkSameType(DestTy, srcObj, "object types for InOutExpr");
}

checkSameType(DestTy, srcObj, "object types for InOutExpr");
verifyCheckedBase(E);
}

Expand Down
27 changes: 21 additions & 6 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2180,10 +2180,10 @@ namespace {
};
} // end anonymous namespace

static bool matchFunctionTypes(CanAnyFunctionType fn1, CanAnyFunctionType fn2,
TypeMatchOptions matchMode,
OptionalUnwrapping insideOptional,
std::function<bool()> paramsAndResultMatch) {
static bool matchesFunctionType(CanAnyFunctionType fn1, CanAnyFunctionType fn2,
TypeMatchOptions matchMode,
OptionalUnwrapping insideOptional,
std::function<bool()> paramsAndResultMatch) {
// FIXME: Handle generic functions in non-ABI matches.
if (!matchMode.contains(TypeMatchFlags::AllowABICompatible)) {
if (!isa<FunctionType>(fn1) || !isa<FunctionType>(fn2))
Expand Down Expand Up @@ -2301,8 +2301,8 @@ static bool matches(CanType t1, CanType t2, TypeMatchOptions matchMode,
OptionalUnwrapping::None));
};

return matchFunctionTypes(fn1, fn2, matchMode, insideOptional,
paramsAndResultMatch);
return matchesFunctionType(fn1, fn2, matchMode, insideOptional,
paramsAndResultMatch);
}

if (matchMode.contains(TypeMatchFlags::AllowNonOptionalForIUOParam) &&
Expand Down Expand Up @@ -2333,6 +2333,21 @@ bool TypeBase::matches(Type other, TypeMatchOptions matchMode) {
ParameterPosition::NotParameter, OptionalUnwrapping::None);
}

bool TypeBase::matchesParameter(Type other, TypeMatchOptions matchMode) {
return ::matches(getCanonicalType(), other->getCanonicalType(), matchMode,
ParameterPosition::Parameter, OptionalUnwrapping::None);
}

bool TypeBase::matchesFunctionType(Type other, TypeMatchOptions matchMode,
std::function<bool()> paramsAndResultMatch) {
auto thisFnTy = dyn_cast<AnyFunctionType>(getCanonicalType());
auto otherFnTy = dyn_cast<AnyFunctionType>(other->getCanonicalType());

assert(thisFnTy && otherFnTy);
return ::matchesFunctionType(thisFnTy, otherFnTy, matchMode,
OptionalUnwrapping::None, paramsAndResultMatch);
}

/// getNamedElementId - If this tuple has a field with the specified name,
/// return the field index, otherwise return -1.
int TupleType::getNamedElementId(Identifier I) const {
Expand Down
8 changes: 4 additions & 4 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1792,7 +1792,7 @@ static ImportedType rectifySubscriptTypes(Type getterType, bool getterIsIUO,

// Create an optional of the object type that can be implicitly
// unwrapped which subsumes both behaviors.
return {ImplicitlyUnwrappedOptionalType::get(setterType), true};
return {OptionalType::get(setterType), true};
}

/// Add an AvailableAttr to the declaration for the given
Expand Down Expand Up @@ -4073,7 +4073,7 @@ namespace {
nullability = translateNullability(*typeNullability);
}
if (nullability != OTK_None && !errorConvention.hasValue()) {
resultTy = OptionalType::get(nullability, resultTy);
resultTy = OptionalType::get(resultTy);
isIUO = nullability == OTK_ImplicitlyUnwrappedOptional;
}

Expand Down Expand Up @@ -6115,8 +6115,8 @@ ConstructorDecl *SwiftDeclConverter::importConstructor(

// Rebuild the function type with the appropriate result type;
Type resultTy = selfTy;
if (failability)
resultTy = OptionalType::get(failability, resultTy);
if (failability != OTK_None)
resultTy = OptionalType::get(resultTy);

type = FunctionType::get(oldFnType->getInput(), resultTy,
oldFnType->getExtInfo());
Expand Down
47 changes: 12 additions & 35 deletions lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,6 @@ namespace {
return OptKind;
}

/// Wrap a type in the Optional type appropriate to the import kind.
static Type getOptionalType(Type payloadType, ImportTypeKind kind,
OptionalTypeKind OptKind) {
OptKind = getOptionalKind(kind, OptKind);

if (OptKind == OTK_None)
return payloadType;

return OptionalType::get(OptKind, payloadType);
}

class SwiftTypeConverter :
public clang::TypeVisitor<SwiftTypeConverter, ImportResult>
{
Expand Down Expand Up @@ -1277,7 +1266,7 @@ static ImportedType adjustTypeForConcreteImport(
auto resultTy = boundGenericTy->getGenericArgs().front();
if (OTK != OTK_None) {
assert(OTK != OTK_ImplicitlyUnwrappedOptional);
resultTy = OptionalType::get(OTK, resultTy);
resultTy = OptionalType::get(resultTy);
}

StringRef pointerName;
Expand Down Expand Up @@ -1398,14 +1387,6 @@ static ImportedType adjustTypeForConcreteImport(
return {importedType, isIUO};
}

static Type adjustOptionality(ImportedType importedType) {
if (!importedType.isImplicitlyUnwrapped())
return importedType.getType();

return ImplicitlyUnwrappedOptionalType::get(
importedType.getType()->getOptionalObjectType());
}

ImportedType ClangImporter::Implementation::importType(
clang::QualType type, ImportTypeKind importKind, bool allowNSUIntegerAsInt,
Bridgeability bridging, OptionalTypeKind optionality,
Expand Down Expand Up @@ -1460,10 +1441,7 @@ ImportedType ClangImporter::Implementation::importType(
assert(!adjustedType ||
!adjustedType.getType()->getImplicitlyUnwrappedOptionalObjectType());

// Make an IUO type based on isIUO. This will be removed when IUOs
// are removed from the type system.
return {adjustOptionality(adjustedType),
adjustedType.isImplicitlyUnwrapped()};
return adjustedType;
}

Type ClangImporter::Implementation::importTypeIgnoreIUO(
Expand All @@ -1474,12 +1452,8 @@ Type ClangImporter::Implementation::importTypeIgnoreIUO(
auto importedType = importType(type, importKind, allowNSUIntegerAsInt,
bridging, optionality, resugarNSErrorPointer);

// We allow IUO types to be returned at the moment. At some point we
// will never generate them and this can be removed.
assert(!importedType || importedType.isImplicitlyUnwrapped() ==
!importedType.getType()
->getImplicitlyUnwrappedOptionalObjectType()
.isNull());
assert(!importedType ||
!importedType.getType()->getImplicitlyUnwrappedOptionalObjectType());

return importedType.getType();
}
Expand Down Expand Up @@ -1558,7 +1532,7 @@ static Type applyNoEscape(Type type) {
// Recurse into optional types.
OptionalTypeKind optKind;
if (Type objectType = type->getAnyOptionalObjectType(optKind)) {
return OptionalType::get(optKind, applyNoEscape(objectType));
return OptionalType::get(applyNoEscape(objectType));
}

// Apply @noescape to function types.
Expand Down Expand Up @@ -2013,7 +1987,7 @@ ImportedType ClangImporter::Implementation::importMethodType(
if (nonOptionalTy->isAnyClassReferenceType()) {
swiftResultTy = getUnmanagedType(*this, nonOptionalTy);
if (OptionalityOfReturn != OTK_None)
swiftResultTy = OptionalType::get(OptionalityOfReturn, swiftResultTy);
swiftResultTy = OptionalType::get(swiftResultTy);
}
}

Expand Down Expand Up @@ -2085,11 +2059,14 @@ ImportedType ClangImporter::Implementation::importMethodType(
bool paramIsIUO;
if (kind == SpecialMethodKind::NSDictionarySubscriptGetter &&
paramTy->isObjCIdType()) {
swiftParamTy = getOptionalType(getNSCopyingType(),
ImportTypeKind::Parameter,
optionalityOfParam);
auto optKind =
getOptionalKind(ImportTypeKind::Parameter, optionalityOfParam);

if (optKind == OTK_None)
swiftParamTy = getNSCopyingType();
else
swiftParamTy = OptionalType::get(getNSCopyingType());

paramIsIUO = optKind == OTK_ImplicitlyUnwrappedOptional;
} else {
ImportTypeKind importKind = ImportTypeKind::Parameter;
Expand Down
5 changes: 2 additions & 3 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1944,9 +1944,6 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
suffix = "!";
}

// FIXME: This goes away when IUOs are removed from the type system.
assert(T->getReferenceStorageReferent()->getImplicitlyUnwrappedOptionalObjectType());

Type ObjectType =
T->getReferenceStorageReferent()->getAnyOptionalObjectType();

Expand Down Expand Up @@ -3188,6 +3185,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
}
}
} else if (Type Unwrapped = ExprType->getImplicitlyUnwrappedOptionalObjectType()) {
// FIXME: This can go away when IUOs have been removed from the type
// system.
lookupVisibleMemberDecls(*this, Unwrapped, CurrDeclContext,
TypeResolver.get(),
IncludeInstanceMembers);
Expand Down
3 changes: 1 addition & 2 deletions lib/SILGen/SILGenConvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1153,8 +1153,7 @@ Optional<Conversion> Conversion::adjustForInitialForceValue() const {

case BridgeToObjC: {
auto sourceOptType =
ImplicitlyUnwrappedOptionalType::get(getBridgingSourceType())
->getCanonicalType();
OptionalType::get(getBridgingSourceType())->getCanonicalType();
return Conversion::getBridging(ForceAndBridgeToObjC,
sourceOptType,
getBridgingResultType(),
Expand Down
3 changes: 1 addition & 2 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5066,8 +5066,7 @@ RValue RValueEmitter::emitForceValue(ForceValueExpr *loc, Expr *E,
// If this is an implicit force of an ImplicitlyUnwrappedOptional,
// and we're emitting into an unbridging conversion, try adjusting the
// context.
if (loc->isImplicit() &&
E->getType()->getImplicitlyUnwrappedOptionalObjectType()) {
if (loc->isImplicit() && loc->isForceOfImplicitlyUnwrappedOptional()) {
if (auto conv = C.getAsConversion()) {
if (auto adjusted = conv->getConversion().adjustForInitialForceValue()) {
auto value =
Expand Down
7 changes: 4 additions & 3 deletions lib/SILGen/SILGenPoly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,10 @@ ManagedValue Transform::transform(ManagedValue v,
});
}

// If the value is IUO, but the desired formal type isn't optional, force it.
if (inputOTK == OTK_ImplicitlyUnwrappedOptional
&& outputOTK == OTK_None) {
// If the value is an optional, but the desired formal type isn't an
// optional or Any, force it.
if (inputOTK != OTK_None && outputOTK == OTK_None
&& !outputSubstType->isExistentialType()) {
v = SGF.emitCheckedGetOptionalValueFrom(Loc, v,
SGF.getTypeLowering(v.getType()),
SGFContext());
Expand Down
Loading