Skip to content

Tolk: stringHexToSlice(\"\") is rejected as “not a constant string” (empty string mishandled) #2052

@Gusarich

Description

@Gusarich

Compile-time string builtins require a constant string literal. However, an empty string literal ("") is rejected as “not a constant string”, while a whitespace-only string (" ") is accepted and evaluated as an empty hex bitstring. This indicates the compiler is conflating “missing/non-constant string” with “empty string literal”.

Reproduction

Control: whitespace-only string compiles and produces an empty slice.

Create /tmp/tolk_stringHexToSlice_spaces_ok.tolk:

tolk 1.0

import "@stdlib/common.tolk";

fun onInternalMessage() { return 0; }

@method_id(100)
fun bitsCount(): int {
    var s: slice = stringHexToSlice("   "); // hex parser skips spaces -> empty slice
    return s.remainingBitsCount();
}

Compile + run:

./artifacts/tolk /tmp/tolk_stringHexToSlice_spaces_ok.tolk > /tmp/tolk_stringHexToSlice_spaces_ok.fif
./artifacts/fift -I artifacts/lib /tmp/tolk_stringHexToSlice_spaces_ok_runner.fif

Create /tmp/tolk_stringHexToSlice_spaces_ok_runner.fif:

"/tmp/tolk_stringHexToSlice_spaces_ok.fif" include <s constant code
100 code 1 runvmx abort"exitcode is not 0" .s cr { drop } depth 1- times

Observed output:

0

Failing case: empty string literal is rejected.

Create /tmp/tolk_stringHexToSlice_empty_rejects.tolk:

tolk 1.0

import "@stdlib/common.tolk";

fun onInternalMessage() { return 0; }

@method_id(100)
fun bitsCount(): int {
    var s: slice = stringHexToSlice("");
    return s.remainingBitsCount();
}

Compile:

./artifacts/tolk /tmp/tolk_stringHexToSlice_empty_rejects.tolk > /tmp/out.fif

Observed:

error: function `stringHexToSlice` requires a constant string, like `stringHexToSlice("some_str")`

Expected behavior

Empty string literals should be treated as valid constant strings. In particular, stringHexToSlice("") should compile and evaluate to the empty slice (the same result as stringHexToSlice(" ")).

Metadata

Metadata

Assignees

No one assigned

    Labels

    TolkRelated to Tolk Language / compiler / toolingllm-fuzzing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions