Skip to content

Refactor toString generation to use the standard method generation path#567

Open
sidepelican wants to merge 2 commits intoswiftlang:mainfrom
sidepelican:tostring_thunk
Open

Refactor toString generation to use the standard method generation path#567
sidepelican wants to merge 2 commits intoswiftlang:mainfrom
sidepelican:tostring_thunk

Conversation

@sidepelican
Copy link
Contributor

Motivation

Currently, the implementations for toString and toDebugString methods are handled by dedicated, hardcoded logic.
However, these methods are fundamentally identical to standard function calls in terms of their underlying mechanism.

This inconsistency makes it difficult to maintain parity with other parts of the generator. Specifically, as I work on implementing support for function calls in generic type, I find myself having to write redundant specialized logic just for toString, which adds unnecessary complexity.

Changes

Treat toString and toDebugString as ImportedFunc.
This allows them to be processed through the general method generation path.

Note

While this change also affects FFM mode, it will currently be ignored because String return values are not yet supported in that mode.

@sidepelican sidepelican requested a review from ktoso as a code owner February 17, 2026 06:47
case subscriptGetter
case subscriptSetter
case toString
case toDebugString
Copy link
Contributor

@madsodgaard madsodgaard Feb 17, 2026

Choose a reason for hiding this comment

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

Instead of adding these to SwiftAPIKind, perhaps we should introduce a new type SynethesizedAPI (or something) else to describe these sort of functions like toString etc?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the feedback. Do you have a specific vision or any examples of how you'd like to implement SynthesizedAPI?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, I was just thinking of adding it to the ImportedFunc as an optional and then we would do

enum SynthesizedAPI {
  case toString
  case toDebugString

Just an idea to not pollute the SwiftAPIKind, as it seems like it should only concern itself with Swift language APIs

Copy link
Contributor Author

@sidepelican sidepelican Feb 18, 2026

Choose a reason for hiding this comment

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

I believe it's important to ensure we can handle the branching logic correctly in the following locations:

Considering this, I think SynthesizedAPI should be integrated into SwiftAPIKind as an associated value, like this:

enum SynthesizedAPI { 
  case toString, toDebugString
}

enum SwiftAPIKind {
  case function(SynthesizedAPI?)
  case initializer
  case getter
  case setter
  case enumCase
  case subscriptGetter
  case subscriptSetter
}

Does this approach look good to you?

(In this case, SynthesizedFunction might be a better than SynthesizedAPI.)

Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe it's function and synthesizedFunction(SynthesizedAPI) in that case?

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

Comments