Skip to content

Commit febcbf6

Browse files
authored
Merge pull request #30655 from hborla/missing-conformance-operator-ref
[Diagnostics] Support diagnosing references to operators without argument application
2 parents 40e67b5 + da715f8 commit febcbf6

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -520,10 +520,11 @@ bool MissingConformanceFailure::diagnoseAsAmbiguousOperatorRef() {
520520
// about missing conformance just in case.
521521
auto operatorID = name.getIdentifier();
522522

523-
auto *applyExpr = cast<ApplyExpr>(findParentExpr(anchor));
524-
if (auto *binaryOp = dyn_cast<BinaryExpr>(applyExpr)) {
525-
auto lhsType = getType(binaryOp->getArg()->getElement(0));
526-
auto rhsType = getType(binaryOp->getArg()->getElement(1));
523+
auto *fnType = getType(anchor)->getAs<AnyFunctionType>();
524+
auto params = fnType->getParams();
525+
if (params.size() == 2) {
526+
auto lhsType = params[0].getPlainType();
527+
auto rhsType = params[1].getPlainType();
527528

528529
if (lhsType->isEqual(rhsType)) {
529530
emitDiagnostic(anchor->getLoc(), diag::cannot_apply_binop_to_same_args,
@@ -534,7 +535,7 @@ bool MissingConformanceFailure::diagnoseAsAmbiguousOperatorRef() {
534535
}
535536
} else {
536537
emitDiagnostic(anchor->getLoc(), diag::cannot_apply_unop_to_arg,
537-
operatorID.str(), getType(applyExpr->getArg()));
538+
operatorID.str(), params[0].getPlainType());
538539
}
539540

540541
diagnoseAsNote();

test/Constraints/operator.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,10 @@ func rdar_60185506() {
265265
let _ = (x?.foo ?? 0) <= 0.5 // Ok
266266
}
267267
}
268+
269+
// rdar://problem/60727310
270+
func rdar60727310() {
271+
func myAssertion<T>(_ a: T, _ op: ((T,T)->Bool), _ b: T) {}
272+
var e: Error? = nil
273+
myAssertion(e, ==, nil) // expected-error {{binary operator '==' cannot be applied to two 'Error?' operands}}
274+
}

0 commit comments

Comments
 (0)