Skip to content

Commit 12af451

Browse files
authored
Merge pull request #6835 from rudkx/revert-optional-breakage
Revert optional breakage
2 parents 4db6f81 + c301833 commit 12af451

File tree

2 files changed

+62
-39
lines changed

2 files changed

+62
-39
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,37 +1193,6 @@ static bool isStringCompatiblePointerBaseType(TypeChecker &TC,
11931193
return false;
11941194
}
11951195

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-
12271196
ConstraintSystem::SolutionKind
12281197
ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
12291198
TypeMatchOptions flags,
@@ -2016,15 +1985,57 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
20161985
}
20171986
}
20181987

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+
}
20272027

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+
20282039
// Allow '() -> T' to '() -> ()' and '() -> Never' to '() -> T' for closure
20292040
// literals.
20302041
if (auto elt = locator.last()) {

test/Constraints/optional.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,15 @@ SR_3248().callback("test") // expected-error {{cannot convert value of type 'St
164164

165165
_? = nil // expected-error {{'nil' requires a contextual type}}
166166
_?? = nil // expected-error {{'nil' requires a contextual type}}
167+
168+
169+
infix operator !====
170+
func !====(_ lhs: AnyObject?, _ rhs: AnyObject?) -> Bool { return false }
171+
172+
func calleeRdar29977523(_ lhs: AnyObject?, _ rhs: AnyObject?) { }
173+
174+
func rdar29977523(lhs: AnyObject?!, rhs: AnyObject?) {
175+
if lhs !==== rhs { }
176+
177+
calleeRdar29977523(lhs, rhs)
178+
}

0 commit comments

Comments
 (0)