Skip to content

Commit e2ad4a9

Browse files
authored
Merge pull request #83471 from xedin/rdar-150068895
[CSDiagnostics] Diagnose requirement failures in closure parameter po…
2 parents 8335ce5 + e5080a4 commit e2ad4a9

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,14 @@ ValueDecl *RequirementFailure::getDeclRef() const {
398398
}
399399
}
400400

401+
if (auto *closure = getAsExpr<ClosureExpr>(getRawAnchor())) {
402+
if (auto argElt =
403+
getLocator()->getFirstElementAs<LocatorPathElt::TupleElement>()) {
404+
auto *param = (*closure->getParameters())[argElt->getIndex()];
405+
return getAffectedDeclFromType(getType(param));
406+
}
407+
}
408+
401409
return getAffectedDeclFromType(getOwnerType());
402410
}
403411

test/Constraints/closures.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,3 +1366,29 @@ do {
13661366
try $0.missing // expected-error {{value of type 'Int' has no member 'missing'}}
13671367
}
13681368
}
1369+
1370+
// Generic requirement failures associated with closure parameters should be diagnosed.
1371+
protocol Input {
1372+
associatedtype Value
1373+
var value: Value { get }
1374+
}
1375+
1376+
protocol Idable {
1377+
associatedtype ID
1378+
}
1379+
1380+
struct TestInput<ItemID: Hashable, Value: Collection<ItemID>> : Input { // expected-note {{where 'Value' = 'Item.ID'}}
1381+
var value: Value
1382+
}
1383+
1384+
struct Container<I: Input> {
1385+
var data: (I) -> Void
1386+
}
1387+
1388+
func test_generic_closure_parameter_requirement_failure<Item: Idable>(
1389+
for itemType: Item.Type = Item.self,
1390+
_ payload: @escaping (_ itemID: Item.ID) -> Void
1391+
) where Item.ID : Sendable {
1392+
Container(data: { (input: TestInput) in payload(input.value) })
1393+
// expected-error@-1 {{generic struct 'TestInput' requires that 'Item.ID' conform to 'Collection'}}
1394+
}

0 commit comments

Comments
 (0)