Skip to content

Commit 55c99f5

Browse files
committed
Reject ambiguous cases of 'use' in WIT
1 parent ee4822a commit 55c99f5

File tree

1 file changed

+52
-5
lines changed

1 file changed

+52
-5
lines changed

design/mvp/WIT.md

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,37 @@ interface my-interface {
512512
}
513513
```
514514

515+
Worlds may import and export the same interface which can be useful for
516+
creating components that can be linked together in a call chain. For example:
517+
```wit
518+
interface processor {
519+
resource datum { ... }
520+
process: func(d: datum);
521+
}
522+
523+
world layer {
524+
import processor;
525+
export processor;
526+
}
527+
```
528+
Components targeting this `layer` world can be linked together,
529+
export-to-import.
530+
531+
If a `world` contains a `use` of an interface that is both imported and
532+
exported, this is currently rejected as ambiguous. For example, this world
533+
is ambiguous:
534+
```wit
535+
world invalid {
536+
import processor;
537+
export processor;
538+
use processor.{datum};
539+
frob: func(d: datum) -> datum;
540+
}
541+
```
542+
> **Note**: It's planned in the future to allow `use` to explicitly specify
543+
> whether to use the imported or exported version of the named interface.
544+
545+
515546
#### Top-level `use`
516547

517548
If a package being referred to has a version number, then using the above syntax
@@ -638,9 +669,13 @@ imported into the component as well.
638669
Note that the name `"local:demo/shared"` here is derived from the name of the
639670
`interface` plus the package name `local:demo`.
640671

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`:
672+
For `export`ed interfaces, any transitively `use`d interface must have already
673+
been either imported or exported. If not, the `world` is invalid because it's
674+
ambiguous whether the `use`d interface was meant to be imported or exported. If
675+
a `use`d interface is *both* imported and exported, the export takes
676+
precedence, "shadowing" the import.
677+
678+
For example, the following worlds are valid and resolved as commented:
644679
```wit
645680
interface a {
646681
resource r;
@@ -651,11 +686,23 @@ interface b {
651686
}
652687
653688
world w1 {
654-
export b;
689+
import a;
690+
export b; // b ~~> imported a.r
655691
}
656692
world w2 {
693+
export a;
694+
export b; // b ~~> exported a.r
695+
}
696+
world w3 {
657697
import a;
658-
export b;
698+
export a;
699+
export b; // b ~~> exported a.r
700+
}
701+
```
702+
whereas the following world would be invalid:
703+
```wit
704+
world invalid {
705+
export b; // error: unresolved 'use' of 'a' in 'b'
659706
}
660707
```
661708

0 commit comments

Comments
 (0)