Merge pull request #17921 from DougGregor/optional-not-unwrapped-diags

[Type checker] Improve diagnostics when an optional value is not unwrapped
This commit is contained in:
Doug Gregor
2018-07-13 23:43:25 -07:00
committed by GitHub
31 changed files with 321 additions and 114 deletions

View File

@@ -3548,18 +3548,9 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyMemberConstraint(
// as such and try to recover in various ways.
if (shouldAttemptFixes() && baseObjTy->getOptionalObjectType()) {
// If the base type was an optional, look through it.
// Determine whether or not we want to provide an optional chaining fixit or
// a force unwrap fixit.
bool optionalChain;
if (!getContextualType())
optionalChain = !(Options & ConstraintSystemFlags::PreferForceUnwrapToOptional);
else
optionalChain = !getContextualType()->getOptionalObjectType().isNull();
auto fixKind = optionalChain ? FixKind::OptionalChaining : FixKind::ForceOptional;
// Note the fix.
if (recordFix(fixKind, locator))
// We're unwrapping the base to perform a member access.
if (recordFix(Fix::getUnwrapOptionalBase(*this, member), locator))
return SolutionKind::Error;
// Look through one level of optional.
@@ -4871,8 +4862,8 @@ ConstraintSystem::simplifyFixConstraint(Fix fix, Type type1, Type type2,
getDefaultDecompositionOptions(flags) | TMF_ApplyingFix;
switch (fix.getKind()) {
case FixKind::ForceOptional:
case FixKind::OptionalChaining: {
// Assume that '!' was applied to the first type.
case FixKind::UnwrapOptionalBase: {
// Assume that we've unwrapped the first type.
auto result =
matchTypes(type1->getRValueObjectType()->getOptionalObjectType(), type2,
matchKind, subflags, locator);