Skip to content

D113: brackets make a Vec; a fixed array or set is built where demanded - #2286

Merged
ehartford merged 3 commits into
stc-phase3from
d113-impl
Oct 9, 2026
Merged

ehartford merged 3 commits into
stc-phase3from
d113-impl

Conversation

@ehartford

@ehartford ehartford commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Implements D113 (§4.3c rule 1).

  • With no demand, a bracket literal is a Vec[T]; this includes [v; N] and a literal passed straight to a &Vec parameter. A slice demand views the Vec.
  • A use of a let-bound literal that demands a fixed array is a D93 demand, as Vec and set demands already were. This includes &xs at a &[T; N] parameter.
  • A duplicate string or integer constant in a literal built as a set warns. A Vec keeps every element.

Spec change, so tests follow it: behav_1367, behav_1389, behav_slice_pattern_rest_binding, slice_pat_both, slice_pat_head_rest and slice_pat_exhaustive tested fixed-array patterns on undemanded literals. They now demand the array ([i32; N]). Without that they would need a rest binding on a Vec, which was never implemented; that is filed as #2283, with the spec's count-versus-view contradiction. behav_d93's comments are updated, and its set literal no longer carries a duplicate.

New tests: behav_d113_brackets_make_a_vec and behav_d113_set_duplicate_warns both fail on the base compiler. behav_d113_collection_by_demand guards demand.

Scan: every test, lib, tools and examples file that has a bracket literal (599 files) was checked with this compiler and with the base stage1. No file regresses. No example needed a change.

Not done here: the optional storage optimization (stack or static data for a literal that is never grown or retained). The spec allows it but does not require it.

Battery fixes in this PR (f329092): a Vec or set literal consumes a Drop local placed in it (the use-after-move went undiagnosed before); VecRange is ephemeral, so a VecRange from a temporary Vec is refused (err_vecrange_of_temporary); every demand on a literal's binding is recorded, so two that disagree are again an error at the second use. f57ba3c regenerates src/InitTemplates.w, which #2281 left stale.

A bracket literal is a Vec[T] unless the demanded type is another
collection built from a list (a fixed array, HashSet, BTreeSet). A use
of a let-bound literal that demands a fixed array, including through
`&xs`, is a demand like a Vec or set one (D93). A duplicate string or
integer constant in a literal built as a set warns.

Spec change (D113, §4.3c): tests that relied on the fixed-array default
now demand the array they test; the dynamic rest binding they would
otherwise need is #2283.
@ehartford
ehartford added this pull request to stack #2287 October 9, 2026 04:33
@ehartford ehartford changed the title d113 impl D113: brackets make a Vec; a fixed array or set is built where demanded Oct 9, 2026
Eric Hartford added 2 commits October 8, 2026 22:06
#2281 edited docs/spec/guide/with_for_ai.md and merged as docs-only, so
nothing regenerated src/InitTemplates.w and cli-selfhost-project-tests
(`with init` writes the guide as AGENTS.md) went red on main. Generated by
tools/gen_init_templates.w.
…a temporary; every demand counts

The battery on D113 found three defects that the fixed-array default had
hidden:

- A whole Drop local placed in a Vec or set literal was moved by MIR, but
  Sema never marked it moved: `let a: Vec[W] = [tmp]` then `tmp.id` read
  the vacated local (printed 0). Since #605 only array targets were
  marked, on the claim that other collections manage element ownership
  separately. They don't. Every target now marks the element consumed.
- A VecRange (`split_at`, `split_at_mut`, `range`) views its Vec but was
  not ephemeral, so `mk().split_at(1)` bound two views into a Vec freed
  at the end of the statement. VecRange is ephemeral now, like CStr, and
  the existing temporary-borrow check refuses it. New test:
  err_vecrange_of_temporary.
- D93 recorded a demand only where a use mismatched the binding's type.
  Under D113 a `&Vec` use already matches the default, so it went
  unrecorded, and two disagreeing demands became a plain mismatch at the
  first use. Every expected type checked against a literal's binding is
  now a demand, and the error names both at the second use again.

Tests that relied on the fixed-array default now demand their arrays
(spec change, D113): err_1367 (two files), behav_for_full_patterns,
d_acceptance slice_pat_* and dump_typed_d22_contextual_joins (a sequence
is a Vec now). err_split_at_index_type names `Vec.split_at`.
err_split_at_mut_readonly_receiver tests a read-only `&Vec` receiver.
That is the read-only case for a Vec: a `let` owner may call a `mut fn`
(storage-types note: mutation through a marked operation is not
rebinding).
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery GREEN on f329092 (stack top: #2280, #2284, #2285, #2286), with run tools/battery.w: gate rc=0 (605 s), uat rc=0 (97 s), battery-checks rc=0 (1213 s), fixpoint and the drop and move audits included.

The first battery, on 859e562, was red. It found three defects the fixed-array default had hidden, plus a red main, all fixed in f329092 and f57ba3c:

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.

1 participant