Skip to content

Commit 4934326

Browse files
authored
Merge pull request #54 from peterhuene/subtype-check
Improve subtype check.
2 parents 0b44c06 + 9c9d964 commit 4934326

21 files changed

Lines changed: 418 additions & 158 deletions

crates/wac-parser/src/ast/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<'a> Parse<'a> for Expr<'a> {
3838

3939
let start = primary.span();
4040
let len = postfix.last().map_or(start.len(), |p| {
41-
start.offset() + p.span().offset() + p.span().len()
41+
p.span().offset() + p.span().len() - start.offset()
4242
});
4343

4444
Ok(Self {

crates/wac-parser/src/resolution/ast.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,8 +1535,8 @@ impl<'a> AstResolver<'a> {
15351535
hash_map::Entry::Occupied(e) => Ok(*e.get()),
15361536
hash_map::Entry::Vacant(e) => {
15371537
log::debug!("resolving package `{name}`");
1538-
let bytes = match self.packages.remove(&PackageKey { name, version }) {
1539-
Some(bytes) => bytes,
1538+
let bytes = match self.packages.get(&PackageKey { name, version }) {
1539+
Some(bytes) => bytes.clone(),
15401540
None => {
15411541
return Err(Error::UnknownPackage {
15421542
name: name.to_string(),
@@ -1790,7 +1790,7 @@ impl<'a> AstResolver<'a> {
17901790
);
17911791

17921792
SubtypeChecker::new(&self.definitions, &state.packages)
1793-
.is_subtype(*expected, state.current.items[*item].kind())
1793+
.is_subtype(state.current.items[*item].kind(), *expected)
17941794
.map_err(|e| Error::MismatchedInstantiationArg {
17951795
name: name.clone(),
17961796
span: *span,
@@ -1967,10 +1967,10 @@ impl<'a> AstResolver<'a> {
19671967

19681968
match (
19691969
checker
1970-
.is_subtype(*target_kind, *source_kind)
1970+
.is_subtype(*source_kind, *target_kind)
19711971
.with_context(|| format!("mismatched type for export `{name}`")),
19721972
checker
1973-
.is_subtype(*source_kind, *target_kind)
1973+
.is_subtype(*target_kind, *source_kind)
19741974
.with_context(|| format!("mismatched type for export `{name}`")),
19751975
) {
19761976
(Ok(_), Ok(_)) => {
@@ -2275,6 +2275,7 @@ impl<'a> AstResolver<'a> {
22752275
let mut checker = SubtypeChecker::new(&self.definitions, &state.packages);
22762276

22772277
// The output is allowed to import a subset of the world's imports
2278+
checker.invert();
22782279
for (name, import) in &state.imports {
22792280
let expected = world
22802281
.imports
@@ -2299,6 +2300,8 @@ impl<'a> AstResolver<'a> {
22992300
})?;
23002301
}
23012302

2303+
checker.revert();
2304+
23022305
// The output must export every export in the world
23032306
for (name, expected) in &world.exports {
23042307
let export = state
@@ -2313,8 +2316,8 @@ impl<'a> AstResolver<'a> {
23132316

23142317
checker
23152318
.is_subtype(
2316-
expected.promote(),
23172319
state.root_scope().items[export.item].kind(),
2320+
expected.promote(),
23182321
)
23192322
.map_err(|e| Error::TargetMismatch {
23202323
kind: ExternKind::Export,

0 commit comments

Comments
 (0)