@@ -1193,37 +1193,6 @@ static bool isStringCompatiblePointerBaseType(TypeChecker &TC,
1193
1193
return false ;
1194
1194
}
1195
1195
1196
- static Optional<ConversionRestrictionKind>
1197
- selectOptionalConversionRestriction (Type type1, Type type2,
1198
- ConstraintKind kind) {
1199
- OptionalTypeKind optionalKind1 = OTK_None;
1200
- if (auto boundGeneric1 = type1->getAs <BoundGenericType>())
1201
- optionalKind1 = boundGeneric1->getDecl ()->classifyAsOptionalType ();
1202
-
1203
- OptionalTypeKind optionalKind2 = OTK_None;
1204
- if (auto boundGeneric2 = type2->getAs <BoundGenericType>())
1205
- optionalKind2 = boundGeneric2->getDecl ()->classifyAsOptionalType ();
1206
-
1207
- if (optionalKind2 == OTK_None) {
1208
- if (optionalKind1 == OTK_ImplicitlyUnwrappedOptional &&
1209
- kind >= ConstraintKind::Conversion)
1210
- return ConversionRestrictionKind::ForceUnchecked;
1211
-
1212
- return None;
1213
- }
1214
-
1215
- if (optionalKind1 == OTK_None)
1216
- return ConversionRestrictionKind::ValueToOptional;
1217
-
1218
- if (optionalKind1 == OTK_Optional &&
1219
- optionalKind2 == OTK_ImplicitlyUnwrappedOptional &&
1220
- kind < ConstraintKind::Conversion)
1221
- return None;
1222
-
1223
- return ConversionRestrictionKind::OptionalToOptional;
1224
- }
1225
-
1226
-
1227
1196
ConstraintSystem::SolutionKind
1228
1197
ConstraintSystem::matchTypes (Type type1, Type type2, ConstraintKind kind,
1229
1198
TypeMatchOptions flags,
@@ -2016,15 +1985,57 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
2016
1985
}
2017
1986
}
2018
1987
2019
- // A value of type T! can be converted to type U if T is convertible
2020
- // to U by force-unwrapping the source value.
2021
- // A value of type T, T?, or T! can be converted to type U? or U! if
2022
- // T is convertible to U.
2023
- if (concrete && kind >= ConstraintKind::Subtype)
2024
- if (auto restriction =
2025
- selectOptionalConversionRestriction (type1, type2, kind))
2026
- conversionsOrFixes.push_back (restriction.getValue ());
1988
+ // A value of type T can be converted to type U? if T is convertible to U.
1989
+ // A value of type T? can be converted to type U? if T is convertible to U.
1990
+ // The above conversions also apply to implicitly unwrapped optional types,
1991
+ // except that there is no implicit conversion from T? to T!.
1992
+ {
1993
+ BoundGenericType *boundGenericType2;
1994
+
1995
+ if (concrete && kind >= ConstraintKind::Subtype &&
1996
+ (boundGenericType2 = type2->getAs <BoundGenericType>())) {
1997
+ auto decl2 = boundGenericType2->getDecl ();
1998
+ if (auto optionalKind2 = decl2->classifyAsOptionalType ()) {
1999
+ assert (boundGenericType2->getGenericArgs ().size () == 1 );
2000
+
2001
+ BoundGenericType *boundGenericType1 = type1->getAs <BoundGenericType>();
2002
+ if (boundGenericType1) {
2003
+ auto decl1 = boundGenericType1->getDecl ();
2004
+ if (decl1 == decl2) {
2005
+ assert (boundGenericType1->getGenericArgs ().size () == 1 );
2006
+ conversionsOrFixes.push_back (
2007
+ ConversionRestrictionKind::OptionalToOptional);
2008
+ } else if (optionalKind2 == OTK_Optional &&
2009
+ decl1 == TC.Context .getImplicitlyUnwrappedOptionalDecl ()) {
2010
+ assert (boundGenericType1->getGenericArgs ().size () == 1 );
2011
+ conversionsOrFixes.push_back (
2012
+ ConversionRestrictionKind::OptionalToOptional);
2013
+ } else if (optionalKind2 == OTK_ImplicitlyUnwrappedOptional &&
2014
+ kind >= ConstraintKind::Conversion &&
2015
+ decl1 == TC.Context .getOptionalDecl ()) {
2016
+ assert (boundGenericType1->getGenericArgs ().size () == 1 );
2017
+ conversionsOrFixes.push_back (
2018
+ ConversionRestrictionKind::OptionalToOptional);
2019
+ }
2020
+ }
2021
+
2022
+ conversionsOrFixes.push_back (
2023
+ ConversionRestrictionKind::ValueToOptional);
2024
+ }
2025
+ }
2026
+ }
2027
2027
2028
+ // A value of type T! can be (unsafely) forced to U if T
2029
+ // is convertible to U.
2030
+ {
2031
+ Type objectType1;
2032
+ if (concrete && kind >= ConstraintKind::Conversion &&
2033
+ (objectType1 = lookThroughImplicitlyUnwrappedOptionalType (type1))) {
2034
+ conversionsOrFixes.push_back (
2035
+ ConversionRestrictionKind::ForceUnchecked);
2036
+ }
2037
+ }
2038
+
2028
2039
// Allow '() -> T' to '() -> ()' and '() -> Never' to '() -> T' for closure
2029
2040
// literals.
2030
2041
if (auto elt = locator.last ()) {
0 commit comments