Skip to content

[lldb] Resolve Swift-implemented Objective-C classes using Swift runtime #10548

Merged
adrian-prantl merged 1 commit into
swiftlang:swift/release/6.2from
adrian-prantl:145253225
May 7, 2025
Merged

[lldb] Resolve Swift-implemented Objective-C classes using Swift runtime #10548
adrian-prantl merged 1 commit into
swiftlang:swift/release/6.2from
adrian-prantl:145253225

Conversation

@adrian-prantl
Copy link
Copy Markdown

@adrian-prantl adrian-prantl commented Apr 25, 2025

if the Objective-C runtime fails. If an Objective-C class is lazy, the
Objective-C runtie may not have materialized class metadata for
it. However, if the class is actually implemented in Swift, we can
still resolve it using the Swift runtime. We should probably also add
the same logic to the Objective-C runtime, but I don't want risk
adding an inifinite recursion at this point in the release.

@adrian-prantl adrian-prantl requested a review from a team as a code owner April 25, 2025 02:25
@adrian-prantl
Copy link
Copy Markdown
Author

@swift-ci test

@adrian-prantl
Copy link
Copy Markdown
Author

@swift-ci test

@adrian-prantl
Copy link
Copy Markdown
Author

This is now ready to review.

@adrian-prantl
Copy link
Copy Markdown
Author

@swift-ci test

dynamic_type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>();
if (ts &&
ts->IsImportedType(dynamic_type.GetOpaqueQualType(), nullptr))
type_name = static_type.GetDisplayTypeName();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we always want to use the static type if the dynamic type is an Objective-C type? I guess in the REPL this makes sense

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Right, this is a very blunt tool to hide some of the Foundation implementation detail types that users are not meant to see / know / care about.
It doesn't not work for Swift-implemented types like _SwiftDeferredNSDictionary, which is going to be more prevalent going forward.

auto tss = class_type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>();
if (!tss) {
is_clang_type = true;
if (auto module_sp = in_value.GetModule()) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don't understand why this is needed. If the type does not have a Swift type system (even if it's a swiftified objc type), then something has gone wrong, and we shouldn't be asking the Swift Language Runtime, no?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good question! Let me try replacing this with an assert(false) to answer how we get here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The answer is

bool ValueObjectDynamicValue::UpdateValue() {
...
#ifdef LLDB_ENABLE_SWIFT
  // An Objective-C object inside a Swift frame.
  if (known_type == eLanguageTypeObjC)
    if ((exe_ctx.GetFramePtr() && exe_ctx.GetFramePtr()->GetLanguage().name ==
                                      llvm::dwarf::DW_LNAME_Swift) ||
        (exe_ctx.GetTargetPtr() && exe_ctx.GetTargetPtr()->IsSwiftREPL())) {
      runtime = process->GetLanguageRuntime(lldb::eLanguageTypeSwift);
      if (runtime)
        found_dynamic_type = runtime->GetDynamicTypeAndAddress(
            *m_parent, m_use_dynamic, class_type_or_name, dynamic_address,
            value_type, local_buffer);
    }
#endif // LLDB_ENABLE_SWIFT

let me see if I can simplify this.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I was able to eliminate the entire resolve_objc lambda. The interesting part is that it was also trying to solve the private implementation detail problem, however, this was no longer working.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Now that the resolve_objc lambda has been removed do we still need this block of code in case the type is a clang type then?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes, this is still an actively used code block. I updated the comment to explain why.

@adrian-prantl
Copy link
Copy Markdown
Author

@swift-ci test

@adrian-prantl adrian-prantl requested a review from JDevlieghere May 5, 2025 23:58
auto tss = class_type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>();
if (!tss) {
is_clang_type = true;
if (auto module_sp = in_value.GetModule()) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Now that the resolve_objc lambda has been removed do we still need this block of code in case the type is a clang type then?

if the Objective-C runtime fails. If an Objective-C class is lazy, the
Objective-C runtie may not have materialized class metadata for
it. However, if the class is actually implemented in Swift, we can
still resolve it using the Swift runtime. We should probably also add
the same logic to the Objective-C runtime, but I don't want risk
adding an inifinite recursion at this point in the release.

rdar://145253225
@adrian-prantl
Copy link
Copy Markdown
Author

@swift-ci test

@adrian-prantl
Copy link
Copy Markdown
Author

@augusto2112 I looked at where the Clang types come from, to see if we could avoid checking for Clang types in the SwiftLanguageRuntime. They are children of NSArray implementation objects, and are created by the matching Objective-C NSArray synthetic child provider. The Swift Array formatters forward to the ObjC formatters if the implementation is an Objective-C one, and they create Clang types. That's how situation is possible.

@adrian-prantl
Copy link
Copy Markdown
Author

@swift-ci test windows

@adrian-prantl adrian-prantl merged commit e49951f into swiftlang:swift/release/6.2 May 7, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants