Repository navigation
Description
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
- Add tests proving receiving-context reinterpretation is rejected: assigning a C pointer return directly to an unrelated pointer or safe
strwithout modeled conversion should fail. - Add tests proving
void*does not auto-accept arbitrary safe values orstr; require explicit casts through raw pointer forms where the language permits them. - Add positive tests for explicit conversions that remain intended:
str as *const u8, same-domain raw pointer relabeling, and Cvoid*parameters receiving explicit*mut c_void. - Include a
c_importheader fixture with raw pointer parameters and returns so tests exercise generated bindings, not only manual extern declarations. - 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_voidremains opaque and pointer-only.with build && with build :fixpoint && with build :testpass.
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
ehartford commentedon Jul 3, 2026
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.
test: coverage-sweep batch 5 (#485 #486 #487 #500 #505 #510)