swift icon indicating copy to clipboard operation
swift copied to clipboard

Fix Bad diagnostic for `any P!`

Open iMostfa opened this issue 1 year ago • 7 comments

Hello Team, In This PR tries to solve an issue where we get two errors emitted for:

protocol P {}

// error: using '!' is not allowed here; perhaps '?' was intended? [implicitly_unwrapped_optional_in_illegal_position]
// error: optional 'any' type must be written '(any P2)?' [incorrect_optional_any]
let _: any P!

the first error here isn't useful, so we should only emit a single error.

i fixed it by exclude ExistentialConstraint from resolveImplicitlyUnwrappedOptionalType thus, we don't get the un-needed diagnostic emitted.

i tried to work on a fix without touching resolveImplicitlyUnwrappedOptionalType and instead do something in:

TypeResolver::resolveExistentialType but it didn't work, as at that point we already get a diagnostics emitted from resolveImplicitlyUnwrappedOptionalType

after applying my fix we get only one error emitted

image

Please let me know what do you think about this proposed solution!

Resolves #72662

iMostfa avatar Jun 16 '24 16:06 iMostfa

Hi @AnthonyLatsis would really appreciate your review & feedback here

iMostfa avatar Jun 18 '24 08:06 iMostfa

Thanks @AnthonyLatsis ! you are correct my fix prefers replacing force unwrapping with an optional, i will work on adjusting my solution to prefer force unwrapping

btw, current fix emits the following for the case you mentioined, which - i think- isn't illegal ? it's still preferring optionals. (which what should be adjusted)

 error: optional 'any' type must be written '(any P)?'
let _: (Int, any P!)
             ^~~~~~
             (any P)?

in my next iteration i will test both cases

iMostfa avatar Jun 22 '24 16:06 iMostfa

@swift-ci please smoke test Linux

AnthonyLatsis avatar Jun 24 '24 20:06 AnthonyLatsis

hi @AnthonyLatsis, i know that all the team is busy with swift6 release would appreciate your feedback about the new implementation 🙏🏻

iMostfa avatar Jun 27 '24 05:06 iMostfa

can you take a look here @xedin ;)

iMostfa avatar Jul 01 '24 22:07 iMostfa

Sure, this is next in my queue!

xedin avatar Jul 01 '24 22:07 xedin

Thanks @AnthonyLatsis for the detailed review, i will work on the suggestions and re-assign you for the review

iMostfa avatar Jul 06 '24 09:07 iMostfa

HI @AnthonyLatsis can i get your feedback here 🙏🏻

iMostfa avatar Jul 19 '24 19:07 iMostfa

@xedin would also appreciate your eye here ;)

iMostfa avatar Jul 21 '24 18:07 iMostfa