Skip to content

Allow processor to opt in to new features in KSVisitors - #3111

Merged
jaschdoc merged 2 commits into
mainfrom
visitor-features-opt-in
Aug 4, 2026
Merged

Allow processor to opt in to new features in KSVisitors#3111
jaschdoc merged 2 commits into
mainfrom
visitor-features-opt-in

Conversation

@jaschdoc

@jaschdoc jaschdoc commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

The idea in this PR is that a processor must opt-in to the new features such as backing fields. This is all done without breaking binary compatibility, so processor authors can upgrade to new features and users can still build their projects.
Specifically, this PR addresses the situation where a processor uses one of the built-in KSVisitor implementations provided by KSP. An existing processor will not see any change and will continue to work. An existing processor may opt-in to the new features.

This is similar and related to #3110. Note that the flag introduced in this PR is decoupled from the one in #3110.

Related to #2969
Related to #2472
Related to #2873

Adds a `enableNewFeatures` boolean parameter that toggles new features on or off.
This commit intentionally breaks the build to surface all uses of constructors.
@jaschdoc
jaschdoc marked this pull request as ready for review August 3, 2026 14:31
@jaschdoc
jaschdoc requested a review from troelsbjerre as a code owner August 3, 2026 14:31
open class KSVisitorVoid(val enableNewFeatures: Boolean) : KSVisitorNext<Unit, Unit> {

// For binary compatibility
constructor() : this(enableNewFeatures = false)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why not just pass this as a default parameter?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

That's not binary compatible. Consider the following example:

class A(val a: Int, b: Int = 0) // default parameter
class B(val a: Int, b: Int) {
    constructor(a: Int) : this(a, 42) // Secondary constructor
}

Then the disassembled output has the following constructors:

  public A(int, int);
    descriptor: (II)V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=2, locals=3, args_size=3
         0: aload_0
         1: invokespecial #9                  // Method java/lang/Object."<init>":()V
         4: aload_0
         5: iload_1
         6: putfield      #13                 // Field a:I
         9: aload_0
        10: iload_2
        11: putfield      #16                 // Field b:I
        14: return

  public A(int, int, int, kotlin.jvm.internal.DefaultConstructorMarker);
    descriptor: (IIILkotlin/jvm/internal/DefaultConstructorMarker;)V
    flags: (0x1001) ACC_PUBLIC, ACC_SYNTHETIC
    Code:
      stack=3, locals=5, args_size=5
         0: iload_3
         1: iconst_2
         2: iand
         3: ifeq          8
         6: iconst_0
         7: istore_2
         8: aload_0
         9: iload_1
        10: iload_2
        11: invokespecial #21                 // Method "<init>":(II)V
        14: return

and

  public B(int, int);
    descriptor: (II)V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=2, locals=3, args_size=3
         0: aload_0
         1: invokespecial #9                  // Method java/lang/Object."<init>":()V
         4: aload_0
         5: iload_1
         6: putfield      #13                 // Field a:I
         9: aload_0
        10: iload_2
        11: putfield      #16                 // Field b:I
        14: return

  public B(int);
    descriptor: (I)V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=3, locals=2, args_size=2
         0: aload_0
         1: iload_1
         2: bipush        42
         4: invokespecial #24                 // Method "<init>":(II)V
         7: return

Class A's secondary constructor actually has four parameters instead of one whereas class B's secondary constructor has the one parameter as expected.

@jaschdoc
jaschdoc merged commit 9252473 into main Aug 4, 2026
27 checks passed
@jaschdoc
jaschdoc deleted the visitor-features-opt-in branch August 4, 2026 13:30
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.

2 participants