Skip to content

Commit 09968d2

Browse files
committed
[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 (cherry picked from commit d89c096)
1 parent 5d3b598 commit 09968d2

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,8 +951,6 @@ bool MissingExplicitConversionFailure::diagnoseAsError() {
951951
return false;
952952

953953
bool useAs = TypeChecker::isExplicitlyConvertibleTo(fromType, toType, DC);
954-
if (!useAs && !TypeChecker::checkedCastMaySucceed(fromType, toType, DC))
955-
return false;
956954

957955
auto *expr = findParentExpr(anchor);
958956
if (!expr)

lib/Sema/CSSimplify.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2913,6 +2913,9 @@ static bool
29132913
repairViaBridgingCast(ConstraintSystem &cs, Type fromType, Type toType,
29142914
SmallVectorImpl<RestrictionOrFix> &conversionsOrFixes,
29152915
ConstraintLocatorBuilder locator) {
2916+
if (fromType->hasTypeVariable() || toType->hasTypeVariable())
2917+
return false;
2918+
29162919
auto objectType1 = fromType->getOptionalObjectType();
29172920
auto objectType2 = toType->getOptionalObjectType();
29182921

@@ -2931,6 +2934,9 @@ repairViaBridgingCast(ConstraintSystem &cs, Type fromType, Type toType,
29312934
if (!canBridgeThroughCast(cs, fromType, toType))
29322935
return false;
29332936

2937+
if (!TypeChecker::checkedCastMaySucceed(fromType, toType, cs.DC))
2938+
return false;
2939+
29342940
conversionsOrFixes.push_back(ForceDowncast::create(
29352941
cs, fromType, toType, cs.getConstraintLocator(locator)));
29362942
return true;

test/Constraints/rdar65254452.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -verify %s
2+
// REQUIRES: objc_interop
3+
4+
import Foundation
5+
6+
class Obj: NSObject {
7+
}
8+
9+
class Container {
10+
var objects: [Obj]
11+
init(objects: [Obj]) {}
12+
}
13+
14+
func test(other: Container) {
15+
_ = Container(objects: other)
16+
// expected-error@-1 {{cannot convert value of type 'Container' to expected argument type '[Obj]'}}
17+
}

0 commit comments

Comments
 (0)