Fix Rust codegen matrix return types - #467
Closed
tandede wants to merge 1 commit into
Closed
Conversation
Rust codegen flattened every directly returned matrix into an SVector, even when both dimensions were greater than one. Reuse the existing shape-aware type formatter for both the return signature and constructor so single-output code follows the same rules as output arguments. Add a non-square Matrix23 regression to both symbolic backends and compile the generated Rust crate. This preserves vector returns while proving that true matrices retain their row and column dimensions. Signed-off-by: tandede <1090179959@qq.com>
aaron-skydio
approved these changes
Aug 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Rust codegen formats every matrix returned directly from a generated function as
nalgebra::SVector<scalar, rows * cols>. This loses the original two-dimensional shape. For example, a function returningMatrix23is currently generated with anSVector<f64, 6>signature and constructor.The multi-output path already handles this correctly through
format_typename, which selectsSVectoronly when either matrix dimension is one and otherwise selectsSMatrix.Approach
Reuse the same shape-aware formatter in both places used by the single-return path:
This keeps the single-return and multi-output paths consistent without duplicating their vector-versus-matrix condition. Scalar returns and vector-shaped matrices retain their existing behavior.
Regression coverage
The Rust codegen test now includes a non-square
2×3matrix return. A non-square shape verifies both dimensions explicitly instead of checking only the flattened element count.Before this change, the generated code used:
It now uses:
The generated crate still includes the existing
3×1return case, which remains anSVector<f64, 3>.Validation
git diff --checkFixes #439