Repository navigation
is_empty() on str, arrays, slices, HashMap, HashSet and SlotMap lowers as len() == 0 instead of aborting the compiler (#1010) - #1947
Conversation
…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
left a comment
There was a problem hiding this comment.
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 asty_i64by 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)— baresm.insert(1)is the house style. VecRange.is_empty()still crashes, butVecRange.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.
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.
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.
Fixes #1010.
is_empty()aborted the compiler withBUG: user generic call lacks a concrete contract(rc 134) on sixreceivers, not only
str: arrays, slices,HashMap,HashSetandSlotMaptoo.VecandFixedStringworked.Sema types
is_empty()asboolon every receiver whoselen()is an intrinsic(
collection_len_method_return_type), but MIR had anis_emptyintrinsic only forVecandFixedString. Onthe others the call fell through to the generic-call path and hit the
sema_phase_buginvalidate_generic_call_contracts.The fix is in MIR lowering: when no intrinsic matches
is_empty()and the receiver'slen()is an intrinsic, itlowers as that
lenintrinsic compared with 0. Both backends already lowerlen(), so neither changes(
--emit-cemits the comparison; SlotMap stays LLVM-only, #301).Test:
test/behavior/behav_1010_is_empty_len_family.wcoversstr,&str, an array, a slice,HashMap,HashSetandSlotMap. On the parentf6736c5dit 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 seednightly-20260926-local-1-2e8defaa18ef, the pinned Linux SDK, CI's apt packages): the gate is green, and so isevery target of
:battery-checks(fixpoint, the drop and move audits, every test target) exceptemit-c-smoke,plus
testandtest-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_bugat all, is still open: any other Sema/MIR table mismatch would abort the same way.