Skip to content

text format: local index isn't enforced on type parameter IDs #456

Closed

Description

@codefromthecrypt

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:

Ex.

(module 
 (func (result i32) (local $x i32) (local $x i32) local.get $x)
)

However, the other two combinations pass, and choose the last to define x (the function local):

  1. Redefine param ID in the same type == local.get 2
(module 
 (func (type 0) (local $x i32) local.get $x)
 (type (func (param $x i32) (param $x i32) (result i32)))
)
  1. Redefine an ID on a local with the same ID as a parameter == local.get 1
(module 
 (func (type 0) (local $x i32) local.get $x)
 (type (func (param $x i32) (result i32)))
)

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.

(module 
 (func (type 0) local.get $x)
 (type (func (param $x i32) (result i32)))
)

I'm not sure if unreleased code fixes this, but worth a look!

Activity

  1. alexcrichton commented on Jan 25, 2022

    @alexcrichton
    Member

    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.

  2. codefromthecrypt commented on Jan 25, 2022

    @codefromthecrypt
    Author

    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

    (type $filter (func (param $offset i32) (param $count i32)))
    
    (func $nop_filter (type $filter) ...)
    

    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?

  3. codefromthecrypt commented on Jan 25, 2022

    @codefromthecrypt
    Author

    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

    (type $filter (func (param $offset i32) (param $count i32)))
    
    (func $nop_filter (type $filter) (param $offset i32) (param $count i32))
    

    You can imagine a script could mechanically fixup wat to do the copy/pasting while this issue remains also

  4. alexcrichton commented on Jan 26, 2022

    @alexcrichton
    Member

    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.

  5. rossberg commented on Jan 26, 2022

    @rossberg

    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).

    "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:

    > (module (func (param $x i32) (local $x i32)))
    stdin:1.37-1.39: syntax error: duplicate local $x
    

    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.)

  6. codefromthecrypt commented on Jan 26, 2022

    @codefromthecrypt
    Author

    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!

  7. codefromthecrypt commented on Jan 26, 2022

    @codefromthecrypt
    Author

    moving here. thanks folks WebAssembly/spec#1411

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions