Skip to content

Commit 3ebc541

Browse files
committed
[CSDiagnostics] Verify that member is in collection context
`MissingMemberFailure::diagnoseInLiteralCollectionContext` should verify that a parent (or parent of a parent) expression is indeed a collection expression instead of checking types. Resolves: rdar://91452726
1 parent 5bfef4f commit 3ebc541

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3976,17 +3976,14 @@ bool MissingMemberFailure::diagnoseInLiteralCollectionContext() const {
39763976
if (!parentExpr)
39773977
return false;
39783978

3979-
auto parentType = getType(parentExpr);
3979+
// This could happen if collection is a dictionary literal i.e.
3980+
// ["a": .test] - the element is a tuple - ("a", .test).
3981+
if (isExpr<TupleExpr>(parentExpr))
3982+
parentExpr = findParentExpr(parentExpr);
39803983

3981-
if (!parentType->isKnownStdlibCollectionType() && !parentType->is<TupleType>())
3984+
if (!isExpr<CollectionExpr>(parentExpr))
39823985
return false;
39833986

3984-
if (isa<TupleExpr>(parentExpr)) {
3985-
parentExpr = findParentExpr(parentExpr);
3986-
if (!parentExpr)
3987-
return false;
3988-
}
3989-
39903987
if (auto *defaultableVar =
39913988
getRawType(parentExpr)->getAs<TypeVariableType>()) {
39923989
if (solution.DefaultedConstraints.count(

test/expr/closure/multi_statement.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,3 +546,23 @@ func test_conflicting_pattern_vars() {
546546
}
547547
}
548548
}
549+
550+
// rdar://91452726 - crash in MissingMemberFailure::diagnoseInLiteralCollectionContext
551+
struct Test {
552+
struct ID {
553+
}
554+
555+
enum E : Hashable, Equatable {
556+
case id
557+
}
558+
559+
var arr: [(ID, E)]
560+
561+
func test() {
562+
_ = arr.map { v in
563+
switch v {
564+
case .id: return true // expected-error {{value of tuple type '(Test.ID, Test.E)' has no member 'id'}}
565+
}
566+
}
567+
}
568+
}

0 commit comments

Comments
 (0)