Skip to content
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
11 changes: 10 additions & 1 deletion crates/wit-parser/src/ast/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ pub struct Resolver<'a> {
foreign_dep_spans: Vec<Span>,

foreign_world_spans: Vec<Span>,

/// A list of `TypeDefKind::Unknown` types which are required to be
/// resources when this package is resolved against its dependencies.
required_resource_types: Vec<(TypeId, Span)>,
}

#[derive(PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -209,6 +213,7 @@ impl<'a> Resolver<'a> {
foreign_dep_spans: mem::take(&mut self.foreign_dep_spans),
source_map: SourceMap::default(),
foreign_world_spans: mem::take(&mut self.foreign_world_spans),
required_resource_types: mem::take(&mut self.required_resource_types),
})
}

Expand Down Expand Up @@ -1203,9 +1208,13 @@ impl<'a> Resolver<'a> {
match self.types[cur].kind {
TypeDefKind::Resource => break Ok(id),
TypeDefKind::Type(Type::Id(ty)) => cur = ty,
TypeDefKind::Unknown => {
self.required_resource_types.push((cur, name.span));
break Ok(id);
}
_ => bail!(Error {
span: name.span,
msg: format!("type `{}` is not a resource", name.name),
msg: format!("type `{}` used in a handle must be a resource", name.name),
}),
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/wit-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pub struct UnresolvedPackage {
foreign_dep_spans: Vec<Span>,
source_map: SourceMap,
foreign_world_spans: Vec<Span>,
required_resource_types: Vec<(TypeId, Span)>,
}

#[derive(Debug, Copy, Clone)]
Expand Down
14 changes: 14 additions & 0 deletions crates/wit-parser/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,20 @@ impl Remap {
// what they map to.
self.process_foreign_types(unresolved, resolve)?;

for (id, span) in unresolved.required_resource_types.iter() {
let mut id = self.types[id.index()];
loop {
match resolve.types[id].kind {
TypeDefKind::Type(Type::Id(i)) => id = i,
TypeDefKind::Resource => break,
_ => bail!(Error {
span: *span,
msg: format!("type used in a handle must be a resource"),
}),
}
}
}

Ok(())
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package some:dep

interface foo {
resource r
}
7 changes: 7 additions & 0 deletions crates/wit-parser/tests/ui/cross-package-resource/foo.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package foo:bar

interface foo {
use some:dep/foo.{r}

type t = own<r>
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type `foo` is not a resource
type `foo` used in a handle must be a resource
--> tests/ui/parse-fail/bad-resource13.wit:5:16
|
5 | type t = own<foo>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type `t` is not a resource
type `t` used in a handle must be a resource
--> tests/ui/parse-fail/bad-resource14.wit:6:16
|
6 | type b = own<t>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type used in a handle must be a resource
--> tests/ui/parse-fail/bad-resource15/foo.wit:6:16
|
6 | type t = own<r>
| ^
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package some:dep

interface foo {
type r = u32
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package foo:bar

interface foo {
use some:dep/foo.{r}

type t = own<r>
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type `foo` is not a resource
type `foo` used in a handle must be a resource
--> tests/ui/parse-fail/bad-resource4.wit:5:19
|
5 | type t = borrow<foo>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type `t` is not a resource
type `t` used in a handle must be a resource
--> tests/ui/parse-fail/bad-resource5.wit:6:19
|
6 | type b = borrow<t>
Expand Down