Skip to content

Struct literals silently accept unknown fields, duplicate fields, and default-expr type mismatches #632

Closed

Description

@ehartford

Summary

Three validation gaps in struct-literal checking (missing-required-field IS diagnosed, so the check path is live; these three are absent). All §4.3.

  1. Unknown field accepted: type P { x: i32 } ... P { x: 1, z: 3 } compiles AND runs.
  2. Duplicate field accepted: P { x: 1, x: 2 } accepted.
  3. Default-expr type mismatch accepted: type P { x: i32 = "no" } accepted.

Spec §4.3 / §20b (forbid, don't warn)

Each should be a compile error.

Found

Coverage sweep #529, 2026-07-03. Blocks the planned negative fixtures for #529.

Activity

  1. ehartford commented on Jul 4, 2026

    @ehartford
    ContributorAuthor

    Partly fixed in 2f4cac1: unknown fields and duplicate fields in struct literals are now rejected (§4.3), including on generic structs (unknown-field detection compares against declared field names, not the instantiated type). Verified: P { x:1, y:2, z:3 } → unknown field 'z'; P { x:1, x:2 } → duplicate field 'x'; generic Box { value: 5 } and defaulted Cfg { a: 1 } still compile. Fixtures: err_struct_literal_unknown_field.w, err_struct_literal_duplicate_field.w.

    Remaining (keeping open): the third gap — a default-expr type mismatch at the type declaration (type P { x: i32 = "no" }) — is a decl-time check (SemaDecl.w:594-616) that needs the expression checker in the decl phase; not done here.

  2. ehartford commented on Jul 4, 2026

    @ehartford
    ContributorAuthor

    Completed in 6dbe626 (third of three checks). Unknown + duplicate fields landed earlier in 2f4cac1; this adds the decl-time default-expr check: type P { x: i32 = \"no\" } → field default type mismatch for 'x'. Literal adaptation still works (u8 = 200), generic decls are checked at literal sites. Fixture: err_struct_field_default_type_mismatch.w. Build + fixpoint + full suite green.

  3. added a commit that references this issue on Jul 4, 2026
    6dbe626
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