Skip to content

Commit dac3f52

Browse files
committed
[Sema] Produce expected diagnostic for invalid operator usage in loop
Resolves swiftlang#79999 Snippet: ``` func foo(_ a: Int) -> [Int] { [] } for _ in foo(-) {} ```
1 parent f64014a commit dac3f52

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

lib/Sema/ConstraintSystem.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -4739,6 +4739,19 @@ void ConstraintSystem::diagnoseFailureFor(SyntacticElementTarget target) {
47394739
} else if (target.getAsUninitializedVar()) {
47404740
DE.diagnose(target.getLoc(), diag::failed_to_produce_diagnostic);
47414741
} else if (target.isForEachPreamble()) {
4742+
auto forEachStmt = target.getAsForEachStmt();
4743+
auto sequence = forEachStmt->getParsedSequence();
4744+
// Fix for a specific case:
4745+
//
4746+
// func example(_ a: Int) -> [Int] { [] }
4747+
// for _ in foo(-) {}
4748+
for (auto &arg: sequence->getArgs()->getArgExprs()) {
4749+
if (auto *overloadedDeclRefExpr = dyn_cast<OverloadedDeclRefExpr>(arg)) {
4750+
DE.diagnose(target.getLoc(), diag::type_of_expression_is_ambiguous)
4751+
.highlight(target.getSourceRange());
4752+
return;
4753+
}
4754+
}
47424755
DE.diagnose(target.getLoc(), diag::failed_to_produce_diagnostic);
47434756
} else {
47444757
// Emit a poor fallback message.

0 commit comments

Comments
 (0)