Skip to content

Commit 62a3de6

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.
1 parent fa36206 commit 62a3de6

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
@@ -4335,6 +4335,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
43354335
dc->getDeclaredInterfaceType())
43364336
.fixItInsert(FD->getAttributeInsertionLoc(/*forModifier=*/true),
43374337
"static ");
4338+
4339+
FD->setStatic();
43384340
} else {
43394341
TC.diagnose(FD->getLoc(), diag::nonfinal_operator_in_class,
43404342
operatorName, dc->getDeclaredInterfaceType())
@@ -4350,6 +4352,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
43504352
dc->getDeclaredInterfaceType())
43514353
.fixItInsert(FD->getAttributeInsertionLoc(/*forModifier=*/true),
43524354
"static ");
4355+
FD->setStatic();
43534356
}
43544357
} else if (!dc->isModuleScopeContext()) {
43554358
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)