Skip to content

Commit 7131ec0

Browse files
committed
Fix deleted destructors.
Fixes #829.
1 parent 762dbf3 commit 7131ec0

File tree

7 files changed

+130
-13
lines changed

7 files changed

+130
-13
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ exclude = ["examples/s2", "examples/steam-mini", "examples/subclass", "examples/
3737
#[patch.crates-io]
3838
#cxx = { path="../cxx" }
3939
#cxx-gen = { path="../cxx/gen/lib" }
40-
#autocxx-bindgen = { path="../bindgen" }
40+
#autocxx-bindgen = { path="../bindgen/bindgen" }
4141
#moveit = { path="../moveit" }

engine/src/conversion/analysis/abstract_types.rs

+57-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use super::{
1818
};
1919
use crate::conversion::{
2020
analysis::{depth_first::fields_and_bases_first, fun::ReceiverMutability},
21-
api::{ApiName, TypeKind},
21+
api::{ApiName, CppVisibility, DeletedOrDefaulted, FuncToConvert, TypeKind},
2222
error_reporter::{convert_apis, convert_item_apis},
2323
ConvertErrorFromCpp,
2424
};
@@ -147,13 +147,49 @@ pub(crate) fn mark_types_abstract(apis: ApiVec<FnPrePhase2>) -> ApiVec<FnPrePhas
147147
}
148148
}
149149

150+
// While we're here, we'll look for types which are non-destructible.
151+
let non_destructible_types: HashSet<QualifiedName> = apis
152+
.iter()
153+
.filter_map(|api| match api {
154+
Api::Function {
155+
fun,
156+
analysis:
157+
FnAnalysis {
158+
kind:
159+
FnKind::TraitMethod {
160+
kind: TraitMethodKind::Destructor,
161+
impl_for,
162+
..
163+
},
164+
..
165+
},
166+
..
167+
} if matches!(
168+
fun.as_ref(),
169+
FuncToConvert {
170+
cpp_vis: CppVisibility::Private,
171+
..
172+
} | FuncToConvert {
173+
is_deleted: DeletedOrDefaulted::Deleted,
174+
..
175+
}
176+
) =>
177+
{
178+
Some(impl_for.clone())
179+
}
180+
_ => None,
181+
})
182+
.collect();
183+
150184
// mark abstract types as abstract
151185
let mut apis: ApiVec<_> = apis
152186
.into_iter()
153187
.map(|mut api| {
154188
if let Api::Struct { name, analysis, .. } = &mut api {
155189
if abstract_classes.contains(&name.name) {
156190
analysis.pod.kind = TypeKind::Abstract;
191+
} else if non_destructible_types.contains(&name.name) {
192+
analysis.pod.kind = TypeKind::NotDestructible;
157193
}
158194
}
159195
api
@@ -172,7 +208,7 @@ pub(crate) fn mark_types_abstract(apis: ApiVec<FnPrePhase2>) -> ApiVec<FnPrePhas
172208
..
173209
},
174210
..
175-
} if abstract_classes.contains(self_ty)
211+
} if abstract_classes.contains(self_ty) || non_destructible_types.contains(self_ty)
176212
)
177213
});
178214

@@ -206,6 +242,25 @@ pub(crate) fn mark_types_abstract(apis: ApiVec<FnPrePhase2>) -> ApiVec<FnPrePhas
206242
{
207243
Err(ConvertErrorFromCpp::AbstractNestedType)
208244
}
245+
Api::Struct {
246+
analysis:
247+
PodAndConstructorAnalysis {
248+
pod:
249+
PodAnalysis {
250+
kind: TypeKind::NotDestructible,
251+
..
252+
},
253+
..
254+
},
255+
..
256+
} if api
257+
.cpp_name()
258+
.as_ref()
259+
.map(|n| n.contains("::"))
260+
.unwrap_or_default() =>
261+
{
262+
Err(ConvertErrorFromCpp::NonDestructibleNestedType)
263+
}
209264
_ => Ok(Box::new(std::iter::once(api))),
210265
});
211266

engine/src/conversion/analysis/constructor_deps.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use indexmap::map::IndexMap as HashMap;
1010

