feat/lsp #2

Merged
duke_duck merged 59 commits from feat/lsp into main 2026-10-21 06:06:56 +00:00
Owner
No description provided.
Lower `[T]` method calls through the `Array<T>` instance, forcing the
instance via the driver fixpoint when it does not exist yet. Add a
bytewise `Comparable` impl for `String`, and update the TODO and
std/async comments accordingly.
PriorityQueue
Mark std/core trap as noreturn and cold so the C compiler moves failure
paths out of line. Read the clock in Loop only when a polled frame
publishes a timeout, and silence the clang-unknown GCC warning option.
Snapshot of the llvm-pr working tree, reconstructed on top of 9ef99a5 so it can
be merged with real ancestry. Experimental MIR -> textual LLVM IR backend
(crates/hikic/src/llvm.rs, --llvm), the stdsafety corpus and its e2e suite,
the tools/fuzz harness, and the std/http + std/net refactor with the std/string
bounds and parseInt-overflow fixes it depends on.
Brings in `llvm-pr`: an experimental MIR -> textual LLVM IR backend
(`crates/hikic/src/llvm.rs`, driven by `--llvm`), the `tests/stdsafety`
fixture corpus and its ratchet suite, the `tools/fuzz` harness, and the
`std/http` + `std/net` refactor with the `std/string` bounds and
`parseInt`-overflow fixes it depends on.

Conflict resolution keeps the current branch's newer work wherever both
sides moved:

- `std/string/string.hiki` takes the checked `substring`/`sliceClamped`
  and non-trapping `parseInt` overflow, and keeps the `memcmp`-based
  `String.equals`/`Str.equals`, `String: Comparable`, the word-at-a-time
  `fnv1a`, and the `concat3..concat6` interpolation fast path.
- `crates/hiki-lsp/src/features.rs` keeps the `PathBuf` diagnostic
  comparison; the incoming `.display().to_string()` was a fix for the
  older `String` spelling of the same line and is redundant here.
- `crates/hikic/src/{mir/lower.rs,mir/mod.rs,codegen.rs}` keep their
  current shape and gain `extern_sigs`, its `MirModule` field, and
  `runtime_prelude`.
- `crates/hikic/tests/e2e_http.rs` takes the incoming version outright;
  this branch had only reformatted it.

`tools/fuzz/seeds/**` gets a `-text` gitattributes entry so git does not
normalise the CRLF in the HTTP frame seeds.
fmt move out of C
async frame pointer type
per-thread async ready queue
Introduce std/thread with spawnThread, a lazy bounded worker pool, and
eventfd completion signalling so blocking file reads run off the event
loop. Add callocImmortal, pthread and eventfd C prelude sections, and
e2e_threads tests.
Phase 2's pool was Linux-only: the completion signal was an `eventfd`, which
Darwin lacks, and `pthread_t` is an opaque pointer there rather than an integer.
Port both, keeping the runtime in Hiki rather than a C shim:

- `Wake` carries a completion descriptor as a `(readFd, writeFd)` pair -- one
  `eventfd` on Linux, a non-blocking `pipe` elsewhere -- so the job layout, the
  spare list and `jobWait` do not branch on the platform. The spare list stores
  pairs and the pool block grew to 704 bytes.
- `pthread_t` travels as the bits of a `u64`. `thread_create` writes it through
  a `Ptr<u8>` (C converts any object pointer to `pthread_t *`); `thread_join`
  and `thread_detach` are declared once per platform behind `@cfg` -- integer
  on Linux, pointer on Darwin -- and the handle is converted in a Hiki helper.
  Both backends then reuse one runtime, and no `pthread_t` type is named.

Fix a latent monomorphization bug the port's tests hit: `rewrite_calls_expr`
renamed call sites at every expression except a cast's operand, an
interpolation's pieces and an `unsafe` block's statements. `scan_expr` built
those instances, so a generic call inside any of the three was never renamed:
`newArray<i64>(n)` inside `unsafe { ... }` checked clean and then failed
lowering with "unknown function or type `hk_std_collections__array_newArray`".
Add the three arms; covered by a new `e2e_unsafe` test.

Migrate the raw-`pthread_create` tests and examples to `std/thread`'s portable
`spawnThread`/`Thread`, and raise `std/thread`'s `unsafe` ceiling to the new
count with the reason.

Fast suite: 126/126 on both backends; `diff-backends.sh` 35/35.
A module-level `var` could only be `nil`, `false` or `0`, so a global counter
or config value had nowhere to put its starting value. Extend the restricted
evaluator (`consteval::evaluate_initializer`) to `var`s and carry the result to
both backends:

- integers, floats, `Bool` and arithmetic over `const`s evaluate at compile
  time, so the storage is still written before `main` with no startup code;
