NordSecurity / uniffi-bindgen-go

Uniffi bindings generator for Golang
Mozilla Public License 2.0
75 stars 21 forks source link

Naming error variant `Error` produces invalid code #30

Closed arg0d closed 9 months ago

arg0d commented 10 months ago

Reported by @kegsay https://github.com/NordSecurity/uniffi-bindgen-go/issues/21#issuecomment-1774958892

git diff
diff --git a/fixtures/errors/src/errors.udl b/fixtures/errors/src/errors.udl
index c8fc765..fcbd984 100644
--- a/fixtures/errors/src/errors.udl
+++ b/fixtures/errors/src/errors.udl
@@ -27,6 +27,11 @@ interface ValidationError {
   UnknownError();
 };

+[Error]
+interface ErrorNamedError {
+  Error(string error);
+};
+
 [Error]
 interface ComplexError {
     Struct(Vec2 position_a, Vec2 position_b);
diff --git a/fixtures/errors/src/lib.rs b/fixtures/errors/src/lib.rs
index 7b07383..6379c06 100644
--- a/fixtures/errors/src/lib.rs
+++ b/fixtures/errors/src/lib.rs
@@ -12,6 +12,12 @@ pub enum BoobyTrapError {
     HotDoorKnob,
 }

+#[derive(Debug, thiserror::Error)]
+pub enum ErrorNamedError {
+    #[error("Error {error}")]
+    Error { error: String },
+}
+
 #[derive(Debug, thiserror::Error)]
 pub enum ValidationError {
     #[error("Invalid user_id {user_id}")]

produces:

// Variant structs
type ErrorNamedErrorError struct {
    Error string
}

func NewErrorNamedErrorError(
    error string,
) *ErrorNamedError {
    return &ErrorNamedError{
        err: &ErrorNamedErrorError{
            Error: error,
        },
    }
}

func (err ErrorNamedErrorError) Error() string {
    return fmt.Sprint("Error",
        ": ",

        "Error=",
        err.Error,
    )
}

which is wrong as field and method with the same name Error.