Skip to content

Tests: c import coercion coverage (§16) #486

Closed

Description

@ehartford

Summary

Add negative and positive tests for c_import boundary coercion policy. This is coverage for the current contract-driven model: the compiler should not reinterpret values from spelling or assignment context alone, and broad str/void* magic should stay removed.

Spec

§16.3c says c_import must not reinterpret values from type spelling or receiving context alone; automatic coercion is allowed only when the compiler or binding models the full contract needed for that conversion (docs/requirements.md:8217). §16.11 also frames raw pointer computation as safe while memory access or validity assertion remains unsafe (docs/with-specification.md:8657). §16.3b maps C void* to *mut c_void / *const c_void and defines c_void as opaque (docs/with-specification.md:8328).

Current state

The current C importer emits c_void as opaque for void pointer translation (src/CImport.w:408). Type translation maps void pointers to *mut c_void or *const c_void (src/CImport.w:5463). Sema call checking allows only explicit c_import coercions through try_ci_coercion; otherwise it emits normal argument type mismatches (src/SemaCheck.w:8163). Project state notes that implicit broad str/pointer and void* coercions were removed, while explicit str as *const u8 remains supported. The gap is committed regression coverage to keep those removals from backsliding.

Implementation plan

  1. Add tests proving receiving-context reinterpretation is rejected: assigning a C pointer return directly to an unrelated pointer or safe str without modeled conversion should fail.
  2. Add tests proving void* does not auto-accept arbitrary safe values or str; require explicit casts through raw pointer forms where the language permits them.
  3. Add positive tests for explicit conversions that remain intended: str as *const u8, same-domain raw pointer relabeling, and C void* parameters receiving explicit *mut c_void.
  4. Include a c_import header fixture with raw pointer parameters and returns so tests exercise generated bindings, not only manual extern declarations.
  5. Keep diagnostics focused on type mismatch or unsafe contract, not generic parser errors.

Test plan

Add test/compile_errors/err_c_import_no_context_reinterpret.w, err_c_import_no_auto_void_ptr.w, and err_c_import_no_auto_str_to_cstr.w. Add test/behavior/behav_c_import_explicit_pointer_coercions.h and .w for explicit casts that should compile. Add a generated-surface check ensuring void* appears as *mut c_void.

Acceptance criteria

  • Tests reject context-only pointer reinterpretation.
  • Tests reject broad automatic str/void* coercions.
  • Tests accept explicit raw pointer conversions that are part of the current model.
  • c_void remains opaque and pointer-only.
  • with build && with build :fixpoint && with build :test pass.

Dependencies

Related to #379 for curated safe c_import contracts.

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 16.3.4.28, 16.3.4.29, 16.3.4.31, 16.3.4.43.

Activity

  1. ehartford commented on Jul 3, 2026

    @ehartford
    ContributorAuthor

    Closing — str/int do NOT auto-coerce to void* (err_c_import_no_auto_void_ptr, 3628d82); cstr/const-char/uncurated-ptr/interior-nul coercion rules already covered (err_c_import_str_to_mut_char_, err_c_import_const_char_, err_c_import_uncurated_ptr_return_raw, err_c_import_str_to_cstr_interior_nul_literal, behav_c_import_const_c_string_arg). §16.

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