Skip to content

Improve error message for type mismatch between any P and (any P)? #60730

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

Closed
hborla opened this issue Aug 23, 2022 · 3 comments · Fixed by #61191
Closed

Improve error message for type mismatch between any P and (any P)? #60730

hborla opened this issue Aug 23, 2022 · 3 comments · Fixed by #61191
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. good first issue Good for newcomers

Comments

@hborla
Copy link
Member

hborla commented Aug 23, 2022

If you try to pass a value of type (any P)? to an argument of type any P, Swift should produce an error telling you to unwrap the optional. Instead, it tells you that (any P)? doesn’t conform to P, which is confusing, and the user probably meant to unwrap the argument anyway.

protocol P {}

struct S: P {}

func takesP(_: any P) {}

func passOptional(value: (any P)?) {
  takesP(value) // error: Argument type '(any P)?' does not conform to expected type 'P'

  takesP(value ?? S()) // okay
  takesP(value!) // okay
}

The constraint system currently applies the MissingConformance fix when solving for the takesP(value) expression. Instead, it should identify that the wrapped type of the argument matches the parameter type, and apply the ForceOptional fix.

This is also tracked by rdar://94037733

@hborla hborla added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. good first issue Good for newcomers labels Aug 23, 2022
@hborla
Copy link
Member Author

hborla commented Aug 23, 2022

cc @dfperry5 this is a good constraint system starter bug if you're interested!

@dfperry5
Copy link
Contributor

dfperry5 commented Aug 23, 2022

@hborla - I am so interested and am happy to take it on! Can you assign it to me? For some reason I can't see the "assign yourself" button.

@dfperry5
Copy link
Contributor

Opened PR here: #61191

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. good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants