Repository: google/autocxx (rust)

Issue:
Expected a pinned reference: WasNotPin
c++ header
```
namespace mapnik {
class Map {
public:
  int &a(long);
};
} // namespace mapnik
```
```
thread 'main' panicked at 'Expected a pinned reference: WasNotPin', /home/boydjohnson/.cargo/registry/src/github.com-1ecc6299db9ec823/autocxx-engine-0.22.3/src/conversion/codegen_rs/lifetime.rs:103:33
```
## Expected Behavior
autocxx-gen produces the c++ side of the bridge.

## Actual Behavior
autocxx-bindgen produces 

```
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct mapnik_Map {
    pub _address: u8,
}
extern "C" {
    #[link_name = "\u{1}_ZN6mapnik3Map1aEl"]
    pub fn mapnik_Map_a(
        this: *mut mapnik_Map,
        arg1: ::std::os::raw::c_long,
    ) -> *mut ::std::os::raw::c_int;
}
impl mapnik_Map {
    #[inline]
    pub unsafe fn a(&mut self, arg1: ::std::os::raw::c_long) -> *mut ::std::os::raw::c_int {
        mapnik_Map_a(self, arg1)
    }
}
```
but autocxx-gen fails with the above panic.


## Specifications

- Version: 0.22.3
- Platform: ubuntu

Let me know if this is a bad issue/not a bug/bug in my understanding of C++.


Pull request description:
Fix problem with functions returning references which took non-reference parameters.
Fixes #1143.

