Skip to content

[CSDiag] Start chipping away at FailureDiagnosis::diagnoseAmbiguity #29415

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
Jan 24, 2020
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
99 changes: 0 additions & 99 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1447,29 +1447,6 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
auto lhsType = CS.getType(lhsExpr)->getRValueType();
auto rhsType = CS.getType(rhsExpr)->getRValueType();

// TODO(diagnostics): There are still cases not yet handled by new
// diagnostics framework e.g.
//
// var tuple = (1, 2, 3)
// switch tuple {
// case (let (_, _, _)) + 1: break
// }
if (callExpr->isImplicit() && overloadName == "~=") {
auto flags = ParameterTypeFlags();
if (calleeInfo.candidates.size() == 1)
if (auto fnType = calleeInfo.candidates[0].getFunctionType())
flags = fnType->getParams()[0].getParameterFlags();

auto *locator = CS.getConstraintLocator(
callExpr,
{ConstraintLocator::ApplyArgument,
LocatorPathElt::ApplyArgToParam(0, 0, flags)},
/*summaryFlags=*/0);

ArgumentMismatchFailure failure(CS, lhsType, rhsType, locator);
return failure.diagnosePatternMatchingMismatch();
}

if (isContextualConversionFailure(argTuple))
return false;

Expand Down Expand Up @@ -1517,41 +1494,6 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
AnyFunctionType::decomposeInput(CS.getType(argExpr), params);
auto argString = AnyFunctionType::getParamListAsString(params);

// If we couldn't get the name of the callee, then it must be something of a
// more complex "value of function type".
if (overloadName.empty()) {
// If we couldn't infer the result type of the closure expr, then we have
// some sort of ambiguity, let the ambiguity diagnostic stuff handle this.
if (auto ffty = fnType->getAs<AnyFunctionType>())
if (ffty->getResult()->hasTypeVariable()) {
diagnoseAmbiguity(fnExpr);
return true;
}

// The most common unnamed value of closure type is a ClosureExpr, so
// special case it.
if (isa<ClosureExpr>(fnExpr->getValueProvidingExpr())) {
if (fnType->hasTypeVariable())
diagnose(argExpr->getStartLoc(), diag::cannot_invoke_closure, argString)
.highlight(fnExpr->getSourceRange());
else
diagnose(argExpr->getStartLoc(), diag::cannot_invoke_closure_type,
fnType, argString)
.highlight(fnExpr->getSourceRange());

} else if (fnType->hasTypeVariable()) {
diagnose(argExpr->getStartLoc(), diag::cannot_call_function_value,
argString)
.highlight(fnExpr->getSourceRange());
} else {
diagnose(argExpr->getStartLoc(), diag::cannot_call_value_of_function_type,
fnType, argString)
.highlight(fnExpr->getSourceRange());
}

return true;
}

