Skip to content

[Diagnostics] Improve diagnostics involving implicitly unwrapped optional functions #6187

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
Dec 11, 2016
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
22 changes: 16 additions & 6 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,10 @@ namespace {
->getForwardingSubstitutions(M);
entityType = GFT->substGenericArgs(subs);
} else {
if (auto objType =
entityType->getImplicitlyUnwrappedOptionalObjectType())
entityType = objType;

entityType = DC->mapTypeIntoContext(entityType);
}
}
Expand Down Expand Up @@ -5620,7 +5624,14 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
callExpr->setFn(operatorRef);
};

auto fnType = fnExpr->getType()->getRValueType();
auto getFuncType = [](Type type) -> Type {
auto fnType = type->getRValueType();
if (auto objectType = fnType->getImplicitlyUnwrappedOptionalObjectType())
return objectType;
return fnType;
};

auto fnType = getFuncType(fnExpr->getType());

// If we have a contextual type, and if we have an ambiguously typed function
// result from our previous check, we re-type-check it using this contextual
Expand All @@ -5631,9 +5642,8 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
// produce better diagnostics below by diagnosing this here rather than trying
// to peel apart the failed conversion to function type.
if (CS->getContextualType() &&
(isUnresolvedOrTypeVarType(fnExpr->getType()) ||
(fnExpr->getType()->is<AnyFunctionType>() &&
fnExpr->getType()->hasUnresolvedType()))) {
(isUnresolvedOrTypeVarType(fnType) ||
(fnType->is<AnyFunctionType>() && fnType->hasUnresolvedType()))) {
// FIXME: Prevent typeCheckChildIndependently from transforming expressions,
// because if we try to typecheck OSR expression with contextual type,
// it'll end up converting it into DeclRefExpr based on contextual info,
Expand All @@ -5650,15 +5660,15 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
&listener);

if (type.hasValue())
fnType = type.getValue()->getRValueType();
fnType = getFuncType(type.getValue());
} else {
fnExpr = typeCheckChildIndependently(callExpr->getFn(), Type(),
CTP_CalleeResult, TCC_ForceRecheck,
&listener);
if (!fnExpr)
return true;

fnType = fnExpr->getType()->getRValueType();
fnType = getFuncType(fnExpr->getType());
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/Constraints/optional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,17 @@ func sr2752(x: String?, y: String?) {
y.map { _ in "" } ?? "\(xx)"
}
}

// SR-3248 - Invalid diagnostic calling implicitly unwrapped closure
var sr3248 : ((Int) -> ())!
sr3248?(a: 2) // expected-error {{extraneous argument label 'a:' in call}}
sr3248!(a: 2) // expected-error {{extraneous argument label 'a:' in call}}
sr3248(a: 2) // expected-error {{extraneous argument label 'a:' in call}}

struct SR_3248 {
var callback: (([AnyObject]) -> Void)!
}

SR_3248().callback?("test") // expected-error {{cannot convert value of type 'String' to expected argument type '[AnyObject]'}}
SR_3248().callback!("test") // expected-error {{cannot convert value of type 'String' to expected argument type '[AnyObject]'}}
SR_3248().callback("test") // expected-error {{cannot convert value of type 'String' to expected argument type '[AnyObject]'}}