8000 BEAM: a free get in a merged stdlib capability module captures Headers.get even with the receiver type known · Issue #638 · kexhq/kex · GitHub
[go: up one dir, main page]

Skip to content

BEAM: a free get in a merged stdlib capability module captures Headers.get even with the receiver type known #638

Open

Description

@akoskovacs

On BEAM, a receiver call whose type is known is sent to the wrong dispatcher once the stdlib has a free function of the same name in a capability module.

Reproduce

  1. Add this free function to module TCP in src/stdlib/net/socket.kex, next to receiveChunk, along with the matching line in Mock's make TCP, implement: Net.Socket.TCP block. PR Grand standard library cleanup and refactoring #634 had both, and removed them only because of this bug.

    foul get(connection: TCPConnection) -> Result<Binary?, NetError> do
      match Kex.Intrinsic.NetTCP.receiveChunk(connection, 65536) do
        Ok(bytes) => return Ok(Just(bytes))
        Error(e) => return e.kind == Closed then Ok(None) else Error(e)
      end
    end
    
    foul get(connection) = refusing(@failure, Net.TCP, "TCP has no connection")
    
  2. Rebuild and run with --run:

    module Dsl do
      let get(path: String, n: Integer) = path
    end
    
    using Dsl
    using Net.HTTP, only: [Headers]
    
    main do
      IO.printLine("${Net.HTTP.Response.text(200, "hi").headers.get("Content-Type")}")
    end
    

What happens

Internal error: runtime error: Undefined method: get for Net.HTTP.Headers

The emitted main/0 calls kex_prelude:get, which knows nothing about Headers. Without the Dsl module, the same call is emitted as 'Kex.Net.Socket.Mock':'get'. That happens to print the right answer, but only by luck. Expected in both cases: text/plain; charset=utf-8, as the walker prints, and as spec/ufcs_free_function_vs_unimported_receiver.kex checks. That spec is how CI caught this.

The receiver's static type, Net.HTTP.Headers, is known, and importedFreeFunctionDispatch is meant to settle such a call outright ("a receiver method answers for its own type"). It looks like the extra get providers from the merged Net.Socket modules route the call past that path.

Impact

Sockets can't have a get (the next byte chunk) like FileHandle.get, so the plan for get/put as the byte primitives of Readable/Writable has to leave sockets out for now. Related: #620, #476.

Activity

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

      0