Skip to content

[ConstraintSystem] Fix a misleading argument mismatch error when using wrapped parameters. #39146

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

Merged
merged 2 commits into from
Sep 3, 2021

Conversation

hborla
Copy link
Member

@hborla hborla commented Sep 2, 2021

When calling a function with a wrapped parameter, the solver will produce a misleading error message if the argument type isn't convertible to the parameter type:

import SwiftUI

func messageView(@Binding message: String) -> some View { // error: Property type 'Binding<String>' does not match 'wrappedValue' type 'String'
  EmptyView()
}

struct S {
  @Binding var message: String

  func test() {
    messageView(message: $message) // the actual error is that Binding<String> was passed here
  }
}

This happens because when type checking the function call, the solver will generate constraints for the property wrapper attribute chain, which will equate each wrapped-value type of a wrapper attribute with the next property wrapper attribute type (or the property type itself, if it's the innermost wrapper attribute).

For regular function parameters, these constraints were already generated and solved for when type checking the parameter declaration anyway, and they don't need to be repeated for function calls. This only needs to be done for closure parameters, so the fix is to only call generateWrappedPropertyTypeConstraints when resolving closures.

Resolves: SR-15030

@hborla hborla requested a review from xedin September 2, 2021 20:45
parameter attributes for closure parameters.

For regular function parameters, these constraints will have already been
generated and solved when computing the backing property wrapper type at
the function declaration. If the solver also generates these constraints
when type checking a function call, it leads to misleading diagnostics
about an argument mismatch that will appear at the function declaration
instead of the call-site.
@hborla hborla force-pushed the wrapped-param-diagnostics branch from 98fc7f1 to 947a6c3 Compare September 3, 2021 00:37
@hborla
Copy link
Member Author

hborla commented Sep 3, 2021

@swift-ci please smoke test

@hborla hborla merged commit 09f2ca7 into swiftlang:main Sep 3, 2021
@hborla hborla deleted the wrapped-param-diagnostics branch September 3, 2021 05:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants