Merge pull request #42050 from LucianoPAlmeida/SR-16058-casts

[SR-16058][Sema] Consider wrapping type variable layers of optionality when warning about checked casts
This commit is contained in:
Luciano Almeida
2022-03-30 23:44:54 -03:00
committed by GitHub
2 changed files with 26 additions and 6 deletions

View File

@@ -7501,13 +7501,20 @@ static ConstraintFix *maybeWarnAboutExtraneousCast(
// we need to store the difference as a signed integer.
int extraOptionals = fromOptionals.size() - toOptionals.size();
// "from" expression could be a type variable with value-to-optional
// restrictions that we have to account for optionality mismatch.
// "from" expression could be a type variable wrapped in an optional e.g.
// Optional<$T0>. So when that is the case we have to add this additional
// optionality levels to from type.
const auto subExprType = cs.getType(castExpr->getSubExpr());
if (cs.hasConversionRestriction(fromType, subExprType,
ConversionRestrictionKind::ValueToOptional)) {
extraOptionals++;
origFromType = OptionalType::get(origFromType);
if (subExprType->getOptionalObjectType()) {
SmallVector<Type, 4> subExprOptionals;
const auto unwrappedSubExprType =
subExprType->lookThroughAllOptionalTypes(subExprOptionals);
if (unwrappedSubExprType->is<TypeVariableType>()) {
extraOptionals += subExprOptionals.size();
for (size_t i = 0; i != subExprOptionals.size(); ++i) {
origFromType = OptionalType::get(origFromType);
}
}
}
// Removing the optionality from to type when the force cast expr is an IUO.