Skip to content

Commit 6d90a84

Browse files
committed
[5.1] CastOptimizer: Fix for opaque archetypes
The fallthrough path of this function assumes failure :(. rdar://50544445
1 parent 3dd0e4c commit 6d90a84

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/SIL/DynamicCasts.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ swift::classifyDynamicCast(ModuleDecl *M,
307307
bool isWholeModuleOpts) {
308308
if (source == target) return DynamicCastFeasibility::WillSucceed;
309309

310+
// Return a conservative answer for opaque archetypes for now.
311+
if (source->hasOpaqueArchetype() || target->hasOpaqueArchetype())
312+
return DynamicCastFeasibility::MaySucceed;
313+
310314
auto sourceObject = source.getOptionalObjectType();
311315
auto targetObject = target.getOptionalObjectType();
312316

test/SILOptimizer/cast_folding.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %target-swift-frontend -O -emit-sil %s | %FileCheck %s
2+
// RUN: %target-swift-frontend -Onone -emit-sil %s | %FileCheck %s --check-prefix=MANDATORY
23
// We want to check two things here:
34
// - Correctness
45
// - That certain "is" checks are eliminated based on static analysis at compile-time
@@ -1065,6 +1066,24 @@ public func testCastToPForOptionalFailure() -> Bool {
10651066
return testCastToPForOptional(t)
10661067
}
10671068

1069+
struct Underlying : P {
1070+
}
1071+
1072+
func returnOpaque() -> some P {
1073+
return Underlying()
1074+
}
1075+
1076+
// MANDATORY-LABEL: sil{{.*}} @$s12cast_folding23testCastOpaqueArchetypeyyF
1077+
// MANDATORY: [[O:%.*]] = alloc_stack $@_opaqueReturnTypeOf("$s12cast_folding12returnOpaqueQryF", 0)
1078+
// MANDATORY: [[F:%.*]] = function_ref @$s12cast_folding12returnOpaqueQryF
1079+
// MANDATORY: apply [[F]]([[O]])
1080+
// MANDATORY: [[U:%.*]] = alloc_stack $Underlying
1081+
// MANDATORY: unconditional_checked_cast_addr @_opaqueReturnTypeOf{{.*}}in [[O]] : $*@_opaqueReturnTypeOf{{.*}}to Underlying in [[U]] : $*Underlying
1082+
// MANDATORY: load [[U]] : $*Underlying
1083+
public func testCastOpaqueArchetype() {
1084+
let o = returnOpaque() as! Underlying
1085+
}
1086+
10681087
print("test0=\(test0())")
10691088

10701089
print("test1=\(test1())")

0 commit comments

Comments
 (0)