Skip to content

Commit e446ae7

Browse files
Suyash SrijanSuyash Srijan
authored andcommitted
[typechecker] if we're casting to a nominal type that's a protocol, then skip the check
1 parent f7837c1 commit e446ae7

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4055,13 +4055,14 @@ CheckedCastKind TypeChecker::typeCheckCheckedCast(Type fromType,
40554055
// }
40564056
if (fromExistential) {
40574057
if (auto NTD = toType->getAnyNominal()) {
4058-
4059-
auto protocolDecl =
4060-
dyn_cast_or_null<ProtocolDecl>(fromType->getAnyNominal());
4061-
if (protocolDecl &&
4062-
!conformsToProtocol(toType, protocolDecl, dc,
4063-
ConformanceCheckFlags::InExpression)) {
4064-
return failed();
4058+
if (!isa<ProtocolDecl>(NTD)) {
4059+
auto protocolDecl =
4060+
dyn_cast_or_null<ProtocolDecl>(fromType->getAnyNominal());
4061+
if (protocolDecl &&
4062+
!conformsToProtocol(toType, protocolDecl, dc,
4063+
ConformanceCheckFlags::InExpression)) {
4064+
return failed();
4065+
}
40654066
}
40664067
}
40674068
}

test/Constraints/casts.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ func objc_protocol_casts(_ op1: ObjCProto1, opn: NonObjCProto) {
153153
_ = ObjCClass() as! ObjCProto1 & NonObjCProto
154154

155155
_ = op1 as! ObjCProto1 & ObjCProto2
156-
_ = op1 as! ObjCProto2 // expected-warning {{cast from 'ObjCProto1' to unrelated type 'ObjCProto2' always fails}}
156+
_ = op1 as! ObjCProto2
157157
_ = op1 as! ObjCProto1 & NonObjCProto
158-
_ = opn as! ObjCProto1 // expected-warning {{cast from 'NonObjCProto' to unrelated type 'ObjCProto1' always fails}}
158+
_ = opn as! ObjCProto1
159159

160160
_ = NonObjCClass() as! ObjCProto1
161161
}

0 commit comments

Comments
 (0)