Skip to content

Facade grammar: a producer whose resource parameter is NULL on creation and whose result is the head (curl_slist_append(NULL, s) creates, curl_slist_append(list, s) appends) #3121

Done

Description

@ehartford

Part of #3115; gap 3 of #3111.

fn curl_slist_append / rename new / param list fixed null is refused: "param 0 ... receives a modeled resource; a resource is passed by the value that owns it, never fixed". curl_slist_append(3): "Passing in a NULL pointer in list makes curl_slist_append create a new list ... Returns a pointer to the first item of the resulting list ... If curl_slist_append returns NULL, the list is not touched".

Two needs: (a) a producer with a resource-typed parameter fixed to NULL (HeaderList.new(s)); (b) a mutator that may return a new head the owner must adopt, or NULL leaving the old one (list.append(s)? updating the owned pointer in place, Err on NULL with the list unchanged). Same shape in other singly-linked C list builders (curl_slist, g_list_append/g_list_prepend, json_object_object_add chains).

Done when: both are expressible; fixture over curl_slist with an append that fails leaving the list intact.

Activity

  1. ehartford commented on Nov 18, 2026

    @ehartford
    ContributorAuthor

    Warning from the ruling on #3122 (D214): this is not the same shape. curl_slist_append returns NULL on failure and the original list is still valid, so 'consumes the list, returns the new one' leaks the whole list on a failed append. It takes D59's failed-production form, where the error carries the original resource back.

  2. ehartford commented on Nov 24, 2026

    @ehartford
    ContributorAuthor

    Measured on main 9a2535b with a 15-line header shaped like curl_slist (slist_append(NULL, s) creates, slist_append(list, s) appends, NULL on failure with the list untouched).

    (a) The constructor works now. D214's absent parent (#3588) covers it:

    resource HeaderList wraps *mut slist
        from slist_append
        drop slist_free_all
    fn slist_append
        rename new
        param 0 fixed null
    

    HeaderList.new("a") builds, runs, leak count 0.

    (b) The append has no sound spelling. The only form the facade accepts today is a second presentation that consumes the list on an independent resource:

    fn slist_append
        rename append
        consumes param 0
    

    It works when the append succeeds. When it fails, the facade has consumed the list and receives NULL: the caller holds None and the whole list is unreachable. That is exactly the leak D214's note warned about. (Without independent the item is refused: "a resource that depends on one of its own type is not modeled".)

    Brief for Eric: the spelling of a failed production that hands the input back

    The shape. A function takes a resource, and on success returns the resource to use from now on (possibly a different pointer); on failure it returns NULL and the one passed in is still valid and still the caller's. curl_slist_append, g_list_append, realloc.

    What the ruling already says (D214's note): it takes D59's failed-production form, where the error carries the original resource back.

    Proposed surface. No new clause: the facts are already stated by consumes param N on a producer of the same resource. For that combination the facade presents

    move fn append(s: &str) -> Result[HeaderList, HeaderListError]
    

    where the failure is D59's FailedWithResource(status, resource) with the input as the resource, so list = list.append(s)? either moves on with the new head or returns the old list inside the error, and nothing is lost on either path. A caller who wants to keep going writes match, takes the list out of the error and continues.

    • What the others do. Rust's Vec::try_reserve leaves the vector in place and returns an error; String::from_utf8 is the closer model: on failure the error owns the input and into_bytes() gives it back. Zig's realloc returns an error and leaves the old slice valid. Swift has no such form.
    • What the spec says. §16.2b.4 (D59) defines FailedWithResource for a producer that produced a resource in a failed state; here the resource in the error is the one that was passed in, in a good state. The words need one sentence for that case, and the status for a NULL return (there is no status code).
    • Mission fit. The leak is the thing With is stricter about than Rust; a spelling that consumes and may lose the list cannot be the presented one.
    • Prediction: yes to the error carrying the input back, reusing D59's variant, 70%.

    Needs from you: the form above or another, and the §16.2b.4 sentence. Labelling human attn needed.

    Found while building the fixture and fixed separately: an inline C body with f(x)->field = v was mistranslated, and an untranslated call was reported only as "(kind=103)".

  3. ehartford commented on Nov 25, 2026

    @ehartford
    ContributorAuthor

    Ruled by Eric, 2026-11-25 (answers to briefs.md).

    A: consumes param N on a producer of the same resource presents move fn append(s) -> Result[R, E], the failure carrying the input back (D59's FailedWithResource). Two requirements: the new §16.2b.4 sentence says the resource in the error is in a good state; the contract names the status for a NULL return, the facade does not invent one. An in-place mut presentation can be layered on the same facts later. I bring the sentence before landing. Label removed.

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