@@ -1193,48 +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 llvm::None;
1213
- }
1214
-
1215
- if (optionalKind1 == optionalKind2)
1216
- return ConversionRestrictionKind::OptionalToOptional;
1217
-
1218
- if (optionalKind1 == OTK_None)
1219
- return ConversionRestrictionKind::ValueToOptional;
1220
-
1221
- if (optionalKind1 == OTK_Optional) {
1222
- if (kind >= ConstraintKind::Conversion)
1223
- return ConversionRestrictionKind::OptionalToImplicitlyUnwrappedOptional;
1224
-
1225
- assert (optionalKind2 == OTK_ImplicitlyUnwrappedOptional &&
1226
- " Result has unexpected optional kind!" );
1227
-
1228
- return llvm::None;
1229
- }
1230
-
1231
- assert (optionalKind1 == OTK_ImplicitlyUnwrappedOptional &&
1232
- " Source has unexpected optional kind!" );
1233
-
1234
- return ConversionRestrictionKind::ImplicitlyUnwrappedOptionalToOptional;
1235
- }
1236
-
1237
-
1238
1196
ConstraintSystem::SolutionKind
1239
1197
ConstraintSystem::matchTypes (Type type1, Type type2, ConstraintKind kind,
1240
1198
TypeMatchOptions flags,
@@ -2027,15 +1985,57 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
2027
1985
}
2028
1986
}
2029
1987
2030
- // A value of type T! can be converted to type U if T is convertible
2031
- // to U by force-unwrapping the source value.
2032
- // A value of type T, T?, or T! can be converted to type U? or U! if
2033
- // T is convertible to U.
2034
- if (concrete && kind >= ConstraintKind::Subtype)
2035
- if (auto restriction =
2036
- selectOptionalConversionRestriction (type1, type2, kind))
2037
- 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::ImplicitlyUnwrappedOptionalToOptional);
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::OptionalToImplicitlyUnwrappedOptional);
2019
+ }
2020
+ }
2021
+
2022
+ conversionsOrFixes.push_back (
2023
+ ConversionRestrictionKind::ValueToOptional);
2024
+ }
2025
+ }
2026
+ }
2038
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
+
2039
2039
// Allow '() -> T' to '() -> ()' and '() -> Never' to '() -> T' for closure
2040
2040
// literals.
2041
2041
if (auto elt = locator.last ()) {
0 commit comments