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

Misleading errors when chaining correct code with incorrect code. #74617

Closed
adamwych opened this issue Jun 21, 2024 · 1 comment · Fixed by #74692
Closed

Misleading errors when chaining correct code with incorrect code. #74617

adamwych opened this issue Jun 21, 2024 · 1 comment · Fixed by #74692
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself diagnostics QoI Bug: Diagnostics Quality of Implementation expressions Feature: expressions operators Feature: operators swift 6.0 type checker Area → compiler: Semantic analysis

Comments

@adamwych
Copy link

Description

No response

Reproduction

Here's an example:

struct Foo {
    public var bar: Float {
        return 123
    }

    public static func + (lhs: Foo, rhs: Foo) -> Foo {
        return Foo()
    }
}

let a = Foo()
let b = Foo()
let c = (a + b).bar()

I make 2 instances of Foo and then add them together using the + operator. The issue here is that then, I (accidentally) called bar() when in fact, bar is just a field - and the compiler threw these 2 errors:

11 | let a = Foo()
12 | let b = Foo()
13 | let c = (a + b).bar()
   |            `- error: binary operator '+' cannot be applied to two 'Foo' operands
11 | let a = Foo()
12 | let b = Foo()
13 | let c = (a + b).bar()
   |                 `- error: value of type 'Never' has no member 'bar'

Both of those are confusing to me:

  1. The + operator is clearly defined for Foo and both a and b are Foo, so why does it complain that this operator can not be applied?
  2. Result of + is Foo, so why does it say that bar is not a member of Never?

Additionally, I had a case (I'll try to recreate it later) where the LSP was only showing the 1st error and I spent a lot of time trying to understand why it says that I can't use + when it is clearly implemented before I figured out what the real issue was.

If I make split all those calls instead of chaining them...

let a = Foo()
let b = Foo()
let c = (a + b)
let d = c.bar()

... then I get a more appropriate error:

12 | let b = Foo()
13 | let c = (a + b)
14 | let d = c.bar()
   |           `- error: cannot call value of non-function type 'Float'

Expected behavior

The compiler should not complain about not being able to apply the + operator, but only about the fact that bar is not a function.

Environment

Apple Swift version 6.0-dev (LLVM de395d39a90ed7a, Swift 490cf64)
Target: arm64-apple-macosx14.0

Additional information

No response

@adamwych adamwych added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jun 21, 2024
@hamishknight hamishknight added compiler The Swift compiler itself diagnostics QoI Bug: Diagnostics Quality of Implementation and removed triage needed This issue needs more specific labels labels Jun 24, 2024
@hamishknight
Copy link
Contributor

@gregomni you may be interested in this

@AnthonyLatsis AnthonyLatsis added type checker Area → compiler: Semantic analysis operators Feature: operators expressions Feature: expressions swift 6.0 labels Jun 24, 2024
@gregomni gregomni self-assigned this Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself diagnostics QoI Bug: Diagnostics Quality of Implementation expressions Feature: expressions operators Feature: operators swift 6.0 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants