Repository: dtolnay/cxx (language: rust)

Issue:
Placement new requires #include <new>
Using the example from #237 (with the extra interop header), I now get:

```
gen/base/rs/enterprise_util.cc:16:3: error: no matching function for call to 'operator new'
  new (return$) DeviceUserDomainJoinState(AreDeviceAndUserJoinedToDomain$());
  ^   ~~~~~~~~~
note: candidate function not viable: requires 1 argument, but 2 were provided
```

This is fixed if the generated .cc file has an extra line:

```cpp
#include <new>
```

This [quote from the C++ standard suggests that, yes, inclusion of a header is required to use placement new](https://stackoverflow.com/a/2788548).


Discussion / hints:

Fix patch:
diff --git a/gen/src/include.rs b/gen/src/include.rs
index 0ed76ac16..4309c9ced 100644
--- a/gen/src/include.rs
+++ b/gen/src/include.rs
@@ -56,6 +56,7 @@ pub struct Includes {
     pub cstring: bool,
     pub exception: bool,
     pub memory: bool,
+    pub new: bool,
     pub string: bool,
     pub type_traits: bool,
     pub utility: bool,
@@ -106,6 +107,9 @@ impl Display for Includes {
         if self.memory {
             writeln!(f, "#include <memory>")?;
         }
+        if self.new {
+            writeln!(f, "#include <new>")?;
+        }
         if self.string {
             writeln!(f, "#include <string>")?;
         }
diff --git a/gen/src/write.rs b/gen/src/write.rs
index 2cf9faf6e..46b6151c1 100644
--- a/gen/src/write.rs
+++ b/gen/src/write.rs
@@ -149,11 +149,13 @@ fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
     for ty in types {
         match ty {
             Type::RustBox(_) => {
+                out.include.new = true;
                 out.include.type_traits = true;
                 needs_rust_box = true;
             }
             Type::RustVec(_) => {
                 out.include.array = true;
+                out.include.new = true;
                 out.include.type_traits = true;
                 needs_rust_vec = true;
             }
@@ -463,6 +465,7 @@ fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
         write!(out, "        ");
     }
     if indirect_return {
+        out.include.new = true;
         write!(out, "new (return$) ");
         write_indirect_return_type(out, efn.ret.as_ref().unwrap());
         write!(out, "(");
@@ -1149,6 +1152,7 @@ fn write_unique_ptr(out: &mut OutFile, ident: &Ident, types: &Types) {
 
 // Shared by UniquePtr<T> and UniquePtr<CxxVector<T>>.
 fn write_unique_ptr_common(out: &mut OutFile, ty: &Type, types: &Types) {
+    out.include.new = true;
     out.include.utility = true;
     let inner = to_typename(&out.namespace, ty);
     let instance = to_mangled(&out.namespace, ty);
diff --git a/include/cxx.h b/include/cxx.h
index 9f4fb4767..f211b3be1 100644
--- a/include/cxx.h
+++ b/include/cxx.h
@@ -4,6 +4,7 @@
 #include <cstdint>
 #include <exception>
 #include <iosfwd>
+#include <new>
 #include <string>
 #include <type_traits>
 #include <utility>


Test patch: