[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:
Pavel Yaskevich
2019-08-30 17:05:39 -07:00
parent 6d5cf3e43d
commit 1a57fc403a
2 changed files with 19 additions and 0 deletions

View File

@@ -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;
}