Skip to content
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

No way to use extern_cpp_type!() for a nested type #1422

Open
GoldsteinE opened this issue Jan 7, 2025 · 2 comments
Open

No way to use extern_cpp_type!() for a nested type #1422

GoldsteinE opened this issue Jan 7, 2025 · 2 comments

Comments

@GoldsteinE
Copy link

Describe the bug
extern_cpp_type!() accepts a single name as an argument, but autocxx uses two different names for nested types: outer_inner and outer::inner. As far as I’m aware, that means there’s no way to use extern_cpp_type!() for nested classes.

To Reproduce

// test.hpp
#pragma once
namespace myns {
    struct mystruct {
        enum myenum { A, B };
        mystruct(myenum) {}
    };
};
// build.rs
fn main() {
    let path = ".";
    let mut b = autocxx_build::Builder::new("src/main.rs", &[path])
        .build()
        .unwrap();
    b.flag_if_supported("-std=c++20").compile("minimal-repro");
    println!("cargo::rerun-if-changed=src/main.rs");
}
// src/main.rs
use autocxx::prelude::*;

#[cxx::bridge]
mod manual {
    #[repr(u32)]
    pub enum mystruct_myenum {
        A, B,
    }
}

include_cpp! {
    #include "test.hpp"
    safety!(unsafe)

    // variant 1: ParseError(AutocxxCodegenError(Conversion(Cpp(DidNotGenerateAnything("myns::mystruct::myenum")))))
    extern_cpp_type!("myns::mystruct::myenum", crate::manual::mystruct_myenum)
    pod!("myns::mystruct::myenum")
    // variant 2:   cargo:warning=/.../autocxx-build-dir/cxx/gen1.cxx:178:79: error: 'myns::mystruct_myenum' has not been declared
    extern_cpp_type!("myns::mystruct_myenum", crate::manual::mystruct_myenum)
    pod!("myns::mystruct_myenum")
    // variant 3:   cargo:warning=/.../autocxx-build-dir/cxx/gen1.cxx:137:79: error: 'myns::mystruct_myenum' has not been declared
    extern_cpp_type!("myns::mystruct_myenum", crate::manual::mystruct_myenum)
    // variant 4: generates, but uses autocxx-generated enum in constructor instead of extern one
    extern_cpp_type!("myns::mystruct::myenum", crate::manual::mystruct_myenum)


    generate!("myns::mystruct")
}

fn main() {
    println!("Hello, world!");
}

Expected behavior
There’s some way to specify extern_cpp_type!() for a nested enum.

@GoldsteinE
Copy link
Author

Okay, I found a workaround: I can add a type alias like

namespace myns {
    using mystruct_myenum = mystruct::myenum;
}

and then use variant 2. This still feels like a bug though.

@adetaylor
Copy link
Collaborator

Yes, this sounds like a valid bug. I'd be happy to accept a PR to fix it.

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

No branches or pull requests

2 participants