Skip to content

Disallow [method]... naming unless the resource was defined in the same context #765

Open

Description

@alexcrichton

Currently wasmparser accepts this component:

  (component
    (import "a" (instance $a
      (export "r" (type (sub resource)))
    ))
    (alias export $a "r" (type $r))
    (import "b" (instance
      (export "r" (type $x (eq $r)))
      (export "[method]r.f" (func (param "self" (borrow $x))))
    ))
  )

But arguably this is a bad idea. This isn't representable in WIT at all and there aren't any actual users of this AFAIK, so I think it would be best to disallow this. In essence only the resource definition should be able to attach methods, because otherwise you could have different instances define the same method and it'd be ambiguous of what to call.

More problematically, however, wasmparser also accepts this:

(component
  (type (component
    (import "a" (type $a (sub resource)))
    (export "r" (type $x (eq $a)))
    (export "[method]r.f" (func (param "self" (borrow $x))))
  ))
)

and this is how wit-component works for any component which exports a resource in an interface. This means that this pattern is widely used meaning that the rule of "must be on the definition" would break a lot of components today.

Ideally wit-component would generate a component that looks like htis:

(component
  (type (component
    (import "a" (type $a (sub resource)))
    (export "r" (type $x (eq $a)) (type (sub resource))) ;; note the type ascription here
    (export "[method]r.f" (func (param "self" (borrow $x))))
  ))
)

but wasmparser does not yet implement that today (bytecodealliance/wasm-tools#2731)

I'm also a bit stuck on what "must be next to the definition" means. For example a resource could be declared, then exported, and then methods must be on the type introduced by the export. In that sense some aliases work, but some don't. Hence, the issue here!

Activity

  1. lukewagner commented on Nov 13, 2026

    @lukewagner
    Member

    In general, agreed that this second (re-export) case you gave is in the same category as the first case and would ideally be rejected too. Incidentally, the new JS API throws in this case specifically to avoid this sort of weird "adding methods to a separately-defined class" case which would otherwise be implied for the JS bindings (and it makes sense that other languages would hit the same weirdness).

    For the 3rd component you listed: how do you get WIT to generate that today? I tried a few ways and couldn't (for the same reasons we were thinking it wasn't possible to hit the first case from WIT). And for the 4th component: I'm guessing you didn't mean to have the imports/exports wrapped in a (type (component ... )) so that the export is a non-type-level definition (which is the only place ascription applies)? Assuming that, same question: how would you generate one of these components from WIT today?

    I think the "must be next to the definition" condition would apply to every component type (written explicitly as a type definition or inferred from a component definition being validated, just like the "external visibility" rules) and say that the r of an imported/exported [method]r.foo-named function must be preceded in the same immediately-containing instance/component scope as a sub-bound type import/export (respectively!) named r.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions