Skip to content

[Diagnostics] Do more checking before recording force downcast fix #33272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/Sema/BuilderTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,13 @@ Optional<BraceStmt *> 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());

Expand Down
2 changes: 0 additions & 2 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2968,6 +2968,9 @@ static bool
repairViaBridgingCast(ConstraintSystem &cs, Type fromType, Type toType,
SmallVectorImpl<RestrictionOrFix> &conversionsOrFixes,
ConstraintLocatorBuilder locator) {
if (fromType->hasTypeVariable() || toType->hasTypeVariable())
return false;

auto objectType1 = fromType->getOptionalObjectType();
auto objectType2 = toType->getOptionalObjectType();

Expand All @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions test/Constraints/rdar65254452.swift
Original file line number Diff line number Diff line change
@@ -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]'}}
}