Skip to content

Commit d89c096

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
1 parent a0426df commit d89c096

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
@@ -988,8 +988,6 @@ bool MissingExplicitConversionFailure::diagnoseAsError() {
988988
return false;
989989

990990
bool useAs = TypeChecker::isExplicitlyConvertibleTo(fromType, toType, DC);
991-
if (!useAs && !TypeChecker::checkedCastMaySucceed(fromType, toType, DC))
992-
return false;
993991

994992
auto *expr = findParentExpr(anchor);
995993
if (!expr)

lib/Sema/CSSimplify.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2968,6 +2968,9 @@ static bool
29682968
repairViaBridgingCast(ConstraintSystem &cs, Type fromType, Type toType,
29692969
SmallVectorImpl<RestrictionOrFix> &conversionsOrFixes,
29702970
ConstraintLocatorBuilder locator) {
2971+
if (fromType->hasTypeVariable() || toType->hasTypeVariable())
2972+
return false;
2973+
29712974
auto objectType1 = fromType->getOptionalObjectType();
29722975
auto objectType2 = toType->getOptionalObjectType();
29732976

@@ -2986,6 +2989,9 @@ repairViaBridgingCast(ConstraintSystem &cs, Type fromType, Type toType,
29862989
if (!canBridgeThroughCast(cs, fromType, toType))
29872990
return false;
29882991

2992+
if (!TypeChecker::checkedCastMaySucceed(fromType, toType, cs.DC))
2993+
return false;
2994+
29892995
conversionsOrFixes.push_back(ForceDowncast::create(
29902996
cs, fromType, toType, cs.getConstraintLocator(locator)));
29912997
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)