Skip to content

D100: fields are private to their package unless pub #2209

Closed

Description

@ehartford

D100 (spec v7.27 §18.3): "The rule is the same for functions, methods, types, fields, constants, and globals." Eric: "Vec's buffer and length should be private to the std package, invisible to users, and freely usable across std's own files."

Today a field has no visibility at all: type P { pub x: i32 } does not parse ("expected identifier"), and every field of every pub type is readable and constructible from any package (Vec { ptr: …, len: …, cap: …, elem_size: … }).

Needed:

  1. Parser: pub before a field name, in both type-body forms (Parser.w parse_struct_body, parse_struct_body_block), carried in the AST so copies and the renderer keep it.
  2. Sema: a field read, a struct literal naming the field, and a pattern binding it, from another package, refused when the field is not pub (same message family as other privacy errors).
  3. std: mark pub the fields that are API (measured: every field user code in test/, examples/, uat/ reads); Vec's ptr/len/cap/elem_size and similar representation fields stay private.
  4. Tests: a cross-package field read and literal refused; same-package access allowed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions