From d6f1496e132f779ebd1882dcfc4aa15cb0dbd0b9 Mon Sep 17 00:00:00 2001 From: Calvin Prewitt Date: Fri, 24 May 2024 12:45:50 -0500 Subject: [PATCH] fixed generator used for `cargo component new` to include bindings for resources that have no methods and incorrectly did borrow type when inlined in world (#304) --- src/generator.rs | 20 ++++++++++++++++++++ tests/new.rs | 13 +++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/generator.rs b/src/generator.rs index 33db9dde..3391b5eb 100644 --- a/src/generator.rs +++ b/src/generator.rs @@ -435,6 +435,9 @@ impl<'a> UnimplementedFunction<'a> { fn is_exported_resource(&self, id: TypeId) -> bool { let type_info = &self.resolve.types[id]; + if let TypeOwner::World(_) = type_info.owner { + return false; + } match type_info.kind { TypeDefKind::Type(Type::Id(kind_id)) => { let kind_type = &self.resolve.types[kind_id]; @@ -573,6 +576,23 @@ impl<'a> InterfaceGenerator<'a> { } } + // Add resources that did not have any methods + for (_, id) in interface.types.iter() { + let ty = &resolve.types[*id]; + if ty.kind == TypeDefKind::Resource && !resources.contains_key(id) { + let name = ty.name.as_deref().expect("unnamed resource type"); + let impl_name = names.reserve(name); + resources.insert( + *id, + Resource { + ty, + impl_name, + functions: vec![], + }, + ); + } + } + Self { resolve, key, diff --git a/tests/new.rs b/tests/new.rs index 32d0c895..d53d0dcd 100644 --- a/tests/new.rs +++ b/tests/new.rs @@ -208,11 +208,20 @@ interface baz { z: func(b: borrow); } +interface another { + resource yet-another { + hello: func() -> string; + } + resource empty {} +} + world not-used { export not-exported; } world foo { + use another.{yet-another, empty}; + resource file { open: static func(path: string) -> file; path: func() -> string; @@ -223,6 +232,10 @@ world foo { export baz; export is-exported; export is-exported-and-aliased; + + export another; + export another-func: func(yet: borrow) -> result; + export another-func-empty: func(empty: borrow) -> result; }"#, true, )