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-11104] Slightly misleading diagnostics for contextual failures with multiple fixes #53499

Closed
hamishknight opened this issue Jul 11, 2019 · 4 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis

Comments

@hamishknight
Copy link
Collaborator

Previous ID SR-11104
Radar rdar://problem/55572849
Original Reporter @hamishknight
Type Bug
Status Resolved
Resolution Done
Environment

Swift version 5.1-dev (LLVM 200186e28b, Swift 3954a4a)
Target: x86_64-apple-darwin18.5.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, TypeChecker
Assignee @hamishknight
Priority Medium

md5: cadd30745722d9225de2ee4510bba974

Issue Description:

For the following code:

func bar(_: Int) {}
bar(["hello"].first)

We emit these diagnostics:

error: value of optional type 'Int?' must be unwrapped to a value of type 'Int'
bar(["hello"].first)
              ^
note: coalesce using '??' to provide a default when the optional value contains 'nil'
bar(["hello"].first)
              ^
                    ?? <#default value#>
note: force-unwrap using '!' to abort execution if the optional value contains 'nil'
bar(["hello"].first)
              ^
                   !
error: cannot convert value of type 'String' to expected element type 'Int'
bar(["hello"].first)
     ^

While the second error is correct, the first error is a bit misleading as there's no Int? expression written in the user's code. This is happening because after applying the collection element mismatch fix, we go onto assume that the array is an [Int]. While this is important in allowing us to end up with a fully solved constraint system, we should find a better way to present these diagnostics to the user.

@belkadan
Copy link
Contributor

cc @xedin

@xedin
Copy link
Member

xedin commented Sep 20, 2019

@swift-ci create

@xedin
Copy link
Member

xedin commented Sep 20, 2019

After argument-to-parameter mismatches landed example from the description produces correct diagnostic - `error: cannot convert value of type 'String?' to expected argument type 'Int'` but the problem remains because we'd still find a solution with two fixes (unwrap + invalid element type) which rank the same as argument mismatch, which we need tot address.

The best solution here would be to increase impact of the original unwrap fix if subsequent solving requires more fixes to the element.

@xedin
Copy link
Member

xedin commented Nov 20, 2019

Fixed by #28370. I have also added a regression test for this to the suite.

@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
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants