Repository: dtolnay/cxx (language: rust)

Issue:
Box not found in cxx::bridge scope when using alloc
 `cxx::bridge` appears to be using `Box` instead of a fully elaborated name when `Box` is used in a function declaration, which prevents it from being found. There doesn't appear to be a way to import another path for `Box` in a `cxx::bridge` module (`use items are not allowed within cxx bridge`).

```
error[E0412]: cannot find type `Box` in this scope
  --> src/lib.rs:15:21
   |
15 |         fn foo() -> Box<Bar>;
   |                     ^^^ not found in this scope
   |
help: consider importing one of these items
   |
14 |         type use alloc::prelude::v1::Box;
   |              ++++++++++++++++++++++++++++
14 |         type use crate::Box;
   |              +++++++++++++++
   ```
   
```toml
# Cargo.toml
[package]
name = "cxx_test"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cxx = {version="1.0.71", default-features=false, features=["alloc"]}
```

```rust
// src/lib.rs
#![no_std]
extern crate alloc;
use alloc::boxed::Box;

struct Bar {}

fn foo() -> Box<Bar> {
    Box::new(Bar {})
}

#[cxx::bridge]
mod ffi {
    extern "Rust" {
        type Bar;
        fn foo() -> Box<Bar>;
    }
}
```





Discussion / hints:
Hi,

I am pretty sure I am hitting this same issue, but with `Vec` and `String` instead of `Box`.

Did you find a way to make it work?

Fix patch:
diff --git a/macro/src/load.rs b/macro/src/load.rs
index a9958aee8..dccece44b 100644
--- a/macro/src/load.rs
+++ b/macro/src/load.rs
@@ -264,7 +264,7 @@ fn translate_qual_type(cx: &mut Errors, enm: &Enum, qual_type: &str) -> Path {
         .unwrap()
         .span();
     let ident = Ident::new(rust_std_name, span);
-    let path = quote_spanned!(span=> ::std::os::raw::#ident);
+    let path = quote_spanned!(span=> ::cxx::core::ffi::#ident);
     parse_quote!(#path)
 }
 
diff --git a/macro/src/tokens.rs b/macro/src/tokens.rs
index 135be9aef..805af227b 100644
--- a/macro/src/tokens.rs
+++ b/macro/src/tokens.rs
@@ -33,7 +33,7 @@ impl ToTokens for ReceiverType<'_> {
             mutability,
         } = &self.0;
         if let Some((pin, langle, _rangle)) = pin_tokens {
-            tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
+            tokens.extend(quote_spanned!(pin.span=> ::cxx::core::pin::Pin));
             langle.to_tokens(tokens);
         }
         ampersand.to_tokens(tokens);
@@ -61,7 +61,7 @@ impl ToTokens for ReceiverTypeSelf<'_> {
             mutability,
         } = &self.0;
         if let Some((pin, langle, _rangle)) = pin_tokens {
-            tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
+            tokens.extend(quote_spanned!(pin.span=> ::cxx::core::pin::Pin));
             langle.to_tokens(tokens);
         }
         ampersand.to_tokens(tokens);
diff --git a/src/c_char.rs b/src/c_char.rs
index a0d418032..333d8491c 100644
--- a/src/c_char.rs
+++ b/src/c_char.rs
@@ -1,5 +1,4 @@
-#![cfg(feature = "alloc")]
-
+#[allow(missing_docs)]
 pub type c_char = c_char_definition::c_char;
 
 // Validate that our definition is consistent with libstd's definition, without
diff --git a/src/lib.rs b/src/lib.rs
index 360f73891..9ad6fe14f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -490,6 +490,7 @@ pub type Vector<T> = CxxVector<T>;
 // Not public API.
 #[doc(hidden)]
 pub mod private {
+    pub use crate::c_char::c_char;
     pub use crate::cxx_vector::VectorElement;
     pub use crate::extern_type::{verify_extern_kind, verify_extern_type};
     pub use crate::function::FatFunction;
diff --git a/syntax/tokens.rs b/syntax/tokens.rs
index 62d37c23d..a9f42bd43 100644
--- a/syntax/tokens.rs
+++ b/syntax/tokens.rs
@@ -13,10 +13,13 @@ impl ToTokens for Type {
             Type::Ident(ident) => {
                 if ident.rust == Char {
                     let span = ident.rust.span();
-                    tokens.extend(quote_spanned!(span=> ::std::os::raw::));
+                    tokens.extend(quote_spanned!(span=> ::cxx::private::));
                 } else if ident.rust == CxxString {
                     let span = ident.rust.span();
                     tokens.extend(quote_spanned!(span=> ::cxx::));
+                } else if ident.rust == RustString {
+                    let span = ident.rust.span();
+                    tokens.extend(quote_spanned!(span=> ::cxx::alloc::string::));
                 }
                 ident.to_tokens(tokens);
             }
@@ -66,8 +69,11 @@ impl ToTokens for Ty1 {
             "UniquePtr" | "SharedPtr" | "WeakPtr" | "CxxVector" => {
                 tokens.extend(quote_spanned!(span=> ::cxx::));
             }
+            "Box" => {
+                tokens.extend(quote_spanned!(span=> ::cxx::alloc::boxed::));
+            }
             "Vec" => {
-                tokens.extend(quote_spanned!(span=> ::std::vec::));
+                tokens.extend(quote_spanned!(span=> ::cxx::alloc::vec::));
             }
             _ => {}
         }
@@ -90,7 +96,7 @@ impl ToTokens for Ref {
             mutability,
         } = self;
         if let Some((pin, langle, _rangle)) = pin_tokens {
-            tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
+            tokens.extend(quote_spanned!(pin.span=> ::cxx::core::pin::Pin));
             langle.to_tokens(tokens);
         }
         ampersand.to_tokens(tokens);


Test patch: