Skip to content

IUO: Put the pieces in place to handle coercions and casts to IUO types. #13722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 56 additions & 3 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3170,7 +3170,7 @@ namespace {
cast->setImplicit();

// Type-check this conditional case.
Expr *result = visitConditionalCheckedCastExpr(cast, true);
Expr *result = handleConditionalCheckedCastExpr(cast, true);
if (!result)
return nullptr;

Expand Down Expand Up @@ -3409,7 +3409,28 @@ namespace {
return addFinalOptionalInjections(result);
}

bool hasForcedOptionalResult(ExplicitCastExpr *expr) {
auto *TR = expr->getCastTypeLoc().getTypeRepr();
if (TR && TR->getKind() == TypeReprKind::ImplicitlyUnwrappedOptional) {
auto *locator = cs.getConstraintLocator(
expr, ConstraintLocator::ImplicitlyUnwrappedCoercionResult);
return solution.getDisjunctionChoice(locator);
}
return false;
}

Expr *visitCoerceExpr(CoerceExpr *expr) {
// If we need to insert a force-unwrap for coercions of the form
// 'as T!', do so now.
if (hasForcedOptionalResult(expr)) {
auto *coerced = visitCoerceExpr(expr, None);
if (!coerced)
return nullptr;

return coerceImplicitlyUnwrappedOptionalToValue(
coerced, cs.getType(coerced)->getOptionalObjectType());
}

return visitCoerceExpr(expr, None);
}

Expand Down Expand Up @@ -3482,7 +3503,24 @@ namespace {
return expr;
}

// Rewrite ForcedCheckedCastExpr based on what the solver computed.
Expr *visitForcedCheckedCastExpr(ForcedCheckedCastExpr *expr) {
// If we need to insert a force-unwrap for coercions of the form
// 'as! T!', do so now.
if (hasForcedOptionalResult(expr)) {
auto *coerced = handleForcedCheckedCastExpr(expr);
if (!coerced)
return nullptr;

return coerceImplicitlyUnwrappedOptionalToValue(
coerced, cs.getType(coerced)->getOptionalObjectType());
}

return handleForcedCheckedCastExpr(expr);
}

// Most of the logic for dealing with ForcedCheckedCastExpr.
Expr *handleForcedCheckedCastExpr(ForcedCheckedCastExpr *expr) {
// Simplify the type we're casting to.
auto toType = simplifyType(expr->getCastTypeLoc().getType());
expr->getCastTypeLoc().setType(toType, /*validated=*/true);
Expand Down Expand Up @@ -3546,8 +3584,23 @@ namespace {
OptionalBindingsCastKind::Forced);
}

Expr *visitConditionalCheckedCastExpr(ConditionalCheckedCastExpr *expr,
bool isInsideIsExpr = false) {
Expr *visitConditionalCheckedCastExpr(ConditionalCheckedCastExpr *expr) {
// If we need to insert a force-unwrap for coercions of the form
// 'as! T!', do so now.
if (hasForcedOptionalResult(expr)) {
auto *coerced = handleConditionalCheckedCastExpr(expr);
if (!coerced)
return nullptr;

return coerceImplicitlyUnwrappedOptionalToValue(
coerced, cs.getType(coerced)->getOptionalObjectType());
}

return handleConditionalCheckedCastExpr(expr);
}

Expr *handleConditionalCheckedCastExpr(ConditionalCheckedCastExpr *expr,
bool isInsideIsExpr = false) {
// Simplify the type we're casting to.
auto toType = simplifyType(expr->getCastTypeLoc().getType());
checkForImportedUsedConformances(toType);
Expand Down
40 changes: 38 additions & 2 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2494,6 +2494,18 @@ namespace {
llvm_unreachable("Already type-checked");
}

Type
createTypeVariableAndDisjunctionForIUOCoercion(Type toType,
ConstraintLocator *locator) {
auto implicitUnwrapLocator = CS.getConstraintLocator(
locator, ConstraintLocator::ImplicitlyUnwrappedCoercionResult);
auto typeVar =
CS.createTypeVariable(implicitUnwrapLocator, /*options=*/0);
CS.buildDisjunctionForImplicitlyUnwrappedOptional(typeVar, toType,
implicitUnwrapLocator);
return typeVar;
}

Type visitForcedCheckedCastExpr(ForcedCheckedCastExpr *expr) {
auto &tc = CS.getTypeChecker();
auto fromExpr = expr->getSubExpr();
Expand All @@ -2517,6 +2529,12 @@ namespace {
// The source type can be checked-cast to the destination type.
CS.addConstraint(ConstraintKind::CheckedCast, fromType, toType, locator);

// If the result type was declared IUO, add a disjunction for
// bindings for the result of the coercion.
auto *TR = expr->getCastTypeLoc().getTypeRepr();
if (TR && TR->getKind() == TypeReprKind::ImplicitlyUnwrappedOptional)
return createTypeVariableAndDisjunctionForIUOCoercion(toType, locator);

return toType;
}

Expand All @@ -2537,8 +2555,17 @@ namespace {
auto fromType = CS.getType(expr->getSubExpr());
auto locator = CS.getConstraintLocator(expr);

CS.addExplicitConversionConstraint(fromType, toType, /*allowFixes=*/true,
locator);
// Add a conversion constraint for the direct conversion between
// types.
CS.addExplicitConversionConstraint(fromType, toType,
/*allowFixes=*/true, locator);

// If the result type was declared IUO, add a disjunction for
// bindings for the result of the coercion.
auto *TR = expr->getCastTypeLoc().getTypeRepr();
if (TR && TR->getKind() == TypeReprKind::ImplicitlyUnwrappedOptional)
return createTypeVariableAndDisjunctionForIUOCoercion(toType, locator);

return toType;
}

Expand All @@ -2561,7 +2588,16 @@ namespace {

auto fromType = CS.getType(fromExpr);
auto locator = CS.getConstraintLocator(expr);

CS.addConstraint(ConstraintKind::CheckedCast, fromType, toType, locator);

// If the result type was declared IUO, add a disjunction for
// bindings for the result of the coercion.
auto *TR = expr->getCastTypeLoc().getTypeRepr();
if (TR && TR->getKind() == TypeReprKind::ImplicitlyUnwrappedOptional)
return createTypeVariableAndDisjunctionForIUOCoercion(
OptionalType::get(toType), locator);

return OptionalType::get(toType);
}

Expand Down