wit-bindgen icon indicating copy to clipboard operation
wit-bindgen copied to clipboard

Using an interface for two imports causes an error

Open surma opened this issue 2 years ago • 3 comments

interface iface {
	f: func()
}

default world w {
	import p1: self.iface
	import p2: self.iface
}

This produces the following error:

$ wit-bindgen markdown example.wit
Error: interface `p2` imported more than once
     --> example.wit:7:9
      |
    7 |         import p2: self.iface
      |

Is it actually not allowed to have two imports with the same interface? That seems surprising to me.

surma avatar Mar 06 '23 10:03 surma

This is currently intended, yes, although perhaps not with the best error message. Additionally the specification for WIT.md in the component-model repository should probably have more words about this. @lukewagner would be able to explain in more detail where this restriction comes from.

alexcrichton avatar Mar 06 '23 15:03 alexcrichton

Interesting! Yeah I’d love to hear more context. It’s especially confusing because it works just fine™️ if you spell out the interfaces:

default world w {
	import p1: interface {
		f: func()
	}
	import p2: interface {
		f: func()
	}
}

surma avatar Mar 06 '23 15:03 surma

The basic idea (described more here) is that each import/export name has two parts, a developer-facing kebab-case string (meant to be converted into source-code bindings) and an optional (id <URL>) field which, when present, is meant to uniquely identify the (standards- or vendor-defined) interface that is being requested/implemented. The id is supposed to uniquely determine what is being requested, so that two imports of the same id can always be de-duplicated. But if a component can import the same id twice, that would imply that extra context is being used to identify the interface and thus duplicate imports/exports of the same id is disallowed. In your first example, self.iface turned into an id, hence the duplicate error, while, in the second example, because the interfaces are anonymous/inline, there is no id.

A better error message could help of course, but I do wonder if we should make it more explicit when an import/export includes an id or not.

lukewagner avatar Mar 06 '23 22:03 lukewagner