From d89c096af75524dd5e1cf7bc2d1d0fb43997db9c Mon Sep 17 00:00:00 2001 From: Pavel Yaskevich Date: Mon, 3 Aug 2020 16:24:19 -0700 Subject: [PATCH 1/2] [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 --- lib/Sema/CSDiagnostics.cpp | 2 -- lib/Sema/CSSimplify.cpp | 6 ++++++ test/Constraints/rdar65254452.swift | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 test/Constraints/rdar65254452.swift diff --git a/lib/Sema/CSDiagnostics.cpp b/lib/Sema/CSDiagnostics.cpp index 9409da96097cb..433c0e9d64733 100644 --- a/lib/Sema/CSDiagnostics.cpp +++ b/lib/Sema/CSDiagnostics.cpp @@ -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) diff --git a/lib/Sema/CSSimplify.cpp b/lib/Sema/CSSimplify.cpp index fbc473622529d..6e882b6c38e98 100644 --- a/lib/Sema/CSSimplify.cpp +++ b/lib/Sema/CSSimplify.cpp @@ -2968,6 +2968,9 @@ static bool repairViaBridgingCast(ConstraintSystem &cs, Type fromType, Type toType, SmallVectorImpl &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; diff --git a/test/Constraints/rdar65254452.swift b/test/Constraints/rdar65254452.swift new file mode 100644 index 0000000000000..7e49b45f3c716 --- /dev/null +++ b/test/Constraints/rdar65254452.swift @@ -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]'}} +} From b47698bd544af93e0e55736a97f77828bcd4deb1 Mon Sep 17 00:00:00 2001 From: Pavel Yaskevich Date: Mon, 3 Aug 2020 16:44:32 -0700 Subject: [PATCH 2/2] [TypeChecker] NFC: Add debug logging before applying solution in `applyFunctionBuilderBodyTransform` --- lib/Sema/BuilderTransform.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/Sema/BuilderTransform.cpp b/lib/Sema/BuilderTransform.cpp index a0ecaae819918..b00deec91536f 100644 --- a/lib/Sema/BuilderTransform.cpp +++ b/lib/Sema/BuilderTransform.cpp @@ -1564,6 +1564,13 @@ Optional TypeChecker::applyFunctionBuilderBodyTransform( // The system was salvaged; continue on as if nothing happened. } + if (cs.isDebugMode()) { + auto &log = llvm::errs(); + log << "--- Applying Solution ---\n"; + solutions.front().dump(log); + log << '\n'; + } + // FIXME: Shouldn't need to do this. cs.applySolution(solutions.front());