Skip to content

Conversation

@adrian-prantl
Copy link

@adrian-prantl adrian-prantl commented Jun 2, 2025

(Payload-carrying) Swift enums are a challenging datastructure to
support in LLDB; this patch makes an attempt to clean up a problem
with the previous implementation, namely the dynamic nature of the
children of an enum, which has, in the past, lead to caching bugs when
displaying the children of enums. The new scheme is as follows:

  • The static value of an enum is its discriminator (its case).

  • An enum has no static children.

  • The synthtic value of an enum is its projected case's payload.

  • A synthetic child provider for all Swift enums provides synthetic
    children, which are the children of the projected case's payload.

For example:

enum E {
  case a
  case b(Int, (Int, Int))
}

let a : E= .a
let b : E= .b(1, (2, 3))

The value of a is .a.
The value of b is .b.
The synthtic value of b is (1, (2, 3)).
The synthetic child 0 of b is 1.
The synthetic child 1 of b is (2, 3).
The number of (non-synthetic) children of b is 0.

The Swift Optional formatter behaves similarly.

Known Issues:

(FIXED!) - There is a bug in upstream LLDB that prevents
ValueObject::GetSyntheticValue to return the synthetic value from a
synthtic frontend. To temporarily work around this problem the
Swift.Optional summary formatter implements its own printing of
children. This is not a permanent solution. I will try to fix the
upstream bug ASAP.

  • SwiftRuntimeTypeVisitor does currently not visit the children of
    Objective-C types; this also needs to get fixed, and is surfaced by
    the new synthtic child providers in 1 test.

Implementation Notes:

This removes a lot of enum-handling code from SwiftLanguageRuntime;
simplifying the type visitor and dynamic type resolution, adn moves it
into the Enum/Optional synthetic frontends.

@adrian-prantl adrian-prantl requested a review from a team as a code owner June 2, 2025 23:23
Copy link

@augusto2112 augusto2112 left a comment

Choose a reason for hiding this comment

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

Very high level first pass (didn't look too deeply into the code yet).

@adrian-prantl adrian-prantl force-pushed the fix-enums branch 3 times, most recently from 611335b to 4423a2d Compare June 3, 2025 22:10
@adrian-prantl adrian-prantl requested a review from fredriss June 3, 2025 22:12
@adrian-prantl
Copy link
Author

@swift-ci test

(Payload-carrying) Swift enums are a challenging datastructure to
support in LLDB; this patch makes an attempt to clean up a problem
with the previous implementation, namely the dynamic nature of the
children of an enum, which has, in the past, lead to caching bugs when
displaying the children of enums. The new scheme is as follows:

- The static value of an enum is its discriminator (its case).

- An enum has no static children.

- The synthtic value of an enum is its projected case's payload.

- A synthetic child provider for all Swift enums provides synthetic
  children, which are the children of the projected case's payload.

For example:

```
enum E {
  case a
  case b(Int, (Int, Int))
}

let a : E= .a
let b : E= .b(1, (2, 3))
```

The value of `a` is `.a`.
The value of `b` is `.b`.
The synthtic value of `b` is (1, (2, 3)).
The synthetic child 0 of `b` is 1.
The synthetic child 1 of `b` is (2, 3).
The number of (non-synthetic) children of `b` is 0.

The Swift Optional formatter behaves similarly.

Known Issues:

- SwiftRuntimeTypeVisitor does currently not visit the children of
  Objective-C types; this also needs to get fixed, and is surfaced by
  the new synthtic child providers in 1 test.

Implementation Notes:

This removes a lot of enum-handling code from SwiftLanguageRuntime;
simplifying the type visitor and dynamic type resolution, adn moves it
into the Enum/Optional synthetic frontends.
This uncovers a couple of bugs in embedded Swift, which are now
explicitly surfaced in the testcase.
This mostly makes debugging easier.
@adrian-prantl
Copy link
Author

@swift-ci test

Copy link

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

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

🥳

@adrian-prantl adrian-prantl merged commit f2964c2 into swiftlang:swift/release/6.2 Jun 6, 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.

4 participants