Skip to content

Commit 7dd2489

Browse files
committed
Revert "[Constraint solver] Collapse the three optional-to-optional restriction kinds."
This reverts commit d79ba78 so that I can revert 1efafbc, which caused a source compatibility regression.
1 parent f267521 commit 7dd2489

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

lib/Sema/CSApply.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5545,6 +5545,8 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
55455545
return result;
55465546
}
55475547

5548+
case ConversionRestrictionKind::OptionalToImplicitlyUnwrappedOptional:
5549+
case ConversionRestrictionKind::ImplicitlyUnwrappedOptionalToOptional:
55485550
case ConversionRestrictionKind::OptionalToOptional:
55495551
return coerceOptionalToOptional(expr, toType, locator, typeFromPattern);
55505552

lib/Sema/CSSimplify.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,18 +1209,29 @@ selectOptionalConversionRestriction(Type type1, Type type2,
12091209
kind >= ConstraintKind::Conversion)
12101210
return ConversionRestrictionKind::ForceUnchecked;
12111211

1212-
return None;
1212+
return llvm::None;
12131213
}
12141214

1215+
if (optionalKind1 == optionalKind2)
1216+
return ConversionRestrictionKind::OptionalToOptional;
1217+
12151218
if (optionalKind1 == OTK_None)
12161219
return ConversionRestrictionKind::ValueToOptional;
12171220

1218-
if (optionalKind1 == OTK_Optional &&
1219-
optionalKind2 == OTK_ImplicitlyUnwrappedOptional &&
1220-
kind < ConstraintKind::Conversion)
1221-
return None;
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!");
12221233

1223-
return ConversionRestrictionKind::OptionalToOptional;
1234+
return ConversionRestrictionKind::ImplicitlyUnwrappedOptionalToOptional;
12241235
}
12251236

12261237

@@ -3807,6 +3818,8 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
38073818
// T $< U ===> T! $< U?
38083819
// also:
38093820
// T <c U ===> T? <c U!
3821+
case ConversionRestrictionKind::OptionalToImplicitlyUnwrappedOptional:
3822+
case ConversionRestrictionKind::ImplicitlyUnwrappedOptionalToOptional:
38103823
case ConversionRestrictionKind::OptionalToOptional: {
38113824
addContextualScore();
38123825
assert(matchKind >= ConstraintKind::Subtype);

lib/Sema/CSSolver.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2223,7 +2223,11 @@ static bool shortCircuitDisjunctionAt(Constraint *constraint,
22232223
if (auto restriction = constraint->getRestriction()) {
22242224
// Non-optional conversions are better than optional-to-optional
22252225
// conversions.
2226-
if (*restriction == ConversionRestrictionKind::OptionalToOptional)
2226+
if (*restriction == ConversionRestrictionKind::OptionalToOptional ||
2227+
*restriction
2228+
== ConversionRestrictionKind::ImplicitlyUnwrappedOptionalToOptional ||
2229+
*restriction
2230+
== ConversionRestrictionKind::OptionalToImplicitlyUnwrappedOptional)
22272231
return true;
22282232

22292233
// Array-to-pointer conversions are better than inout-to-pointer conversions.

lib/Sema/Constraint.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,10 @@ StringRef swift::constraints::getName(ConversionRestrictionKind kind) {
365365
return "[value-to-optional]";
366366
case ConversionRestrictionKind::OptionalToOptional:
367367
return "[optional-to-optional]";
368+
case ConversionRestrictionKind::ImplicitlyUnwrappedOptionalToOptional:
369+
return "[unchecked-optional-to-optional]";
370+
case ConversionRestrictionKind::OptionalToImplicitlyUnwrappedOptional:
371+
return "[optional-to-unchecked-optional]";
368372
case ConversionRestrictionKind::ClassMetatypeToAnyObject:
369373
return "[class-metatype-to-object]";
370374
case ConversionRestrictionKind::ExistentialMetatypeToAnyObject:

lib/Sema/Constraint.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ enum class ConversionRestrictionKind {
188188
ValueToOptional,
189189
/// T? -> U? optional to optional conversion (or unchecked to unchecked).
190190
OptionalToOptional,
191+
/// T! -> U? unchecked-optional to optional conversion
192+
ImplicitlyUnwrappedOptionalToOptional,
193+
/// T? -> U! optional to implicitly unwrapped optional conversion
194+
OptionalToImplicitlyUnwrappedOptional,
191195
/// Implicit forces of implicitly unwrapped optionals to their presumed values
192196
ForceUnchecked,
193197
/// Implicit upcast conversion of array types.

0 commit comments

Comments
 (0)