1111
use crate::{
1212
conversion::{
13-
api::{Api, ApiName, StructDetails, TypeKind},
13+
api::{Api, ApiName, StructDetails},
1414
apivec::ApiVec,
1515
convert_error::ConvertErrorWithContext,
1616
error_reporter::convert_apis,
@@ -51,8 +51,7 @@ fn decorate_struct(
5151
constructors_and_allocators_by_type: &mut HashMap<QualifiedName, Vec<QualifiedName>>,
5252
) -> Result<Box<dyn Iterator<Item = Api<FnPhase>>>, ConvertErrorWithContext> {
5353
let pod = fn_struct.pod;
54-
let is_abstract = matches!(pod.kind, TypeKind::Abstract);
55-
let constructor_and_allocator_deps = if is_abstract || pod.is_generic {
54+
let constructor_and_allocator_deps = if !pod.kind.is_instantiable() || pod.is_generic {
5655
Vec::new()
5756
} else {
5857
constructors_and_allocators_by_type

engine/src/conversion/api.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,21 @@ use super::{
3838

3939
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
4040
pub(crate) enum TypeKind {
41-
Pod, // trivial. Can be moved and copied in Rust.
42-
NonPod, // has destructor or non-trivial move constructors. Can only hold by UniquePtr
41+
Pod, // trivial. Can be moved and copied in Rust.
42+
NonPod, // has destructor or non-trivial move constructors. Can only hold by UniquePtr
4343
Abstract, // has pure virtual members - can't even generate UniquePtr.
44-
// It's possible that the type itself isn't pure virtual, but it inherits from
45-
// some other type which is pure virtual. Alternatively, maybe we just don't
46-
// know if the base class is pure virtual because it wasn't on the allowlist,
47-
// in which case we'll err on the side of caution.
44+
// It's possible that the type itself isn't pure virtual, but it inherits from
45+
// some other type which is pure virtual. Alternatively, maybe we just don't
46+
// know if the base class is pure virtual because it wasn't on the allowlist,
47+
// in which case we'll err on the side of caution.
48+
NotDestructible, // private or deleted destructor. We treat it the same as Abstract.
49+
}
50+
51+
impl TypeKind {
52+
/// Whether this TypeKind can be instantiated.
53+
pub(crate) fn is_instantiable(&self) -> bool {
54+
!matches!(self, Self::Abstract | Self::NotDestructible)
55+
}
4856
}
4957

5058
/// C++ visibility.

engine/src/conversion/codegen_rs/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ impl<'a> RsCodeGenerator<'a> {
946946
}
947947
}
948948
}
949-
TypeKind::Abstract => {
949+
TypeKind::Abstract | TypeKind::NotDestructible => {
950950
if is_generic {
951951
RsCodegenResult::default()
952952
} else {

engine/src/conversion/convert_error.rs

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ pub enum ConvertErrorFromCpp {
105105
RustTypeWithAPath(QualifiedName),
106106
#[error("This type is nested within another struct/class, yet is abstract (or is not on the allowlist so we can't be sure). This is not yet supported by autocxx. If you don't believe this type is abstract, add it to the allowlist.")]
107107
AbstractNestedType,
108+
#[error("This type is nested within another struct/class, yet is not destructible (private or deleted destructor). This is not yet supported by autocxx.")]
109+
NonDestructibleNestedType,
108110
#[error("This typedef was nested within another struct/class. autocxx is unable to represent inner types if they might be abstract. Unfortunately, autocxx couldn't prove that this type isn't abstract, so it can't represent it.")]
109111
NestedOpaqueTypedef,
110112
#[error(

integration-tests/tests/integration_test.rs

+53
Original file line numberDiff line numberDiff line change
@@ -12293,6 +12293,59 @@ fn test_cpp_union_pod() {
1229312293
run_test_expect_fail("", hdr, quote! {}, &[], &["CorrelationId_t_"]);
1229412294
}
1229512295

12296+
#[test]
12297+
fn test_deleted_destructor() {
12298+
let hdr = indoc! {"
12299+
#include <stdarg.h>
12300+
class A {
12301+
public:
12302+
void foo();
12303+
~A() = delete;
12304+
};
12305+
inline A* get_a() {
12306+
static A* a = new A();
12307+
return a;
12308+
}
12309+
"};
12310+
run_test("", hdr, quote! {}, &["A", "get_a"], &[]);
12311+
run_test_expect_fail(
12312+
"",
12313+
hdr,
12314+
quote! {
12315+
let _ = ffi::A::within_unique_ptr();
12316+
},
12317+
&["A", "get_a"],
12318+
&[],
12319+
);
12320+
}
12321+
12322+
#[test]
12323+
fn test_private_destructor() {
12324+
let hdr = indoc! {"
12325+
#include <stdarg.h>
12326+
class A {
12327+
public:
12328+
void foo();
12329+
private:
12330+
~A() {};
12331+
};
12332+
inline A* get_a() {
12333+
static A* a = new A();
12334+
return a;
12335+
}
12336+
"};
12337+
run_test("", hdr, quote! {}, &["A", "get_a"], &[]);
12338+
run_test_expect_fail(
12339+
"",
12340+
hdr,
12341+
quote! {
12342+
let _ = ffi::A::within_unique_ptr();
12343+
},
12344+
&["A", "get_a"],
12345+
&[],
12346+
);
12347+
}
12348+
1229612349
// Yet to test:
1229712350
// - Ifdef
1229812351
// - Out param pointers

0 commit comments

Comments
 (0)