Skip to content

Commit fc73e79

Browse files
committed
Revert "Fix one source of exponential behavior in the type checker."
This reverts commit 1efafbc because results in a regression in source compatibility with Swift 3. It also adds a test demonstrating what was broken by the change. I will take another look at fixing the exponential behavior that this was attempting to fix. Fixes rdar://problem/29977523.
1 parent 7dd2489 commit fc73e79

File tree

2 files changed

+62
-50
lines changed

2 files changed

+62
-50
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,48 +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 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-
12381196
ConstraintSystem::SolutionKind
12391197
ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
12401198
TypeMatchOptions flags,
@@ -2027,15 +1985,57 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
20271985
}
20281986
}
20291987

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

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+
20392039
// Allow '() -> T' to '() -> ()' and '() -> Never' to '() -> T' for closure
20402040
// literals.
20412041
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)