Skip to content

[cxx-interop] Relax a SILVerifier assertion for immortal reference types #81614

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
May 21, 2025
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
8 changes: 7 additions & 1 deletion lib/SIL/Verifier/SILVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,13 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {

auto objectTy = value->getType().unwrapOptionalType();

require(objectTy.isReferenceCounted(F.getModule()),
// Immortal C++ foreign reference types are represented as trivially lowered
// types since they do not require retain/release calls.
bool isImmortalFRT = objectTy.isForeignReferenceType() &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this also true for SWIFT_UNSAFE_REFERENCEs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeap! From the compiler point of view, they're the same thing, except if strictly safe mode is enabled – both SWIFT_UNSAFE_REFERENCE and SWIFT_IMMORTAL_REFERENCE macros expand into retain:immortal and release:immortal.

objectTy.getASTType()->getReferenceCounting() ==
ReferenceCounting::None;

require(objectTy.isReferenceCounted(F.getModule()) || isImmortalFRT,
valueDescription + " must have reference semantics");
}

Expand Down
20 changes: 20 additions & 0 deletions test/Interop/Cxx/foreign-reference/unmanaged.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=default -Xfrontend -disable-availability-checking) | %FileCheck %s

// REQUIRES: executable_test

import POD

extension Empty {
public static func == (lhs: Empty, rhs: Empty) -> Bool {
Unmanaged.passUnretained(lhs).toOpaque() == Unmanaged.passUnretained(rhs).toOpaque()
}
}

let x = Empty.create()
let y = Empty.create()

print(x == y)
// CHECK: false

print(x == x)
// CHECK: true