Skip to content

Commit cb8b0ac

Browse files
authored
Handle resources in is_structurally_equal (#1530)
Follow-up from #1468 to account for work in #1526.
1 parent 895b93a commit cb8b0ac

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

crates/core/src/types.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,9 @@ impl Types {
338338
(TypeDefKind::Handle(_), _) => false,
339339
(TypeDefKind::Unknown, _) => unreachable!(),
340340

341-
// TODO: for now consider all resources not-equal to each other.
342-
// This is because the same type id can be used for both an imported
343-
// and exported resource where those should be distinct types.
341+
// Resources are only equal if their original ids are equal,
342+
// otherwise all resources are un-equal to each other.
343+
(TypeDefKind::Resource, TypeDefKind::Resource) => a == b,
344344
(TypeDefKind::Resource, _) => false,
345345
}
346346
}

tests/runtime/rust/equal-types/host.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,24 @@ impl HGuest for Component {
5656
x
5757
}
5858
}
59+
60+
const _: () = {
61+
use crate::exports::test::equal_types::resources::*;
62+
63+
struct GuestResource;
64+
65+
impl Guest for Component {
66+
type R1 = GuestResource;
67+
68+
// Intentionally swap the 1/2 relative to WIT to ensure that the types
69+
// are equivalent.
70+
fn alias_own(x: T2) -> T1 {
71+
x
72+
}
73+
fn alias_aggregate(x: Option<T2>) -> Option<T1> {
74+
x
75+
}
76+
}
77+
78+
impl GuestR1 for GuestResource {}
79+
};

tests/runtime/rust/equal-types/test.wit

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,20 @@ interface blah {
4141
alias-type: func(x: r1) -> r2;
4242
}
4343

44+
interface resources {
45+
resource r1;
46+
type r2 = r1;
47+
48+
record t1 { a: r1 }
49+
record t2 { a: r2 }
50+
alias-own: func(x: t1) -> t2;
51+
alias-aggregate: func(x: option<t1>) -> option<t2>;
52+
}
53+
4454
world host {
4555
export blah;
4656
export blag;
57+
export resources;
4758
}
4859
world proxy {
4960
import blag;

0 commit comments

Comments
 (0)