dtolnay/cxx

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

Ouverte

#460 ouverte le 12 nov. 2020

 (1 commentaire) (0 réaction) (0 personne assignée)Rust (253 forks)batch import
bughelp wanted

Métriques du dépôt

Stars
 (4 472 étoiles)
Métriques de merge PR
 (Merge moyen 8m) (3 PRs mergées en 30 j)

Description

As observed in https://github.com/dtolnay/cxx/issues/441#issuecomment-725204700:

#[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.

Guide contributeur