Skip to content

Commit fdc76b6

Browse files
committed
Merge pull request #1205 from robinkunde/improved_note
[Sema] improved binary op diagnostic notes for structs and payloaded enums
2 parents e7e0a87 + 90b6f0e commit fdc76b6

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ NOTE(suggest_partial_overloads,none,
243243
"partially matching parameter lists|result types}0: %2",
244244
(bool, StringRef, StringRef))
245245

246+
NOTE(no_binary_op_overload_for_enum_with_payload,none,
247+
"binary operator '%0' cannot be synthesized for enums "
248+
"with associated values",
249+
(StringRef))
250+
246251
ERROR(cannot_convert_initializer_value,none,
247252
"cannot convert value of type %0 to specified type %1", (Type,Type))
248253
ERROR(cannot_convert_initializer_value_protocol,none,

lib/Sema/CSDiag.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3975,8 +3975,19 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
39753975
.highlight(lhsExpr->getSourceRange())
39763976
.highlight(rhsExpr->getSourceRange());
39773977
}
3978-
3979-
calleeInfo.suggestPotentialOverloads(callExpr->getLoc());
3978+
3979+
if (lhsType->isEqual(rhsType) &&
3980+
isNameOfStandardComparisonOperator(overloadName) &&
3981+
lhsType->is<EnumType>() &&
3982+
!lhsType->getAs<EnumType>()->getDecl()
3983+
->hasOnlyCasesWithoutAssociatedValues()) {
3984+
diagnose(callExpr->getLoc(),
3985+
diag::no_binary_op_overload_for_enum_with_payload,
3986+
overloadName);
3987+
} else {
3988+
calleeInfo.suggestPotentialOverloads(callExpr->getLoc());
3989+
}
3990+
39803991
return true;
39813992
}
39823993

test/Constraints/diagnostics.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,4 +718,10 @@ func test17875634() {
718718
match.append(row, col) // expected-error {{extra argument in call}}
719719
}
720720

721+
// <https://github.com/apple/swift/pull/1205> Improved diagnostics for enums with associated values
722+
enum AssocTest {
723+
case one(Int)
724+
}
721725

726+
if AssocTest.one(1) == AssocTest.one(1) {} // expected-error{{binary operator '==' cannot be applied to two 'AssocTest' operands}}
727+
// expected-note @-1 {{binary operator '==' cannot be synthesized for enums with associated values}}

0 commit comments

Comments
 (0)