Unhelpful/inconsistent compiler error

For the following test, I wish the compiler error say the actual problem:

"Cannot apply / on or between Numeric"

func fooAAA<T : Numeric>(_ a: T, _ b: T) -> T {
    // this results in two compile errors, first is correct, second should not be:
    // Binary operator '/' cannot be applied to operands of type 'T' and 'Int'    <=== this error is correct
    // Binary operator '+' cannot be applied to two 'T' operands    <=== !!! but this should not be an error!!!
    (a + b) / 2
}

// this version returns void, now the same expression give different compiler error
func fooDDD<T : Numeric>(_ a: T, _ b: T) {
    // same expression but give different error:
    // Cannot convert value of type 'T' to expected argument type 'Int'
    // Replace '(a + b)' with 'Int((a + b))'
    // so the compiler give totally different diagnostic for the same expression as above
    (a + b) / 2
}

Breaking the expression up produce better error:

    let sum = a + b
    return sum / 2  // Binary operator '/' cannot be applied to operands of type 'T' and 'Int'

Could you file a bug in https://bugs.swift.org ?
cc: @xedin @hborla

https://bugs.swift.org/browse/SR-15053

1 Like
Terms of Service

Privacy Policy

Cookie Policy