Skip to content

CastOptimizer: Fix for opaque archetypes #24576

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
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
4 changes: 4 additions & 0 deletions lib/SIL/DynamicCasts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ swift::classifyDynamicCast(ModuleDecl *M,
bool isWholeModuleOpts) {
if (source == target) return DynamicCastFeasibility::WillSucceed;

// Return a conservative answer for opaque archetypes for now.
if (source->hasOpaqueArchetype() || target->hasOpaqueArchetype())
return DynamicCastFeasibility::MaySucceed;

auto sourceObject = source.getOptionalObjectType();
auto targetObject = target.getOptionalObjectType();

Expand Down
19 changes: 19 additions & 0 deletions test/SILOptimizer/cast_folding.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %target-swift-frontend -O -emit-sil %s | %FileCheck %s
// RUN: %target-swift-frontend -Onone -emit-sil %s | %FileCheck %s --check-prefix=MANDATORY
// We want to check two things here:
// - Correctness
// - That certain "is" checks are eliminated based on static analysis at compile-time
Expand Down Expand Up @@ -1065,6 +1066,24 @@ public func testCastToPForOptionalFailure() -> Bool {
return testCastToPForOptional(t)
}

struct Underlying : P {
}

func returnOpaque() -> some P {
return Underlying()
}

// MANDATORY-LABEL: sil{{.*}} @$s12cast_folding23testCastOpaqueArchetypeyyF
// MANDATORY: [[O:%.*]] = alloc_stack $@_opaqueReturnTypeOf("$s12cast_folding12returnOpaqueQryF", 0)
// MANDATORY: [[F:%.*]] = function_ref @$s12cast_folding12returnOpaqueQryF
// MANDATORY: apply [[F]]([[O]])
// MANDATORY: [[U:%.*]] = alloc_stack $Underlying
// MANDATORY: unconditional_checked_cast_addr @_opaqueReturnTypeOf{{.*}}in [[O]] : $*@_opaqueReturnTypeOf{{.*}}to Underlying in [[U]] : $*Underlying
// MANDATORY: load [[U]] : $*Underlying
public func testCastOpaqueArchetype() {
let o = returnOpaque() as! Underlying
}

print("test0=\(test0())")

print("test1=\(test1())")
Expand Down