[Diagnostics] Do more checking before recording force downcast fix

Solver should do more checking upfront before recording
`force downcast` fix, to make sure that it's indeed always
applicable when recorded, otherwise it would be possible
to misdiagnose or omit diagnostics in certain situations.

Resolves: rdar://problem/65254452
This commit is contained in:
Pavel Yaskevich
2020-08-03 16:24:19 -07:00
parent a0426df7a7
commit d89c096af7
3 changed files with 23 additions and 2 deletions

View File

@@ -988,8 +988,6 @@ bool MissingExplicitConversionFailure::diagnoseAsError() {
return false;
bool useAs = TypeChecker::isExplicitlyConvertibleTo(fromType, toType, DC);
if (!useAs && !TypeChecker::checkedCastMaySucceed(fromType, toType, DC))
return false;
auto *expr = findParentExpr(anchor);
if (!expr)

View File

@@ -2968,6 +2968,9 @@ static bool
repairViaBridgingCast(ConstraintSystem &cs, Type fromType, Type toType,
SmallVectorImpl<RestrictionOrFix> &conversionsOrFixes,
ConstraintLocatorBuilder locator) {
if (fromType->hasTypeVariable() || toType->hasTypeVariable())
return false;
auto objectType1 = fromType->getOptionalObjectType();
auto objectType2 = toType->getOptionalObjectType();
@@ -2986,6 +2989,9 @@ repairViaBridgingCast(ConstraintSystem &cs, Type fromType, Type toType,
if (!canBridgeThroughCast(cs, fromType, toType))
return false;
if (!TypeChecker::checkedCastMaySucceed(fromType, toType, cs.DC))
return false;
conversionsOrFixes.push_back(ForceDowncast::create(
cs, fromType, toType, cs.getConstraintLocator(locator)));
return true;

View File

@@ -0,0 +1,17 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -verify %s
// REQUIRES: objc_interop
import Foundation
class Obj: NSObject {
}
class Container {
var objects: [Obj]
init(objects: [Obj]) {}
}
func test(other: Container) {
_ = Container(objects: other)
// expected-error@-1 {{cannot convert value of type 'Container' to expected argument type '[Obj]'}}
}