Skip to content

Commit 3994a1d

Browse files
committed
[TypeChecker] NFC: Extract array/set element cast checking into a closure
1 parent a99f24f commit 3994a1d

File tree

1 file changed

+25
-36
lines changed

1 file changed

+25
-36
lines changed

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4075,27 +4075,32 @@ CheckedCastKind TypeChecker::typeCheckCheckedCast(Type fromType,
40754075
}
40764076
}
40774077

4078-
// Check for casts between specific concrete types that cannot succeed.
4079-
if (auto toElementType = ConstraintSystem::isArrayType(toType)) {
4080-
if (auto fromElementType = ConstraintSystem::isArrayType(fromType)) {
4081-
switch (typeCheckCheckedCast(*fromElementType, *toElementType,
4082-
CheckedCastContextKind::None, dc,
4083-
SourceLoc(), nullptr, SourceRange())) {
4084-
case CheckedCastKind::Coercion:
4085-
return CheckedCastKind::Coercion;
4078+
auto checkElementCast = [&](Type fromElt, Type toElt,
4079+
CheckedCastKind castKind) -> CheckedCastKind {
4080+
switch (typeCheckCheckedCast(fromElt, toElt, CheckedCastContextKind::None,
4081+
dc, SourceLoc(), nullptr, SourceRange())) {
4082+
case CheckedCastKind::Coercion:
4083+
return CheckedCastKind::Coercion;
40864084

4087-
case CheckedCastKind::BridgingCoercion:
4088-
return CheckedCastKind::BridgingCoercion;
4085+
case CheckedCastKind::BridgingCoercion:
4086+
return CheckedCastKind::BridgingCoercion;
40894087

4090-
case CheckedCastKind::ArrayDowncast:
4091-
case CheckedCastKind::DictionaryDowncast:
4092-
case CheckedCastKind::SetDowncast:
4093-
case CheckedCastKind::ValueCast:
4094-
return CheckedCastKind::ArrayDowncast;
4088+
case CheckedCastKind::ArrayDowncast:
4089+
case CheckedCastKind::DictionaryDowncast:
4090+
case CheckedCastKind::SetDowncast:
4091+
case CheckedCastKind::ValueCast:
4092+
return castKind;
40954093

4096-
case CheckedCastKind::Unresolved:
4097-
return failed();
4098-
}
4094+
case CheckedCastKind::Unresolved:
4095+
return failed();
4096+
}
4097+
};
4098+
4099+
// Check for casts between specific concrete types that cannot succeed.
4100+
if (auto toElementType = ConstraintSystem::isArrayType(toType)) {
4101+
if (auto fromElementType = ConstraintSystem::isArrayType(fromType)) {
4102+
return checkElementCast(*fromElementType, *toElementType,
4103+
CheckedCastKind::ArrayDowncast);
40994104
}
41004105
}
41014106

@@ -4165,24 +4170,8 @@ CheckedCastKind TypeChecker::typeCheckCheckedCast(Type fromType,
41654170

41664171
if (auto toElementType = ConstraintSystem::isSetType(toType)) {
41674172
if (auto fromElementType = ConstraintSystem::isSetType(fromType)) {
4168-
switch (typeCheckCheckedCast(*fromElementType, *toElementType,
4169-
CheckedCastContextKind::None, dc,
4170-
SourceLoc(), nullptr, SourceRange())) {
4171-
case CheckedCastKind::Coercion:
4172-
return CheckedCastKind::Coercion;
4173-
4174-
case CheckedCastKind::BridgingCoercion:
4175-
return CheckedCastKind::BridgingCoercion;
4176-
4177-
case CheckedCastKind::ArrayDowncast:
4178-
case CheckedCastKind::DictionaryDowncast:
4179-
case CheckedCastKind::SetDowncast:
4180-
case CheckedCastKind::ValueCast:
4181-
return CheckedCastKind::SetDowncast;
4182-
4183-
case CheckedCastKind::Unresolved:
4184-
return failed();
4185-
}
4173+
return checkElementCast(*fromElementType, *toElementType,
4174+
CheckedCastKind::SetDowncast);
41864175
}
41874176
}
41884177

0 commit comments

Comments
 (0)