Skip to content

SIL: Fix SILType::isLoweringOf() to correctly handle opaque archetypes #77299

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 1 commit into from
Oct 31, 2024
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
9 changes: 4 additions & 5 deletions lib/SIL/IR/SILType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,12 +778,11 @@ bool SILType::hasAbstractionDifference(SILFunctionTypeRepresentation rep,

bool SILType::isLoweringOf(TypeExpansionContext context, SILModule &Mod,
CanType formalType) {
formalType =
substOpaqueTypesWithUnderlyingTypes(formalType, context)
->getCanonicalType();

SILType loweredType = *this;
if (formalType->hasOpaqueArchetype() &&
context.shouldLookThroughOpaqueTypeArchetypes() &&
loweredType.getASTType() ==
Mod.Types.getLoweredRValueType(context, formalType))
return true;

// Optional lowers its contained type.
SILType loweredObjectType = loweredType.getOptionalObjectType();
Expand Down
21 changes: 20 additions & 1 deletion test/SILGen/opaque_result_type_structural.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-emit-silgen -disable-availability-checking %s
// RUN: %target-swift-emit-silgen -target %target-swift-5.1-abi-triple %s | %FileCheck %s

public protocol P {}

Expand Down Expand Up @@ -36,3 +36,22 @@ extension P {
}
}

// Issues with metatypes
struct G<T> {}
struct S: P {}
extension G: P where T: P {}

func f2() -> G<some P>.Type {
return G<S>.self
}

func g() -> Any {
return f2()
}

// CHECK-LABEL: sil hidden [ossa] @$s29opaque_result_type_structural1gypyF : $@convention(thin) () -> @out Any {
// CHECK: bb0(%0 : $*Any):
// CHECK: [[METATYPE:%.*]] = metatype $@thick G<S>.Type
// CHECK: [[ADDR:%.*]] = init_existential_addr %0 : $*Any, $G<@_opaqueReturnTypeOf("$s29opaque_result_type_structural2f2AA1GVyQrGmyF", 0) __>.Type
// CHECK: store [[METATYPE]] to [trivial] [[ADDR]] : $*@thick G<S>.Type
// CHECK: return
110 changes: 110 additions & 0 deletions validation-test/compiler_crashers_2_fixed/rdar138655637.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// RUN: %target-swift-frontend -emit-ir -target %target-swift-5.9-abi-triple %s

protocol P {
var p: Int { get }
}

protocol Q {
func p(content: Int) -> Int
}

struct Zero: P {
var p: Int { 0 }
}

struct One: P {
var p: Int { 1 }
}

struct Add: Q {
let rhs: Int

func p(content: Int) -> Int {
content + rhs
}
}

struct Multiply: Q {
let rhs: Int

func p(content: Int) -> Int {
content * rhs
}
}

struct G<Content: P, each Modifier: Q>: P {
var content: Content
var modifiers: (repeat each Modifier)

init(content: Content, modifiers: (repeat each Modifier)) {
self.content = content
self.modifiers = modifiers
}

var p: Int {
var r = content.p
for m in repeat each modifiers {
r = m.p(content: r)
}
return r
}
}

extension G: Equatable where Content: Equatable,
repeat each Modifier: Equatable
{
static func ==(lhs: Self, rhs: Self) -> Bool {
guard lhs.content == rhs.content else { return false}
for (left, right) in repeat (each lhs.modifiers, each rhs.modifiers) {
guard left == right else { return false }
}
return true
}
}


extension G {
func modifier<T>(_ modifier: T) -> G<Content, repeat each Modifier, T> {
.init(content: content, modifiers: (repeat each modifiers, modifier))
}

func add(_ rhs: Int) -> G<Content, repeat each Modifier, some Q> {
modifier(Add(rhs: rhs))
}

func multiply(_ rhs: Int) -> G<Content, repeat each Modifier, some Q> {
modifier(Multiply(rhs: rhs))
}
}

extension P {
func modifier<T>(_ modifier: T) -> G<Self, T> {
return G(content: self, modifiers: modifier)
}

func add(_ rhs: Int) -> G<Self, some Q> {
modifier(Add(rhs: rhs))
}

func multiply(_ rhs: Int) -> G<Self, some Q> {
modifier(Multiply(rhs: rhs))
}
}

public func test() {
let r = Zero()
.multiply(1)
.multiply(2)
.add(3)
.multiply(4)
.add(2)
.multiply(6)
.add(2)
.multiply(6)
.add(2)
.multiply(6)

print(type(of: r))
}

test()