Skip to content

Commit 33bee20

Browse files
authored
don't combine stream/future ctors so aggressively (#249)
Previously, we would only emit one such constructor function per future or stream "canonical" payload type, where canonicalization would combine structurally equal (but not necessarily nominally equal) types. Since we encode the name of the payload type in the name of the constructor function, that made it hard to predict which names would be generated. Now we deduplicate based on the mangled name instead.
1 parent 23c7428 commit 33bee20

4 files changed

Lines changed: 59 additions & 25 deletions

File tree

‎src/summary.rs‎

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,11 +2019,13 @@ class {camel}(Protocol):
20192019
| TypeDefKind::Result(_)
20202020
| TypeDefKind::Handle(_) => (None, Vec::new()),
20212021
TypeDefKind::Stream(ty) => {
2022-
let canonical = canonical_payload(self.resolve, &mut types, ty);
2023-
let code = if stream_payloads.contains(&canonical) {
2022+
let snake = ty
2023+
.map(|ty| names.mangle_name(ty))
2024+
.unwrap_or_else(|| "unit".into());
2025+
let code = if stream_payloads.contains(&snake) {
20242026
None
20252027
} else {
2026-
stream_payloads.insert(canonical);
2028+
stream_payloads.insert(snake.clone());
20272029

20282030
Some(if let Some(Type::U8 | Type::S8) = ty {
20292031
if stub_runtime_calls {
@@ -2044,9 +2046,6 @@ def byte_stream() -> tuple[ByteStreamWriter, ByteStreamReader]:
20442046
)
20452047
}
20462048
} else {
2047-
let snake = ty
2048-
.map(|ty| names.mangle_name(ty))
2049-
.unwrap_or_else(|| "unit".into());
20502049
let camel = ty
20512050
.map(|ty| names.type_name(ty, &seen, None))
20522051
.unwrap_or_else(|| "None".into());
@@ -2073,15 +2072,14 @@ def {snake}_stream() -> tuple[StreamWriter[{camel}], StreamReader[{camel}]]:
20732072
(code.map(Code::Shared), Vec::new())
20742073
}
20752074
TypeDefKind::Future(ty) => {
2076-
let canonical = canonical_payload(self.resolve, &mut types, ty);
2077-
let code = if future_payloads.contains(&canonical) {
2075+
let snake = ty
2076+
.map(|ty| names.mangle_name(ty))
2077+
.unwrap_or_else(|| "unit".into());
2078+
let code = if future_payloads.contains(&snake) {
20782079
None
20792080
} else {
2080-
future_payloads.insert(canonical);
2081+
future_payloads.insert(snake.clone());
20812082

2082-
let snake = ty
2083-
.map(|ty| names.mangle_name(ty))
2084-
.unwrap_or_else(|| "unit".into());
20852083
let camel = ty
20862084
.map(|ty| names.type_name(ty, &seen, None))
20872085
.unwrap_or_else(|| "None".into());
@@ -3260,19 +3258,6 @@ fn docstring(docs: Option<&str>, indent_level: usize, error: Option<&str>) -> St
32603258
}
32613259
}
32623260

3263-
fn canonical_payload(resolve: &Resolve, types: &mut Types, ty: &Option<Type>) -> Option<Type> {
3264-
match ty {
3265-
Some(Type::Id(id)) => {
3266-
let id = types.get_representative_type(*id);
3267-
match resolve.types[id].kind {
3268-
TypeDefKind::Type(t) => Some(t),
3269-
_ => Some(Type::Id(id)),
3270-
}
3271-
}
3272-
other => *other,
3273-
}
3274-
}
3275-
32763261
fn dealias(resolve: &Resolve, mut id: TypeId) -> TypeId {
32773262
loop {
32783263
match &resolve.types[id].kind {

‎src/test/python_source/app.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from tests.imports.componentize_py.test import host_thing_interface
2020
from tests.exports.componentize_py.test import resource_alias2
2121
from tests.exports.componentize_py.test import streams_and_futures
22+
from tests.exports.componentize_py.test import similar_streams_and_futures
2223
from typing import Tuple, List, Optional
2324
from foo_sdk.wit.exports.foo import sdk as foo_exports
2425
from foo_sdk.wit.exports.foo.sdk import foo_interface as foo_iface
@@ -297,3 +298,11 @@ def test(self, s: str) -> str:
297298
class BarInterface(bar_exports.BarInterface):
298299
def test(self, s: str) -> str:
299300
return bar_test(f"{s} BarInterface.test")
301+
302+
@exports.similar_streams_and_futures.guest
303+
class SimilarStreamsAndFutures(exports.SimilarStreamsAndFutures):
304+
async def baz(self) -> tuple[StreamReader[similar_streams_and_futures.Foo], StreamReader[similar_streams_and_futures.Bar], FutureReader[similar_streams_and_futures.Foo], FutureReader[similar_streams_and_futures.Bar]]:
305+
return (tests.componentize_py_test_similar_streams_and_futures_foo_stream()[1],
306+
tests.componentize_py_test_similar_streams_and_futures_bar_stream()[1],
307+
tests.componentize_py_test_similar_streams_and_futures_foo_future(lambda: similar_streams_and_futures.Foo_A())[1],
308+
tests.componentize_py_test_similar_streams_and_futures_bar_future(lambda: similar_streams_and_futures.Bar_A())[1])

‎src/test/tests.rs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,33 @@ fn test_echo_stream_u8(delay: bool) -> Result<()> {
14001400
})
14011401
}
14021402

1403+
#[test]
1404+
fn test_similar_streams_and_futures() -> Result<()> {
1405+
// Previously, `componentize-py` would coalesce stream and future
1406+
// constructor functions whose payloads were structurally (but not
1407+
// nominally) equivalent, making the names unpredictable. This test ensures
1408+
// we don't do that anymore.
1409+
TESTER.test(|world, store, runtime| {
1410+
runtime.block_on(async {
1411+
store
1412+
.run_concurrent(async |store| {
1413+
// Just call the function and make sure the stream and
1414+
// future constructor functions are resolved in the Python
1415+
// code:
1416+
world
1417+
.componentize_py_test_similar_streams_and_futures()
1418+
.call_baz(store)
1419+
.await?;
1420+
1421+
anyhow::Ok(())
1422+
})
1423+
.await?
1424+
})?;
1425+
1426+
Ok(())
1427+
})
1428+
}
1429+
14031430
struct OptionProducer<T> {
14041431
source: Option<T>,
14051432
sleep: Pin<Box<dyn Future<Output = ()> + Send>>,

‎src/test/wit/tests.wit‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,18 @@ interface streams-and-futures {
165165
dropped-future-reader-host: async func(value: string) -> tuple<future<host-thing>, future<host-thing>>;
166166
}
167167

168+
interface similar-streams-and-futures {
169+
variant foo {
170+
a, b, c
171+
}
172+
173+
variant bar {
174+
a, b, c
175+
}
176+
177+
baz: async func() -> tuple<stream<foo>, stream<bar>, future<foo>, future<bar>>;
178+
}
179+
168180
world tests {
169181
use resource-alias1.{thing};
170182
use resource-floats.{float};
@@ -190,6 +202,7 @@ world tests {
190202
import resource-borrow-in-record;
191203
export resource-borrow-in-record;
192204
export streams-and-futures;
205+
export similar-streams-and-futures;
193206

194207
export resource-floats-exports: interface {
195208
resource float {

0 commit comments

Comments
 (0)