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

Fix misuse of reified type parameters #981

Merged
merged 1 commit into from
Sep 21, 2024
Merged

Conversation

lukellmann
Copy link
Member

KT-52469 issues warnings for Kord.getGlobalApplicationCommandOf, Kord.getGlobalApplicationCommandOfOrNull and VoiceState.getChannelOrNull since Kotlin 2.1.0-dev-7621.

KT-52469 [1] issues warnings for Kord.getGlobalApplicationCommandOf,
Kord.getGlobalApplicationCommandOfOrNull and VoiceState.getChannelOrNull
since Kotlin 2.1.0-dev-7621.

[1] https://youtrack.jetbrains.com/issue/KT-52469
@DRSchlaubi
Copy link
Member

I do not understand this

@lukellmann
Copy link
Member Author

lukellmann commented Sep 20, 2024

This was a weird interaction between type inference and reified type parameters that will cause a warning with Kotlin 2.1.0. These functions were in fact buggy:


Kord.getGlobalApplicationCommandOf and Kord.getGlobalApplicationCommandOfOrNull were basically ignoring the type argument for T (it wasn't reified) and calling EntitySupplier.getGlobalApplicationCommandOf/EntitySupplier.getGlobalApplicationCommandOfOrNull with GlobalApplicationCommand for the reified T of the EntitySupplier functions unconditionally. Meaning you would get a ClassCastException instead of null for this code:

val command = kord.createGlobalUserCommand("user")
val shouldBeNull: GlobalChatInputCommand? = kord.getGlobalApplicationCommandOfOrNull<GlobalChatInputCommand>(command.id)
println(shouldBeNull)

The type parameter also had no bound so this was possible but also threw a ClassCastException:

val command = kord.createGlobalUserCommand("user")
val howShouldThisBeAString: String = kord.getGlobalApplicationCommandOf<String>(command.id)
println(howShouldThisBeAString)

With this PR, the first example works as expected and the second one no longer compiles.


VoiceState.getChannelOrNull also wasn't properly checking for and casting to BaseVoiceChannelBehavior but only for/to ChannelBehavior.


The warnings issued with Kotlin 2.1.0-dev-7621 are:

w: file:///.../kord/core/src/commonMain/kotlin/Kord.kt:505:32 Type argument for reified type parameter 'T' was inferred to the intersection of ['dev.kord.core.entity.application.GlobalApplicationCommand' & 'T & Any']. Reification of an intersection type results in the common supertype being used. This may lead to subtle issues and an explicit type argument is encouraged. This will become an error in a future release.

w: file:///.../kord/core/src/commonMain/kotlin/Kord.kt:510:32 Type argument for reified type parameter 'T' was inferred to the intersection of ['dev.kord.core.entity.application.GlobalApplicationCommand' & 'T? & Any']. Reification of an intersection type results in the common supertype being used. This may lead to subtle issues and an explicit type argument is encouraged. This will become an error in a future release.

w: file:///.../kord/core/src/commonMain/kotlin/entity/VoiceState.kt:63:98 Type argument for reified type parameter 'T' was inferred to the intersection of ['dev.kord.core.entity.channel.Channel' & 'dev.kord.core.behavior.channel.BaseVoiceChannelBehavior']. Reification of an intersection type results in the common supertype being used. This may lead to subtle issues and an explicit type argument is encouraged. This will become an error in a future release.

The important part is this:

Reification of an intersection type results in the common supertype being used.

@lukellmann lukellmann merged commit 59630f2 into main Sep 21, 2024
4 checks passed
@lukellmann lukellmann deleted the fix/reified-misuse branch September 21, 2024 12:12
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