Skip to content

FixedString[N] for no_std (§18.7) #457

Closed

Description

@ehartford

FixedString[N] for no_std (§18.7)

Summary

§18.7 names FixedString[N] as the owned-string answer for freestanding (core-tier) code, where str/String are unavailable because they heap-allocate. No such type exists anywhere in lib/ or src/. Delivering it requires a small piece of compiler machinery (a type-level integer length, which With's generics do not currently support) plus a core-tier stdlib type with no heap, no OS, and no with_* string-runtime dependencies.

Spec

§18.7 (docs/with-specification.md:10035-10040):

String literals in no_std: Bare "hello" is &str (static reference) in no_std mode — there is no allocator to create an owned str. ... If you need owned strings in no_std, bring your own allocator and use FixedString[N] or a similar stack-allocated string type.

Tier table (:10094-10100): core tier has no heap types; str/String/Vec require alloc (:10021-10028).

Requirement ID: 18.7.1.34.

Current state

  • FixedString appears nowhere except the spec sentence (grep over lib/, src/: only docs/with-specification.md:10039).
  • No const generics: type-level arguments resolve to types only. Sema.resolve_type_level_arg_expr (src/SemaCheck.w:424-475) handles NK_GROUPED/NK_UNARY/NK_IDENT/NK_TYPE_GENERIC/NK_INDEX, and an integer literal argument returns 0 (resolution failure). Fixed arrays [T; N] take their literal length through a separate dedicated parse/sema path, not through generics.
  • Comptime integer evaluation exists (try_eval_const_int in Codegen.w; const X: i32 = ... desugars to comptime) and can be reused for N.
  • Tier plumbing is in place to host the type: PRELUDE_CORE prelude is lib/std/prelude_core.w (imports std.builtins/option/result/traits); core_without_alloc gating (src/Sema.w:705-728) demotes string literals to &str views in core tier (src/SemaCheck.w:2830-2836) — FixedString.push_str consuming &str fits this exactly.
  • Adding a stdlib module changes the embedded stdlib (src/compiler/EmbeddedStdlib.w is generated/refreshed by the build) — normal, no Link.w/rt hazard.

Implementation plan

Phased; phase 1 is the design decision, get it confirmed before building.

  1. Type-machinery decision — two options:
    • (a) General const generics (type Foo[N: i32]): large, unspecced language feature. Out of scope here; would need its own spec section first.
    • (b) Recommended: make FixedString[N] a compiler-known generic, like Vec/String already are (Sema syms table, symbol_requires_alloc_tier Sema.w:712-718 special-cases them by symbol). Sema recognizes FixedString in NK_INDEX type position, evaluates N as a comptime integer constant (literal or const ident via the existing const-eval path), and produces a struct type laid out as { buf: [u8; N], len: usize } — reusing the fixed-array layout machinery. Diagnostic for non-constant N: FixedString length must be a compile-time integer constant.
  2. Sema: add syms.fixed_string; arm in resolve_type_level_arg_expr/resolve_type_expr NK_INDEX handling (SemaCheck.w:456-475) before the generic-inst fallthrough; ensure it is exempt from symbol_requires_alloc_tier (it is the core-tier type) and available in all tiers.
  3. Methods (core-tier, no heap, no with_str_* runtime calls; implement over the buffer with safe indexing, unsafe only where raw copies need it):
    • FixedString[N].new() -> FixedString[N] (len 0)
    • len() -> usize, capacity() -> usize (= N), is_empty() -> bool, clear()
    • push_str(s: &str) -> bool / push_byte(b: u8) -> bool — false on overflow (no panic in freestanding hot paths; no Result ceremony for a condition the caller checks)
    • as_view() -> &str — borrowed view of buf[0..len]; ephemeral per the borrow rules
    • Eq/Hash/Debug impls where derivable in core tier
      Decide home: methods in a new lib/std/fixed_string.w imported by lib/std/prelude_core.w (and the full prelude, so the type behaves identically under std), with the type itself compiler-known per step 1.
  4. Codegen: lowering falls out of the struct-over-fixed-array representation; verify no implicit calls into the allocator or with_* string runtime are emitted for any method (inspect --dump-mir for a sample program).
  5. Docs: add FixedString to the §18.6/§18.7 surface notes in docs/libstd-spec.md.
  6. Run the full gate after each phase: with build && with build :fixpoint && with build :test (embedded-stdlib regeneration participates in fixpoint).

Test plan

  • test/spec/spec_ss18_7_fixed_string_core.w — //! check-only, //! args: --no-std (panic_handler + entry per existing pattern, cf. test/spec/spec_ss18_7_freestanding_mode.w): declare FixedString[16], push_str, check len/capacity/as_view comparisons.
  • test/behavior/behav_fixed_string.w — full-std runtime test: push to capacity, overflow push_str returns false and leaves contents intact, clear, as_view equality with a literal, Debug formatting; //! expect-stdout assertions.
  • test/compile_errors/err_fixed_string_dynamic_len.w — let n = 4; let s: FixedString[n] = ... → //! expect-check-fail: FixedString length must be a compile-time integer constant.
  • test/compile_errors/err_fixed_string_type_mismatch.w — passing FixedString[8] where FixedString[16] is expected → type error (lengths are part of the type).
  • Negative interaction: under --no-std (core), let s: str = ... still errors (existing err_no_std_owned_str_requires_alloc.w) while the FixedString test passes — proves the spec'd "use FixedString instead" story end to end.

Acceptance criteria

  • FixedString[N] resolves with a comptime-constant N in all tiers, including --no-std core.
  • Methods above work with zero heap allocation and no OS/runtime symbol dependencies (verified via MIR/objdump on a core-tier build).
  • Non-constant N and cross-length mismatches produce the diagnostics above.
  • Spec example workflow (owned-ish string in freestanding code) demonstrated by the spec test.
  • with build && with build :fixpoint && with build :test pass.

Dependencies

Related: #458 (no_std gating leaks — FixedString is the constructive half of the same §18.7 story), #510 (no_std test campaign references this type once it exists), #475 (Box[T] — sibling compiler-known generic, share the registration pattern), #391 (sema generic-instantiation erasure — reason to prefer the compiler-known route over general generics for now).

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 18.7.1.34.

Activity

  1. ehartford commented on Jun 13, 2026

    @ehartford
    ContributorAuthor

    Implemented in 352d12f. Added std.fixed_string / FixedString[N] for core and no_std use, compiler-known type-level length handling, no-allocation MIR/LLVM lowering for the FixedString method surface, positive and negative coverage, and spec inventory coverage. During the final rebase over §3.8, conformed build action/helper ownership boundaries so build.w remains valid under by-value consume rules. Verification passed: with build, with build :fixpoint, with build :test, and with build :test-green.

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