Skip to content

is_empty() on str, arrays, slices, HashMap, HashSet and SlotMap lowers as len() == 0 instead of aborting the compiler (#1010) - #1947

Merged
ehartford merged 1 commit into
withlang-dev:mainfrom
joshhickson:josh/is-empty-len-family
Oct 2, 2026
Merged

ehartford merged 1 commit into
withlang-dev:mainfrom
joshhickson:josh/is-empty-len-family

Conversation

@joshhickson

Copy link
Copy Markdown
Contributor

Fixes #1010.

is_empty() aborted the compiler with BUG: user generic call lacks a concrete contract (rc 134) on six
receivers, not only str: arrays, slices, HashMap, HashSet and SlotMap too. Vec and FixedString worked.
Sema types is_empty() as bool on every receiver whose len() is an intrinsic
(collection_len_method_return_type), but MIR had an is_empty intrinsic only for Vec and FixedString. On
the others the call fell through to the generic-call path and hit the sema_phase_bug in
validate_generic_call_contracts.

The fix is in MIR lowering: when no intrinsic matches is_empty() and the receiver's len() is an intrinsic, it
lowers as that len intrinsic compared with 0. Both backends already lower len(), so neither changes
(--emit-c emits the comparison; SlotMap stays LLVM-only, #301).

Test: test/behavior/behav_1010_is_empty_len_family.w covers str, &str, an array, a slice, HashMap,
HashSet and SlotMap. On the parent f6736c5d it aborts (rc 134); with the fix it passes.

Seed-driven battery on this commit (with run tools/battery.w, Linux x86_64 under WSL2, the pinned seed
nightly-20260926-local-1-2e8defaa18ef, the pinned Linux SDK, CI's apt packages): the gate is green, and so is
every target of :battery-checks (fixpoint, the drop and move audits, every test target) except emit-c-smoke,
plus test and test-with-audits, which fail only because they depend on it. emit-c-smoke's "prelude runtime"
case times out compiling the emitted C (exit 124, the 300 s cap) on this machine; run alone it fails the same
way on the parent. It stays a draft until you're satisfied with that.

This covers the first half of your review on #1010. The second half, that an unresolved method on a builtin type
can reach a sema_phase_bug at all, is still open: any other Sema/MIR table mismatch would abort the same way.

…s as len() == 0 instead of aborting the compiler (withlang-dev#1010)

Sema types is_empty() as bool on every receiver whose len() is an intrinsic
(collection_len_method_return_type), but MIR had an is_empty intrinsic only
for Vec and FixedString. Everywhere else the call fell through to the
generic-call path and hit the sema_phase_bug abort in
validate_generic_call_contracts (rc 134). It now lowers through the
receiver's own len intrinsic and compares with 0, so both backends handle it
the way they handle len().

@ehartford ehartford left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed on a worktree at the PR head (ca194d9 on main f6736c5), driven by the pinned seed.

Premise verified. On main's compiler is_empty() aborts (rc 134, BUG: user generic call lacks a concrete contract) on str, arrays, slices, HashMap, &HashMap, HashSet and SlotMap; with the PR each runs and prints the right value. The PR's test aborts on main and passes on the PR build (red-first confirmed).

Owner-correct (D65). Sema already types is_empty as bool on every receiver whose len is an intrinsic; MIR only had the intrinsic for Vec/FixedString. The fix is one fallback in MIR's method-call lowering keyed on Sema's existing len classification — it realizes Sema's fact, adds no acceptance, no per-path decision. Lowering as len() == 0 matches the only shape the spec gives (functions.md:448).

Checks: --validate-all ok; analyze audit:all 0 violations; --emit-c shows _3 = (_2 == 0) from __with_len; src/main build :gate green (53 targets, 239 s).

Non-blocking:

  • src/MirLower.w:13352 spells len's return type as ty_i64 by hand — a third copy of a fact Sema owns (collection_len_method_return_type; MIR's older copy at :1899 even says usize). Ask Sema for it.
  • test line 40: let _h = sm.insert(1) — bare sm.insert(1) is the house style.
  • VecRange.is_empty() still crashes, but VecRange.len() alone crashes the compiler on main too — pre-existing, filing separately.

Not re-run here: your Linux battery/audits. #1010's remaining acceptance criteria (Sema diagnostic before any phase bug; doc-vs-table audit) are out of scope as you say, so the issue stays open after merge. Needs a rebase onto main before the stack's battery.

@ehartford
ehartford marked this pull request as ready for review October 2, 2026 06:21
@ehartford
ehartford merged commit 1688728 into withlang-dev:main Oct 2, 2026
ehartford pushed a commit that referenced this pull request Oct 5, 2026
 test

Review notes on #1947. lower_is_empty_via_len spelled len()'s return type as ty_i64 by hand, a third copy of a fact Sema owns; it now asks Sema (collection_len_method_return_type) for the type of the len call and of the 0 it compares with. The MIR is unchanged (binop(eq, copy _2, const 0ty4) for str.is_empty()). The test discards SlotMap.insert's handle with a bare call, the house style.
ehartford pushed a commit that referenced this pull request Oct 5, 2026
 test (#2069)

Review notes on #1947. lower_is_empty_via_len spelled len()'s return type as ty_i64 by hand, a third copy of a fact Sema owns; it now asks Sema (collection_len_method_return_type) for the type of the len call and of the 0 it compares with. The MIR is unchanged (binop(eq, copy _2, const 0ty4) for str.is_empty()). The test discards SlotMap.insert's handle with a bare call, the house style.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Documented str.is_empty() has no intrinsic: compiler aborts with internal BUG (core dump)

2 participants