Skip to content

SC9001 ICE: bytesNew source of kind union when Buffer.from(union) is inside a function used as a value (.map callback) #401

Description

@somarlyonks

Summary

Buffer.from() on a union narrowed to its object arm crashes when the enclosing function is used as a value (e.g. as an Array.prototype.map callback):

error SC9001: internal compiler error: in encodeParam: bytesNew source of kind union — please report this

Reproduction

type SqlParam = string | number | boolean | null | Uint8Array

function encodeParam (param: SqlParam): string | number | boolean | null | {$hex: string} {
    if (param !== null && typeof param === 'object') {
        return {$hex: Buffer.from(param).toString('hex')}
    }
    return param
}

function encodeParams (params: SqlParam[]): string {
    return JSON.stringify(params.map(encodeParam))
}

console.log(encodeParams(['a', 1, true, null, new Uint8Array([0xde, 0xad])]))

Actual

repro.ts:4:23 - error SC9001: internal compiler error: in encodeParam: bytesNew source of kind union — please report this

Expected

Compiles - param is narrowed to Uint8Array by the typeof param === 'object' guard.

Notes

  • Calling encodeParam(...) directly (not through .map) compiles; the crash appears when the function is used as a value/callback.
  • The same call flow compiled under 0.0.36.

Workaround

Bind the narrowed value to an explicitly typed local first:

if (param !== null && typeof param === 'object') {
    const bytes: Uint8Array = param
    return {$hex: Buffer.from(bytes).toString('hex')}
}

Environment

  • scriptc 0.1.4
  • Node v24.14.0
  • macOS (Darwin arm64)

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