File tree Expand file tree Collapse file tree 2 files changed +26
-6
lines changed Expand file tree Collapse file tree 2 files changed +26
-6
lines changed Original file line number Diff line number Diff line change @@ -7467,13 +7467,20 @@ static ConstraintFix *maybeWarnAboutExtraneousCast(
7467
7467
// we need to store the difference as a signed integer.
7468
7468
int extraOptionals = fromOptionals.size() - toOptionals.size();
7469
7469
7470
- // "from" expression could be a type variable with value-to-optional
7471
- // restrictions that we have to account for optionality mismatch.
7470
+ // "from" expression could be a type variable wrapped in an optional e.g.
7471
+ // Optional<$T0>. So when that is the case we have to add this additional
7472
+ // optionality levels to from type.
7472
7473
const auto subExprType = cs.getType(castExpr->getSubExpr());
7473
- if (cs.hasConversionRestriction(fromType, subExprType,
7474
- ConversionRestrictionKind::ValueToOptional)) {
7475
- extraOptionals++;
7476
- origFromType = OptionalType::get(origFromType);
7474
+ if (subExprType->getOptionalObjectType()) {
7475
+ SmallVector<Type, 4> subExprOptionals;
7476
+ const auto unwrappedSubExprType =
7477
+ subExprType->lookThroughAllOptionalTypes(subExprOptionals);
7478
+ if (unwrappedSubExprType->is<TypeVariableType>()) {
7479
+ extraOptionals += subExprOptionals.size();
7480
+ for (size_t i = 0; i != subExprOptionals.size(); ++i) {
7481
+ origFromType = OptionalType::get(origFromType);
7482
+ }
7483
+ }
7477
7484
}
7478
7485
7479
7486
// Removing the optionality from to type when the force cast expr is an IUO.
Original file line number Diff line number Diff line change @@ -680,3 +680,16 @@ func test_SR_15562() {
680
680
// expected-error@-1{{left side of mutating operator has immutable type 'Int'}}
681
681
}
682
682
}
683
+
684
+ // SR-16058
685
+ extension Dictionary {
686
+ func SR16058( _: Key ) -> Value ? ? { nil }
687
+ }
688
+ func SR_16058_tests( ) {
689
+ let dict : [ Int : String ? ] = [ : ]
690
+ let foo : Int ? = 1
691
+ let _: String ? = foo. flatMap { dict [ $0] } as? String // OK
692
+
693
+ // More than one optionality wrapping
694
+ let _: String ? = foo. flatMap { dict. SR16058 ( _: $0) } as? String // OK
695
+ }
You can’t perform that action at this time.
0 commit comments