- the implicit zero (`nil`, `false`, `0`, and any zero-valued initializer)
  still emits a bare declaration (`static T hkg_x;`, `internal global T
  zeroinitializer`);
- a value that would need allocation (`String`, `[T]`, a struct holding
  either) and a non-constant (a call, another `var`) are refused with a
  diagnostic naming the user's spelling.

`MirModule.globals` becomes `Vec<Global { name, ty, init }>`; codegen emits the
C initializer, LLVM the constant. Tests: a positive case across `i64`, `f64`,
`Bool`, `u32`, a `const` operand and `@threadLocal`, plus a negative for a call;
`examples/globals.hiki` extended for the differential run.

Fast suite 128/128 on both backends; `diff-backends.sh` 35/35.
The plan's `Atomic<T>` could not exist while a generic had no way to pick
the 32- or 64-bit intrinsic. `T.sizeOf()` is a constant of each instance,
so the choice is a compile-time `if` whose untaken branch is dead in both
backends:

- `std/sync` loses `AtomicU64`/`AtomicU32` and gains `pub struct Atomic<T>`
  with load/store/add/sub/swap/compareExchange; `AtomicBool` stays, now an
  `Atomic<u32>` with the two Bool conversions.
- `new` allocates an eight-byte cell for either width, so the constructor
  has no size branch; `at(ptr)` wraps existing storage as before.
- callers in `examples/` and `e2e_atomics`/`e2e_globals`/`e2e_threads`
  move from `AtomicU64` to `Atomic<u64>`.

Two monomorphization gaps blocked a generic type named only by a cast, the
way `Atomic<T>` is shared with a thread:

- `scan_expr` descended into a cast's operand but not its target, so the
  instance was never enqueued;
- `rewrite_calls_expr` left the target as written, so the `Applied` survived
  into lowering even when something else had instantiated it.

Both reported "unknown generic type `<Name><...>`". Covered by
`e2e_modules::a_generic_type_named_only_by_a_cast_is_instantiated`; the
width dispatch by `e2e_atomics::a_generic_atomic_dispatches_on_its_width`
(both widths, signed read-back, four threads on a `u32`).

std/sync's `unsafe` ceiling rises 4 -> 8: one block per operation's width
branch, plus the cell's malloc/free. Fast suite 129/129 on both backends;
diff-backends.sh 35/35.
Add workstream (4c): struct and array globals feasibility lands as struct
globals.

- consteval gains ConstValue::Aggregate + StructShape; a struct literal is
  evaluated field-by-field against declared field order/types/defaults, nil
  works where a field is a pointer, and a missing-field-without-default is a
  compile-time error.
- mir/lower: check_global_init recurse through the aggregate (String/[T]
  fields still refused), global_init_is_zero recurses, and writing a global's
  field lowers through the global's address via PlaceElem::PtrDeref (unsafe-
  only, same hop p.f = v takes).
- codegen emits designated initializers in C and literal structs in LLVM;
  struct/enum tags are now typedef'd up front so self- and mutually-referential
  structs (Ptr<Self>, A<->B) compile on C as they did on LLVM.
- monomorphizer seeds its type worklist from module const/var annotations and
  rewrites them + their initializer call sites, so var g: Holder<i64> =
  Holder<i64>(v: 9) resolves to the concrete instance.
- Tests: 4 new e2e_globals tests, 1 recursive-struct e2e_unsafe test,
  examples/globals.hiki extended. diff-backends.sh 35/35; fast suite 132/132
  on both backends; extras green.

Remaining from the four workstreams:
- array globals (Array<T> allocates; no constant form yet)
- Sendable/Sync checks (blocked on typed thread API, Mutex/Shared, Lazy/Once)
- reject raw module-level var outside std (blocked on wrappers; examples/
  globals.hiki and its tests must be migrated in the same change)

