Skip to content

Commit 9b33ffa

Browse files
authored
Merge pull request swiftlang#33272 from xedin/rdar-65254452
[Diagnostics] Do more checking before recording `force downcast` fix
2 parents 857dce9 + b47698b commit 9b33ffa

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

lib/Sema/BuilderTransform.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,13 @@ Optional<BraceStmt *> TypeChecker::applyFunctionBuilderBodyTransform(
15871587
// The system was salvaged; continue on as if nothing happened.
15881588
}
15891589

1590+
if (cs.isDebugMode()) {
1591+
auto &log = llvm::errs();
1592+
log << "--- Applying Solution ---\n";
1593+
solutions.front().dump(log);
1594+
log << '\n';
1595+
}
1596+
15901597
// FIXME: Shouldn't need to do this.
15911598
cs.applySolution(solutions.front());
15921599

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)