Repository: dtolnay/cxx (language: rust)

Issue:
Improve error message on enum used as method receiver
```console
error[cxxbridge]: unrecognized receiver type
  ┌─ /dev/stdin:1:66
  │
1 │ #[cxx::bridge] mod ffi { enum Enum {} extern "Rust" { fn f(self: &Enum); }}
  │                                                                  ^^^^^ unrecognized receiver type
```

This needs to give some indication that member functions are not allowed by C++ on enums.


Discussion / hints:

Fix patch:
diff --git a/syntax/check.rs b/syntax/check.rs
index 2098a1bf6..0aa383717 100644
--- a/syntax/check.rs
+++ b/syntax/check.rs
@@ -389,6 +389,8 @@ fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
                 mutability = mutability,
             );
             cx.error(span, msg);
+        } else if cx.types.enums.contains_key(&receiver.ty.rust) {
+            cx.error(span, "C++ does not allow member functions on enums");
         } else if !cx.types.structs.contains_key(&receiver.ty.rust)
             && !cx.types.cxx.contains(&receiver.ty.rust)
             && !cx.types.rust.contains(&receiver.ty.rust)


Test patch:
diff --git a/tests/ui/enum_receiver.rs b/tests/ui/enum_receiver.rs
new file mode 100644
index 000000000..a27f27468
--- /dev/null
+++ b/tests/ui/enum_receiver.rs
@@ -0,0 +1,11 @@
+#[cxx::bridge]
+mod ffi {
+    enum Enum {
+        Variant,
+    }
+    extern "Rust" {
+        fn f(self: &Enum);
+    }
+}
+
+fn main() {}
diff --git a/tests/ui/enum_receiver.stderr b/tests/ui/enum_receiver.stderr
new file mode 100644
index 000000000..8523c7726
--- /dev/null
+++ b/tests/ui/enum_receiver.stderr
@@ -0,0 +1,5 @@
+error: C++ does not allow member functions on enums
+ --> $DIR/enum_receiver.rs:7:20
+  |
+7 |         fn f(self: &Enum);
+  |                    ^^^^^