Skip to content

Commit 2b58a75

Browse files
committed
[Diagnostics] Fix requirement note to properly handle layout requirements
Layout requirement doesn't have a second type since it's always `AnyObject`, requirement failure note should handle that, otherwise in no assertion builds this would crash in diagnostic engine trying to emit the note. Resolves: rdar://79672230
1 parent 51fe32d commit 2b58a75

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,14 @@ bool RequirementFailure::diagnoseAsNote() {
399399
const auto &req = getRequirement();
400400
const auto *reqDC = getRequirementDC();
401401

402+
// Layout requirement doesn't have a second type, let's always
403+
// `AnyObject`.
404+
auto requirementTy = req.getKind() == RequirementKind::Layout
405+
? getASTContext().getAnyObjectType()
406+
: req.getSecondType();
407+
402408
emitDiagnosticAt(reqDC->getAsDecl(), getDiagnosticAsNote(), getLHS(),
403-
getRHS(), req.getFirstType(), req.getSecondType());
409+
getRHS(), req.getFirstType(), requirementTy);
404410
return true;
405411
}
406412

test/Constraints/overload.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,14 @@ func test_no_hole_propagation() {
246246
return arguments.reduce(0, +) // expected-error {{cannot convert value of type 'Int' to expected argument type 'String'}}
247247
}
248248
}
249+
250+
// rdar://79672230 - crash due to unsatisfied `: AnyObject` requirement
251+
func rdar79672230() {
252+
struct MyType {}
253+
254+
func test(_ representation: MyType) -> Bool {} // expected-note {{found candidate with type 'MyType'}}
255+
func test<T>(_ object: inout T) -> Bool where T : AnyObject {} // expected-note {{candidate requires that 'MyType' conform to 'AnyObject' (requirement specified as 'T' : 'AnyObject')}}
256+
257+
var t: MyType = MyType()
258+
test(&t) // expected-error {{no exact matches in call to local function 'test'}}
259+
}

0 commit comments

Comments
 (0)