Left untracked: docs/SAFE-ABSTRACTIONS.md and docs/SYNTAX.md (user's own).
values
Hover, go-to-definition, references, and rename each scanned every
identifier in the file. Build an occurrence index once per analysis and
cache derived views (tokens, hints, diagnostics) on the analysis, so
they
are computed once and dropped on the next edit.

Also resolve goto-definition for symbols imported from other files, and
extend rename to update open importers.
`.elastic(min:max:)` starts `min` loops and grows when the live connection
count per active loop reaches `ELASTIC_UP_AT` (8), up to `max`; a surplus
loop that goes idle reports to the primary, which arms one
`ELASTIC_LINGER_MS` (200 ms) timer and stops the slot only if it is still
empty. No timer exists at steady idle.

macOS/BSD and elastic use one listener and a primary acceptor that routes
accepted fds to per-worker `Channel<i32>`s (all routing on one thread, so a
slot's `alive` is a plain check). Linux `.fixed` keeps the kernel-balanced
`SO_REUSEPORT` path. A slot is reusable only after its worker reports exit
through the new `running` flag, never while the previous thread is draining.

`serveOne` takes an opaque per-loop track pointer so the live count falls on
the loop that owns the connection without a nested `await` (roadmap step 7).

Tests: `four_http_workers_share_a_port_and_each_serve_some` no longer skips
macOS; new `elastic_workers_grow_under_load_and_shrink_back` holds 32
keep-alive connections, checks growth on both backends and thread count via
`/proc` on Linux, then shrink. Green on C and LLVM; diff-backends 37/37;
TSan clean on an elastic program.

Docs: roadmap step 6 marked DONE; handover gains the step 6 section.
Step 1: add ByteView over an owned RcBuf and ByteBuffer.byteAt/setByte/
view/appendRange. Move string/json/http/net scans and case conversions
onto them. Unsafe goes 345 -> 291 across 16 modules; http/internal no
longer holds any.

Step 2: add std/fd (FdCell behind Rc, close-once via Deinit) and
std/errno (platform-correct again/interrupted classification). fs and
process adopt descriptors, removing hand-written closes on error paths;
net uses Errno for EAGAIN but keeps its raw-i32 API for now.

Document the remaining order in docs/SAFE-ABSTRACTIONS.md and TODO.md:
optional RcBuf release, net Fd boundary, std/raw decision, pub-Ptr
ratchet, RcBuf.get/set, address-of, ownership slots, sync.
The C backend's struct/enum retain-release glue treated an optional
owning field as non-owning, so an RcBuf? field leaked its payload.
Emit the call under the field's has guard, matching LLVM's rc_op arm.

lower_expr_as also now wraps a T into T? when the destination names the
optional. Struct fields and arguments previously wrote a bare payload
over the tag in C and produced a malformed insertvalue in LLVM. This is
the implicit constructor spec 11.3 describes.

With both fixes ByteView.body is RcBuf? again: static views store nil
instead of allocating a zero-byte placeholder, so a literal view costs
no allocation. Add tests for the optional field lifecycle and the
static-view codegen, and record the accepted decisions in
docs/SAFE-ABSTRACTIONS.md (std/raw skipped, Pipe stays raw because Fd
is not Sendable).
The ratchet now records the 14 public Ptr signatures outside core/buffer by (module, name), so adding one fails as unexpected and removing one requires deleting its entry. Includes the multi-line declaration scan and a failure that prints module, name and line.
RcBuf.get<T>/set<T> lower to a typed element address plus the existing Deref/Store pair, so ArrayBody and Map no longer cast through Ptr<T> in unsafe. The monomorphizer now renames an explicit-type-argument receiver only when it names a declared type, not a value receiver such as buf.set<u32>().

Store does not define its Assign destination, so the lowered write uses a Bool temp instead of an element-typed temp; otherwise the refcount pass released an uninitialized owned local. Array and map drop from 6/4 unsafe uses to 0; the ratchet is 281 across 14 modules.
Step 6 of docs/SAFE-ABSTRACTIONS.md: `&place` lowers to a real Ptr<T> for a variable, field or dereference, and a call accepts it for both an inout parameter and a Ptr<T> parameter. The pointee type and the place's mutability are checked. Writing through a let pointer is allowed, still inside unsafe.

Migrate std/thread's pool, job and channel layouts to typed @packed fields (56 -> 48 unsafe) and give async/queue's generated frame head a typed @packed FrameHead with the same pinned offsets. Lower fs/http ceilings where the counts had slack.
store retains into an empty slot, replace retains the new value before releasing the old occupant, take moves the slot's reference out, and get returns an owned copy. ArrayBody now uses only those forms, so array.hiki no longer calls retain/release and the §16.1.4 carve-out is closed. set stays as the raw bit-copy escape hatch.

Verified e2e_rc, e2e_collections, e2e_std, e2e_everything, e2e_ownership_diff, e2e_json, e2e_async, e2e_threads, e2e_rcbuf, and e2e_unsafe_count on C and LLVM.
Add an opaque Cell to std/core with typed allocation, read/write and
width-selecting atomic helpers, plus the shared count-then-value
helpers. Rewrite Atomic, Mutex and Shared in std/sync on top of them;
the module now contains no unsafe and no raw pointer arithmetic.

sync drops 20 unsafe uses to 0, core rises 50 to 65, total 270 to 265.
Atomic<T>.at keeps its Ptr<T> parameter as the documented static-cell
exception, since taking the address of a module-level var already needs
unsafe and the body only forwards it to cellFrom.

Update spec (10.6.2, 16.2, 17, 18.3), the SAFE/TODO/threading docs,
and the ratchet comments. Checked e2e_atomics, e2e_threads, e2e_globals,
e2e_everything, e2e_http, e2e_std, e2e_rc and e2e_unsafe_count on C and
LLVM.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
duke_duck/hiki-lang!2
No description provided.