Skip to content

Modeled C: only the first callback case of a variadic contract infers its userdata type; a second pair (CURLOPT_HEADERFUNCTION after WRITEFUNCTION) fails 'cannot infer U' #3113

Closed

Description

@ehartford

Summary

In a discriminated variadic contract (§16.2b.5, D66), only the first-listed callback … userdata param CONST case is callable. Calling the setter for any later callback case fails with "cannot infer generic method type parameter 'U'". Reordering the cases moves the failure to the other one.

That makes a second callback pair on one resource impossible. For libcurl that's CURLOPT_HEADERFUNCTION/CURLOPT_HEADERDATA beside CURLOPT_WRITEFUNCTION/CURLOPT_WRITEDATA, so response headers can't be read safely. with v0.16.0.8, darwin-aarch64, c.libcurl 8.23.0 from with get.

Repro

src/facades/libcurl.w:

use c_import("curl/curl.h", link: "curl")

c facade libcurl:
    resource Easy wraps *mut CURL
        from curl_easy_init
        drop curl_easy_cleanup
        abandon curl_easy_reset
    fn curl_easy_init
        rename new
    fn curl_easy_setopt
        rename setopt
        callbacks none
        ok CURLE_OK
        variadic param 2 selected by param option:
            case CURLOPT_WRITEFUNCTION: callback param 2 as curl_write_callback userdata param CURLOPT_WRITEDATA retains by param 0
            case CURLOPT_HEADERFUNCTION: callback param 2 as curl_write_callback userdata param CURLOPT_HEADERDATA retains by param 0
            case CURLOPT_URL: str
    fn curl_easy_perform
        rename perform
        lend
    fn curl_easy_reset
        rename reset
        lend
        callbacks none

src/main.w, which uses only the header pair:

use facades.libcurl
use c_import("curl/curl.h")

fn on_chunk(ptr: *mut c_char, size: u64, nmemb: u64, sink: &fn(i32) -> Unit) -> u64:
    sink((size * nmemb) as i32)
    size * nmemb

fn fetch() -> i32:
    var head = 0
    let head_sink = n => head = head + n
    let made = Easy.new()
    if made.is_none(): return -1
    var easy = made.unwrap()
    if easy.setopt(CURLOPT_URL, "https://example.com") != CURLE_OK: return -1
    if easy.setopt(CURLOPT_HEADERFUNCTION, on_chunk) != CURLE_OK: return -1
    if easy.setopt(CURLOPT_HEADERDATA, head_sink) != CURLE_OK: return -1
    if easy.perform() != CURLE_OK: return -1
    head

fn main:
    print(f"{fetch()}")
error: cannot infer generic method type parameter 'U'
 --> src/main.w:15:8
15 |     if easy.setopt(CURLOPT_HEADERFUNCTION, on_chunk) != CURLE_OK: return -1

Swap the two case lines so CURLOPT_HEADERFUNCTION comes first, with no other change, and the same program builds and runs (prints 347, the header byte count). With both pairs in use, whichever case is listed second fails the same way.

Expected

Each callback case is an independent pair with its own U, as §16.2b.9 "Pairs configured across calls" describes. A resource can hold several retentions at once ("a resource may hold several retentions at once, and each ends at its own release").

Context: #3111 (gap 6).

Activity

  1. added
    bug
    on Nov 9, 2026
  2. ehartford commented on Nov 11, 2026

    @ehartford
    ContributorAuthor

    Merged to main ae20d89 (#3248): each callback case's slot now names its case by the case's place in the contract's case lists (every earlier callback case also appends its implied userdata case); the written position named the wrong case from the second callback on. Fixture test/behavior/behav_3113_two_callback_pairs_on_one_resource.w configures both pairs on one handle.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions