Design decisions

178. A field nothing reads is left out, with its stores (2026-11-21)

  • Decision. §176 for every field type. A struct field that no expression reads is left out of the emitted struct, its constructors and its cpl_tie. A read is s.f, mut s.f as an argument, a store that is not a plain = (s.f += 1 reads s.f), or any operation that sees the whole value: comparing, ordering, hashing, printing, reading from input, sorting naturally, or using the struct as a map key or set element, which keeps every field of that struct and of the structs inside it. A store place = v into a dropped field writes nothing; it is left out only when the place cannot trap (every index is a position of the domain its array is over), and v is still evaluated as (void)(v); unless it runs nothing, so its traps and effects stay (reading an array at positions of its domains runs nothing). A local whose only uses are such stores, xs.push(v) of a value that runs nothing, and being placed in a dropped field is left out with them, which is what removes the work; repeated until nothing more goes, since a dropped store reads nothing. A parameter used only that way stays in the signature as [[maybe_unused]] const T&, so it is not copied. A method that is never called reads nothing. cpl explain lists each dropped field as [dropped] struct S.f.
  • Why. Result structs carry witnesses most callers never read: Paths.back and Paths.order in std.graph. g++ keeps the stores because the arrays escape through the returned struct. The interpreter still computes every field; since nothing reads a dropped field, both print the same and trap at the same place.
  • Reverse. Drop drop_unread_fields/drop_dead_locals in crates/cpl-codegen/src/devirt.rs (back to fn fields only) and the dropped_store skips in crates/cpl-codegen/src/stmt.rs.