autocxx icon indicating copy to clipboard operation
autocxx copied to clipboard

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

Open GoldsteinE opened this issue 11 months ago • 2 comments

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 avatar Jan 07 '25 10:01 GoldsteinE

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.

GoldsteinE avatar Jan 07 '25 18:01 GoldsteinE

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

adetaylor avatar Jan 08 '25 12:01 adetaylor