Skip to content

Commit

Permalink
Better error message for wrong import implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Levick <ryan.levick@fermyon.com>
  • Loading branch information
rylev committed Dec 6, 2023
1 parent 3386538 commit 0cf8b36
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
5 changes: 3 additions & 2 deletions crates/wasmtime/src/component/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,9 @@ impl<T> Linker<T> {
.strings
.lookup(name)
.and_then(|name| self.map.get(&name));
cx.definition(ty, import)
.with_context(|| format!("import `{name}` has the wrong type"))?;
cx.definition(ty, import).with_context(|| {
format!("component imports `{name}`, but a correct implementation of `{name}` could not be found")
})?;
}

// Now that all imports are known to be defined and satisfied by this
Expand Down
28 changes: 15 additions & 13 deletions crates/wasmtime/src/component/matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,27 @@ impl TypeChecker<'_> {
match *expected {
TypeDef::Module(t) => match actual {
Some(Definition::Module(actual)) => self.module(&self.types[t], actual),
_ => bail!("expected module found {}", desc(actual)),
Some(actual) => bail!("expected module found {}", actual.desc()),
None => bail!("component implementation is missing"),
},
TypeDef::ComponentInstance(t) => match actual {
Some(Definition::Instance(actual)) => self.instance(&self.types[t], Some(actual)),
None => self.instance(&self.types[t], None),
_ => bail!("expected instance found {}", desc(actual)),
Some(actual) => bail!("expected instance found {}", actual.desc()),
},
TypeDef::ComponentFunc(t) => match actual {
Some(Definition::Func(actual)) => self.func(t, actual),
_ => bail!("expected func found {}", desc(actual)),
Some(actual) => bail!("expected function found {}", actual.desc()),
None => bail!("function implementation is missing"),
},
TypeDef::Component(_) => match actual {
Some(actual) => bail!("expected component found {}", actual.desc()),
None => bail!("component implementation is missing"),
},
TypeDef::Interface(_) => match actual {
Some(actual) => bail!("expected interface found {}", actual.desc()),
None => bail!("interface implementation is missing"),
},
TypeDef::Component(_) => bail!("expected component found {}", desc(actual)),
TypeDef::Interface(_) => bail!("expected type found {}", desc(actual)),

TypeDef::Resource(i) => {
let i = self.types[i].ty;
Expand All @@ -63,7 +71,8 @@ impl TypeChecker<'_> {
// Wasmtime API.
None if self.imported_resources.get(i).is_some() => return Ok(()),

_ => bail!("expected resource found {}", desc(actual)),
Some(actual) => bail!("expected resource found {}", actual.desc()),
None => bail!("resource implementation is missing"),
};

match self.imported_resources.get(i) {
Expand Down Expand Up @@ -168,13 +177,6 @@ impl TypeChecker<'_> {
}
}

fn desc(def: Option<&Definition>) -> &'static str {
match def {
Some(def) => def.desc(),
None => "nothing",
}
}

impl Definition {
fn desc(&self) -> &'static str {
match self {
Expand Down
6 changes: 3 additions & 3 deletions tests/misc_testsuite/component-model/linking.wast
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(component
(import "undefined-name" (core module))
)
"expected module found nothing")
"could not be found")
(component $i)
(component
(import "i" (instance))
Expand All @@ -12,7 +12,7 @@
"expected module found instance")
(assert_unlinkable
(component (import "i" (func)))
"expected func found instance")
"expected function found instance")
(assert_unlinkable
(component (import "i" (instance (export "x" (func)))))
"expected func found nothing")
"could not be found")
2 changes: 1 addition & 1 deletion tests/misc_testsuite/component-model/resources.wast
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
(export "missing" (type (sub resource)))
))
)
"expected resource found nothing")
"could not be found")
(assert_unlinkable
(component
(import "host" (instance
Expand Down

0 comments on commit 0cf8b36

Please sign in to comment.