Patch:
diff --git a/engine/src/conversion/codegen_rs/fun_codegen.rs b/engine/src/conversion/codegen_rs/fun_codegen.rs
index db222a64..6ba80564 100644
--- a/engine/src/conversion/codegen_rs/fun_codegen.rs
+++ b/engine/src/conversion/codegen_rs/fun_codegen.rs
@@ -132,7 +132,6 @@ pub(super) fn gen_function(
         params,
         Cow::Borrowed(&ret_type),
         non_pod_types,
-        true,
     );
 
     if analysis.rust_wrapper_needed {
@@ -308,7 +307,6 @@ impl<'a> FnGenerator<'a> {
             wrapper_params,
             ret_type,
             self.non_pod_types,
-            false,
         );
 
         let cxxbridge_name = self.cxxbridge_name;
diff --git a/engine/src/conversion/codegen_rs/lifetime.rs b/engine/src/conversion/codegen_rs/lifetime.rs
index ea2c782b..19189c7f 100644
--- a/engine/src/conversion/codegen_rs/lifetime.rs
+++ b/engine/src/conversion/codegen_rs/lifetime.rs
@@ -36,12 +36,12 @@ pub(crate) fn add_explicit_lifetime_if_necessary<'r>(
     mut params: Punctuated<FnArg, Comma>,
     ret_type: Cow<'r, ReturnType>,
     non_pod_types: &HashSet<QualifiedName>,
-    assert_all_parameters_are_references: bool,
 ) -> (
     Option<TokenStream>,
     Punctuated<FnArg, Comma>,
     Cow<'r, ReturnType>,
 ) {
+    log::info!("Beforehand it's {:?}", params.to_token_stream().to_string());
     let has_mutable_receiver = param_details.iter().any(|pd| {
         matches!(pd.self_type, Some((_, ReceiverMutability::Mutable)))
             && !pd.is_placement_return_destination
@@ -93,25 +93,16 @@ pub(crate) fn add_explicit_lifetime_if_necessary<'r>(
         None => (None, params, ret_type),
         Some(new_return_type) => {
             for mut param in params.iter_mut() {
-                match &mut param {
-                    FnArg::Typed(PatType { ty, .. }) => match ty.as_mut() {
+                if let FnArg::Typed(PatType { ty, .. }) = &mut param {
+                    match ty.as_mut() {
                         Type::Path(TypePath {
                             path: Path { segments, .. },
                             ..
-                        }) => add_lifetime_to_pinned_reference(segments).unwrap_or_else(|e| {
-                            if assert_all_parameters_are_references {
-                                panic!("Expected a pinned reference: {:?}", e)
-                            }
-                        }),
+                        }) => add_lifetime_to_pinned_reference(segments).unwrap_or(()),
                         Type::Reference(tyr) => add_lifetime_to_reference(tyr),
                         Type::ImplTrait(tyit) => add_lifetime_to_impl_trait(tyit),
-                        _ if assert_all_parameters_are_references => {
-                            panic!("Expected Pin<&mut T> or &T")
-                        }
                         _ => {}
-                    },
-                    _ if assert_all_parameters_are_references => panic!("Unexpected fnarg"),
-                    _ => {}
+                    }
                 }
             }
 
diff --git a/engine/src/conversion/codegen_rs/mod.rs b/engine/src/conversion/codegen_rs/mod.rs
index 32fced3a..938101de 100644
--- a/engine/src/conversion/codegen_rs/mod.rs
+++ b/engine/src/conversion/codegen_rs/mod.rs
@@ -1142,16 +1142,13 @@ impl<'a> RsCodeGenerator<'a> {
         let id = name.type_path_from_root();
         let super_duper = std::iter::repeat(make_ident("super"));
         let supers = super_duper.take(ns_depth + 2);
-        let name_final = name.get_final_ident();
         let use_statement = parse_quote! {
             pub use #(#supers)::* :: #id;
         };
         RsCodegenResult {
             bindgen_mod_items: vec![use_statement],
             extern_c_mod_items: vec![self.generate_cxxbridge_type(name, true, Vec::new())],
-            materializations: vec![Use::Custom(Box::new(
-                parse_quote! { pub use #rust_path as #name_final; },
-            ))],
+            materializations: vec![Use::Custom(Box::new(parse_quote! { pub use #rust_path; }))],
             ..Default::default()
         }
     }


Test patch:
diff --git a/integration-tests/tests/integration_test.rs b/integration-tests/tests/integration_test.rs
index 28405abc..f331f76a 100644
--- a/integration-tests/tests/integration_test.rs
+++ b/integration-tests/tests/integration_test.rs
@@ -6642,48 +6642,6 @@ fn test_extern_cpp_type_cxx_bridge() {
     do_run_test_manual("", hdr, rs, None, None).unwrap();
 }
 
-#[test]
-fn test_extern_cpp_type_different_name() {
-    let hdr = indoc! {"
-        #include <cstdint>
-        struct A {
-            A() : a(0) {}
-            int a;
-        };
-        inline void handle_a(const A&) {
-        }
-        inline A create_a() {
-            A a;
-            return a;
-        }
-    "};
-    let hexathorpe = Token![#](Span::call_site());
-    let rs = quote! {
-        use autocxx::prelude::*;
-        include_cpp! {
-            #hexathorpe include "input.h"
-            safety!(unsafe_ffi)
-            generate!("handle_a")
-            generate!("create_a")
-            extern_cpp_opaque_type!("A", crate::DifferentA)
-        }
-        #[cxx::bridge]
-        pub mod ffi2 {
-            unsafe extern "C++" {
-                include!("input.h");
-                type A;
-            }
-            impl UniquePtr<A> {}
-        }
-        pub use ffi2::A as DifferentA;
-        fn main() {
-            let a = ffi::create_a();
-            ffi::handle_a(&a);
-        }
-    };
-    do_run_test_manual("", hdr, rs, None, None).unwrap();
-}
-
 #[test]
 fn test_extern_cpp_type_two_include_cpp() {
     let hdr = indoc! {"
@@ -11584,7 +11542,6 @@ fn test_issue_1125() {
 }
 
 #[test]
-#[ignore]
 fn test_issue_1143() {
     let hdr = indoc! {
         "namespace mapnik {