Skip to content

D100: a field without pub is private to its package (#2209) - #2212

Merged
ehartford merged 8 commits into
mainfrom
field-privacy
Oct 6, 2026
Merged

ehartford merged 8 commits into
mainfrom
field-privacy

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fields follow D100's rule (§18.3): a field without pub is visible throughout its package; pub exports it.

  • Parser: pub before a field name, in both record forms; the bit rides in the field's align slot (FIELD_PUB_ALIGN_FLAG), masked at every alignment reader.
  • Sema: field reads, writes and struct literals across a package boundary are refused (field 'T.f' is private to its package). A Box is judged by its payload type; an alias no longer takes over its target's declaring node (it hid std.libc's mach_timebase_info fields behind mach_timebase_info_data_t). Distinct .value and C-import types are exempt.
  • Codegen: two align-slot reads were unmasked (a pub field became a 1 MiB alignment); the padded struct body must now agree with TypeLayout's size or codegen stops with an ICE naming the struct (shown firing on the unmasked build).
  • std: data records whose fields are their API mark them pub (std.build results and options, process.Finished, regex.Match, zip.ZipEntry, reflection records, Context, Handle, libc C mirrors). Vec gains as_ptr, as_mut_ptr, capacity; compiler sources use the runtime seam until a seed carries them.
  • CImport/migrator: every C record field is emitted pub; the four corpora are re-promoted (only pub differs).
  • Sema (found on the way, also broken on main): a type parameter no argument mentions now takes its type from the demanded result (var xs: ArenaVec[i32] = arena_vec_new_in(arena)), as the uninferable-parameter diagnostic already taught.
  • comptime string concatenation passed a reference's address, not the Vec's.

Tests: err_d100_private_field_read, err_d100_private_field_literal, behav_d100_field_visibility, behav_d100_field_through_alias, behav_generic_param_from_result; representation-reading tests use the public API.

Stage1 corpus: 3395 files; the only failures are behav_1944_comptime_snapshot_memory and behav_rt_in_unit_check_lane, which fail identically under main's stage1 (bde8954).

Battery pending.

