Merge pull request #26459 from xedin/diag-conversion-to-specified-type

[ConstraintSystem] Add a fix to ignore contextual type mismatch
This commit is contained in:
Pavel Yaskevich
2019-08-14 12:58:09 -07:00
committed by GitHub
29 changed files with 845 additions and 701 deletions

View File

@@ -2739,6 +2739,43 @@ bool constraints::isAutoClosureArgument(Expr *argExpr) {
return false;
}
bool constraints::conformsToKnownProtocol(ConstraintSystem &cs, Type type,
KnownProtocolKind protocol) {
if (auto *proto = cs.TC.getProtocol(SourceLoc(), protocol))
return bool(TypeChecker::conformsToProtocol(
type, proto, cs.DC, ConformanceCheckFlags::InExpression));
return false;
}
/// Check whether given type conforms to `RawPepresentable` protocol
/// and return the witness type.
Type constraints::isRawRepresentable(ConstraintSystem &cs, Type type) {
auto &TC = cs.TC;
auto *DC = cs.DC;
auto rawReprType =
TC.getProtocol(SourceLoc(), KnownProtocolKind::RawRepresentable);
if (!rawReprType)
return Type();
auto conformance = TypeChecker::conformsToProtocol(
type, rawReprType, DC, ConformanceCheckFlags::InExpression);
if (!conformance)
return Type();
return conformance->getTypeWitnessByName(type, TC.Context.Id_RawValue);
}
Type constraints::isRawRepresentable(
ConstraintSystem &cs, Type type,
KnownProtocolKind rawRepresentableProtocol) {
Type rawTy = isRawRepresentable(cs, type);
if (!rawTy || !conformsToKnownProtocol(cs, rawTy, rawRepresentableProtocol))
return Type();
return rawTy;
}
void ConstraintSystem::generateConstraints(
SmallVectorImpl<Constraint *> &constraints, Type type,
ArrayRef<OverloadChoice> choices, DeclContext *useDC,