Repository: dotnet/runtime
File: src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorShape.cs (C#)

Code before:
            {
                int rankIndex = lengths.Length - (i + 1);
                int destinationRankIndex = destinationShape.Lengths.Length - (i + 1);

                nint length = lengths[rankIndex];
                nint stride = strides[rankIndex];

                nint index = --indexes[destinationRankIndex];
                linearOffset -= stride;

                // We can have a scenario such as lengths: [1], destinationLengths: [2] in
                // which case we still need to keep incrementing the index but without
                // adjusting the linearOffset

                if (index >= 0)//destinationLengths[destinationRankIndex])
                {
                    if (index >= length)
                    {
                        // We should only be here if we were broadcast
                        Debug.Assert((length == 1) && (stride == 0));
                    }
                    return linearOffset;
                }

                indexes[destinationRankIndex] = destinationLengths[destinationRankIndex] - 1;
                linearOffset += (stride * length);
            }

            if (indexes.Length != Rank)
            {
                for (int i = destinationLengths.Length - 1; i >= strides.Length; i--)
                {
                    int rankIndex = destinationLengths.Length - (i + 1);

                    nint length = destinationLengths[rankIndex];
                    // Strides are always 0 because we are broadcasting at this point in the loop.

                    nint index = ++indexes[rankIndex];

                    if (index < length)
                    {
                        // For any indexes that exist in the destinationShape but not
                        // in the srcShape we will only increment them if all lower
                        // indexes were 0. This means we're starting over at the beginning
                        // of the srcShape and the linearOffset must be 0.
                        break;
                    }

                    indexes[rankIndex] = 0;
                }
            }

Review comment (suggestion):
`indexes[...]` is now reset using `destinationLengths[...]`, but the offset correction still uses `length` (from the source shape). If `AdjustToPreviousIndex` is used in scenarios where `destinationShape` differs from the current shape (the method signature suggests it can be), this can desynchronize `indexes` and `linearOffset` on wrap. Consider using the same basis for both (e.g., apply `destinationLengths[destinationRankIndex]` in the offset adjustment as well), or add an in-code comment explaining why `length` must remain the source length here.

Code after:
            {
                int rankIndex = lengths.Length - (i + 1);
                int destinationRankIndex = destinationShape.Lengths.Length - (i + 1);

                nint length = lengths[rankIndex];
                nint stride = strides[rankIndex];

                nint index = --indexes[destinationRankIndex];
                linearOffset -= stride;

                // We can have a scenario such as lengths: [1], destinationLengths: [2] in
                // which case we still need to keep incrementing the index but without
                // adjusting the linearOffset

                if (index >= 0)
                {
                    if (index >= length)
                    {
                        // We should only be here if we were broadcast
                        Debug.Assert((length == 1) && (stride == 0));
                    }
                    return linearOffset;
                }

                indexes[destinationRankIndex] = destinationLengths[destinationRankIndex] - 1;
                linearOffset += (stride * length);
            }

            if (indexes.Length != Rank)
            {
                for (int i = destinationLengths.Length - 1; i >= strides.Length; i--)
                {
                    int rankIndex = destinationLengths.Length - (i + 1);

                    nint length = destinationLengths[rankIndex];
                    // Strides are always 0 because we are broadcasting at this point in the loop.

                    nint index = ++indexes[rankIndex];

                    if (index < length)
                    {
                        // For any indexes that exist in the destinationShape but not
                        // in the srcShape we will only increment them if all lower
                        // indexes were 0. This means we're starting over at the beginning
                        // of the srcShape and the linearOffset must be 0.
                        break;
                    }

                    indexes[rankIndex] = 0;
                }
            }