Eric Hartford added 8 commits October 6, 2026 01:36
§18.3 (spec v7.27): the rule covers fields. The parser accepts `pub`
before a field; the flag rides in the field's alignment slot
(FIELD_PUB_ALIGN_FLAG, bit 20: alignments are 0 or a power of two up to
65536), so every copy of the AST keeps it and the extras layout after the
fields does not move. Sema strips the bit when it copies alignments and
keeps the pub fields by (declaration, field name). A field read or a
struct literal naming a field, from another package, is refused when the
field is not pub. A distinct type's .value and a C type's fields are
exempt (the language's unwrap; C has no privacy). The renderer and the
bundle interface writer emit `pub` on exported fields.

std marks its API fields pub: the std.cfg Stackify graph types and the
code/offset/message of RegexError, ZipError, ZlibError; Vec's fields stay
private. The compiler stops reading Vec's fields: empty Vec literals are
Vec.new(), raw element addresses come from with_vec_get_ptr (bounds-
checked), CiIR frees through with_vec_free, comptime's strtod reads
CString through as_cstr().ptr(); all valid under the pinned seed. Vec
gains pub as_ptr/as_mut_ptr/capacity for programs that need the buffer.

Closes #2209
…ords and C mirrors export their fields

- An alias no longer takes over the declaring node of the type it names
  (record_type_decl_tid), so a declaration's pub fields stay pub however
  the type is spelled (std.libc's mach_timebase_info_data_t hid them).
- A field read through a Box is judged against the payload type.
- Codegen masked the pub bit in two align-slot reads it had missed: a pub
  field became a 1 MiB alignment. The padded struct body now has to agree
  with TypeLayout's size or codegen stops with an ICE naming the struct.
- C record fields are emitted pub (every C field is public); libc's C
  mirrors, std.build's result records, Context and the reflection records
  mark theirs pub.
- comptime string concatenation passed the reference's address, not the
  Vec's, to the runtime.
- Tests read representation through the public API (as_ptr, as_mut_ptr,
  as_cstr().ptr(), arena_vec_new_in) or a raw first-word read.
- behav_d100_field_through_alias: an alias across a package boundary keeps
  pub fields readable and private ones refused.
…emanded result

The uninferable-parameter diagnostic teaches "annotate the result
binding" (#598, no turbofish), but generic free-function calls never read
the demand: `var xs: ArenaVec[i32] = arena_vec_new_in(arena)` was refused.
check_generic_call now matches the declared return type against the type
the call is checked against (binding annotation, parameter, return, field)
for parameters the arguments left unbound; a return type that does not
match binds nothing.

behav_generic_param_from_result covers each demand position.
The four migrated corpora (std.zl, std.re, std.tommyds, std.c_algorithms
and the c_algorithms programs) re-promoted with the release compiler: the
only change is `pub` on every C record field.
… Unit

An audit of every std `pub type` with private fields and no methods:
records whose fields are their whole interface mark them pub (std.build's
FetchedSource, Artifact, DeclSummary, SourceSpan, DiagnosticSummary,
EnvVar, LinkCommand, GeneratedSource, TestOptions, WorkspaceCompilePlan;
HttpResponse, IndexSpec, ContextError, CancellationToken). Handles,
iterators and contexts with accessors stay private. source-cache-tests'
build.w read FetchedSource.rc.
…ields of record type default to that record's zero literal

- An alias no longer stands in for its target's declaration, so a struct
  literal written through an alias is now checked for missing fields (it
  was skipped). c_import gave a field of another imported record no
  default, so `z_stream {}` in a facade failed: the field now defaults to
  that record's zero literal, as C zero-initializes it (C11 6.7.9p10).
- build/zlib_gzip.w builds its str through StringBuilder's API.
- The runtime-workers fixture reads a Task's fiber id through the struct's
  first word; the bundle-interface demo's Pair and Packet fields are pub.
- da_borrow_vec_drop_element reads the Vec's buffer through as_ptr().
- source-cache-tests prints the child's output when a fetch writes no
  report, instead of panicking on the missing file.
…ce storage uses it

Ruled by Eric 2026-10-06. Sema: `T.zeroed()` on a C record (c_import or
@[repr(C)]) is the all-zero value when every field is zero-valid (integers,
floats, bool, raw pointers, nullable fn pointers, an enum with a 0
discriminant, Option whose None is tag 0, and unions/arrays/records of
those); otherwise it is refused naming the field. MIR lowers it to the
established zero operand. FacadeRender renders in-place storage as
`Repr.zeroed()` (§16.2b.3) instead of `Repr {}`, which compiled only
while an alias skipped the missing-field check. The c_import struct-field
default from the previous commit is reverted: it made union and callback
fields invalid.

Spec §16.2b.3 gains the zero-validity rule; decision log D101. Follow-ups:
#2214 (migrate {0}/memset to zeroed), #2215 (fn-pointer fields nullable?).

Tests: behav_c_record_zeroed, err_c_record_zeroed_fn_pointer,
err_c_record_zeroed_enum_without_zero.
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery GREEN on 825f5b9 (based on main bde8954): gate 511s, uat 25s, battery-checks 916s.

Since the first draft: alias no longer stands in for its target's declaration (closes the missing-field hole through aliases); D101 (Eric 2026-10-06) T.zeroed() on zero-valid C records, facade in-place storage renders Repr.zeroed(); std data-record audit; generic parameters infer from the demanded result. Follow-ups: #2213, #2214, #2215, #2216.

@ehartford
ehartford marked this pull request as ready for review October 6, 2026 15:25
@ehartford
ehartford merged commit 3b1d3fe into main Oct 6, 2026
@ehartford
ehartford deleted the field-privacy branch October 6, 2026 15:25
ehartford pushed a commit that referenced this pull request Oct 7, 2026
…nversions bind nothing, operands before the outer demand (D106)

mission.md law 2 now states the two phases and the order; §4.9a carries
the same sentence beside the value→Option conversion; D106 records the
tension (law 2's "never solves a type variable" against #2212's generic
result parameter and D103's "never in inference") and the inner-first
ruling for `let x: Option[i32] = ident(3)` — `Some(ident(3))` at
`T := i32`, never `ident(Some(3))`. The compiler already does this
(measured on main); the fixture pins it with `ident` printing the size of
the `T` it was instantiated at.
ehartford added a commit that referenced this pull request Oct 7, 2026
…nversions bind nothing, operands before the outer demand (D106) (#2241)

mission.md law 2 now states the two phases and the order; §4.9a carries
the same sentence beside the value→Option conversion; D106 records the
tension (law 2's "never solves a type variable" against #2212's generic
result parameter and D103's "never in inference") and the inner-first
ruling for `let x: Option[i32] = ident(3)` — `Some(ident(3))` at
`T := i32`, never `ident(Some(3))`. The compiler already does this
(measured on main); the fixture pins it with `ident` printing the size of
the `T` it was instantiated at.

Co-authored-by: Eric Hartford <[email protected]>
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