Skip to content

Commit 6cee193

Browse files
committed
[Type System] When explicit existential types are enabled, wrap Error
in ExistentialType for the type of error values.
1 parent efeb709 commit 6cee193

20 files changed

+56
-43
lines changed

include/swift/AST/ASTContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ class ASTContext final {
532532
/// Retrieve the declaration of Swift.Error.
533533
ProtocolDecl *getErrorDecl() const;
534534
CanType getExceptionType() const;
535+
CanType getErrorExistentialType() const;
535536

536537
#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
537538
/** Retrieve the declaration of Swift.NAME. */ \

include/swift/AST/ASTSynthesis.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ inline Type synthesizeType(SynthesisContext &SC,
5656
switch (kind) {
5757
case _any: return SC.Context.TheAnyType;
5858
case _bridgeObject: return SC.Context.TheBridgeObjectType;
59-
case _error: return SC.Context.getExceptionType();
59+
case _error: return SC.Context.getErrorExistentialType();
6060
case _executor: return SC.Context.TheExecutorType;
6161
case _job: return SC.Context.TheJobType;
6262
case _nativeObject: return SC.Context.TheNativeObjectType;

lib/AST/ASTContext.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,16 @@ ProtocolDecl *ASTContext::getErrorDecl() const {
860860
return getProtocol(KnownProtocolKind::Error);
861861
}
862862

863+
CanType ASTContext::getErrorExistentialType() const {
864+
Type errorType = getExceptionType();
865+
if (LangOpts.EnableExplicitExistentialTypes &&
866+
errorType->isConstraintType()) {
867+
errorType = ExistentialType::get(errorType);
868+
}
869+
870+
return errorType->getCanonicalType();
871+
}
872+
863873
EnumElementDecl *ASTContext::getOptionalSomeDecl() const {
864874
if (!getImpl().OptionalSomeDecl)
865875
getImpl().OptionalSomeDecl = getOptionalDecl()->getUniqueElement(/*hasVal*/true);
@@ -4865,7 +4875,7 @@ Type ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,
48654875
if (auto nsErrorTy = getNSErrorType()) {
48664876
// The corresponding value type is Error.
48674877
if (bridgedValueType)
4868-
*bridgedValueType = getErrorDecl()->getDeclaredInterfaceType();
4878+
*bridgedValueType = getErrorExistentialType();
48694879

48704880
return nsErrorTy;
48714881
}
@@ -4902,7 +4912,7 @@ Type ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,
49024912
if (findConformance(KnownProtocolKind::Error)) {
49034913
// The corresponding value type is Error.
49044914
if (bridgedValueType)
4905-
*bridgedValueType = getErrorDecl()->getDeclaredInterfaceType();
4915+
*bridgedValueType = getErrorExistentialType();
49064916

49074917
// Bridge to NSError.
49084918
if (auto nsErrorTy = getNSErrorType())

lib/AST/ASTVerifier.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3422,11 +3422,12 @@ class Verifier : public ASTWalker {
34223422
}
34233423

34243424
Type checkExceptionTypeExists(const char *where) {
3425-
auto exn = Ctx.getErrorDecl();
3426-
if (exn) return exn->getDeclaredInterfaceType();
3425+
if (!Ctx.getErrorDecl()) {
3426+
Out << "exception type does not exist in " << where << "\n";
3427+
abort();
3428+
}
34273429

3428-
Out << "exception type does not exist in " << where << "\n";
3429-
abort();
3430+
return Ctx.getErrorExistentialType();
34303431
}
34313432

34323433
bool isGoodSourceRange(SourceRange SR) {

lib/IDE/Refactoring.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7831,7 +7831,8 @@ class AsyncConverter : private SourceEntityWalker {
78317831
const AsyncHandlerDesc &HandlerDesc) {
78327832
// If the error type is already Error, we can pass it as-is.
78337833
auto ErrorType = *HandlerDesc.getErrorType();
7834-
if (ErrorType->getCanonicalType() == getASTContext().getExceptionType()) {
7834+
if (ErrorType->getCanonicalType() ==
7835+
getASTContext().getErrorExistentialType()) {
78357836
OS << ErrorName;
78367837
return;
78377838
}

lib/IRGen/GenBuiltin.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -592,10 +592,9 @@ if (Builtin.ID == BuiltinValueKind::id) { \
592592

593593
auto *fn = cast<llvm::Function>(IGF.IGM.getWillThrowFn());
594594
auto error = args.claimNext();
595+
auto errorTy = IGF.IGM.Context.getErrorExistentialType();
595596
auto errorBuffer = IGF.getCalleeErrorResultSlot(
596-
SILType::getPrimitiveObjectType(IGF.IGM.Context.getErrorDecl()
597-
->getDeclaredInterfaceType()
598-
->getCanonicalType()));
597+
SILType::getPrimitiveObjectType(errorTy));
599598
IGF.Builder.CreateStore(error, errorBuffer);
600599

601600
auto context = llvm::UndefValue::get(IGF.IGM.Int8PtrTy);

lib/SIL/IR/SILType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ CanArchetypeType swift::getOpenedArchetypeOf(CanType Ty) {
4545
}
4646

4747
SILType SILType::getExceptionType(const ASTContext &C) {
48-
return SILType::getPrimitiveObjectType(C.getExceptionType());
48+
return SILType::getPrimitiveObjectType(C.getErrorExistentialType());
4949
}
5050

5151
SILType SILType::getNativeObjectType(const ASTContext &C) {

lib/SIL/Utils/BasicBlockUtils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ void swift::getEdgeArgs(TermInst *T, unsigned edgeIdx, SILBasicBlock *newEdgeBB,
144144
case 1: {
145145
assert(AACI->getErrorBB());
146146
auto &C = AACI->getFunction()->getASTContext();
147-
auto errorTy = C.getErrorDecl()->getDeclaredType();
148-
auto errorSILTy = SILType::getPrimitiveObjectType(errorTy->getCanonicalType());
147+
auto errorTy = C.getErrorExistentialType();
148+
auto errorSILTy = SILType::getPrimitiveObjectType(errorTy);
149149
// error BB. this takes the error value argument
150150
args.push_back(
151151
newEdgeBB->createPhiArgument(errorSILTy, OwnershipKind::Owned));

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5252,7 +5252,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
52525252
require(AACI->getErrorBB()->getNumArguments() == 1,
52535253
"error successor must take one argument");
52545254
auto arg = AACI->getErrorBB()->getArgument(0);
5255-
auto errorType = C.getErrorDecl()->getDeclaredType()->getCanonicalType();
5255+
auto errorType = C.getErrorExistentialType();
52565256
requireSameType(arg->getType(),
52575257
SILType::getPrimitiveObjectType(errorType),
52585258
"error successor argument must have Error type");

lib/SILGen/ResultPlan.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ class ForeignAsyncInitializationPlan final : public ResultPlan {
508508
auto continuationDecl = SGF.getASTContext().getUnsafeContinuationDecl();
509509

510510
auto errorTy = throws
511-
? SGF.getASTContext().getExceptionType()
511+
? SGF.getASTContext().getErrorExistentialType()
512512
: SGF.getASTContext().getNeverType();
513513
auto continuationTy = BoundGenericType::get(continuationDecl, Type(),
514514
{ calleeTypeInfo.substResultType, errorTy })
@@ -618,7 +618,7 @@ class ForeignAsyncInitializationPlan final : public ResultPlan {
618618

619619
auto continuationDecl = SGF.getASTContext().getUnsafeContinuationDecl();
620620

621-
auto errorTy = SGF.getASTContext().getExceptionType();
621+
auto errorTy = SGF.getASTContext().getErrorExistentialType();
622622
auto continuationBGT =
623623
BoundGenericType::get(continuationDecl, Type(),
624624
{calleeTypeInfo.substResultType, errorTy});
@@ -666,9 +666,8 @@ class ForeignAsyncInitializationPlan final : public ResultPlan {
666666
SGF.B.emitBlock(errorBlock);
667667

668668
Scope errorScope(SGF, loc);
669-
670-
auto errorTy = SGF.getASTContext().getErrorDecl()->getDeclaredType()
671-
->getCanonicalType();
669+
670+
auto errorTy = SGF.getASTContext().getErrorExistentialType();
672671
auto errorVal = SGF.B.createTermResult(
673672
SILType::getPrimitiveObjectType(errorTy), OwnershipKind::Owned);
674673

0 commit comments

Comments
 (0)