Skip to content
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

KSP2: default annotation arguments no longer work in 1.0.21 #1909

Closed
ZacSweers opened this issue May 22, 2024 · 6 comments · Fixed by #1934
Closed

KSP2: default annotation arguments no longer work in 1.0.21 #1909

ZacSweers opened this issue May 22, 2024 · 6 comments · Fixed by #1934
Assignees
Labels
bug Something isn't working

Comments

@ZacSweers
Copy link
Contributor

Example using auto service

public @interface AutoService {
  Class<?>[] value();
}

The below sample

@AutoService // <-- no args, so default [] is used
class CustomCallable : Callable<String> {
    override fun call(): String = "Hello world!"
}

Results in a KSAnnotation that reports that both annotation.arguments and annotation.defaultArguments are empty.

Repro is the failing test in ZacSweers/auto-service-ksp#89

image image
@neetopia neetopia self-assigned this May 22, 2024
@neetopia neetopia added the bug Something isn't working label May 22, 2024
@ZacSweers
Copy link
Contributor Author

This seems to be limited to KClass and annotation types. Primitives and enums appear to work

@neetopia
Copy link
Contributor

To me it seems that this should never worked. The cause here is the default value is not explicitly declared in the source therefore it should get assigned automatically, KSP2 is parsing the binary class file to get the default value but the check failed to find a default value in the class file. Ideally we should assign a default value according to the type, which should be [] in your case. Let me try to find a way to assign default values in the event of implicit default values.

@neetopia
Copy link
Contributor

It appears to have same issue in KSP1

@neetopia
Copy link
Contributor

Hi @ZacSweers given this is also an issue in KSP1, do you know what's the last working version? I wonder if this is due to updates in the class file visitor versions if any.

At the same time, for this case in particular, I think I can try to manually assign default values based on the types of the annotation argument if that helps.

@ZacSweers
Copy link
Contributor Author

This test passed on main, which is on 1.9.24-1.0.20

@neetopia
Copy link
Contributor

Per the language spec JLS-9.6.2, annotations defined without a default value must have the corresponding value specified at use site, failed to do so should result in a compilation error. I feel this might be a mistake use case to apply a default value when there is no default value specified at declaration site.

With that being said, we still need to address the issue where we ignored the annotation values in case of a missing value use case. I will add a special default value for arrays but I will leave the rest of the default value types to return null, hope that will unblock you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants