You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
text format: local index isn't enforced on type parameter IDs #456
According to the MVP spec, a type can include symbolic IDs for parameters. It elaborates that a function's local index is its parameters followed by its locals. Finally, it states "An identifier context is well-formed if no index space contains duplicate identifiers."
Given this, I would expect uniqueness to be enforced both on parameter IDs within a type (or type use), locals within a function, and across parameters and locals in a function.
Right now, only one case seems to be validated: when a local redefines the same ID:
The root cause may be around how local names inherit from the type func. For example, with the latest release, this fails to compile as it doesn't see the names on the type func.
Thanks for the report! Personally I'm under the impression that the identifiers used in a (type (func ...)) declaration are not used for local names in each function that the type is then referenced within. I believe that only identifiers specified inline with the function definition itself are used, so I think that this is working as expected?
That would mean that in 1/2 from your examples the $x parameter is ignored so the only $x is the local itself, and for the third example that doesn't compile it's because $x isn't in scope. I don't know whether your first example is supposed to be valid or not. According to this there is no validation performed on the identifiers of a type declaration, but that may simply be a mistake or an oversight.
Hmm yeah it would be odd to have identifiers on a param and then not use them for anything. For example, being able to reference a type by symbolic ID hints someone is being able to separate and re-use the types. You can consider a lambda type with multiple implementations.
Ex. of interface/impl where param name defaulting to what's on the type could matter
In such an example, I do think the param names would be helpful not just in that the impl can use those for symbolic lookup, but also debug (name section)
on this being an oversight or a mistake..
I would be surprised if someone added param names to types in the spec, yet intended implementations to ignore them, but yeah an oversight is indeed another answer. I guess the mistake was allowing param names on the type as it was intentional to not allow interface/impl divide or other use cases like this. Mistakes, happen, but this seems a pretty significant portability issue. Personally, I wouldn't intentionally disallow It, but what you decide is up to you meanwhile, and appreciate you thinking about it.
what's going on is you can refer to the local on the type, but only by numeric index. ex. (local.get 0). Since the param names are ignored, you can't use them as symbolic IDs. That also means if there are any clashes you don't know as they aren't validated. In other words, the IDs on type func params are useless and compile checks won't help anyone either.
Workaround:
A workaround is someone can copy/paste the param names while both referring to the type by ID and inlining it in the type use. I expected the use case for setting param IDs with the type index was to set or override param names, but it can also be used as a workaround for implementations who choose not to default to the IDs on the type.
Ex. the below is a workaround while this issue is being negotiated
I could also very well be misunderstanding the spec, I've done that multiple times before! It might be good to open an issue on the WebAssembly/spec repo though to help clarify the purpose of identifiers in type declarations.
Identifiers that are "local" to a type definition are not in scope at use sites of the type. That's intentional -- as a universal principle of scoping, identifiers should only be accessible in scopes local to their binder, not be implicitly injected into unrelated scopes merely by use of some outer identifier.
So indeed, parameter names in type definitions are essentially useless, except for documentation. They are still allowed, for no deep reason other than using the same param syntax everywhere, and because it's common in many other languages (e.g., in C a function type can have parameter names as well, although they can't be used for anything either).
"An identifier context is well-formed if no index space contains duplicate identifiers."
I believe this statement from the spec is still correct. Non-duplicate names are enforced everywhere where the names actually bind symbolic indices. Function type parameters are the only case I can think of that are not binders. It's still checked for other occurrences of parameter names, like here:
(FWIW, the situation is a bit more muddy for struct field names in the GC extension. Technically, they are not directly in scope either, instead a form of a dependent scope lookup is happening, again similar to the rules common in C and other such languages.)
Activity
alexcrichton commentedon Jan 25, 2022
Thanks for the report! Personally I'm under the impression that the identifiers used in a
(type (func ...))declaration are not used for local names in each function that the type is then referenced within. I believe that only identifiers specified inline with the function definition itself are used, so I think that this is working as expected?That would mean that in 1/2 from your examples the
$xparameter is ignored so the only$xis the local itself, and for the third example that doesn't compile it's because$xisn't in scope. I don't know whether your first example is supposed to be valid or not. According to this there is no validation performed on the identifiers of a type declaration, but that may simply be a mistake or an oversight.codefromthecrypt commentedon Jan 25, 2022
Hmm yeah it would be odd to have identifiers on a param and then not use them for anything. For example, being able to reference a type by symbolic ID hints someone is being able to separate and re-use the types. You can consider a lambda type with multiple implementations.
Ex. of interface/impl where param name defaulting to what's on the type could matter
In such an example, I do think the param names would be helpful not just in that the impl can use those for symbolic lookup, but also debug (name section)
on this being an oversight or a mistake..
I would be surprised if someone added param names to types in the spec, yet intended implementations to ignore them, but yeah an oversight is indeed another answer. I guess the mistake was allowing param names on the type as it was intentional to not allow interface/impl divide or other use cases like this. Mistakes, happen, but this seems a pretty significant portability issue. Personally, I wouldn't intentionally disallow It, but what you decide is up to you meanwhile, and appreciate you thinking about it.
@rossberg any thoughts on this?
codefromthecrypt commentedon Jan 25, 2022
Current status:
what's going on is you can refer to the local on the type, but only by numeric index. ex. (local.get 0). Since the param names are ignored, you can't use them as symbolic IDs. That also means if there are any clashes you don't know as they aren't validated. In other words, the IDs on type func params are useless and compile checks won't help anyone either.
Workaround:
A workaround is someone can copy/paste the param names while both referring to the type by ID and inlining it in the type use. I expected the use case for setting param IDs with the type index was to set or override param names, but it can also be used as a workaround for implementations who choose not to default to the IDs on the type.
Ex. the below is a workaround while this issue is being negotiated
You can imagine a script could mechanically fixup wat to do the copy/pasting while this issue remains also
alexcrichton commentedon Jan 26, 2022
I could also very well be misunderstanding the spec, I've done that multiple times before! It might be good to open an issue on the WebAssembly/spec repo though to help clarify the purpose of identifiers in type declarations.
rossberg commentedon Jan 26, 2022
Ah, let me give some background.
Identifiers that are "local" to a type definition are not in scope at use sites of the type. That's intentional -- as a universal principle of scoping, identifiers should only be accessible in scopes local to their binder, not be implicitly injected into unrelated scopes merely by use of some outer identifier.
So indeed, parameter names in type definitions are essentially useless, except for documentation. They are still allowed, for no deep reason other than using the same param syntax everywhere, and because it's common in many other languages (e.g., in C a function type can have parameter names as well, although they can't be used for anything either).
I believe this statement from the spec is still correct. Non-duplicate names are enforced everywhere where the names actually bind symbolic indices. Function type parameters are the only case I can think of that are not binders. It's still checked for other occurrences of parameter names, like here:
Hope this helps. :)
(FWIW, the situation is a bit more muddy for struct field names in the GC extension. Technically, they are not directly in scope either, instead a form of a dependent scope lookup is happening, again similar to the rules common in C and other such languages.)
codefromthecrypt commentedon Jan 26, 2022
Thanks @rossberg, this does help. I think we should change the spec wording to clarify this, so I'll raise an issue with a suggestion!
codefromthecrypt commentedon Jan 26, 2022
moving here. thanks folks WebAssembly/spec#1411