Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-15053] Improve diagnostics on type errors involving arithmetic expressions #57380

Closed
mattyoung opened this issue Aug 10, 2021 · 2 comments
Closed

Comments

@mattyoung
Copy link

@mattyoung mattyoung commented Aug 10, 2021

Previous ID SR-15053
Radar rdar://problem/81774103
Original Reporter @mattyoung
Type Improvement
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, DiagnosticsQoI, TypeChecker
Assignee @LucianoPAlmeida
Priority Medium

md5: d9ae95cc29673bbed4806ff8fa1380ca

Issue Description:

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'

see: https://forums.swift.org/t/unhelpful-inconsistent-compiler-error/51092

@typesanitizer
Copy link

@typesanitizer typesanitizer commented Aug 11, 2021

@swift-ci create

@LucianoPAlmeida
Copy link
Collaborator

@LucianoPAlmeida LucianoPAlmeida commented Oct 12, 2021

Fixed on main by #39220
@mattyoung Can you please verify on next available snapshot and close? Thanks =]

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants