Skip to content

Commit

Permalink
Fix deleted destructors.
Browse files Browse the repository at this point in the history
Fixes #829.
  • Loading branch information
adetaylor committed Sep 14, 2023
1 parent 762dbf3 commit 7131ec0
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ exclude = ["examples/s2", "examples/steam-mini", "examples/subclass", "examples/
#[patch.crates-io]
#cxx = { path="../cxx" }
#cxx-gen = { path="../cxx/gen/lib" }
#autocxx-bindgen = { path="../bindgen" }
#autocxx-bindgen = { path="../bindgen/bindgen" }
#moveit = { path="../moveit" }
59 changes: 57 additions & 2 deletions engine/src/conversion/analysis/abstract_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use super::{
};
use crate::conversion::{
analysis::{depth_first::fields_and_bases_first, fun::ReceiverMutability},
api::{ApiName, TypeKind},
api::{ApiName, CppVisibility, DeletedOrDefaulted, FuncToConvert, TypeKind},
error_reporter::{convert_apis, convert_item_apis},
ConvertErrorFromCpp,
};
Expand Down Expand Up @@ -147,13 +147,49 @@ pub(crate) fn mark_types_abstract(apis: ApiVec<FnPrePhase2>) -> ApiVec<FnPrePhas
}
}

// While we're here, we'll look for types which are non-destructible.
let non_destructible_types: HashSet<QualifiedName> = apis
.iter()
.filter_map(|api| match api {
Api::Function {
fun,
analysis:
FnAnalysis {
kind:
FnKind::TraitMethod {
kind: TraitMethodKind::Destructor,
impl_for,
..
},
..
},
..
} if matches!(
fun.as_ref(),
FuncToConvert {
cpp_vis: CppVisibility::Private,
..
} | FuncToConvert {
is_deleted: DeletedOrDefaulted::Deleted,
..
}
) =>
{
Some(impl_for.clone())
}
_ => None,
})
.collect();

// mark abstract types as abstract
let mut apis: ApiVec<_> = apis
.into_iter()
.map(|mut api| {
if let Api::Struct { name, analysis, .. } = &mut api {
if abstract_classes.contains(&name.name) {
analysis.pod.kind = TypeKind::Abstract;
} else if non_destructible_types.contains(&name.name) {
analysis.pod.kind = TypeKind::NotDestructible;
}
}
api
Expand All @@ -172,7 +208,7 @@ pub(crate) fn mark_types_abstract(apis: ApiVec<FnPrePhase2>) -> ApiVec<FnPrePhas
..
},
..
} if abstract_classes.contains(self_ty)
} if abstract_classes.contains(self_ty) || non_destructible_types.contains(self_ty)
)
});

Expand Down Expand Up @@ -206,6 +242,25 @@ pub(crate) fn mark_types_abstract(apis: ApiVec<FnPrePhase2>) -> ApiVec<FnPrePhas
{
Err(ConvertErrorFromCpp::AbstractNestedType)
}
Api::Struct {
analysis:
PodAndConstructorAnalysis {
pod:
PodAnalysis {
kind: TypeKind::NotDestructible,
..
},
..
},
..
} if api
.cpp_name()
.as_ref()
.map(|n| n.contains("::"))
.unwrap_or_default() =>
{
Err(ConvertErrorFromCpp::NonDestructibleNestedType)
}
_ => Ok(Box::new(std::iter::once(api))),
});

Expand Down
5 changes: 2 additions & 3 deletions engine/src/conversion/analysis/constructor_deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use indexmap::map::IndexMap as HashMap;

