mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[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:
@@ -988,8 +988,6 @@ bool MissingExplicitConversionFailure::diagnoseAsError() {
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
bool useAs = TypeChecker::isExplicitlyConvertibleTo(fromType, toType, DC);
|
bool useAs = TypeChecker::isExplicitlyConvertibleTo(fromType, toType, DC);
|
||||||
if (!useAs && !TypeChecker::checkedCastMaySucceed(fromType, toType, DC))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
auto *expr = findParentExpr(anchor);
|
auto *expr = findParentExpr(anchor);
|
||||||
if (!expr)
|
if (!expr)
|
||||||
|
|||||||
@@ -2968,6 +2968,9 @@ static bool
|
|||||||
repairViaBridgingCast(ConstraintSystem &cs, Type fromType, Type toType,
|
repairViaBridgingCast(ConstraintSystem &cs, Type fromType, Type toType,
|
||||||
SmallVectorImpl<RestrictionOrFix> &conversionsOrFixes,
|
SmallVectorImpl<RestrictionOrFix> &conversionsOrFixes,
|
||||||
ConstraintLocatorBuilder locator) {
|
ConstraintLocatorBuilder locator) {
|
||||||
|
if (fromType->hasTypeVariable() || toType->hasTypeVariable())
|
||||||
|
return false;
|
||||||
|
|
||||||
auto objectType1 = fromType->getOptionalObjectType();
|
auto objectType1 = fromType->getOptionalObjectType();
|
||||||
auto objectType2 = toType->getOptionalObjectType();
|
auto objectType2 = toType->getOptionalObjectType();
|
||||||
|
|
||||||
@@ -2986,6 +2989,9 @@ repairViaBridgingCast(ConstraintSystem &cs, Type fromType, Type toType,
|
|||||||
if (!canBridgeThroughCast(cs, fromType, toType))
|
if (!canBridgeThroughCast(cs, fromType, toType))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
if (!TypeChecker::checkedCastMaySucceed(fromType, toType, cs.DC))
|
||||||
|
return false;
|
||||||
|
|
||||||
conversionsOrFixes.push_back(ForceDowncast::create(
|
conversionsOrFixes.push_back(ForceDowncast::create(
|
||||||
cs, fromType, toType, cs.getConstraintLocator(locator)));
|
cs, fromType, toType, cs.getConstraintLocator(locator)));
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
17
test/Constraints/rdar65254452.swift
Normal file
17
test/Constraints/rdar65254452.swift
Normal 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]'}}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user