Skip to content

Fix distinct borrow handling for bracket operators and restrict type-level borrow pragma to .#9

Merged
Clemente90 merged 3 commits intodevelfrom
codex/fix-borrow-behavior-for-distinct-types-uuhdfn
Jan 19, 2026
Merged

Fix distinct borrow handling for bracket operators and restrict type-level borrow pragma to .#9
Clemente90 merged 3 commits intodevelfrom
codex/fix-borrow-behavior-for-distinct-types-uuhdfn

Conversation

@Clemente90
Copy link
Owner

Motivation

  • Recent changes allowed [] in the distinct type-level {.borrow.} pragma and introduced regressions in bracket-operator borrowing for builtin-like containers; the language manual limits type-level borrow to dot only and bracket operators must be borrowed via procs.
  • The compiler must correctly resolve proc-level {.borrow.} for magic bracket ops (array/seq/openArray/etc.) across multiple distinct types, generic instantiations and block scopes without creating global ambiguities.

Description

  • Restrict type-level borrow pragma to accept only . and emit a clear error for attempts to use [] in type declarations by updating compiler/pragmas.nim.
  • Stop treating borrowed bracket operators as a type-level flag by removing/controlling tfBorrowBrackets propagation and ensure []. borrows are handled via proc-level logic in compiler/semstmts.nim, compiler/semexprs.nim and compiler/semcall.nim.
  • Make overload/borrow resolution robust for magic bracket operators by skipping return-type checks for bracket-named borrows and preventing the automatic magic-match path from producing ambiguities for borrowed procs via changes in compiler/semcall.nim and compiler/sigmatch.nim.
  • Preserve correct handling for generic/instantiated distinct parameter types when marking borrowed [] proc parameters, including tyGenericInvocation cases, via compiler/semstmts.nim adjustments.
  • Update AST and encoding helpers so flags remain consistent (compiler/astdef.nim, compiler/ic/enum2nif.nim) and rework the semantic subscript handling to respect borrow flags in compiler/semexprs.nim.
  • Tests: remove the now-invalid type-pragma test tests/distinct/tborrow_type_pragma_brackets.nim, add tests/distinct/tborrow_seq_brackets.nim, and update several tests/distinct/* files to use proc-level {.borrow.} for brackets and exercise multiple distinct types, generics and block-scope cases.

Testing

  • Rebuilt the compiler and tools with ./bin/nim c ... koch and ./koch boot and the build succeeded.
  • Ran modified/added test cases individually with the new compiler: ./bin/nim r tests/distinct/tborrow_array_brackets.nim, ./bin/nim r tests/distinct/tborrow_negative_generics_and_blockscope.nim, and ./bin/nim r tests/distinct/tborrow_seq_brackets.nim, all of which executed successfully.
  • Ran the invalid/negative check ./bin/nim check --hints:off --warnings:off tests/distinct/tinvalidborrow.nim and it produced the expected reject diagnostics.
  • Overall: updated tests now pass and the reject-case emits the expected error messages.

Codex Task

@Clemente90 Clemente90 merged commit ee0a6e5 into devel Jan 19, 2026
@Clemente90 Clemente90 deleted the codex/fix-borrow-behavior-for-distinct-types-uuhdfn branch January 19, 2026 22:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant