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

Conditional Compilation Error in Kotlin Bindings for Rust Enum with message Field #2065

Open
martinmose opened this issue Apr 5, 2024 · 0 comments

Comments

@martinmose
Copy link
Contributor

Hello,

When a custom error has a field named message, the generating Kotlin code gives a compilation error.
This error arises due to a naming conflict between the custom field named message in Rust and the message property that is inherited from Kotlin's Exception class.

Steps to Reproduce:

  1. Define a Error that includes a variant with an optional message field, like so:
#[derive(Debug, uniffi::Error)]
pub enum HandlerError {
    ServiceError { message: Option<String> },
}

Implement the necessary traits (fmt::Display and std::error::Error) for the enum.

  1. Generate the Kotlin bindings

  2. Review the generated Kotlin code and observe the attempted override of the message property, leading to the compilation error.

Expected Behavior:

The generated Kotlin code should handle property naming conflicts gracefully, avoiding any compilation errors due to such conflicts 🤞

Actual Behavior:

The Kotlin compiler reports a conflict:

Conflicting declarations: public open val message: String, public final val message: String?

The generated code looks like this:

sealed class HandlerException : Exception() {
    class ServiceException(
        val `message`: String?,
    ) : HandlerException() {
        override val message
            get() = "message=${ `message` }"
    }

    .....
}

Workaround:

Of course renaming the message field for the uniffi::Error variant to another name (e.g., errorMessage) resolves the compilation issue:

ServiceError { errorMessage: Option<String> }

😇

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

No branches or pull requests

1 participant