Skip to content

Commit 74f65ba

Browse files
committed
[ConstraintSystem] Find solutions for code with invalid '_' using
holes.
1 parent f1ef08a commit 74f65ba

File tree

5 files changed

+5
-53
lines changed

5 files changed

+5
-53
lines changed

lib/Sema/CSDiag.cpp

-48
Original file line numberDiff line numberDiff line change
@@ -1447,29 +1447,6 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
14471447
auto lhsType = CS.getType(lhsExpr)->getRValueType();
14481448
auto rhsType = CS.getType(rhsExpr)->getRValueType();
14491449

1450-
// TODO(diagnostics): There are still cases not yet handled by new
1451-
// diagnostics framework e.g.
1452-
//
1453-
// var tuple = (1, 2, 3)
1454-
// switch tuple {
1455-
// case (let (_, _, _)) + 1: break
1456-
// }
1457-
if (callExpr->isImplicit() && overloadName == "~=") {
1458-
auto flags = ParameterTypeFlags();
1459-
if (calleeInfo.candidates.size() == 1)
1460-
if (auto fnType = calleeInfo.candidates[0].getFunctionType())
1461-
flags = fnType->getParams()[0].getParameterFlags();
1462-
1463-
auto *locator = CS.getConstraintLocator(
1464-
callExpr,
1465-
{ConstraintLocator::ApplyArgument,
1466-
LocatorPathElt::ApplyArgToParam(0, 0, flags)},
1467-
/*summaryFlags=*/0);
1468-
1469-
ArgumentMismatchFailure failure(CS, lhsType, rhsType, locator);
1470-
return failure.diagnosePatternMatchingMismatch();
1471-
}
1472-
14731450
if (isContextualConversionFailure(argTuple))
14741451
return false;
14751452

@@ -1754,22 +1731,6 @@ void FailureDiagnosis::diagnoseAmbiguity(Expr *E) {
17541731
if (auto *assignment = dyn_cast<AssignExpr>(E)) {
17551732
if (isa<DiscardAssignmentExpr>(assignment->getDest())) {
17561733
auto *srcExpr = assignment->getSrc();
1757-
1758-
bool diagnosedInvalidUseOfDiscardExpr = false;
1759-
srcExpr->forEachChildExpr([&](Expr *expr) -> Expr * {
1760-
if (auto *DAE = dyn_cast<DiscardAssignmentExpr>(expr)) {
1761-
diagnose(DAE->getLoc(), diag::discard_expr_outside_of_assignment)
1762-
.highlight(srcExpr->getSourceRange());
1763-
diagnosedInvalidUseOfDiscardExpr = true;
1764-
return nullptr;
1765-
}
1766-
1767-
return expr;
1768-
});
1769-
1770-
if (diagnosedInvalidUseOfDiscardExpr)
1771-
return;
1772-
17731734
diagnoseAmbiguity(srcExpr);
17741735
return;
17751736
}
@@ -1783,15 +1744,6 @@ void FailureDiagnosis::diagnoseAmbiguity(Expr *E) {
17831744
return;
17841745
}
17851746

1786-
// A DiscardAssignmentExpr (spelled "_") needs contextual type information to
1787-
// infer its type. If we see one at top level, diagnose that it must be part
1788-
// of an assignment so we don't get a generic "expression is ambiguous" error.
1789-
if (isa<DiscardAssignmentExpr>(E)) {
1790-
diagnose(E->getLoc(), diag::discard_expr_outside_of_assignment)
1791-
.highlight(E->getSourceRange());
1792-
return;
1793-
}
1794-
17951747
// Diagnose ".foo" expressions that lack context specifically.
17961748
if (auto UME =
17971749
dyn_cast<UnresolvedMemberExpr>(E->getSemanticsProvidingExpr())) {

lib/Sema/CSGen.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -2827,7 +2827,8 @@ namespace {
28272827

28282828
Type visitDiscardAssignmentExpr(DiscardAssignmentExpr *expr) {
28292829
auto locator = CS.getConstraintLocator(expr);
2830-
auto typeVar = CS.createTypeVariable(locator, TVO_CanBindToNoEscape);
2830+
auto typeVar = CS.createTypeVariable(locator, TVO_CanBindToNoEscape |
2831+
TVO_CanBindToHole);
28312832
return LValueType::get(typeVar);
28322833
}
28332834

test/Constraints/diagnostics.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,7 @@ func badTypes() {
12711271
// rdar://34357545
12721272
func unresolvedTypeExistential() -> Bool {
12731273
return (Int.self==_{})
1274-
// expected-error@-1 {{expression type 'Bool' is ambiguous without more context}}
1274+
// expected-error@-1 {{'_' can only appear in a pattern or on the left side of an assignment}}
12751275
}
12761276

12771277
func rdar43525641(_ a: Int, _ b: Int = 0, c: Int = 0, _ d: Int) {}

test/Parse/matching_patterns.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,7 @@ case (_, var e, 3) +++ (1, 2, 3):
288288
// expected-error@-2{{'var' binding pattern cannot appear in an expression}}
289289
()
290290
case (let (_, _, _)) + 1:
291-
// expected-error@-1 2 {{'var' binding pattern cannot appear in an expression}}
292-
// expected-error@-2 {{expression pattern of type 'Int' cannot match values of type '(Int, Int, Int)'}}
291+
// expected-error@-1 {{expression pattern of type 'Int' cannot match values of type '(Int, Int, Int)'}}
293292
()
294293
}
295294

test/expr/expressions.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ func test() {
708708
func unusedExpressionResults() {
709709
// Unused l-value
710710
_ // expected-error{{'_' can only appear in a pattern or on the left side of an assignment}}
711-
711+
// expected-error@-1 {{expression resolves to an unused variable}}
712712

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

0 commit comments

Comments
 (0)