Skip to content

fix(sema): declaration based type identity for structs, enums and generic instances - #3758

Merged
BaseMax merged 4 commits into
mainfrom
fix/sema-struct-identity
Nov 21, 2026
Merged

BaseMax merged 4 commits into
mainfrom
fix/sema-struct-identity

Conversation

@BaseMax

@BaseMax BaseMax commented Nov 21, 2026

Copy link
Copy Markdown
Member

type_equiv fell back to comparing mangled names, so a local struct a_Item unified with the imported a.Item (check passed, exec failed with a missing field, clang and LLVM failed differently). Generic instance keys used bare type names, so a.Meters and b.Meters collided.

Types now record the declaration they came from (file, offset, name) and type_equiv compares that; the name fallback is gone. Qualified type names use the pkg.Name form, with lookups for interfaces, LLVM and the interpreter updated to match, and generic instance keys include the full identity of every argument. A duplicate alias typedef in the C output for imported aliases is fixed too.

Tested with a main-built compiler: check, run, exec, js, llvm and native on the reported repros; 141 related error tests via check; 80 related feature runs on run and exec; check compiler/main.salam is clean and the compiler bootstraps through stage 2. New tests: tests/en/features/struct_pkg_name_identity and tests/en/errors/struct_pkg_name_identity.

@BaseMax
BaseMax requested a review from a team as a code owner November 21, 2026 12:20
@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 22 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8f42252f-526d-4e5f-af9d-863986b4813e
Commits

Reviewing files that changed from the base of the PR and between d067b2e and 171bc31.

Files ignored due to path filters: 1

  • tests/en/features/struct_pkg_name_identity.out is excluded by !**/*.out
Files selected for processing: 19

  • compiler/codegen/codegen_header.salam
  • compiler/interp/interp_core.salam
  • compiler/jsgen/jsgen_call.salam
  • compiler/llvmgen/llvm_type.salam
  • compiler/semantic/sema_decl.salam
  • compiler/semantic/sema_generic.salam
  • compiler/semantic/sema_layout.salam
  • compiler/semantic/sema_lit.salam
  • compiler/semantic/sema_typeres.salam
  • compiler/semantic/sema_types.salam
  • compiler/semantic/sema_util.salam
  • compiler/semantic/semantic.salam
  • tests/en/errors/_hidden_pkg.salam
  • tests/en/errors/_type_identity_pkg.salam
  • tests/en/errors/pkg_private_struct_qualified.salam
  • tests/en/errors/struct_pkg_name_identity.salam
  • tests/en/features/_type_identity_pkg.salam
  • tests/en/features/_type_identity_pkg2.salam
  • tests/en/features/struct_pkg_name_identity.salam
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

Copy link
Copy Markdown
Contributor
sha=a08b52a3f48d626a7027a9528b6b399e2a70bca7

🤖 Salam AI review

Verdict: 🛠️ Changes needed before merging

commit a08b52a · 18 files · +324 −36 · model salam

Summary

This PR fixes type identity in semantic analysis by replacing name-based struct/enum/dyn equivalence with declaration-origin identity and by making generic instance keys include full argument identity. The core direction is correct and the added tests cover the reported name-collision and generic-argument-collision cases, but the qualified-name handling introduces a lookup bug for dotted package names in LLVM and a possible visibility gap in the new semantic fallback.

What changed

This PR changes type identity so struct, enum, and dyn types are no longer considered equivalent just because their mangled names match. Type now carries an origin, type_equiv compares declaration/origin identity (with generic struct instances matched by instance identity), and generic instantiation keys include g_type_key over argument types. Qualified type names move to the pkg.Name form, with matching updates in codegen, interpreter, JS, LLVM, type resolution, and struct literal resolution. It also fixes alias typedef emission to use the qualified/canonical name, and adds feature/error tests for local-vs-imported same-named structs and for generic/distinct type collisions.

Review

  • compiler/llvmgen/llvm_type.salam:447-453: sym_qualified splits on the first . (idx_byte(name, 46)) and treats everything after it as the base symbol name. For dotted package names, e.g. encoding.json.Decoder produced by QualTypeName, this gives json.Decoder, so the lookup will fail to validate and may not resolve the symbol. Since the PR moves qualified names to pkg.Name, can this be changed to split at the last package/name boundary or otherwise resolve the full dotted package prefix?
  • compiler/semantic/sema_util.salam:624-645, compiler/semantic/sema_typeres.salam:205-215, compiler/semantic/sema_lit.salam:150-155: sym_by_qual_name scans package members and returns matching struct/enum/interface symbols without checking is_pub. The new fallbacks in type resolution and struct literals can therefore resolve private types through a qualified name. Is that intended? If normal member visibility rules apply, this fallback should enforce the same visibility check.
  • compiler/semantic/sema_types.salam:324-329: Removing the name fallback is the right fix for the reported collision. The new rule depends on origin being present for real declarations; synthetic struct/enum/dyn types with empty origins and different decl pointers will no longer be equivalent. That seems safer, but it is worth confirming there are no compiler-generated synthetic type declarations that relied on name equivalence.
  • compiler/codegen/codegen_header.salam:887: Emitting aliases as cg_cident(QualTypeName(g.pkg, d.name)) addresses the duplicate imported-alias typedef mentioned in the PR. I could not verify from the diff alone that every other alias-emission path uses the same canonical qualified name, so it would be worth confirming there are no remaining unqualified alias typedef sites.

Tests

The new tests are well targeted: tests/en/errors/struct_pkg_name_identity.salam covers the local tidpkg_Item vs imported tidpkg.Item mismatch, and tests/en/features/struct_pkg_name_identity.salam covers value identity, generic Box<T>/Show<T> instantiation, and same-named distinct Meters types from different packages. I did not run a build or tests. Consider adding coverage for a dotted-package qualified type path, since that exercises the new pkg.Name form in lookups/codegen backends.

Risks

The main risk is incomplete migration from underscore-mangled qualified names to dotted pkg.Name names. The LLVM first-dot split is a concrete example; there may be other string-splitting or prefix assumptions outside the touched files. There is also a behavioral risk if the new qualified-name fallback bypasses visibility rules for private package types.


⚠️ This review was written automatically by an AI and may contain mistakes. It is a helper for maintainers, not a replacement for a human review: check the code yourself before merging.

@BaseMax

BaseMax commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Checked with real programs against the main-built baseline.

  1. Dotted packages: a program using encoding.json.JSONEncoder as a struct field and net.http.Response in a Vector and a HashMap prints the same result on run, exec, --backend=llvm and --backend=native, and matches the baseline compiler. sym_qualified only accepts the first-dot candidate when the resolved symbol's own type string equals the full name, so a nested name cannot resolve to a different symbol.

  2. Visibility: hidpkg.Hidden {} (private struct), parameters of type pkg.Hidden and pkg.Secret (private enum) give the same E017 as the baseline compiler; the new fallbacks only run when the package symbol is missing (per-module analysis), never for a name the user can write. Added tests/en/errors/pkg_private_struct_qualified.

  3. The compiler bootstraps through stage 2 and check compiler/main.salam is clean, so nothing generated relies on the removed name equivalence. Alias typedefs go through one emission site (hdr_aliases) and enum typedefs use the canonical name helper.

@BaseMax
BaseMax merged commit 6d60cea into main Nov 21, 2026
23 checks passed
@BaseMax
BaseMax deleted the fix/sema-struct-identity branch November 21, 2026 14:11
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.

1 participant