Skip to content

Conversation

@lilkeet
Copy link
Contributor

@lilkeet lilkeet commented Nov 10, 2025

Solves issue #682.
The & proc was used before the std/strformat module was imported.
The test suite missed this because it imported the proc itself.

@gemini-code-assist
Copy link

Summary of Changes

Hello @lilkeet, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a subtle dependency bug where the & string formatting operator was used in the special_matrices.nim module without its originating std/strformat module being explicitly imported. The problem was previously unnoticed because the test suite's own imports provided the necessary dependency. The changes introduce the correct import in the affected module and remove the now-redundant imports from the test files, ensuring proper module dependencies and preventing similar issues in the future.

Highlights

  • Dependency Resolution: The & operator, used for string formatting, was being utilized in src/arraymancer/linear_algebra/special_matrices.nim without an explicit import of the std/strformat module, leading to a hidden dependency.
  • Test Suite Correction: The issue was previously masked because the test files (tests/linear_algebra/test_linear_algebra.nim and tests/linear_algebra/test_special_matrices.nim) inadvertently imported std/strformat, making the & operator available globally during testing. These redundant imports have now been removed from the test files.
  • Explicit Operator Binding: The std/strformat module is now explicitly imported in src/arraymancer/linear_algebra/special_matrices.nim. Additionally, bind & statements have been added to the diagonal, set_diagonal, and tri procedures within this file to explicitly bind the & operator to their scope, ensuring its availability and clarity.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly fixes a bug where std/strformat was used without being imported. The changes to the test files to remove the now-unnecessary imports are also appropriate. My review includes suggestions to improve the maintainability of the fix by centralizing the bind &`` statement to avoid code duplication across multiple procedures.

import ../tensor
import ./helpers/triangular
import std / [sequtils, bitops]
import std / [sequtils, bitops, strformat]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To avoid repeating bind &`` in the diagonal, `set_diagonal`, and `tri` procedures, you can declare it once at the module level here. This improves maintainability by removing code duplication. The `bind` statement at the module level will ensure the `&` operator is available for all procedures in this file, including within generics.

import std / [sequtils, bitops, strformat]
bind `&`

## - anti: If true, get the k-th "anti-diagonal" instead of the k-th regular diagonal.
## Result:
## - A copy of the diagonal elements as a rank-1 tensor
bind `&`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

With bind &`` declared at the module scope (as suggested in another comment), this redundant declaration can be removed.

## - k: The index k of the diagonal that will be changed. The default is 0 (i.e. the main diagonal).
## Use k>0 for diagonals above the main diagonal, and k<0 for diagonals below the main diagonal.
## - anti: If true, set the k-th "anti-diagonal" instead of the k-th regular diagonal.
bind `&`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This bind statement is also redundant if it's declared once at the module level. It can be removed to improve code clarity and reduce duplication.

## diagonal. The default is false.
## Result:
## - The constructed, rank-2 triangular tensor.
bind `&`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This is another duplicated bind statement that can be removed by declaring it once at the module scope.

@mratsim
Copy link
Owner

mratsim commented Nov 10, 2025

The issue is that this will happen for every single generic proc, and all procs are genetic on Tensor[T] in the library. It's a Nim limitation that is documented in strformat though it only mentions template expansions, generics are the same: https://nim-lang.org/docs/strformat.html#limitations

@mratsim
Copy link
Owner

mratsim commented Nov 10, 2025

Ah but here it's internally that I use strformat.

@mratsim mratsim merged commit bb73497 into mratsim:master Nov 10, 2025
0 of 6 checks passed
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.

2 participants