Skip to content

Commit

Permalink
[CSDiagnostics] Verify that member is in collection context
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
xedin committed Aug 17, 2022
1 parent 5bfef4f commit 3ebc541
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
13 changes: 5 additions & 8 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3976,17 +3976,14 @@ bool MissingMemberFailure::diagnoseInLiteralCollectionContext() const {
if (!parentExpr)
return false;

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

if (!parentType->isKnownStdlibCollectionType() && !parentType->is<TupleType>())
if (!isExpr<CollectionExpr>(parentExpr))
return false;

if (isa<TupleExpr>(parentExpr)) {
parentExpr = findParentExpr(parentExpr);
if (!parentExpr)
return false;
}

if (auto *defaultableVar =
getRawType(parentExpr)->getAs<TypeVariableType>()) {
if (solution.DefaultedConstraints.count(
Expand Down
20 changes: 20 additions & 0 deletions test/expr/closure/multi_statement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -546,3 +546,23 @@ func test_conflicting_pattern_vars() {
}
}
}

// rdar://91452726 - crash in MissingMemberFailure::diagnoseInLiteralCollectionContext
struct Test {
struct ID {
}

enum E : Hashable, Equatable {
case id
}

var arr: [(ID, E)]

func test() {
_ = arr.map { v in
switch v {
case .id: return true // expected-error {{value of tuple type '(Test.ID, Test.E)' has no member 'id'}}
}
}
}
}

0 comments on commit 3ebc541

Please sign in to comment.