Skip to content

Integer generics not recognized when defining operators #82400

Open
@BruceMcRooster

Description

@BruceMcRooster

Description

No response

Reproduction

The project in which this issue arose was while playing with matrices, and I can't think of another sensible example. Here's a simplified version of a matrix implementation.

@available(macOS 16.0, *)
struct Matrix<let rows: Int, let columns: Int> {
    var data: InlineArray<rows, InlineArray<columns, Int>>
}

Let's say we're trying to implement matrix multiplication. As a reminder, a m×n matrix times a n×p matrix results in a m×p matrix. The code for this looks something like this.

@available(macOS 16.0, *)
extension Matrix {
    static func *<let sharedDimension: Int>(lhs: Matrix<rows, sharedDimension>, rhs: Matrix<sharedDimension, columns>) -> Matrix<rows, columns> {
        
        var result: InlineArray<rows, InlineArray<columns, Int>> = .init(repeating: .init(repeating: 0)).self
        
        for i in 0..<rows {
            for j in 0..<columns {
                var sum = 0
                for k in 0..<sharedDimension {
                    sum += lhs.data[i][k] * rhs.data[k][j]
                }
                result[i][j] = sum
            }
        }
        
        return Matrix(data: result)
    }
}

This code should be perfectly valid, but the compiler throws several errors stemming from the fact that it tries to interpret *< as the operator being defined in the function rather than *, causing a chain of errors regarding the rest of the statement.

Expected behavior

The expected behavior of correctly compiling can be found by inserting a single space between * and <, making the declaration

static func * <let sharedDimension: Int>(lhs: Matrix<rows, sharedDimension>, rhs: Matrix<sharedDimension, columns>) -> Matrix<rows, columns> 

However, there are two cases where adding a space is not required. If we renamed this function to a name instead of an operator, the compiler would have no issue with no space.

static func multiply<let sharedDimension: Int>(...) -> Matrix<rows, columns> 

In addition, in some similar hypothetical signature, adding a generic parameter before the integer generic makes this issue go away as well.

static func *<U: BinaryInteger, let sharedDimension: Int>(...) -> InlineArray<rows, InlineArray<columns, U>> 

Environment

swift-driver version: 1.127.4.2 Apple Swift version 6.2 (swiftlang-6.2.0.9.909 clang-1700.3.9.907)
Target: arm64-apple-macosx15.0

This is the version included with the Xcode 26 beta. I couldn't get the toolchain from Swiftly for 6.2 working because it fails to recognize InlineArrays, saying something about it only being available in macOS 9999 or higher. This is definitely just a me problem.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions