Skip to content

Automatically opt-in all experimental annotations for types publicly exposed by a benchmark class #301

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

Open
dkhalanskyjb opened this issue Apr 8, 2025 · 2 comments
Labels
enhancement New feature or request

Comments

@dkhalanskyjb
Copy link

I've used https://github.com/fzhinkin/benchmarks-template.

@State(Scope.Benchmark)
open class ExampleBenchmark {
    @Benchmark
    fun parse() =
        @OptIn(ExperimentalTime::class) Instant.parse("2023-10-01T16:12:42.546124Z")

    @Benchmark
    fun parseNoFrac() =
        @OptIn(ExperimentalTime::class) Instant.parse("2023-10-01T16:12:42Z")
}

then fails to compile with

> Task :compileJsBenchmarkKotlinJs FAILED
e: file:///home/jb/IdeaProjects/benchmark-Instant-parse/build/benchmarks/js/sources/kotlinx/benchmark/generated/org/example/ExampleBenchmark_Descriptor.kt:30:52 This declaration needs opt-in. Its usage must be marked with '@kotlin.time.ExperimentalTime' or '@OptIn(kotlin.time.ExperimentalTime::class)'
e: file:///home/jb/IdeaProjects/benchmark-Instant-parse/build/benchmarks/js/sources/kotlinx/benchmark/generated/org/example/ExampleBenchmark_Descriptor.kt:32:52 This declaration needs opt-in. Its usage must be marked with '@kotlin.time.ExperimentalTime' or '@OptIn(kotlin.time.ExperimentalTime::class)'

Moving OptIn somewhere outside also causes the compilation failure. The one way I've found to write this benchmark is to use block bodies for these functions:

    @Benchmark
    fun parse() {
        @OptIn(ExperimentalTime::class) Instant.parse("2023-10-01T16:12:42.546124Z")
    }
@fzhinkin fzhinkin added bug Something isn't working and removed bug Something isn't working labels Apr 21, 2025
@fzhinkin
Copy link
Collaborator

The issue is not specific to K/JS and I was able to reproduce it with other targets as well.
The compilation fails because ExampleBenchmark::parse and ::parseNoFrac had an experimental return type (kotlin.time.Instant).

As a workaround that won't affect benchmark's semantics, the experimental annotation could be opted-in using a compiler flag (-opt-in=kotlin.time.ExperimentalTime).

It would be nice to add all necessary plugins to a generated plugin automatically, however, supporting it might be tricky:

  • for klib-based targets (well, all non-JVM targets), we have to load all transitive dependencies in order to track down all experimental annotations; I'm not sure if it is worth support right now.
  • for JVM target, we need to intercept JMH's code generator to emit all required opt-ins (further investigation is required here).

@fzhinkin fzhinkin added the enhancement New feature or request label Apr 21, 2025
@fzhinkin fzhinkin changed the title Opt-ins don't propagate to JS and Wasm/JS benchmarks, causing a compilation failure Automatically opt-in all experimental annotations for types publicly exposed by a benchmark class Apr 21, 2025
@dkhalanskyjb
Copy link
Author

Putting the opt-in into the block also fixes the issue nicely, so it's not a big deal. I think the problem is only with the annotation processing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants