Skip to content

Commit dfadee3

Browse files
committed
[CSDiagnostics] Attach missing member diagnostic to a pattern/statement
If missing member is found in e.g. case statement or a pattern, let's attach diagnostic directly to it.
1 parent 609b3e5 commit dfadee3

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

lib/Sema/CSDiagnostics.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,12 @@ class MissingMemberFailure : public InvalidMemberRefFailure {
11881188
: InvalidMemberRefFailure(solution, baseType, memberName, locator) {}
11891189

11901190
SourceLoc getLoc() const override {
1191+
auto *locator = getLocator();
1192+
1193+
if (locator->findLast<LocatorPathElt::SyntacticElement>()) {
1194+
return constraints::getLoc(getAnchor());
1195+
}
1196+
11911197
// Diagnostic should point to the member instead of its base expression.
11921198
return constraints::getLoc(getRawAnchor());
11931199
}

test/expr/closure/multi_statement.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,18 @@ func test_no_crash_with_circular_ref_due_to_error() {
350350
return 0
351351
}
352352
}
353+
354+
func test_diagnosing_on_missing_member_in_case() {
355+
enum E {
356+
case one
357+
}
358+
359+
func test(_: (E) -> Void) {}
360+
361+
test {
362+
switch $0 {
363+
case .one: break
364+
case .unknown: break // expected-error {{type 'E' has no member 'unknown'}}
365+
}
366+
}
367+
}

0 commit comments

Comments
 (0)