Skip to content

Commit

Permalink
Allow aliases to core wasm types outside of depth 1 (#1539)
Browse files Browse the repository at this point in the history
This accompanies WebAssembly/component-model#350 and relaxes the
previous constraint implemented in #640 to reflect the spec-at-the-time,
which has since been relaxed.
  • Loading branch information
alexcrichton authored May 20, 2024
1 parent 6797729 commit 64c0b26
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 39 deletions.
7 changes: 0 additions & 7 deletions crates/wasmparser/src/validator/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1503,13 +1503,6 @@ impl ComponentState {
state.add_export(name, ty, features, offset, true, types)?;
}
crate::ModuleTypeDeclaration::OuterAlias { kind, count, index } => {
if count > 1 {
return Err(BinaryReaderError::new(
"outer type aliases in module type declarations are limited to a \
maximum count of 1",
offset,
));
}
match kind {
crate::OuterAliasKind::Type => {
let ty = if count == 0 {
Expand Down
20 changes: 6 additions & 14 deletions crates/wast/src/component/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl<'a> Resolver<'a> {
ComponentField::CoreType(t) => self.core_ty(t),
ComponentField::Component(c) => self.component(c),
ComponentField::Instance(i) => self.instance(i),
ComponentField::Alias(a) => self.alias(a, false),
ComponentField::Alias(a) => self.alias(a),
ComponentField::Type(t) => self.ty(t),
ComponentField::CanonicalFunc(f) => self.canonical_func(f),
ComponentField::CoreFunc(_) => unreachable!("should be expanded already"),
Expand Down Expand Up @@ -288,7 +288,6 @@ impl<'a> Resolver<'a> {
index: &mut Index<'a>,
kind: T,
span: Span,
enclosing_only: bool,
) -> Result<(), Error> {
// Short-circuit when both indices are already resolved as this
// helps to write tests for invalid modules where wasmparser should
Expand Down Expand Up @@ -328,13 +327,6 @@ impl<'a> Resolver<'a> {
));
}

if enclosing_only && depth > 1 {
return Err(Error::new(
span,
"only the local or enclosing scope can be aliased".to_string(),
));
}

*outer = Index::Num(depth, span);

// Resolve `index` within the computed scope depth.
Expand All @@ -344,7 +336,7 @@ impl<'a> Resolver<'a> {
Ok(())
}

fn alias(&mut self, alias: &mut Alias<'a>, enclosing_only: bool) -> Result<(), Error> {
fn alias(&mut self, alias: &mut Alias<'a>) -> Result<(), Error> {
match &mut alias.target {
AliasTarget::Export {
instance,
Expand All @@ -357,7 +349,7 @@ impl<'a> Resolver<'a> {
kind: _,
} => self.resolve_ns(instance, Ns::CoreInstance),
AliasTarget::Outer { outer, index, kind } => {
self.outer_alias(outer, index, *kind, alias.span, enclosing_only)
self.outer_alias(outer, index, *kind, alias.span)
}
}
}
Expand Down Expand Up @@ -550,7 +542,7 @@ impl<'a> Resolver<'a> {
self.resolve_prepending_aliases(
&mut c.decls,
|resolver, decl| match decl {
ComponentTypeDecl::Alias(alias) => resolver.alias(alias, false),
ComponentTypeDecl::Alias(alias) => resolver.alias(alias),
ComponentTypeDecl::CoreType(ty) => resolver.core_ty(ty),
ComponentTypeDecl::Type(ty) => resolver.ty(ty),
ComponentTypeDecl::Import(import) => resolver.item_sig(&mut import.item),
Expand Down Expand Up @@ -583,7 +575,7 @@ impl<'a> Resolver<'a> {
self.resolve_prepending_aliases(
&mut c.decls,
|resolver, decl| match decl {
InstanceTypeDecl::Alias(alias) => resolver.alias(alias, false),
InstanceTypeDecl::Alias(alias) => resolver.alias(alias),
InstanceTypeDecl::CoreType(ty) => resolver.core_ty(ty),
InstanceTypeDecl::Type(ty) => resolver.ty(ty),
InstanceTypeDecl::Export(export) => resolver.item_sig(&mut export.item),
Expand Down Expand Up @@ -740,7 +732,7 @@ impl<'a> Resolver<'a> {
return self.resolve_prepending_aliases(
&mut ty.decls,
|resolver, decl| match decl {
ModuleTypeDecl::Alias(alias) => resolver.alias(alias, true),
ModuleTypeDecl::Alias(alias) => resolver.alias(alias),
ModuleTypeDecl::Type(_) => Ok(()),
ModuleTypeDecl::Import(import) => resolve_item_sig(resolver, &mut import.item),
ModuleTypeDecl::Export(_, item) => resolve_item_sig(resolver, item),
Expand Down
40 changes: 22 additions & 18 deletions tests/local/component-model/types.wast
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,12 @@
)
"type index out of bounds")

(assert_invalid
(component $c
(type $f (func))
(core type $t (module
(alias outer 100 0 (type))
))
)
"outer type aliases in module type declarations are limited to a maximum count of 1")
(component $c
(core type $f (func))
(core type $t (module
(alias outer $c $f (type))
))
)

(assert_invalid
(component $c
Expand Down Expand Up @@ -262,17 +260,14 @@
)
)

(assert_invalid
(component $C
(core type $t (func))
(component $C2
(core type (module
(alias outer $C $t (type $a))
(import "" "" (func (type $a)))
))
)
(component $C
(core type $t (func))
(component $C2
(core type (module
(alias outer $C $t (type $a))
(import "" "" (func (type $a)))
))
)
"only the local or enclosing scope can be aliased"
)

(component
Expand Down Expand Up @@ -319,3 +314,12 @@
(type (tuple))
)
"tuple type must have at least one type")

(component $c
(core type $f (func))
(component $c2
(core type $t (module
(alias outer $c $f (type))
))
)
)
8 changes: 8 additions & 0 deletions tests/snapshots/local/component-model/types.wast/17.print
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(component $c
(core type $f (;0;) (func))
(core type $t (;1;)
(module
(alias outer $c $f (type (;0;)))
)
)
)
11 changes: 11 additions & 0 deletions tests/snapshots/local/component-model/types.wast/32.print
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(component $C
(core type $t (;0;) (func))
(component $C2 (;0;)
(core type (;0;)
(module
(alias outer $C $t (type (;0;)))
(import "" "" (func (type 0)))
)
)
)
)
10 changes: 10 additions & 0 deletions tests/snapshots/local/component-model/types.wast/40.print
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(component $c
(core type $f (;0;) (func))
(component $c2 (;0;)
(core type $t (;0;)
(module
(alias outer $c $f (type (;0;)))
)
)
)
)

0 comments on commit 64c0b26

Please sign in to comment.