Repository: dtolnay/cxx
Pull request #638 (2021-01-02 08:38:59)

Remove rust::Str repr conversion in C++
Extracted from #636 since it does not depend on #637.


Patch:
diff --git a/gen/src/builtin.rs b/gen/src/builtin.rs
index b5c6e4dd..ddfd7f64 100644
--- a/gen/src/builtin.rs
+++ b/gen/src/builtin.rs
@@ -20,8 +20,6 @@ pub struct Builtins<'a> {
     pub maybe_uninit: bool,
     pub trycatch: bool,
     pub ptr_len: bool,
-    pub rust_str_new_unchecked: bool,
-    pub rust_str_repr: bool,
     pub rust_slice_new: bool,
     pub rust_slice_repr: bool,
     pub exception: bool,
@@ -234,33 +232,6 @@ pub(super) fn write(out: &mut OutFile) {
         out.end_block(Block::Namespace("repr"));
     }
 
-    if builtin.rust_str_new_unchecked || builtin.rust_str_repr {
-        out.next_section();
-        writeln!(out, "template <>");
-        writeln!(out, "class impl<Str> final {{");
-        writeln!(out, "public:");
-        if builtin.rust_str_new_unchecked {
-            writeln!(
-                out,
-                "  static Str new_unchecked(repr::PtrLen repr) noexcept {{",
-            );
-            writeln!(out, "    Str str;");
-            writeln!(out, "    str.ptr = static_cast<const char *>(repr.ptr);");
-            writeln!(out, "    str.len = repr.len;");
-            writeln!(out, "    return str;");
-            writeln!(out, "  }}");
-        }
-        if builtin.rust_str_repr {
-            writeln!(out, "  static repr::PtrLen repr(Str str) noexcept {{");
-            writeln!(
-                out,
-                "    return repr::PtrLen{{const_cast<char *>(str.ptr), str.len}};",
-            );
-            writeln!(out, "  }}");
-        }
-        writeln!(out, "}};");
-    }
-
     if builtin.rust_slice_new || builtin.rust_slice_repr {
         out.next_section();
         writeln!(out, "template <typename T>");
diff --git a/gen/src/write.rs b/gen/src/write.rs
index 73a4b8c8..ff63f9fd 100644
--- a/gen/src/write.rs
+++ b/gen/src/write.rs
@@ -733,10 +733,6 @@ fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
     }
     match &efn.ret {
         Some(Type::Ref(_)) => write!(out, "&"),
-        Some(Type::Str(_)) if !indirect_return => {
-            out.builtin.rust_str_repr = true;
-            write!(out, "::rust::impl<::rust::Str>::repr(");
-        }
         Some(ty @ Type::SliceRef(_)) if !indirect_return => {
             out.builtin.rust_slice_repr = true;
             write!(out, "::rust::impl<");
@@ -759,13 +755,6 @@ fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
         } else if let Type::UniquePtr(_) = &arg.ty {
             write_type(out, &arg.ty);
             write!(out, "({})", arg.name.cxx);
-        } else if let Type::Str(_) = arg.ty {
-            out.builtin.rust_str_new_unchecked = true;
-            write!(
-                out,
-                "::rust::impl<::rust::Str>::new_unchecked({})",
-                arg.name.cxx,
-            );
         } else if arg.ty == RustString {
             out.builtin.unsafe_bitcopy = true;
             write!(
@@ -796,7 +785,7 @@ fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
     match &efn.ret {
         Some(Type::RustBox(_)) => write!(out, ".into_raw()"),
         Some(Type::UniquePtr(_)) => write!(out, ".release()"),
-        Some(Type::Str(_)) | Some(Type::SliceRef(_)) if !indirect_return => write!(out, ")"),
+        Some(Type::SliceRef(_)) if !indirect_return => write!(out, ")"),
         _ => {}
     }
     if indirect_return {
@@ -995,10 +984,6 @@ fn write_rust_function_shim_impl(
                 write!(out, "(");
             }
             Type::Ref(_) => write!(out, "*"),
-            Type::Str(_) => {
-                out.builtin.rust_str_new_unchecked = true;
-                write!(out, "::rust::impl<::rust::Str>::new_unchecked(");
-            }
             Type::SliceRef(_) => {
                 out.builtin.rust_slice_new = true;
                 write!(out, "::rust::impl<");
@@ -1023,10 +1008,6 @@ fn write_rust_function_shim_impl(
             write!(out, ", ");
         }
         match &arg.ty {
-            Type::Str(_) => {
-                out.builtin.rust_str_repr = true;
-                write!(out, "::rust::impl<::rust::Str>::repr(");
-            }
             Type::SliceRef(_) => {
                 out.builtin.rust_slice_repr = true;
                 write!(out, "::rust::impl<");
@@ -1040,7 +1021,7 @@ fn write_rust_function_shim_impl(
         match &arg.ty {
             Type::RustBox(_) => write!(out, ".into_raw()"),
             Type::UniquePtr(_) => write!(out, ".release()"),
-            Type::Str(_) | Type::SliceRef(_) => write!(out, ")"),
+            Type::SliceRef(_) => write!(out, ")"),
             ty if ty != RustString && out.types.needs_indirect_abi(ty) => write!(out, "$.value"),
             _ => {}
         }
@@ -1062,7 +1043,7 @@ fn write_rust_function_shim_impl(
     write!(out, ")");
     if !indirect_return {
         if let Some(ret) = &sig.ret {
-            if let Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) | Type::SliceRef(_) = ret {
+            if let Type::RustBox(_) | Type::UniquePtr(_) | Type::SliceRef(_) = ret {
                 write!(out, ")");
             }
         }
@@ -1133,7 +1114,7 @@ fn write_extern_return_type_space(out: &mut OutFile, ty: &Option<Type>) {
             write_type(out, &ty.inner);
             write!(out, " *");
         }
-        Some(Type::Str(_)) | Some(Type::SliceRef(_)) => {
+        Some(Type::SliceRef(_)) => {
             out.builtin.ptr_len = true;
             write!(out, "::rust::repr::PtrLen ");
         }
@@ -1148,7 +1129,7 @@ fn write_extern_arg(out: &mut OutFile, arg: &Var) {
             write_type_space(out, &ty.inner);
             write!(out, "*");
         }
-        Type::Str(_) | Type::SliceRef(_) => {
+        Type::SliceRef(_) => {
             out.builtin.ptr_len = true;
             write!(out, "::rust::repr::PtrLen ");
         }


Test patch:
diff --git a/tests/cxx_gen.rs b/tests/cxx_gen.rs
index d33092c7..e91675d9 100644
--- a/tests/cxx_gen.rs
+++ b/tests/cxx_gen.rs
@@ -20,7 +20,7 @@ fn test_extern_c_function() {
     let output = str::from_utf8(&generated.implementation).unwrap();
     // To avoid continual breakage we won't test every byte.
     // Let's look for the major features.
-    assert!(output.contains("void cxxbridge1$do_cpp_thing(::rust::repr::PtrLen foo)"));
+    assert!(output.contains("void cxxbridge1$do_cpp_thing(::rust::Str foo)"));
 }
 
 #[test]
@@ -30,5 +30,5 @@ fn test_impl_annotation() {
     let source = BRIDGE0.parse().unwrap();
     let generated = generate_header_and_cc(source, &opt).unwrap();
     let output = str::from_utf8(&generated.implementation).unwrap();
-    assert!(output.contains("ANNOTATION void cxxbridge1$do_cpp_thing(::rust::repr::PtrLen foo)"));
+    assert!(output.contains("ANNOTATION void cxxbridge1$do_cpp_thing(::rust::Str foo)"));
 }