Skip to content

Commit 16ac70f

Browse files
[CSDiagnostics] Improving inout to pointer argument conversions with optionals diagnostics
1 parent b255b09 commit 16ac70f

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,9 @@ bool GenericArgumentsMismatchFailure::diagnoseAsError() {
675675
// InoutToPointer argument mismatch failure.
676676
if (isa<InOutExpr>(anchor)) {
677677
diagnostic = diag::cannot_convert_argument_value;
678+
auto applyInfo = getFunctionArgApplyInfo(getLocator());
679+
if (applyInfo)
680+
toType = applyInfo->getParamType();
678681
}
679682
break;
680683
}

test/Constraints/diagnostics.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,3 +1443,30 @@ func assignGenericMismatch() {
14431443
// expected-note@-3 {{coalesce using '??' to provide a default when the optional value contains 'nil'}}
14441444
// expected-note@-4 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
14451445
}
1446+
1447+
// [Int] to [String]? argument to param conversion
1448+
let value: [Int] = []
1449+
func gericArgToParamOptional(_ param: [String]?) {}
1450+
1451+
gericArgToParamOptional(value) // expected-error {{convert value of type '[Int]' to expected argument type '[String]?'}}
1452+
// expected-note@-1 {{arguments to generic parameter 'Element' ('Int' and 'String') are expected to be equal}}
1453+
1454+
// Inout Expr conversions
1455+
func gericArgToParamInout1(_ x: inout [[Int]]) {}
1456+
func gericArgToParamInout2(_ x: inout [[String]]) {
1457+
gericArgToParamInout1(&x) // expected-error {{cannot convert value of type '[[String]]' to expected argument type '[[Int]]'}}
1458+
// expected-note@-1 {{arguments to generic parameter 'Element' ('String' and 'Int') are expected to be equal}}
1459+
}
1460+
1461+
func gericArgToParamInoutOptional(_ x: inout [[String]]?) {
1462+
gericArgToParamInout1(&x) // expected-error {{cannot convert value of type '[[String]]?' to expected argument type '[[Int]]'}}
1463+
// expected-note@-1 {{arguments to generic parameter 'Element' ('String' and 'Int') are expected to be equal}}
1464+
// expected-error@-2 {{value of optional type '[[String]]?' must be unwrapped to a value of type '[[String]]'}}
1465+
// expected-note@-3 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
1466+
}
1467+
1468+
func gericArgToParamInout(_ x: inout [[Int]]) { // expected-note {{change variable type to '[[String]]?' if it doesn't need to be declared as '[[Int]]'}}
1469+
gericArgToParamInoutOptional(&x) // expected-error {{cannot convert value of type '[[Int]]' to expected argument type '[[String]]?'}}
1470+
// expected-note@-1 {{arguments to generic parameter 'Element' ('Int' and 'String') are expected to be equal}}
1471+
// expected-error@-2 {{inout argument could be set to a value with a type other than '[[Int]]'; use a value declared as type '[[String]]?' instead}}
1472+
}

test/Constraints/valid_pointer_conversions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ func givesPtr(_ str: String) {
3131
takesDoubleOptionalPtr(i) // expected-error {{cannot convert value of type 'Int' to expected argument type 'UnsafeRawPointer??'}}
3232
takesMutableDoubleOptionalPtr(arr) // expected-error {{cannot convert value of type '[Int]' to expected argument type 'UnsafeMutableRawPointer??'}}
3333

34-
takesMutableDoubleOptionalTypedPtr(&i) // expected-error {{cannot convert value of type 'UnsafeMutablePointer<Int>' to expected argument type 'UnsafeMutablePointer<Double>'}}
34+
takesMutableDoubleOptionalTypedPtr(&i) // expected-error {{cannot convert value of type 'UnsafeMutablePointer<Int>' to expected argument type 'UnsafeMutablePointer<Double>??'}}
3535
// expected-note@-1 {{arguments to generic parameter 'Pointee' ('Int' and 'Double') are expected to be equal}}
3636
}
3737

3838
// SR12382
3939
func SR12382(_ x: UnsafeMutablePointer<Double>??) {}
4040

4141
var i = 0
42-
SR12382(&i) // expected-error {{cannot convert value of type 'UnsafeMutablePointer<Int>' to expected argument type 'UnsafeMutablePointer<Double>'}}
42+
SR12382(&i) // expected-error {{cannot convert value of type 'UnsafeMutablePointer<Int>' to expected argument type 'UnsafeMutablePointer<Double>??'}}
4343
// expected-note@-1 {{arguments to generic parameter 'Pointee' ('Int' and 'Double') are expected to be equal}}

0 commit comments

Comments
 (0)