Skip to content

Commit 3ebafeb

Browse files
authored
Generate explicit borrow dropping functions when autodrop borrows is disabled (#1320)
1 parent cfb16fd commit 3ebafeb

File tree

7 files changed

+122
-1
lines changed

7 files changed

+122
-1
lines changed

crates/c/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,8 @@ void {ns}_{snake}_drop_own({own} handle) {{
12951295
"\ntypedef struct {borrow} {{\nint32_t __handle;\n}} {borrow};\n"
12961296
));
12971297

1298-
if self.autodrop_enabled() {
1298+
// Explicit borrow dropping is not required if autodrop is enabled.
1299+
if !self.autodrop_enabled() {
12991300
// As we have two different types for owned vs borrowed resources,
13001301
// but owns and borrows are dropped using the same intrinsic we
13011302
// also generate a version of the drop function for borrows that we
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ args = '--autodrop-borrows=yes'
2+
3+
#include "autodropper.h"
4+
#include <assert.h>
5+
6+
void exports_test_resource_borrow_imported_autodrop_borrow_thing_do_borrow(
7+
test_resource_borrow_imported_test_borrow_thing_t thing) {
8+
uint32_t result = test_resource_borrow_imported_test_method_thing_get_int(thing);
9+
assert(result == 42);
10+
// Intentionally do not drop the borrow, as it will be done automatically
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ args = '--autodrop-borrows=no'
2+
3+
#include "borrower.h"
4+
#include <assert.h>
5+
6+
void exports_test_resource_borrow_imported_borrow_thing_do_borrow(
7+
test_resource_borrow_imported_test_borrow_thing_t thing) {
8+
uint32_t result = test_resource_borrow_imported_test_method_thing_get_int(thing);
9+
assert(result == 42);
10+
// We must explicitly drop the borrow, because autodrop borrows is turned off
11+
test_resource_borrow_imported_test_thing_drop_borrow(thing);
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package example:composition;
2+
3+
let exporter = new test:exporter { ... };
4+
let borrower = new test:borrower {
5+
test: exporter.test,
6+
...
7+
};
8+
let autodropper = new test:autodropper {
9+
test: exporter.test,
10+
...
11+
};
12+
let runner = new test:runner {
13+
test: exporter.test,
14+
borrow-thing: borrower.borrow-thing,
15+
autodrop-borrow-thing: autodropper.autodrop-borrow-thing,
16+
...
17+
};
18+
19+
export runner...;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "exporter.h"
2+
#include <stdlib.h>
3+
4+
struct exports_test_resource_borrow_imported_test_thing_t {
5+
uint32_t my_state;
6+
};
7+
8+
uint32_t exports_test_resource_borrow_imported_test_method_thing_get_int(
9+
exports_test_resource_borrow_imported_test_borrow_thing_t thing) {
10+
return thing->my_state;
11+
}
12+
13+
void exports_test_resource_borrow_imported_test_thing_destructor(
14+
exports_test_resource_borrow_imported_test_thing_t *rep) {
15+
free(rep);
16+
}
17+
18+
exports_test_resource_borrow_imported_test_own_thing_t exports_test_resource_borrow_imported_test_constructor_thing(void) {
19+
exports_test_resource_borrow_imported_test_thing_t *rep = malloc(sizeof(exports_test_resource_borrow_imported_test_thing_t));
20+
rep->my_state = 42;
21+
return exports_test_resource_borrow_imported_test_thing_new(rep);
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <assert.h>
2+
#include "runner.h"
3+
4+
int main() {
5+
test_resource_borrow_imported_test_own_thing_t thing = test_resource_borrow_imported_test_constructor_thing();
6+
assert(thing.__handle != 0);
7+
8+
test_resource_borrow_imported_borrow_thing_do_borrow(
9+
test_resource_borrow_imported_test_borrow_thing(thing)
10+
);
11+
12+
test_resource_borrow_imported_autodrop_borrow_thing_do_borrow(
13+
test_resource_borrow_imported_test_borrow_thing(thing)
14+
);
15+
16+
test_resource_borrow_imported_test_thing_drop_own(thing);
17+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//@ dependencies = ['exporter', 'borrower', 'autodropper']
2+
//@ wac = 'compose.wac'
3+
4+
package test:resource-borrow-imported;
5+
6+
interface test {
7+
resource thing {
8+
constructor();
9+
get-int: func() -> u32;
10+
}
11+
}
12+
13+
interface borrow-thing {
14+
use test.{thing};
15+
do-borrow: func(handle: borrow<thing>);
16+
}
17+
18+
interface autodrop-borrow-thing {
19+
use test.{thing};
20+
do-borrow: func(handle: borrow<thing>);
21+
}
22+
23+
world exporter {
24+
export test;
25+
}
26+
27+
world borrower {
28+
export borrow-thing;
29+
}
30+
31+
world autodropper {
32+
export autodrop-borrow-thing;
33+
}
34+
35+
world runner {
36+
import test;
37+
import autodrop-borrow-thing;
38+
import borrow-thing;
39+
}

0 commit comments

Comments
 (0)