Skip to content

v3: keep the module of selectively imported generics in global types - #28946

Open
medvednikov wants to merge 1 commit into
masterfrom
fix/28881-selective-import-generic-global-iter
Open

medvednikov wants to merge 1 commit into
masterfrom
fix/28881-selective-import-generic-global-iter

Conversation

@medvednikov

Copy link
Copy Markdown
Member

Summary

With import foo { Foo } in module bar, a module global __global bar_value Foo[Bar] was emitted as Foo_bar__Bar instead of foo__Foo_bar__Bar, so the generated C failed with unknown type name 'Foo_bar__Bar'. Spelling the type as foo.Foo[Bar] worked. There were two gaps, and each change below is needed on its own.

  • Checker (resolve_selective_import_type_symbol): cgen reads global types after monomorphization has erased generic struct templates such as foo.Foo from structs. The selective import lookup then no longer recognized foo.Foo and fell back to the bare Foo. It now also accepts candidates that are still known as generic struct templates (struct_generic_params). Candidates are tried in the same order as before, so nothing that resolved before resolves differently. This alone fixes the reported iterator case.
  • Transform (collect_types): global types were normalized without the declaring file set, so the file's selective imports were ignored. For example, bar_by_name['a'].iter() on a map[string]Foo[Bar] global still failed with use of undeclared identifier 'Foo_bar__Bar'. They are now normalized with cur_file/cur_module set to the declaring file and module, using the same save/restore pattern as elsewhere in the transformer.

Fixes #28881

Validation

All runs use V_MACOS_V3_NO_FALLBACK=1, so a V3 failure is not hidden by the compatibility fallback.

  • New vlib/v/tests/selective_import_generic_global_28881/:

    • globals of types Foo[Bar], []Foo[Bar], map[string]Foo[Bar], a plain struct and Pair[string, Bar], all named only through a selective import;
    • iterators that yield real items, used from both modules, and a method call and field access on the plain global.

    1/1 passed; it fails on master.

  • ./v -silent vlib/v/compiler_errors_test.v: 1721 passed, 0 failed

  • ./v -silent test vlib/v/gen/c/: 27/27 passed

  • ./v -silent test vlib/v/transform/: 17/17 passed

  • vlib/v/types/*_test.v (without checker_ownership_alias_test.v, which hangs on master, V3: checker_ownership_alias_test.v never finishes compiling (-d ownership compiler spins at 100% CPU) #28923): 30/30 passed

  • ./v -silent test vlib/v/tests/generics/: 320 passed, 1 failed (generic_sort_multi_instantiation_test.v, same failure on master)

  • The 41 vlib/v/tests files that use __global or selective imports: 41/41 passed

With `import foo { Foo }`, a module global `__global g Foo[Bar]` was
emitted as `Foo_bar__Bar` instead of `foo__Foo_bar__Bar`, so the C
output failed to compile.

cgen parses global types after monomorphization has erased the generic
struct templates from `structs`, so the selective import lookup no
longer recognized `foo.Foo` and fell back to the bare name. Accept
candidates that are still known as generic struct templates.

The transformer also normalized global types without the declaring
file set, which skipped the file's selective imports and broke
expressions such as `g_map['a'].iter()`. Normalize them in the
declaring file and module.

Fixes #28881
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-24T22:00:16.440007Z a28943b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v3: C backend loses the namespace of a selectively imported generic global used by an iterator

1 participant