Skip to content

[CS] Handle placeholder in buildDisjunctionForOptionalVsUnderlying #39442

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 2 commits into from
Sep 24, 2021
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
56 changes: 2 additions & 54 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -4527,60 +4527,8 @@ class ConstraintSystem {
// Build a disjunction that attempts both T? and T for a particular
// type binding. The choice of T? is preferred, and we will not
// attempt T if we can type check with T?
void
buildDisjunctionForOptionalVsUnderlying(Type boundTy, Type type,
ConstraintLocator *locator) {
// NOTE: If we use other locator kinds for these disjunctions, we
// need to account for it in solution scores for forced-unwraps.
assert(locator->getPath().back().getKind() ==
ConstraintLocator::ImplicitlyUnwrappedDisjunctionChoice ||
locator->getPath().back().getKind() ==
ConstraintLocator::DynamicLookupResult);

// Create the constraint to bind to the optional type and make it
// the favored choice.
auto *bindToOptional =
Constraint::create(*this, ConstraintKind::Bind, boundTy, type, locator);
bindToOptional->setFavored();

Type underlyingType;
if (auto *fnTy = type->getAs<AnyFunctionType>())
underlyingType = replaceFinalResultTypeWithUnderlying(fnTy);
else if (auto *typeVar =
type->getWithoutSpecifierType()->getAs<TypeVariableType>()) {
auto *locator = typeVar->getImpl().getLocator();

// If `type` hasn't been resolved yet, we need to allocate a type
// variable to represent an object type of a future optional, and
// add a constraint beetween `type` and `underlyingType` to model it.
underlyingType = createTypeVariable(
getConstraintLocator(locator, LocatorPathElt::GenericArgument(0)),
TVO_PrefersSubtypeBinding | TVO_CanBindToLValue |
TVO_CanBindToNoEscape);

// Using a `typeVar` here because l-value is going to be applied
// to the underlying type below.
addConstraint(ConstraintKind::OptionalObject, typeVar, underlyingType,
locator);
} else {
underlyingType = type->getWithoutSpecifierType()->getOptionalObjectType();
}

assert(underlyingType);

if (type->is<LValueType>())
underlyingType = LValueType::get(underlyingType);
assert(!type->is<InOutType>());

auto *bindToUnderlying = Constraint::create(
*this, ConstraintKind::Bind, boundTy, underlyingType, locator);

llvm::SmallVector<Constraint *, 2> choices = {bindToOptional,
bindToUnderlying};

// Create the disjunction
addDisjunctionConstraint(choices, locator, RememberChoice);
}
void buildDisjunctionForOptionalVsUnderlying(Type boundTy, Type ty,
ConstraintLocator *locator);

// Build a disjunction for types declared IUO.
void
Expand Down
62 changes: 62 additions & 0 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2597,6 +2597,68 @@ bool ConstraintSystem::isAsynchronousContext(DeclContext *dc) {
return false;
}

void ConstraintSystem::buildDisjunctionForOptionalVsUnderlying(
Type boundTy, Type ty, ConstraintLocator *locator) {
// NOTE: If we use other locator kinds for these disjunctions, we
// need to account for it in solution scores for forced-unwraps.
assert(locator->getPath().back().getKind() ==
ConstraintLocator::ImplicitlyUnwrappedDisjunctionChoice ||
locator->getPath().back().getKind() ==
ConstraintLocator::DynamicLookupResult);
assert(!ty->is<InOutType>());
auto rvalueTy = ty->getWithoutSpecifierType();

// If the type to bind is a placeholder, we can propagate it, as we don't know
// whether it can be optional or non-optional, and we would have already
// recorded a fix for it.
if (rvalueTy->isPlaceholder()) {
addConstraint(ConstraintKind::Bind, boundTy, ty, locator);
return;
}

// Create the constraint to bind to the optional type and make it the favored
// choice.
auto *bindToOptional =
Constraint::create(*this, ConstraintKind::Bind, boundTy, ty, locator);
bindToOptional->setFavored();

Type underlyingType;
if (auto *fnTy = ty->getAs<AnyFunctionType>())
underlyingType = replaceFinalResultTypeWithUnderlying(fnTy);
else if (auto *typeVar = rvalueTy->getAs<TypeVariableType>()) {
auto *locator = typeVar->getImpl().getLocator();

// If `ty` hasn't been resolved yet, we need to allocate a type variable to
// represent an object type of a future optional, and add a constraint
// between `ty` and `underlyingType` to model it.
underlyingType = createTypeVariable(
getConstraintLocator(locator, LocatorPathElt::GenericArgument(0)),
TVO_PrefersSubtypeBinding | TVO_CanBindToLValue |
TVO_CanBindToNoEscape);

// Using a `typeVar` here because l-value is going to be applied
// to the underlying type below.
addConstraint(ConstraintKind::OptionalObject, typeVar, underlyingType,
locator);
} else {
underlyingType = rvalueTy->getOptionalObjectType();
}

assert(underlyingType);

if (ty->is<LValueType>())
underlyingType = LValueType::get(underlyingType);

auto *bindToUnderlying = Constraint::create(*this, ConstraintKind::Bind,
boundTy, underlyingType, locator);

llvm::SmallVector<Constraint *, 2> choices = {bindToOptional,
bindToUnderlying};

// Create the disjunction
addDisjunctionConstraint(choices, locator, RememberChoice);
}

void ConstraintSystem::bindOverloadType(
const SelectedOverload &overload, Type boundType,
ConstraintLocator *locator, DeclContext *useDC,
Expand Down
9 changes: 9 additions & 0 deletions test/Constraints/iuo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,12 @@ struct CurriedIUO {
let _: Int = CurriedIUO.silly(self)()
}
}

// SR-15219 (rdar://83352038): Make sure we don't crash if an IUO param becomes
// a placeholder.
func rdar83352038() {
func foo(_: UnsafeRawPointer) -> Undefined {} // expected-error {{cannot find type 'Undefined' in scope}}
let _ = { (cnode: AlsoUndefined!) -> UnsafeMutableRawPointer in // expected-error {{cannot find type 'AlsoUndefined' in scope}}
return foo(cnode)
}
}