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

SwiftUI: Failed to produce diagnostic for expression when using keyword 'open' in some contexts #61039

Open
j-war opened this issue Sep 11, 2022 · 14 comments
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 failed to produce diagnostic Bug → internal error: Failed to produce diagnostic for expression good first issue Good for newcomers swift 5.9 type checker Area → compiler: Semantic analysis

Comments

@j-war
Copy link

j-war commented Sep 11, 2022

Describe the bug
Xcode/Swift/VSCode fail to notice that the variable has not been defined in some contexts.

Steps To Reproduce

  1. Attempt to use an undefined variable in the following context where the name is also a keyword.
  2. See following code that fails to compile and fails to provide a useful reason why.

Expected behavior
A notice that the variable was not defined rather than "failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project sourcekitd".
Or, a notice that the use of 'open' is incorrect as it's a keyword.

Screenshots
N/A

Environment (please fill out the following information)

  • OS: macOS 13 beta 7
  • Xcode Version/Tag/Branch: 14.0 RC or VS Code 1.71.0 with Swift extension 0.8.1
import SwiftUI
struct MyView: View {
   // var open = true
   var body: some View {
       Button {
           // no action
       } label: {
           Image(systemName: "plus")
               .rotationEffect(.degrees(open ? 45 : 0)) // Using keyword 'open'
       }
   }
}

Additional context
Placing the 'open ? 45 : 0' inside of another context, say a print(), correctly identifies the issue of an undefined variable.

@j-war j-war added the bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. label Sep 11, 2022
@xedin xedin added type checker Area → compiler: Semantic analysis diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers labels Sep 11, 2022
@xedin
Copy link
Contributor

xedin commented Sep 11, 2022

No ambiguity logic for [fix: ignore specified contextual type] @ locator [If@<stdin>:9:46 -> condition expression]

@AnthonyLatsis AnthonyLatsis added the compiler The Swift compiler itself label Dec 13, 2022
@AnthonyLatsis AnthonyLatsis added failed to produce diagnostic Bug → internal error: Failed to produce diagnostic for expression SwiftUI labels Dec 22, 2022
@dfperry5
Copy link
Contributor

@xedin / @AnthonyLatsis - is this still a problem? If so, is it open for me to take a shot at?

@j-war
Copy link
Author

j-war commented Jun 15, 2023

Yes. Xcode 15 beta 1,
Screenshot 2023-06-14 at 7 36 34 PM

@xedin
Copy link
Contributor

xedin commented Jun 15, 2023

Please feel free to take it, I don’t think anybody is working on this at the moment.

@AnthonyLatsis
Copy link
Collaborator

@dfperry5 Issues that are not assigned to anyone and show no signs of recent activity are yours for the taking. In other cases, the etiquette is to first ask the assignee or last person to have claimed it whether they’re (still) working on it. Note that an assignee need not imply that the person ever worked on the issue: some contributors like myself use assignments to simply bookmark issues.


Please let us know if you want to be assigned to this.

@dfperry5
Copy link
Contributor

@AnthonyLatsis - Please assign this one to me! Sorry for the delay, took a vacation and we're just getting back :)

@AnthonyLatsis
Copy link
Collaborator

A more illuminating reproduction:

func `open`(_: Int) {}
func `open`(_: Bool) {}

func degrees(_: Int) {}
do {
  degrees(open)
}

And a reduced version of the original example:

import SwiftUI
struct MyView: View {
  var body: some View {
    Color.black.rotationEffect(.degrees(open))
  }
}

@dfperry5
Copy link
Contributor

@AnthonyLatsis - been taking a peak at this and am feeling a little stuck. I see where it calls " DE.diagnose(expr->getLoc(), diag::type_of_expression_is_ambiguous)
.highlight(expr->getSourceRange());" in ConstraintSystem.cpp, but I can't figure out a way to have it check if there are any keywords in that expression.

Any thoughts or suggestions would be much appreciated :)

@AnthonyLatsis
Copy link
Collaborator

Hi Dylan,

The overloaded name is irrelevant, you can try foo instead of open to confirm. The problem is that we are not properly diagnosing an ambiguity when neither of several overloads have a type that is compatible with the contextual, parameter type (Pavel left a hint here). A specific ConstraintFix gets recorded for this kind of type mismatch in the constraint system; from what I understand, this ConstraintFix subclass is lacking a custom override of the diagnoseForAmbiguity method.

@dfperry5
Copy link
Contributor

dfperry5 commented Nov 2, 2023

@AnthonyLatsis / @xedin - sorry to bug ya'll (and for the radio silence for a while). But, I am struggling with this issue still. In my debugging I am seeing

"[fix: allow argument to parameter type conversion mismatch]" which leads me to believe we are trying to apply the AllowArgumentMismatch subclass of ContextualMismatch. This type did not have a diagnoseForAmbiguity override. I created the function, AllowArgumentMismatch::diagnoseForAmbiguity, and can see it being called.

I'm fuzzy on the logic that should be applied within that function though. Will continue playing around - but if you get time - some guidance would be appreciated! :)

@xedin
Copy link
Contributor

xedin commented Nov 3, 2023

I don't think we should not add diagnoseForAmbiguity to AllowArgumentMismatch because it's intended only for situations where it's exactly the same fix but in different context. This is not the case for AllowArgumentMismatch because each fix denotes a different overload's parameter type (for the same argument type) which means we cannot diagnose that as a single error message, it was to be an ambiguity diagnostic that is produced by diagnoseAmbiguityWithFixes.

@dfperry5
Copy link
Contributor

dfperry5 commented Nov 5, 2023

@xedin just to make sure I'm following - the thought here is that diagnoseAmbiguityWithFixes should be what produces the appropriate error message, not an override specific to AllowArgumentMismatch:: diagnoseForAmbiguity?

@xedin
Copy link
Contributor

xedin commented Nov 6, 2023

Yes, that is correct.

@dfperry5
Copy link
Contributor

I got to admit I am pretty stuck here - I can see the it flowing through diagnoseAmbiguityWithFixes - but I'm not sure how to catch the specific error here.

@hborla hborla removed the SwiftUI label Jul 14, 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 failed to produce diagnostic Bug → internal error: Failed to produce diagnostic for expression good first issue Good for newcomers swift 5.9 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

5 participants