Skip to content

Commit b255b09

Browse files
[test] Adding test cases for assign expr in GenericMismatch diagnostics
1 parent 6652d03 commit b255b09

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ bool GenericArgumentsMismatchFailure::diagnoseAsError() {
698698
}
699699

700700
if (!diagnostic) {
701-
// In cases like `[[Int]]` vs. `[[String]]`
701+
// Handle all mismatches involving an `AssignExpr`
702702
if (auto *assignExpr = dyn_cast<AssignExpr>(anchor)) {
703703
diagnostic = getDiagnosticFor(CTP_AssignSource);
704704
fromType = getType(assignExpr->getSrc());

test/Constraints/diagnostics.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,3 +1429,17 @@ func genericFunc<SR_12242_T: SR_12242_P>(_ completion: @escaping (SR_12242_R<[S
14291429
completion(t) // expected-error {{cannot convert value of type 'diagnostics.SR_12242_R<[diagnostics.SR_12242_T]>' to expected argument type 'diagnostics.SR_12242_R<[SR_12242_T]>'}}
14301430
// expected-note@-1 {{arguments to generic parameter 'Element' ('diagnostics.SR_12242_T' and 'SR_12242_T') are expected to be equal}}
14311431
}
1432+
1433+
func assignGenericMismatch() {
1434+
var a: [Int]?
1435+
var b: [String]
1436+
1437+
a = b // expected-error {{cannot assign value of type '[String]' to type '[Int]?'}}
1438+
// expected-note@-1 {{arguments to generic parameter 'Element' ('String' and 'Int') are expected to be equal}}
1439+
1440+
b = a // expected-error {{cannot assign value of type '[Int]' to type '[String]'}}
1441+
// expected-note@-1 {{arguments to generic parameter 'Element' ('Int' and 'String') are expected to be equal}}
1442+
// expected-error@-2 {{value of optional type '[Int]?' must be unwrapped to a value of type '[Int]'}}
1443+
// expected-note@-3 {{coalesce using '??' to provide a default when the optional value contains 'nil'}}
1444+
// expected-note@-4 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
1445+
}

0 commit comments

Comments
 (0)