Skip to content

[5.1] IRGen: Save the current generic signature before mangling an opaque type decl #27022

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
2 changes: 2 additions & 0 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,9 @@ void ASTMangler::appendOpaqueDeclName(const OpaqueTypeDecl *opaqueDecl) {
if (canSymbolicReference(opaqueDecl)) {
appendSymbolicReference(opaqueDecl);
} else if (auto namingDecl = opaqueDecl->getNamingDecl()) {
CanGenericSignature savedSignature = CurGenericSignature;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should CurGenericSignature be a stack then, with an RAII utility to save and restore the signature?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

llvm::SaveAndRestore is a good way to indicate what's happening here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

appendEntity(namingDecl);
CurGenericSignature = savedSignature;
appendOperator("QO");
} else {
llvm_unreachable("todo: independent opaque type decls");
Expand Down
73 changes: 73 additions & 0 deletions test/IRGen/Inputs/mangle-opaque-return-types-A.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
public protocol Proto {
associatedtype Assoc : Proto
var value: Assoc { get }
}

extension Never : Proto {}

extension Never {
public typealias Assoc = Never

public var value: Never {
switch self {}
}
}
protocol PrimitiveProto : Proto {}

extension PrimitiveProto {
public var value: Never { valueError() }
}

extension Proto {
func valueError() -> Never {
fatalError("value() should not be called on \(Self.self).")
}
}

public struct EmptyProto : PrimitiveProto {
public init() {}
}

struct M<Content: Proto> : Proto {
var t: Content

init(_ t: Content) {
self.t = t
}

var value: some Proto {
return t.value
}
}

public struct Group<T> {
var t: T

public init(_ t: T) {
self.t = t
}
}

extension Group : Proto, PrimitiveProto where T : Proto {
public typealias Assoc = Never
}

public struct Choice<T, V>{
var v: V

public init(_ t: T, _ v: V) {
self.v = v
}
}

extension Choice : Proto where T: Proto, V: Proto {
public var value: some Proto {
return v.value
}
}

extension Proto {
public func add() -> some Proto {
return M(self)
}
}
19 changes: 19 additions & 0 deletions test/IRGen/mangle-opaque-return-type.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -disable-availability-checking -emit-module -enable-library-evolution -emit-module-path=%t/A.swiftmodule -module-name=A %S/Inputs/mangle-opaque-return-types-A.swift
// RUN: %target-swift-frontend -disable-availability-checking -I %t -emit-ir %s
import A

public struct C<T, Content: Proto> {
let data: T
let content: Content

init(_ t: T, _ c: Content) {
data = t
content = c
}

public var dontCrash : some Proto {
return Group(Choice(content, EmptyProto().add()))
}
}