Skip to content

[SR-13899] [Sema] Adjustments on coerce to checked cast diagnostics #34883

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

Conversation

LucianoPAlmeida
Copy link
Contributor

Based on https://forums.swift.org/t/missing-conditional-cast-fix-it/42282
This proposes to adjust coerce to checked cast diagnostic to be broken into the diagnostic and fix-it note.
Also, cases were the coercion result has a value to optional restriction e.g. conditional binding

let any: Any = 1
if let int = any as Int { // an `as!` here would lead to another malformed code because conditional binding expects an optional type

}

we suggest conditional downcast as? instead of force downcast.

The break into a note is more like a proposal not really the SR, so let me know what you think :)

cc @dan-zheng

Resolves SR-13899.

@LucianoPAlmeida
Copy link
Contributor Author

@swift-ci Please smoke test

@dan-zheng
Copy link
Contributor

dan-zheng commented Nov 29, 2020

Thanks for the holiday gift, you are our secret 🎅

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good but it would be great if we could detect that conditional cast could be used during solving, would it be possible? That way we could use a special fix/diagnostic for it instead of adding that for "forced" downcast which is kind of misleading...

@LucianoPAlmeida
Copy link
Contributor Author

That way we could use a special fix/diagnostic for it instead of adding that for "forced" downcast which is kind of misleading...

Humm, do you think is worth another fix/diagnostic for that? Maybe we could make the "forced" downcast more generic and if we detect that conditional cast could be used when solving pass as an additional parameter to the fix and diagnostics. WDYT?

@LucianoPAlmeida
Copy link
Contributor Author

@xedin Let me know what you think of this approach :)

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks much better! I have left a nit comment about naming of the failure, feel free to merge once that's fixed.

@LucianoPAlmeida LucianoPAlmeida force-pushed the SR-13899-coerce-to-checked branch from 85ac0a1 to ac65e6f Compare December 1, 2020 12:03
@LucianoPAlmeida
Copy link
Contributor Author

@swift-ci Please smoke test

@LucianoPAlmeida LucianoPAlmeida merged commit 3e38655 into swiftlang:main Dec 2, 2020
@LucianoPAlmeida LucianoPAlmeida deleted the SR-13899-coerce-to-checked branch December 2, 2020 00:30
@LucianoPAlmeida
Copy link
Contributor Author

Thanks @xedin!

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.

3 participants