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 generated bindings for list-of-borrows #670

Merged
merged 1 commit into from
Sep 18, 2023
Merged
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
57 changes: 39 additions & 18 deletions crates/rust-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,9 @@ pub trait RustGenerator<'a> {
// If the type recursively owns data and it's a
// variant/record/list, then we need to place the
// lifetime parameter on the type as well.
if (info.has_list || info.has_borrow_handle) && needs_generics(self.resolve(), &ty.kind)
if (info.has_list || info.has_borrow_handle)
&& !info.has_own_handle
&& needs_generics(self.resolve(), &ty.kind)
{
self.print_generics(lt);
}
Expand Down Expand Up @@ -470,6 +472,11 @@ pub trait RustGenerator<'a> {
} else {
mode
};
// Lists with `own` handles must always be owned
let mode = match *ty {
Type::Id(id) if self.info(id).has_own_handle => TypeMode::Owned,
_ => mode,
};
sunfishcode marked this conversation as resolved.
Show resolved Hide resolved
match mode {
TypeMode::AllBorrowed(lt) => {
self.print_borrowed_slice(false, ty, lt, next_mode);
Expand Down Expand Up @@ -529,21 +536,29 @@ pub trait RustGenerator<'a> {

fn modes_of(&self, ty: TypeId) -> Vec<(String, TypeMode)> {
let info = self.info(ty);
// If this type isn't actually used, no need to generate it.
if !info.owned && !info.borrowed {
return Vec::new();
}
let mut result = Vec::new();
let first_mode =
if info.owned || !info.borrowed || matches!(self.ownership(), Ownership::Owning) {
if info.has_borrow_handle {
TypeMode::HandlesBorrowed("'a")
} else {
TypeMode::Owned
}

// Prioritize generating an "owned" type. This is done to simplify
// generated bindings by default. Borrowed handles always use a borrow,
// however.
let first_mode = if info.owned
|| !info.borrowed
|| matches!(self.ownership(), Ownership::Owning)
|| info.has_own_handle
{
if info.has_borrow_handle {
TypeMode::HandlesBorrowed("'a")
} else {
assert!(!self.uses_two_names(&info));
TypeMode::AllBorrowed("'a")
};
TypeMode::Owned
}
} else {
assert!(!self.uses_two_names(&info));
TypeMode::AllBorrowed("'a")
};
result.push((self.result_name(ty), first_mode));
if self.uses_two_names(&info) {
result.push((self.param_name(ty), TypeMode::AllBorrowed("'a")));
Expand Down Expand Up @@ -1025,15 +1040,21 @@ pub trait RustGenerator<'a> {
}

fn uses_two_names(&self, info: &TypeInfo) -> bool {
info.has_list
// Types are only duplicated if explicitly requested ...
matches!(
self.ownership(),
Ownership::Borrowing {
duplicate_if_necessary: true
}
)
// ... and if they're both used in a borrowed/owned context
&& info.borrowed
&& info.owned
&& matches!(
self.ownership(),
Ownership::Borrowing {
duplicate_if_necessary: true
}
)
// ... and they have a list ...
&& info.has_list
// ... and if there's NOT an `own` handle since those are always
// done by ownership.
&& !info.has_own_handle
}

fn lifetime_for(&self, info: &TypeInfo, mode: TypeMode) -> Option<&'static str> {
Expand Down
8 changes: 7 additions & 1 deletion crates/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,13 @@ impl Bindgen for FunctionBindgen<'_, '_> {
}

fn is_list_canonical(&self, resolve: &Resolve, ty: &Type) -> bool {
resolve.all_bits_valid(ty)
if !resolve.all_bits_valid(ty) {
return false;
}
match ty {
Type::Id(id) => !self.gen.gen.types.get(*id).has_resource,
_ => true,
}
}

fn emit(
Expand Down