Repository navigation
Description
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) inno_stdmode — there is no allocator to create an ownedstr. ... If you need owned strings inno_std, bring your own allocator and useFixedString[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
FixedStringappears 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_intin Codegen.w;const X: i32 = ...desugars to comptime) and can be reused for N. - Tier plumbing is in place to host the type:
PRELUDE_COREprelude is lib/std/prelude_core.w (imports std.builtins/option/result/traits);core_without_allocgating (src/Sema.w:705-728) demotes string literals to&strviews in core tier (src/SemaCheck.w:2830-2836) —FixedString.push_strconsuming&strfits 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.
- 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, likeVec/Stringalready are (Semasymstable,symbol_requires_alloc_tierSema.w:712-718 special-cases them by symbol). Sema recognizesFixedStringin NK_INDEX type position, evaluatesNas a comptime integer constant (literal orconstident 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.
- (a) General const generics (
- Sema: add
syms.fixed_string; arm inresolve_type_level_arg_expr/resolve_type_exprNK_INDEX handling (SemaCheck.w:456-475) before the generic-inst fallthrough; ensure it is exempt fromsymbol_requires_alloc_tier(it is the core-tier type) and available in all tiers. - Methods (core-tier, no heap, no
with_str_*runtime calls; implement over the buffer with safe indexing,unsafeonly 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—falseon overflow (no panic in freestanding hot paths; no Result ceremony for a condition the caller checks)as_view() -> &str— borrowed view ofbuf[0..len]; ephemeral per the borrow rulesEq/Hash/Debugimpls where derivable in core tier
Decide home: methods in a newlib/std/fixed_string.wimported 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.
- 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-mirfor a sample program). - Docs: add FixedString to the §18.6/§18.7 surface notes in docs/libstd-spec.md.
- 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): declareFixedString[16], push_str, check len/capacity/as_view comparisons.test/behavior/behav_fixed_string.w— full-std runtime test: push to capacity, overflowpush_strreturns false and leaves contents intact,clear,as_viewequality with a literal, Debug formatting;//! expect-stdoutassertions.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— passingFixedString[8]whereFixedString[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-stdcore. - 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 :testpass.
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
ehartford commentedon Jun 13, 2026
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.