Skip to content

c_import: selective import and strict completeness flag (§16.2) #418

Closed

Description

@ehartford

Summary

Implement the remaining c_import completeness controls: explicit selective imports must fail when they name inexpressible symbols, and a strict-completeness mode must turn any omission into a non-zero import failure. The importer already records omitted/untranslated symbols and supports allow_untranslated, but the user cannot request a strict complete import or select a specific symbol from c_import.

Spec

§16.2 reserves whole-import non-zero failure for selective imports, strict completeness, and real import failures. The interop rule is the same as the repository's no-silent-fallback policy: an importer that cannot produce correct bindings must fail loudly when the user asked for that symbol or for strict completeness.

use c_import("lib.h", only: ["needed_fn"], strict: true)

Current state

Parser.parse_c_import accepts link: and allow_untranslated: only; unknown options emit unknown c_import option (src/Parser.w:2272, src/Parser.w:2285, src/Parser.w:2301, src/Parser.w:2327). It does not parse only, exclude, or strict.

The frontend can emit untranslated diagnostics (src/compiler/Frontend.w:464) and checks allow_untranslated names (src/compiler/Frontend.w:473, src/compiler/Frontend.w:486). It records omitted symbols from generated manifest comments (src/compiler/Frontend.w:419). Ordinary use Foo.{Bar} selector handling exists for local type/enum selectors (src/Parser.w:2187, src/SemaDecl.w:124, src/compiler/Frontend.w:1771), but that is not wired to c_import.

Implementation plan

  1. Extend NK_C_IMPORT option storage in the parser to include only and strict while preserving existing link and allow_untranslated packing. If the packed count field is too small, replace it with an extra-record layout.
  2. Define exact syntax in parser tests. Recommended: only: ["foo", "Bar"], exclude: ["..."] optional later, and strict: true.
  3. Thread selected symbol names into Zcu.expand_c_imports_frontend. After synthetic source generation and omission recording, filter declarations to the selected set.
  4. If only names a symbol that was omitted or untranslated, emit a primary diagnostic at the c_import declaration and return non-zero from check/build. The diagnostic should include the symbol name and omission reason when available.
  5. If strict: true, fail on the first omitted symbol not listed in allow_untranslated, using the existing c_import_emit_untranslated_error_frontend style.
  6. Keep non-strict whole imports permissive for omitted unsupported declarations, but never generate stubs or extern fallbacks for bodies that failed translation.
  7. Include selector names and strict mode in the c_import cache key (src/compiler/Frontend.w:352) so cached non-strict output cannot satisfy strict/selective imports.

Test plan

Add parser tests for c_import("x.h", only: ["foo"], strict: true) and malformed option values.

Add test/wave*/cases/c_import_selective_ok.w with a header containing multiple declarations and asserting only requested names are visible.

Add test/compile_errors/err_c_import_selective_missing_symbol.w with //! expect-error: c_import: requested symbol.

Add test/compile_errors/err_c_import_strict_omitted_symbol.w using a deliberately inexpressible macro/declaration and strict: true.

Add a cache regression test showing strict and non-strict imports of the same header do not share incompatible cached output.

Acceptance criteria

  • c_import accepts a documented selective import option and strict-completeness flag.
  • Explicitly requested but inexpressible symbols fail loudly.
  • strict: true turns unallowed omissions into non-zero import failure.
  • Existing allow_untranslated behavior still acknowledges known omissions.
  • with build && with build :fixpoint && with build :test pass.

Dependencies

None.

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 16.2.1.24.

Activity

  1. ehartford commented on Jun 17, 2026

    @ehartford
    ContributorAuthor

    Implemented in 0307fdc.

    Parser accepts only: [...] and strict: true, stored in a selective-import record appended to the NK_C_IMPORT extra array (the packed d2 counts field was full) and folded into the c_import cache key.

    Frontend (expand_c_imports_frontend):

    • only — keeps just the requested symbols (and their auto-methods) plus the foundational c_* primitive typedefs they resolve through; a requested symbol the header doesn't provide fails loudly (c_import: requested symbol '…').
    • strict — any omission in the synthetic manifest not acknowledged via allow_untranslated turns the whole import non-zero (c_import: strict import omitted '…').

    Acceptance criteria

    • c_import accepts a selective-import option and strict flag
    • explicitly requested but inexpressible symbols fail loudly
    • strict: true turns unallowed omissions into non-zero failure
    • existing allow_untranslated behavior preserved (full suite green)
    • build / fixpoint / tests pass (native-compile-error 601, native-spec 172, behavior 690, fixpoint holds)

    Tests: spec_ss16_2_c_import_selective, err_c_import_selective_missing_symbol, err_c_import_selective_excludes_others, err_c_import_strict_omitted_symbol.

    Note: a requested type's non-primitive dependencies must themselves be listed in only (only c_* scaffolding is auto-kept); a full transitive-dependency closure is left as a follow-up refinement.

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