mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[ConstraintSystem] Make sure that l-value is preserved in argument positions
There is logic in `matchTypes` which would unwrap l-value if other type is not an `inout`, which is not great for cases where parameter type is a pointer and argument is an l-value which requires explicit `&` to be converted.
This commit is contained in:
@@ -4466,6 +4466,19 @@ bool ArgumentMismatchFailure::diagnoseAsError() {
|
||||
diagnostic = diag::cannot_convert_argument_value_protocol;
|
||||
|
||||
auto diag = emitDiagnostic(getLoc(), diagnostic, argType, paramType);
|
||||
|
||||
// If argument is an l-value type and parameter is a pointer type,
|
||||
// let's match up its element type to the argument to see whether
|
||||
// it would be appropriate to suggest adding `&`.
|
||||
auto *argExpr = getAnchor();
|
||||
if (getType(argExpr)->is<LValueType>()) {
|
||||
auto elementTy = paramType->getAnyPointerElementType();
|
||||
if (elementTy && argType->isEqual(elementTy)) {
|
||||
diag.fixItInsert(argExpr->getStartLoc(), "&");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
tryFixIts(diag);
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user