Repository navigation
Description
Summary
Implement the spec'd no_methods opt-out option on c_import: no_methods: "Type" disables auto-method generation for one imported type, no_methods: true disables it globally for that import. Today the option is not parsed at all — use c_import("lib.h", no_methods: true) is the "unknown c_import option" parse error — so users who hit a bad prefix-claim or want flat C style have no escape hatch.
Spec
§16.2a Auto-Method Generation — Opt-out:
- "Per-type:
use c_import("lib.h", no_methods: "Type"). Global:use c_import("lib.h", no_methods: true). Flat C functions are always available regardless."
Requirements: 16.2.2.29 (per-type), 16.2.2.30 (global).
Current state
c_importoption parsing (src/Parser.w:2273-2337) recognizes exactly two keys:link(2286) andallow_untranslated(2301); any other key →emit_error("unknown c_import option")(2327). Options are stored as extras onNK_C_IMPORTwith counts packed in d2 viapack_c_import_counts(link_count, allow_count)(src/Ast.w:299-306, node layout documented at Ast.w:1453).- Auto-method generation happens during header translation in
src/CImport.w:1223-1305: snake_case prefixes are precomputed per struct (ci_compute_snake_prefix, CImport.w:1308), and matching imported functions get thin wrapper method sugar emitted (first-param self match ~1255-1281). Flat functions are always emitted regardless (req 16.2.2.31 verdict: ok). - The translation layer has no concept of an opt-out: nothing in CImport.w consumes a per-import option besides links/allow-list, which reach it via the frontend (
Zcu.c_import_cache_key_frontendsrc/compiler/Frontend.w:352-374 and the allow-check helpers Frontend.w:473-499).
Implementation plan
- Parser (
src/Parser.woption loop at 2273-2331): addelse if key == "no_methods":accepting:true/falsekeyword literal → global flag;- one string literal or
[ "A", "B" ]array → per-type list (mirror theallow_untranslatedparsing shape at 2301-2325).
Store per-type syms as a third extras group after links and allow-list; extend the packed counts — d2 currently packs two counts (pack_c_import_counts, Ast.w:305); repack as three bitfields plus a global-flag bit (e.g. 12 bits link / 12 bits allow / 7 bits no_methods_count / 1 bit no_methods_all), addingc_import_no_methods_count/c_import_no_methods_allaccessors next to Ast.w:299-306. Update the node-layout comment at Ast.w:1453 and every consumer of the packed counts:src/render.w:207-208,src/Resolve.w:337,src/compiler/Frontend.w:356-357, 479-481.
- Plumb to translation: pass the no_methods set/flag from the frontend's c_import evaluation into the CImport translation entry (same path the header spec and links travel). In
src/CImport.w:1223-1236, skip prefix computation entirely when the global flag is set; when per-type, skip emitting wrappers for structs whose name is in the set (compare against the imported struct name beforestruct_prefixes.push). Constructor sugar (TypeName(args)→.new, req 16.2.2.10) must also be suppressed for opted-out types since it derives from the generated.newwrapper. - Cache key: append a
\n#no-methods:line with the flag and sorted type list inZcu.c_import_cache_key_frontend(Frontend.w:352-374), exactly like#allow-untranslated:(361-364) — otherwise toggling the option reuses stale generated bindings. - Diagnostics: unknown key error text stays for typos; a per-type name that matches no imported struct should be a warning in the import diagnostics ("no_methods: 'GHashTabel' matches no imported type") so misspellings don't silently no-op — emit where the type set is consumed in CImport.w.
- Fmt/render:
src/render.w:207-208renders c_import decls for formatting/printing; teach it the new option sowith fmtround-trips. - No rt/Link.w impact; parser/frontend/CImport only.
Test plan
(Use inline-header imports, pattern of test/compile_errors/err_c_import_str_to_c_char_requires_contract.w, declaring typedef struct Counter Counter; Counter *counter_new(void); void counter_incr(Counter *c); int counter_get(Counter *c);.)
test/behavior/behav_c_import_no_methods_global.w:use c_import(..., no_methods: true); flat callscounter_incr(c)work; method form is an unknown-method compile error — split the negative half intotest/compile_errors/err_c_import_no_methods_global.wwith//! expect-error:onc.incr().test/compile_errors/err_c_import_no_methods_type.w:no_methods: "Counter";c.incr()errors while a second type in the same header keeps its methods (positive half asserted in a behavior testbehav_c_import_no_methods_type.w).test/behavior/behav_c_import_no_methods_flat.w: with both opt-out forms, all flat functions remain callable (req 16.2.2.31).- Constructor sugar: with
no_methods: "Counter",Counter(...)call form errors;counter_new()works. - Cache-key regression: same header, toggled option across two imports/compiles produces different surfaces (internals test or driver test relying on
WITH_CIMPORT_CACHE_EPOCHhygiene). - Warning case:
no_methods: "Typo"emits the matches-no-type warning.
Acceptance criteria
-
no_methods: true,no_methods: "Type", andno_methods: ["A", "B"]parse; anything else under that key is a clear parse error. - Opted-out types get no auto methods and no constructor-call sugar; flat functions unaffected; other types in the same import unaffected.
- Option participates in the c_import cache key.
-
with fmtpreserves the option. -
with build && with build :fixpoint && with build :testpass.
Dependencies
- Tests: c import auto methods coverage (§16) #485 (auto-method test coverage — its positive tests are the baseline these negatives complement).
- Overlaps c_import omission manifest: source locations, reason chains, directional diagnostics (§16.2) #417/c_import: selective import and strict completeness flag (§16.2) #418 only via shared option-parsing plumbing.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 16.2.2.29, 16.2.2.30.
Activity
c_import: no_methods opt-out option (#416)
ehartford commentedon Jun 16, 2026
Implemented in 538e208. c_import now parses no_methods: true | "Type" | ["A","B"] (anything else is a parse error). Per-type names are stored as a third extras group; the NK_C_IMPORT packing gained a no_methods count + global-flag bit. The frontend applies the set before generation; ci_detect_member_functions emits no auto-methods/constructor sugar for opted-out types (or globally), while flat C functions remain. The option participates in the c_import cache key (format v12) and round-trips through with fmt. Tests: behav_c_import_no_methods_flat, behav_c_import_auto_method_baseline, err_c_import_no_methods_global, err_c_import_no_methods_type. build + fixpoint + compile-error/spec/behavior suites pass.