Skip to content

Deriving uniffi::Error on an enum with a variant that contains a message field leads to a Kotlin error #2938

Description

@bes

This error type in Rust

#[derive(Debug, thiserror::Error, uniffi::Error)]
pub enum FfiError {
    #[error("Cancelled")]
    Cancelled,
    #[error("Generic error")]
    GenericError,
    #[error("StringError {0}")]
    StringError(String),
    #[error("ApiError url: {url}, status_code: {status_code:?}, message: {message}")]
    ApiError {
        url: String,
        status_code: Option<u16>,
        message: String,
    },
}

works well in Swift, but when generating code for Kotlin, the following type is generated:

sealed class FfiException: kotlin.Exception() {
    class Cancelled(
        ) : FfiException() {
        override val message
            get() = ""
    }
    
    class GenericException(
        ) : FfiException() {
        override val message
            get() = ""
    }
    
    class StringException(
        val v1: kotlin.String
        ) : FfiException() {
        override val message
            get() = "v1=${ v1 }"
    }
    
    class ApiException(
        val `url`: kotlin.String,         
        val `statusCode`: kotlin.UShort?, 
        
        val `message`: kotlin.String
        ) : FfiException() {
        override val message
            get() = "url=${ `url` }, statusCode=${ `statusCode` }, message=${ `message` }"
    }

    companion object ErrorHandler : UniffiRustCallStatusErrorHandler<FfiException> {
        override fun lift(error_buf: RustBuffer.ByValue): FfiException = FfiConverterTypeFfiError.lift(error_buf)
    }   
}

Which gives the following compiler errors:

> Task :app:compileDebugKotlin FAILED
.../ffi.kt:11905:13 Conflicting declarations:
val message: String
.../ffi.kt:11905:13 'message' hides member of supertype 'Throwable' and needs an 'override' modifier.
.../ffi.kt:11907:22 Conflicting declarations:
val message: String
.../ffi.kt:11908:79 Overload resolution ambiguity between candidates:
val message: String
val message: String
.../ffi.kt:11964:59 Overload resolution ambiguity between candidates:
val message: String
val message: String
.../ffi.kt:11988:48 Overload resolution ambiguity between candidates:
val message: String
val message: String

This is in part a "bug report" and in part just a question: Can't I use the identifier message in my error types? Obviously there is a conflict here, but the cause isn't obvious. Maybe uniffi::Error should warn / error if I use an incompatible field name?

Workaround: Rename my message field to msg.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions