Skip to content

Facade clause length param N: a count parameter takes a non-negative isize without a spelled cast (D51-shaped) #2261

Closed

Description

@ehartford

Follow-on to the len() width ruling (2026-10-07: len() and the length family return isize, the signed pointer-width integer). The remaining friction at the modeled-C boundary is the sign change: a len() into a size_t parameter needs a spelled cast under §4.2.6 on every target. Proposal, same tier and shape as nullable param N (D102): a facade fn item may state length param N, asserting the parameter is a count; a non-negative isize then passes without a cast and a negative one is refused (a panic at the call, the compiler finishing its own sentence at run time, as the dynamic-size ruling in docs/proposals/vx.md Part IX does). Inference never grants it (Law 4: wrong would create unsafety at the C boundary); it comes from the clause or an adopted convention profile. Not ruled; this files it.

Activity

  1. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): reproduced, and the next step is a ruling (C interop, D51).

    $ cat main.w
    use c_import("string.h")
    fn main:
        let s = "hello"
        let n = strnlen(c"hello".ptr, s.len())
        print(n)
    $ with run main.w
    error: implicit integer narrowing or sign change from `isize` to `u64` (argument 2 of `strnlen`); use an explicit `as` cast (§4.2.6)
    

    What the rulings say. D108 names this follow-on: "a length param N facade clause for the sign change at the C boundary, the shape of D102's nullable param N". The spec has no such clause. D51's test applies: "what happens if this inference is wrong?"

    The question. Should a facade fn item be able to state length param N, so that a non-negative isize passes to that size_t parameter without a cast and a negative one panics at the call? The alternatives are to keep the as cast at every such call, or to derive the count from a paired buffer the way D64 buffer pairing already does for slices.

  2. added this to the post-v0.16.0 milestone on Oct 28, 2026
  3. ehartford commented on Nov 18, 2026

    @ehartford
    ContributorAuthor

    Ruled, 2026-11-18: rule A, widened. The D183 warning is not issued when the declaration on the other side of the conversion was imported from C (a parameter, a return value, a record field, a global); the range check stays in every case. D108's length param N follow-on is not needed: D183 made the conversion checked everywhere, so a per-parameter clause would restate what the type already says. Closes with the change that implements it.

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions