-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[cxx-interop] Support for printing C++ foreign references #81832
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
Conversation
@swift-ci please smoke test |
4aadc58
to
ebdea6a
Compare
@swift-ci please smoke test |
ebdea6a
to
68fb206
Compare
@swift-ci please smoke test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Some minor nits inline.
@@ -381,6 +381,10 @@ internal func _adHocPrint_unlocked<T, TargetStream: TextOutputStream>( | |||
target.write(")") | |||
} | |||
} | |||
case .foreign: | |||
printTypeName(mirror.subjectType) | |||
// FRT has no children |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to print the (public) fields similar to structs in the future? I think it is fine to not do it in this PR but we might want to have a rdar for it if this is planned and possible to achieve?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the moment, we don't emit field metadata for FRT, so we don't have any field information at runtime:
Lines 2006 to 2009 in 68fb206
if (getType()->isForeign()) | |
return; | |
MetadataLayout = &IGM.getClassMetadataLayout(Type); |
Lines 2120 to 2125 in 68fb206
Size getFieldVectorOffset() { | |
if (!MetadataLayout) return Size(0); | |
return (MetadataLayout->hasResilientSuperclass() | |
? MetadataLayout->getRelativeFieldOffsetVectorOffset() | |
: MetadataLayout->getStaticFieldOffsetVectorOffset()); | |
} |
I'm not sure if this is on purpose or it's simply not yet implemented, but I agree it would be useful to figure this out and, potentially, print more information in the future
@@ -147,7 +147,7 @@ public enum _DebuggerSupport { | |||
return value.map(String.init(reflecting:)) | |||
case .collection?, .dictionary?, .set?, .tuple?: | |||
return count == 1 ? "1 element" : "\(count) elements" | |||
case .`struct`?, .`enum`?, nil: | |||
case .`struct`?, .`enum`?, .foreign?, nil: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if foreign reference types should behave more like classes and print the memory address.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
asObjectAddress(...)
of a FRT returns 0, so for now I think we shouldn't print the memory address. Most likely this happens for the same reason that we don't have metadata about the fields of FRTs at runtime.
If in the future we decide to work on emitting more metadata for FRTs, it could make sense to add .foreign
to the same case
as classes. For now, I would keep it as is, if you agree?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If supporting this requires additional work I am fine to leave this as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed that we should ideally print an address, also agreed that it doesn't have to be in this PR 😉
68fb206
to
3ec1479
Compare
@swift-ci please smoke test |
@swift-ci please test Windows platform |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Just a couple of minor comments, and then this should be good to go.
@@ -147,7 +147,7 @@ public enum _DebuggerSupport { | |||
return value.map(String.init(reflecting:)) | |||
case .collection?, .dictionary?, .set?, .tuple?: | |||
return count == 1 ? "1 element" : "\(count) elements" | |||
case .`struct`?, .`enum`?, nil: | |||
case .`struct`?, .`enum`?, .foreign?, nil: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed that we should ideally print an address, also agreed that it doesn't have to be in this PR 😉
stdlib/public/core/Mirror.swift
Outdated
@@ -313,6 +313,7 @@ extension Mirror { | |||
public enum DisplayStyle: Sendable { | |||
case `struct`, `class`, `enum`, tuple, optional, collection | |||
case dictionary, `set` | |||
@available(SwiftStdlib 6.2, *) case foreign |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should call this foreignReference
to make it clear that this is about foreign reference types specifically. IRGen uses the term "foreign" to refer to C structs that are imported as value types, and here this means something different.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, thanks!
stdlib/public/core/Mirror.swift
Outdated
@@ -481,6 +482,7 @@ public struct Mirror { | |||
public enum DisplayStyle: Sendable { | |||
case `struct`, `class`, `enum`, tuple, optional, collection | |||
case dictionary, `set` | |||
@available(SwiftStdlib 6.2, *) case foreign |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto
As this isn't a trivial change, I think we could do it in another PR 🙂 |
3ec1479
to
9975ac8
Compare
@swift-ci please smoke test |
9975ac8
to
848fad0
Compare
@swift-ci please smoke test |
Adds support for printing a C++ foreign reference in Swift.
Also skips metadata of private fields in C++ records imported as Swift classes, following up on #81035
Resolves rdar://124164228