uniffi-rs icon indicating copy to clipboard operation
uniffi-rs copied to clipboard

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

Open martinmose opened this issue 1 year ago • 0 comments

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> }

😇

martinmose avatar Apr 05 '24 07:04 martinmose