diff --git a/testdata/disambiguate-diamond.wit.json.golden.wit b/testdata/disambiguate-diamond.wit.json.golden.wit index f0e6e3ca..2a5195d7 100644 --- a/testdata/disambiguate-diamond.wit.json.golden.wit +++ b/testdata/disambiguate-diamond.wit.json.golden.wit @@ -10,10 +10,10 @@ interface shared2 { world foo { import bar: interface { - use foo:diamond/shared2.{the-type}; + use shared2.{the-type}; }; import foo: interface { - use foo:diamond/shared1.{the-type}; + use shared1.{the-type}; }; import shared1; import shared2; diff --git a/testdata/multi-file.wit.json.golden.wit b/testdata/multi-file.wit.json.golden.wit index e57fa738..111df9c8 100644 --- a/testdata/multi-file.wit.json.golden.wit +++ b/testdata/multi-file.wit.json.golden.wit @@ -1,13 +1,13 @@ package foo:multi-file; interface bar { - use foo:multi-file/irrelevant-name.{a-name}; - use foo:multi-file/foo.{x}; - use foo:multi-file/foo.{x as x2}; - use foo:multi-file/foo.{x as x3}; - use foo:multi-file/depend-on-me.{x as x4}; - use foo:multi-file/something-else.{y}; - use foo:multi-file/something-else.{y as y2}; + use irrelevant-name.{a-name}; + use foo.{x}; + use foo.{x as x2}; + use foo.{x as x3}; + use depend-on-me.{x as x4}; + use something-else.{y}; + use something-else.{y as y2}; } interface cycle1 { @@ -15,11 +15,11 @@ interface cycle1 { } interface cycle2 { - use foo:multi-file/cycle1.{t}; + use cycle1.{t}; } interface cycle3 { - use foo:multi-file/cycle2.{t}; + use cycle2.{t}; } interface depend-on-me { @@ -27,7 +27,7 @@ interface depend-on-me { } interface depends-on-later-item { - use foo:multi-file/depend-on-me.{x}; + use depend-on-me.{x}; } interface foo { @@ -52,5 +52,5 @@ world more-depends-on-later-things { world the-world { import foo: func() -> x; import depend-on-me; - use foo:multi-file/depend-on-me.{x}; + use depend-on-me.{x}; } diff --git a/testdata/stress-export-elaborate.wit.json.golden.wit b/testdata/stress-export-elaborate.wit.json.golden.wit index 336c8a72..268d89d2 100644 --- a/testdata/stress-export-elaborate.wit.json.golden.wit +++ b/testdata/stress-export-elaborate.wit.json.golden.wit @@ -14,120 +14,120 @@ interface i1 { } interface i10 { - use foo:bar/i9.{t1}; - use foo:bar/i9.{t10}; - use foo:bar/i9.{t2}; - use foo:bar/i9.{t3}; - use foo:bar/i9.{t4}; - use foo:bar/i9.{t5}; - use foo:bar/i9.{t6}; - use foo:bar/i9.{t7}; - use foo:bar/i9.{t8}; - use foo:bar/i9.{t9}; + use i9.{t1}; + use i9.{t10}; + use i9.{t2}; + use i9.{t3}; + use i9.{t4}; + use i9.{t5}; + use i9.{t6}; + use i9.{t7}; + use i9.{t8}; + use i9.{t9}; } interface i2 { - use foo:bar/i1.{t1}; - use foo:bar/i1.{t10}; - use foo:bar/i1.{t2}; - use foo:bar/i1.{t3}; - use foo:bar/i1.{t4}; - use foo:bar/i1.{t5}; - use foo:bar/i1.{t6}; - use foo:bar/i1.{t7}; - use foo:bar/i1.{t8}; - use foo:bar/i1.{t9}; + use i1.{t1}; + use i1.{t10}; + use i1.{t2}; + use i1.{t3}; + use i1.{t4}; + use i1.{t5}; + use i1.{t6}; + use i1.{t7}; + use i1.{t8}; + use i1.{t9}; } interface i3 { - use foo:bar/i2.{t1}; - use foo:bar/i2.{t10}; - use foo:bar/i2.{t2}; - use foo:bar/i2.{t3}; - use foo:bar/i2.{t4}; - use foo:bar/i2.{t5}; - use foo:bar/i2.{t6}; - use foo:bar/i2.{t7}; - use foo:bar/i2.{t8}; - use foo:bar/i2.{t9}; + use i2.{t1}; + use i2.{t10}; + use i2.{t2}; + use i2.{t3}; + use i2.{t4}; + use i2.{t5}; + use i2.{t6}; + use i2.{t7}; + use i2.{t8}; + use i2.{t9}; } interface i4 { - use foo:bar/i3.{t1}; - use foo:bar/i3.{t10}; - use foo:bar/i3.{t2}; - use foo:bar/i3.{t3}; - use foo:bar/i3.{t4}; - use foo:bar/i3.{t5}; - use foo:bar/i3.{t6}; - use foo:bar/i3.{t7}; - use foo:bar/i3.{t8}; - use foo:bar/i3.{t9}; + use i3.{t1}; + use i3.{t10}; + use i3.{t2}; + use i3.{t3}; + use i3.{t4}; + use i3.{t5}; + use i3.{t6}; + use i3.{t7}; + use i3.{t8}; + use i3.{t9}; } interface i5 { - use foo:bar/i4.{t1}; - use foo:bar/i4.{t10}; - use foo:bar/i4.{t2}; - use foo:bar/i4.{t3}; - use foo:bar/i4.{t4}; - use foo:bar/i4.{t5}; - use foo:bar/i4.{t6}; - use foo:bar/i4.{t7}; - use foo:bar/i4.{t8}; - use foo:bar/i4.{t9}; + use i4.{t1}; + use i4.{t10}; + use i4.{t2}; + use i4.{t3}; + use i4.{t4}; + use i4.{t5}; + use i4.{t6}; + use i4.{t7}; + use i4.{t8}; + use i4.{t9}; } interface i6 { - use foo:bar/i5.{t1}; - use foo:bar/i5.{t10}; - use foo:bar/i5.{t2}; - use foo:bar/i5.{t3}; - use foo:bar/i5.{t4}; - use foo:bar/i5.{t5}; - use foo:bar/i5.{t6}; - use foo:bar/i5.{t7}; - use foo:bar/i5.{t8}; - use foo:bar/i5.{t9}; + use i5.{t1}; + use i5.{t10}; + use i5.{t2}; + use i5.{t3}; + use i5.{t4}; + use i5.{t5}; + use i5.{t6}; + use i5.{t7}; + use i5.{t8}; + use i5.{t9}; } interface i7 { - use foo:bar/i6.{t1}; - use foo:bar/i6.{t10}; - use foo:bar/i6.{t2}; - use foo:bar/i6.{t3}; - use foo:bar/i6.{t4}; - use foo:bar/i6.{t5}; - use foo:bar/i6.{t6}; - use foo:bar/i6.{t7}; - use foo:bar/i6.{t8}; - use foo:bar/i6.{t9}; + use i6.{t1}; + use i6.{t10}; + use i6.{t2}; + use i6.{t3}; + use i6.{t4}; + use i6.{t5}; + use i6.{t6}; + use i6.{t7}; + use i6.{t8}; + use i6.{t9}; } interface i8 { - use foo:bar/i7.{t1}; - use foo:bar/i7.{t10}; - use foo:bar/i7.{t2}; - use foo:bar/i7.{t3}; - use foo:bar/i7.{t4}; - use foo:bar/i7.{t5}; - use foo:bar/i7.{t6}; - use foo:bar/i7.{t7}; - use foo:bar/i7.{t8}; - use foo:bar/i7.{t9}; + use i7.{t1}; + use i7.{t10}; + use i7.{t2}; + use i7.{t3}; + use i7.{t4}; + use i7.{t5}; + use i7.{t6}; + use i7.{t7}; + use i7.{t8}; + use i7.{t9}; } interface i9 { - use foo:bar/i8.{t1}; - use foo:bar/i8.{t10}; - use foo:bar/i8.{t2}; - use foo:bar/i8.{t3}; - use foo:bar/i8.{t4}; - use foo:bar/i8.{t5}; - use foo:bar/i8.{t6}; - use foo:bar/i8.{t7}; - use foo:bar/i8.{t8}; - use foo:bar/i8.{t9}; + use i8.{t1}; + use i8.{t10}; + use i8.{t2}; + use i8.{t3}; + use i8.{t4}; + use i8.{t5}; + use i8.{t6}; + use i8.{t7}; + use i8.{t8}; + use i8.{t9}; } world foo { diff --git a/testdata/use-chain.wit.json.golden.wit b/testdata/use-chain.wit.json.golden.wit index 065724fc..290306f5 100644 --- a/testdata/use-chain.wit.json.golden.wit +++ b/testdata/use-chain.wit.json.golden.wit @@ -5,5 +5,5 @@ interface foo { } interface name { - use foo:name/foo.{foo}; + use foo.{foo}; } diff --git a/testdata/use.wit.json.golden.wit b/testdata/use.wit.json.golden.wit index e2bd064c..a19e5837 100644 --- a/testdata/use.wit.json.golden.wit +++ b/testdata/use.wit.json.golden.wit @@ -5,25 +5,25 @@ interface bar { } interface baz { - use foo:foo/bar.{the-type as test}; - use foo:foo/foo.{the-type}; + use bar.{the-type as test}; + use foo.{the-type}; } interface empty {} interface foo { - use foo:foo/bar.{the-type}; + use bar.{the-type}; } interface trailing-comma { record the-foo { a: the-type }; - use foo:foo/foo.{the-type}; + use foo.{the-type}; } interface use-from-empty {} interface use-multiple { - use foo:foo/baz.{test}; - use foo:foo/baz.{the-type}; + use baz.{test}; + use baz.{the-type}; some-function: func(x: the-type) -> test; } diff --git a/testdata/wasi/clocks.wit.json.golden.wit b/testdata/wasi/clocks.wit.json.golden.wit index 1cc35c64..9ba7ce92 100644 --- a/testdata/wasi/clocks.wit.json.golden.wit +++ b/testdata/wasi/clocks.wit.json.golden.wit @@ -22,7 +22,7 @@ interface monotonic-clock { } interface timezone { - use wasi:clocks/wall-clock.{datetime}; + use wall-clock.{datetime}; record timezone-display { utc-offset: s32, name: string, diff --git a/testdata/world-diamond.wit.json.golden.wit b/testdata/world-diamond.wit.json.golden.wit index 7a33d1f7..79a05cb7 100644 --- a/testdata/world-diamond.wit.json.golden.wit +++ b/testdata/world-diamond.wit.json.golden.wit @@ -1,12 +1,12 @@ package foo:foo; interface i1 { - use foo:foo/shared-items.{foo}; + use shared-items.{foo}; a: func() -> foo; } interface i2 { - use foo:foo/shared-items.{foo}; + use shared-items.{foo}; a: func() -> foo; } diff --git a/testdata/world-iface-no-collide.wit.json.golden.wit b/testdata/world-iface-no-collide.wit.json.golden.wit index 46ef7e86..39dc71c0 100644 --- a/testdata/world-iface-no-collide.wit.json.golden.wit +++ b/testdata/world-iface-no-collide.wit.json.golden.wit @@ -7,5 +7,5 @@ interface foo { world bar { import foo: func(); import foo; - use foo:foo/foo.{t}; + use foo.{t}; } diff --git a/testdata/world-implicit-import1.wit.json.golden.wit b/testdata/world-implicit-import1.wit.json.golden.wit index bdefc976..22cc601e 100644 --- a/testdata/world-implicit-import1.wit.json.golden.wit +++ b/testdata/world-implicit-import1.wit.json.golden.wit @@ -6,7 +6,7 @@ interface foo { world the-world { import bar: interface { - use foo:foo/foo.{a}; + use foo.{a}; }; import foo: interface {}; import foo; diff --git a/testdata/world-implicit-import2.wit.json.golden.wit b/testdata/world-implicit-import2.wit.json.golden.wit index fb990fd9..0caed22b 100644 --- a/testdata/world-implicit-import2.wit.json.golden.wit +++ b/testdata/world-implicit-import2.wit.json.golden.wit @@ -6,6 +6,6 @@ interface foo { world w { import foo: func() -> g; - use foo:foo/foo.{g}; + use foo.{g}; import foo; } diff --git a/testdata/world-implicit-import3.wit.json.golden.wit b/testdata/world-implicit-import3.wit.json.golden.wit index ce5ed595..246f2150 100644 --- a/testdata/world-implicit-import3.wit.json.golden.wit +++ b/testdata/world-implicit-import3.wit.json.golden.wit @@ -5,7 +5,7 @@ interface foo { } world w { - use foo:foo/foo.{g}; + use foo.{g}; import foo; export foo: func() -> g; } diff --git a/testdata/world-same-fields4.wit.json.golden.wit b/testdata/world-same-fields4.wit.json.golden.wit index a10f12d3..6dfe6f62 100644 --- a/testdata/world-same-fields4.wit.json.golden.wit +++ b/testdata/world-same-fields4.wit.json.golden.wit @@ -8,6 +8,6 @@ world foo { import shared-items; import shared-items: interface {}; export a-name: interface { - use foo:foo/shared-items.{a}; + use shared-items.{a}; }; } diff --git a/testdata/world-top-level-resources.wit.json.golden.wit b/testdata/world-top-level-resources.wit.json.golden.wit index e858af2a..b42bc59d 100644 --- a/testdata/world-top-level-resources.wit.json.golden.wit +++ b/testdata/world-top-level-resources.wit.json.golden.wit @@ -1,8 +1,8 @@ package foo:foo; interface handler { - use foo:foo/types.{request}; - use foo:foo/types.{response}; + use types.{request}; + use types.{response}; handle: func(some: borrow) -> borrow; handle-owned: func(some: own) -> own; } diff --git a/testdata/worlds-with-types.wit.json.golden.wit b/testdata/worlds-with-types.wit.json.golden.wit index 8088f3f1..7ead0a97 100644 --- a/testdata/worlds-with-types.wit.json.golden.wit +++ b/testdata/worlds-with-types.wit.json.golden.wit @@ -6,7 +6,7 @@ interface disambiguate { world bar { import disambiguate; - use foo:foo/disambiguate.{t}; + use disambiguate.{t}; export foo: func() -> t; } diff --git a/wit/ast.go b/wit/ast.go index 66aac183..cbc27dae 100644 --- a/wit/ast.go +++ b/wit/ast.go @@ -84,6 +84,17 @@ type TypeDef struct { _type } +// Package returns the [Package] that t is associated with, if any. +func (t *TypeDef) Package() *Package { + switch owner := t.Owner.(type) { + case *Interface: + return owner.Package + case *World: + return owner.Package + } + return nil +} + // Size returns the byte size for values of type t. func (t *TypeDef) Size() uintptr { return t.Kind.Size() @@ -525,7 +536,10 @@ func (*Stream) Align() uintptr { return 0 } // TypeOwner is the interface implemented by any type that can own a TypeDef, // currently [World] and [Interface]. -type TypeOwner interface{ isTypeOwner() } +type TypeOwner interface { + RelativeName(*Package) string + isTypeOwner() +} type _typeOwner struct{ _node } diff --git a/wit/wit.go b/wit/wit.go index 321ad484..c37ef4ee 100644 --- a/wit/wit.go +++ b/wit/wit.go @@ -87,6 +87,13 @@ func (w *World) itemWIT(motion, name string, v WorldItem) string { panic("BUG: unknown WorldItem") } +func (w *World) RelativeName(p *Package) string { + if w.Package == p { + return w.Name + } + return w.Package.Name.String() + "/" + w.Name +} + // WIT returns the WIT representation of i. func (i *Interface) WIT(ctx Node, name string) string { if i.Name != nil && name == "" { @@ -103,15 +110,12 @@ func (i *Interface) WIT(ctx Node, name string) string { b.WriteString(name) b.WriteRune(' ') case *World: - if i.Package != ctx.Package { - // Import by name from another package - // TODO: check i.Name != nil - return fmt.Sprintf("%s/%s", i.Package.Name.String(), *i.Name) - } else if i.Name != nil { - // Import by name within same package - return *i.Name + rname := i.RelativeName(ctx.Package) + if rname != "" { + return rname } - // Otherwise, this is an inline interface decl + + // Otherwise, this is an inline interface decl. b.WriteString(name) b.WriteString(": interface ") } @@ -141,6 +145,16 @@ func (i *Interface) WIT(ctx Node, name string) string { return b.String() } +func (i *Interface) RelativeName(p *Package) string { + if i.Name == nil { + return "" + } + if i.Package == p { + return *i.Name + } + return i.Package.Name.String() + "/" + *i.Name +} + // WIT returns the WIT representation of [TypeDef] t. func (t *TypeDef) WIT(ctx Node, name string) string { if t.Name != nil && name == "" { @@ -153,23 +167,11 @@ func (t *TypeDef) WIT(ctx Node, name string) string { if t.Owner == ctx.Owner && t.Name != nil { return "type " + name + " = " + *t.Name } - - // TODO: add a TypeOwnerName method to TypeDef. - var ownerName string - var pkg *Package - switch owner := t.Owner.(type) { - case *Interface: - ownerName = *owner.Name - pkg = owner.Package - case *World: - ownerName = owner.Name - pkg = owner.Package - } - // TODO: use less-qualified name (without package) if this is an import within the same package. + ownerName := t.Owner.RelativeName(ctx.Package()) if t.Name != nil && *t.Name != name { - return fmt.Sprintf("use %s/%s.{%s as %s}", pkg.Name.String(), ownerName, *t.Name, name) + return fmt.Sprintf("use %s.{%s as %s}", ownerName, *t.Name, name) } - return fmt.Sprintf("use %s/%s.{%s}", pkg.Name.String(), ownerName, name) + return fmt.Sprintf("use %s.{%s}", ownerName, name) case *World, *Interface: switch t.Kind.(type) {