Skip to content

Commit f4377b9

Browse files
committed
Remove implicit import behavior of 'use'
1 parent 76a3927 commit f4377b9

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

design/mvp/WIT.md

+32-32
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,9 @@ interface types {
142142

143143
More information about [`use`][use] and [types] are described below, but this
144144
is an example of a collection of items within an `interface`. All items defined
145-
in an `interface`, including [`use`][use] items, are considered as exports from
146-
the interface. This means that types can further be used from the interface by
147-
other interfaces. An interface has a single namespace which means that none of
148-
the defined names can collide.
145+
in an `interface` are considered as exports from the interface. This means that
146+
types can further be used from the interface by other interfaces. An interface
147+
has a single namespace which means that none of the defined names can collide.
149148

150149
A WIT package can contain any number of interfaces listed at the top-level and
151150
in any order. The WIT validator will ensure that all references between
@@ -589,23 +588,22 @@ use wasi:http/[email protected] as http-types2;
589588
### Transitive imports and worlds
590589

591590
A `use` statement is not implemented by copying type information around but
592-
instead retains that it's a reference to a type defined elsewhere. This
593-
representation is plumbed all the way through to the final component, meaning
594-
that `use`d types have an impact on the structure of the final generated
595-
component.
591+
instead retains that it's a reference to a type that must have already been
592+
imported or exported.
596593

597594
For example this document:
598595

599596
```wit
600597
package local:demo;
601598
602599
interface shared {
603-
record metadata {
600+
resource metadata {
604601
// ...
605602
}
606603
}
607604
608605
world my-world {
606+
import shared; // required by 'use'
609607
import host: interface {
610608
use shared.{metadata};
611609
@@ -617,52 +615,53 @@ world my-world {
617615
would generate this component:
618616

619617
```wasm
620-
(component
618+
(component $C
621619
(import "local:demo/shared" (instance $shared
622-
(type $metadata (record (; ... ;)))
623-
(export "metadata" (type (eq $metadata)))
620+
(export "metadata" (type (sub resource)))
624621
))
625622
(alias export $shared "metadata" (type $metadata_from_shared))
626623
(import "host" (instance $host
627-
(export $metadata_in_host "metadata" (type (eq $metadata_from_shared)))
628-
(export "get" (func (result $metadata_in_host)))
624+
(alias outer $C $metadata_from_shared (type $metadata))
625+
(export "get" (func (result $metadata)))
629626
))
630627
)
631628
```
632629

633-
Here it can be seen that despite the `world` only listing `host` as an import
634-
the component additionally imports a `local:demo/shared` interface. This is due
635-
to the fact that the `use shared.{ ... }` implicitly requires that `shared` is
636-
imported into the component as well.
630+
Thus, the `metadata` resource type defined in the `shared` interface is
631+
directly shared (via Component Model `alias` definitions) with the `host`
632+
interface. If the `import shared;` line were deleted, this WIT document would
633+
fail to validate since there is no `shared.metadata` type to `use`.
637634

638635
Note that the name `"local:demo/shared"` here is derived from the name of the
639636
`interface` plus the package name `local:demo`.
640637

641-
For `export`ed interfaces, any transitively `use`d interface is assumed to be an
642-
import unless it's explicitly listed as an export. For example, here `w1` is
643-
equivalent to `w2`:
638+
For `export`ed interfaces, any transitively `use`d interfaces can be satisfied
639+
by *either* imports or exports.
640+
641+
For example, the following world `w` uses both the imported `r` defined by `a`
642+
and the exported `s` defined by `b`:
644643
```wit
645644
interface a {
646645
resource r;
647646
}
648647
interface b {
649-
use a.{r};
650-
foo: func() -> r;
651-
}
652-
653-
world w1 {
654-
export b;
648+
resource s;
655649
}
656-
world w2 {
650+
world w {
657651
import a;
658652
export b;
653+
export converter: interface {
654+
use a.{r};
655+
use b.{s};
656+
convert: func(in: r) -> s;
657+
}
659658
}
660659
```
661660

662-
> **Note**: It's planned in the future to have "power user syntax" to configure
663-
> this on a more fine-grained basis for exports, for example being able to
664-
> configure that a `use`'d interface is a particular import or a particular
665-
> export.
661+
> **Note**: It's planned in the future to add additional syntax to support and
662+
> disambiguate the case where the same interface is both imported and exported.
663+
> With this addition, `w` could contain `import b` and `converter` could `use`
664+
> *both* the imported `s` *and* the exported `s`.
666665
667666
## WIT Functions
668667
[functions]: #wit-functions
@@ -1898,6 +1897,7 @@ package wasi:http;
18981897
18991898
world proxy {
19001899
import wasi:logging/logger;
1900+
import types;
19011901
import handler;
19021902
export handler;
19031903
}

0 commit comments

Comments
 (0)