Skip to content

[4.0] Yet more targeted fixes #9009

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
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
6 changes: 4 additions & 2 deletions lib/SILOptimizer/Utils/Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1661,6 +1661,9 @@ optimizeBridgedSwiftToObjCCast(SILInstruction *Inst,
if ((ConvTy == DestTy || DestTy.isExactSuperclassOf(ConvTy))) {
CastedValue = SILValue(
(ConvTy == DestTy) ? NewI : Builder.createUpcast(Loc, NewAI, DestTy));
} else if (ConvTy.isExactSuperclassOf(DestTy)) {
CastedValue = SILValue(
Builder.createUnconditionalCheckedCast(Loc, NewAI, DestTy));
} else if (ConvTy.getSwiftRValueType() ==
getNSBridgedClassOfCFClass(M.getSwiftModule(),
DestTy.getSwiftRValueType()) ||
Expand All @@ -1673,8 +1676,7 @@ optimizeBridgedSwiftToObjCCast(SILInstruction *Inst,
} else {
llvm_unreachable(
"Destination should have the same type, be bridgeable CF "
"type or be a superclass "
"of the source operand");
"type or be a superclass/subclass of the source operand");
}
NewI = Builder.createStore(Loc, CastedValue, Dest,
StoreOwnershipQualifier::Unqualified);
Expand Down
9 changes: 4 additions & 5 deletions lib/Sema/TypeCheckError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -665,18 +665,17 @@ class ApplyClassifier {
Classification classifyRethrowsArgument(Expr *arg, Type paramType) {
arg = arg->getValueProvidingExpr();

// If the parameter was a tuple, try to look through the various
// tuple operations.
if (auto paramTupleType = paramType->getAs<TupleType>()) {
// If the parameter was structurally a tuple, try to look through the
// various tuple operations.
if (auto paramTupleType = dyn_cast<TupleType>(paramType.getPointer())) {
if (auto tuple = dyn_cast<TupleExpr>(arg)) {
return classifyTupleRethrowsArgument(tuple, paramTupleType);
} else if (auto shuffle = dyn_cast<TupleShuffleExpr>(arg)) {
return classifyShuffleRethrowsArgument(shuffle, paramTupleType);
}

int scalarElt = paramTupleType->getElementForScalarInit();
if (scalarElt < 0 ||
!paramTupleType->getElementType(scalarElt)->isEqual(arg->getType())) {
if (scalarElt < 0) {
// Otherwise, we're passing an opaque tuple expression, and we
// should treat it as contributing to 'rethrows' if the original
// parameter type included a throwing function type.
Expand Down
12 changes: 12 additions & 0 deletions test/SILOptimizer/bridged_casts_folding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -900,4 +900,16 @@ public func testCondCastSwiftToCFSetInt() -> CFSet? {
return setOpt
}

public class NSObjectSubclass : NSObject { }

var anyHashable: AnyHashable = 0

// CHECK-LABEL: _T021bridged_casts_folding29testUncondCastSwiftToSubclassAA08NSObjectI0CyF
// CHECK: [[GLOBAL:%[0-9]+]] = global_addr @_T021bridged_casts_folding11anyHashables03AnyE0Vv
// CHECK: function_ref @_T0s11AnyHashableV10FoundationE19_bridgeToObjectiveCSo8NSObjectCyF
// CHECK-NEXT: apply
// CHECK-NEXT: unconditional_checked_cast {{%.*}} : $NSObject to $NSObjectSubclass
@inline(never)
public func testUncondCastSwiftToSubclass() -> NSObjectSubclass {
return anyHashable as! NSObjectSubclass
}
5 changes: 5 additions & 0 deletions test/decl/func/rethrows.swift
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,11 @@ func throwWhileGettingFoo() throws -> Foo.Type { return Foo.self }
// <rdar://problem/31794932> [Source compatibility] Call to sort(by):) can throw, but is not marked with 'try'
func doRethrow(fn: (Int, Int) throws -> Int) rethrows { }

struct DoRethrowGeneric<T> {
func method(fn: (T, T) throws -> T) rethrows { }
}

func testDoRethrow() {
doRethrow(fn:) { (a, b) in return a }
DoRethrowGeneric<Int>().method(fn:) { (a, b) in return a }
}