Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deleted destructors. #1329

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions engine/src/conversion/analysis/abstract_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use autocxx_bindgen::callbacks::Explicitness;
use indexmap::map::IndexMap as HashMap;
use syn::{punctuated::Punctuated, token::Comma};

Expand All @@ -19,7 +20,7 @@ use super::{
use crate::{
conversion::{
analysis::{depth_first::fields_and_bases_first, fun::ReceiverMutability},
api::{ApiName, TypeKind},
api::{ApiName, CppVisibility, FuncToConvert, TypeKind},
error_reporter::{convert_apis, convert_item_apis},
ConvertErrorFromCpp, CppEffectiveName,
},
Expand Down Expand Up @@ -150,13 +151,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: Some(Explicitness::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 @@ -175,7 +212,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 @@ -209,6 +246,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.is_nested())
.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.num_generics > 0 {
let constructor_and_allocator_deps = if !pod.kind.is_instantiable() || pod.num_generics > 0 {
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)
}
}

/// Details about a C++ struct.
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 @@ -813,7 +813,7 @@ impl<'a> RsCodeGenerator<'a> {
}
}
}
TypeKind::Abstract => {
TypeKind::Abstract | TypeKind::NotDestructible => {
if num_generics > 0 {
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 @@ -103,6 +103,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 @@ -12413,6 +12413,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"],
&[],
);
}

#[test]
fn test_using_string_function() {
let hdr = indoc! {"
Expand Down
Loading