Skip to content

sema: struct-literal fields skip value-type checking entirely (str into i32 field accepted) #896

Closed

Description

@ehartford

Found while fixing #886. The struct-literal field loop's only type-mismatch
rejection is gated on the VALUE being a &T view; a non-ref wrong-typed value
is never checked. The union case is now rejected (§16.4, #886), but the
general hole remains:

type P { n: i32, s: str }
fn main:
    let a = P { n: "hello", s: "ok" }   // ACCEPTED — zero diagnostics
    print(f"{a.n}")                      // garbage at codegen

Also accepted: type Q { n: i32 }; P2 { q: 7 } (scalar into a plain
struct-typed field) — which happens to "work" by accidental positional
flattening, and is blessed nowhere in the spec (checked: §struct-literal
forms defines named/positional/block/update/shorthand only; no scalar
wrapping).

The binding/call/assignment paths all reject these via types_compatible +
arithmetic_result_type; the field loop needs the same predicate. Held out
of the #886 commit because the blast radius is different: generic fields
(field types may be unresolved params after the rebind-to-BASE step) need
a careful gate, and any in-tree reliance on the accidental acceptance
surfaces as new errors — wants its own battery.

Repro of the sema hole: see above; the mismatch check to mirror is
SemaCheck's binding path ("type mismatch in binding": types_compatible ==
0 && no contextual-copy && arithmetic_result_type == 0).

Activity

  1. ehartford commented on Sep 1, 2026

    @ehartford
    ContributorAuthor

    Fixed: the field loop's mismatch rejection is now the ordinary structural predicate (types_compatible) instead of firing only when the VALUE was a &T view. P { n: "oops", s: 42 } errors on both fields with the field name and both types; carve-outs are explicit — union fields keep their §16.4 message, dyn-object fields coerce, contextual-Copy adjustments stand, and integer literals still initialize pointer fields (the stdlib's own Vec{ ptr: 0 } null idiom, which the first full-corpus run caught). The scalar-into-struct-field accidental flattening from the report is likewise rejected by the general predicate. Full :test green, evidence recorded.

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