Skip to content

Noncompilable C++ code if method has different explicit namespace than the receiver #460

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

Open
dtolnay opened this issue Nov 12, 2020 · 0 comments

Comments

@dtolnay
Copy link
Owner

dtolnay commented Nov 12, 2020

As observed in #441 (comment):

#[cxx::bridge]
mod ffi {
    #[namespace = "shared"]
    struct Color {
        r: u8,
        g: u8,
        b: u8,
    }

    #[namespace = "rust_part"]
    extern "Rust" {
        fn is_white(self: &Color) -> bool;
    }
}

Currently this results in generating something like the following, which does not compile.

namespace shared {
struct Color final {
  uint8_t r;
  uint8_t g;
  uint8_t b;

  bool is_white() const noexcept;
};
} // namespace shared

namespace rust_part {
bool Color::is_white() const noexcept {
  ...
}
} // namespace rust_part
cxxbridge/sources/demo/src/main.rs.cc:25:6: error: ‘Color’ has not been declared
   25 | bool Color::is_white() const noexcept {
      |      ^~~~~

I wouldn't be opposed to accepting that code and just making it put the member function in the struct's namespace all the time, regardless of namespace attribute on the member function.

But if someone feels strongly this should be an error (with a better message), that would be reasonable too.

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

No branches or pull requests

1 participant