Skip to content

Commit 3b8e377

Browse files
committed
[Type checker] Add ‘static’ to member operators to improve recovery.
Eliminates a crash due to missing ‘static’ on member operations. Fixes part of rdar://problem/27940842. (cherry picked from commit 62a3de6)
1 parent 1fe272b commit 3b8e377

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4357,6 +4357,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
43574357
dc->getDeclaredInterfaceType())
43584358
.fixItInsert(FD->getAttributeInsertionLoc(/*forModifier=*/true),
43594359
"static ");
4360+
4361+
FD->setStatic();
43604362
} else {
43614363
TC.diagnose(FD->getLoc(), diag::nonfinal_operator_in_class,
43624364
operatorName, dc->getDeclaredInterfaceType())
@@ -4372,6 +4374,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
43724374
dc->getDeclaredInterfaceType())
43734375
.fixItInsert(FD->getAttributeInsertionLoc(/*forModifier=*/true),
43744376
"static ");
4377+
FD->setStatic();
43754378
}
43764379
} else if (!dc->isModuleScopeContext()) {
43774380
TC.diagnose(FD, diag::operator_in_local_scope);

test/decl/func/operator.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,12 @@ extension P3 {
336336
// Okay: refers to P3
337337
static func %%%%(lhs: P3, rhs: Unrelated) -> Unrelated { }
338338
}
339+
340+
// rdar://problem/27940842 - recovery with a non-static '=='.
341+
class C5 {
342+
func == (lhs: C5, rhs: C5) -> Bool { return false } // expected-error{{operator '==' declared in type 'C5' must be 'static'}}
343+
344+
func test1(x: C5) {
345+
_ = x == x
346+
}
347+
}

0 commit comments

Comments
 (0)