if (auto MTT = fnType->getAs<MetatypeType>()) {
if (MTT->getInstanceType()->isExistentialType()) {
diagnose(fnExpr->getLoc(), diag::construct_protocol_value, fnType);
Expand Down Expand Up @@ -1754,22 +1696,6 @@ void FailureDiagnosis::diagnoseAmbiguity(Expr *E) {
if (auto *assignment = dyn_cast<AssignExpr>(E)) {
if (isa<DiscardAssignmentExpr>(assignment->getDest())) {
auto *srcExpr = assignment->getSrc();

bool diagnosedInvalidUseOfDiscardExpr = false;
srcExpr->forEachChildExpr([&](Expr *expr) -> Expr * {
if (auto *DAE = dyn_cast<DiscardAssignmentExpr>(expr)) {
diagnose(DAE->getLoc(), diag::discard_expr_outside_of_assignment)
.highlight(srcExpr->getSourceRange());
diagnosedInvalidUseOfDiscardExpr = true;
return nullptr;
}

return expr;
});

if (diagnosedInvalidUseOfDiscardExpr)
return;

diagnoseAmbiguity(srcExpr);
return;
}
Expand All @@ -1783,15 +1709,6 @@ void FailureDiagnosis::diagnoseAmbiguity(Expr *E) {
return;
}

// A DiscardAssignmentExpr (spelled "_") needs contextual type information to
// infer its type. If we see one at top level, diagnose that it must be part
// of an assignment so we don't get a generic "expression is ambiguous" error.
if (isa<DiscardAssignmentExpr>(E)) {
diagnose(E->getLoc(), diag::discard_expr_outside_of_assignment)
.highlight(E->getSourceRange());
return;
}

// Diagnose ".foo" expressions that lack context specifically.
if (auto UME =
dyn_cast<UnresolvedMemberExpr>(E->getSemanticsProvidingExpr())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, I think this should be obsolete as well because diagnostics for _ = .foo have been ported already as missing base type for unresolved member.

Copy link
Member Author

@hborla hborla Jan 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't yet :/ it's still handling cases like this, where the solver does find multiple solutions with fixes but it isn't yet diagnosed in diagnoseAmbiguityWithFixes :

func f(_: Int) {}
func f(_: String) {}

f(.nothing)

I'm currently working on this case though!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that comment on top threw me off :)

Expand All @@ -1803,22 +1720,6 @@ void FailureDiagnosis::diagnoseAmbiguity(Expr *E) {
}
}

// Diagnose empty collection literals that lack context specifically.
if (auto CE = dyn_cast<CollectionExpr>(E->getSemanticsProvidingExpr())) {
if (CE->getNumElements() == 0) {
diagnose(E->getLoc(), diag::unresolved_collection_literal)
.highlight(E->getSourceRange());
return;
}
}

// Diagnose 'nil' without a contextual type.
if (isa<NilLiteralExpr>(E->getSemanticsProvidingExpr())) {
diagnose(E->getLoc(), diag::unresolved_nil_literal)
.highlight(E->getSourceRange());
return;
}

// Attempt to re-type-check the entire expression, allowing ambiguity, but
// ignoring a contextual type.
if (expr == E) {
Expand Down
3 changes: 2 additions & 1 deletion lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2827,7 +2827,8 @@ namespace {

Type visitDiscardAssignmentExpr(DiscardAssignmentExpr *expr) {
auto locator = CS.getConstraintLocator(expr);
auto typeVar = CS.createTypeVariable(locator, TVO_CanBindToNoEscape);
auto typeVar = CS.createTypeVariable(locator, TVO_CanBindToNoEscape |
TVO_CanBindToHole);
return LValueType::get(typeVar);
}

Expand Down
2 changes: 1 addition & 1 deletion test/Constraints/diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,7 @@ func badTypes() {
// rdar://34357545
func unresolvedTypeExistential() -> Bool {
return (Int.self==_{})
// expected-error@-1 {{expression type 'Bool' is ambiguous without more context}}
// expected-error@-1 {{'_' can only appear in a pattern or on the left side of an assignment}}
}

func rdar43525641(_ a: Int, _ b: Int = 0, c: Int = 0, _ d: Int) {}
Expand Down
3 changes: 1 addition & 2 deletions test/Parse/matching_patterns.swift
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,7 @@ case (_, var e, 3) +++ (1, 2, 3):
// expected-error@-2{{'var' binding pattern cannot appear in an expression}}
()
case (let (_, _, _)) + 1:
// expected-error@-1 2 {{'var' binding pattern cannot appear in an expression}}
// expected-error@-2 {{expression pattern of type 'Int' cannot match values of type '(Int, Int, Int)'}}
// expected-error@-1 {{expression pattern of type 'Int' cannot match values of type '(Int, Int, Int)'}}
()
}

Expand Down
2 changes: 1 addition & 1 deletion test/expr/expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ func test() {
func unusedExpressionResults() {
// Unused l-value
_ // expected-error{{'_' can only appear in a pattern or on the left side of an assignment}}

// expected-error@-1 {{expression resolves to an unused variable}}

// <rdar://problem/20749592> Conditional Optional binding hides compiler error
let optionalc:C? = nil
Expand Down