use crate::{
conversion::{
api::{Api, ApiName, StructDetails, TypeKind},
api::{Api, ApiName, StructDetails},
apivec::ApiVec,
convert_error::ConvertErrorWithContext,
error_reporter::convert_apis,
Expand Down Expand Up @@ -51,8 +51,7 @@ fn decorate_struct(
constructors_and_allocators_by_type: &mut HashMap<QualifiedName, Vec<QualifiedName>>,
) -> Result<Box<dyn Iterator<Item = Api<FnPhase>>>, ConvertErrorWithContext> {
let pod = fn_struct.pod;
let is_abstract = matches!(pod.kind, TypeKind::Abstract);
let constructor_and_allocator_deps = if is_abstract || pod.is_generic {
let constructor_and_allocator_deps = if !pod.kind.is_instantiable() || pod.is_generic {
Vec::new()
} else {
constructors_and_allocators_by_type
Expand Down
20 changes: 14 additions & 6 deletions engine/src/conversion/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,21 @@ use super::{

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub(crate) enum TypeKind {
Pod, // trivial. Can be moved and copied in Rust.
NonPod, // has destructor or non-trivial move constructors. Can only hold by UniquePtr
Pod, // trivial. Can be moved and copied in Rust.
NonPod, // has destructor or non-trivial move constructors. Can only hold by UniquePtr
Abstract, // has pure virtual members - can't even generate UniquePtr.
// It's possible that the type itself isn't pure virtual, but it inherits from
// some other type which is pure virtual. Alternatively, maybe we just don't
// know if the base class is pure virtual because it wasn't on the allowlist,
// in which case we'll err on the side of caution.
// It's possible that the type itself isn't pure virtual, but it inherits from
// some other type which is pure virtual. Alternatively, maybe we just don't
// know if the base class is pure virtual because it wasn't on the allowlist,
// in which case we'll err on the side of caution.
NotDestructible, // private or deleted destructor. We treat it the same as Abstract.
}

impl TypeKind {
/// Whether this TypeKind can be instantiated.
pub(crate) fn is_instantiable(&self) -> bool {
!matches!(self, Self::Abstract | Self::NotDestructible)
}
}

/// C++ visibility.
Expand Down
2 changes: 1 addition & 1 deletion engine/src/conversion/codegen_rs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ impl<'a> RsCodeGenerator<'a> {
}
}
}
TypeKind::Abstract => {
TypeKind::Abstract | TypeKind::NotDestructible => {
if is_generic {
RsCodegenResult::default()
} else {
Expand Down
2 changes: 2 additions & 0 deletions engine/src/conversion/convert_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ pub enum ConvertErrorFromCpp {
RustTypeWithAPath(QualifiedName),
#[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.")]
AbstractNestedType,
#[error("This type is nested within another struct/class, yet is not destructible (private or deleted destructor). This is not yet supported by autocxx.")]
NonDestructibleNestedType,
#[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.")]
NestedOpaqueTypedef,
#[error(
Expand Down
53 changes: 53 additions & 0 deletions integration-tests/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12293,6 +12293,59 @@ fn test_cpp_union_pod() {
run_test_expect_fail("", hdr, quote! {}, &[], &["CorrelationId_t_"]);
}

#[test]
fn test_deleted_destructor() {
let hdr = indoc! {"
#include <stdarg.h>
class A {
public:
void foo();
~A() = delete;
};
inline A* get_a() {
static A* a = new A();
return a;
}
"};
run_test("", hdr, quote! {}, &["A", "get_a"], &[]);
run_test_expect_fail(
"",
hdr,
quote! {
let _ = ffi::A::within_unique_ptr();
},
&["A", "get_a"],
&[],
);
}

#[test]
fn test_private_destructor() {
let hdr = indoc! {"
#include <stdarg.h>
class A {
public:
void foo();
private:
~A() {};
};
inline A* get_a() {
static A* a = new A();
return a;
}
"};
run_test("", hdr, quote! {}, &["A", "get_a"], &[]);
run_test_expect_fail(
"",
hdr,
quote! {
let _ = ffi::A::within_unique_ptr();
},
&["A", "get_a"],
&[],
);
}

// Yet to test:
// - Ifdef
// - Out param pointers
Expand Down

0 comments on commit 7131ec0

Please sign in to comment.