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

Get rid of "An explicit conversion exists (are you missing a cast?)" from CS0266 #48431

Open
YairHalberstadt opened this issue Oct 8, 2020 · 4 comments
Assignees
Labels
Area-Compilers Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings.
Milestone

Comments

@YairHalberstadt
Copy link
Contributor

CS0266 has the following error message:

Cannot implicitly convert type 'type1' to 'type2'. An explicit conversion exists (are you missing a cast?)

It comes up pretty much everywhere where the target type doesn't match the expression type, since almost any type can be explicitly converted to any other type. However in 99% of cases, it's not the correct solution, and so is just noise - it can also be confusing for users, who may add an explicit cast (thinking the diagnostic means an explicit cast operator exists) only to find things blow up at runtime.

I would suggest either getting rid of this diagnostic completely, or using CS0029 "Cannot implicitly convert type 'int' to 'string'" in most cases, and adding new diagnostics for the following cases:

  1. The target type is a subtype of the expression type (may want to downcast).
  2. There is an explicit cast operator from the expression type to the target type (may want to use this cast).
@Joe4evr
Copy link

Joe4evr commented Oct 8, 2020

+100. The amount of times I've seen people doing something like (User)o.Users because they think that'll get them a single thing out of a collection of things (and proceed to complain that their code blows up) is staggering.

@CyrusNajmabadi
Copy link
Member

I strongly agree wiht this. The benefit of the proposal as well is that we can have much better diagnostics and diagnostic-ids for the cases that can be likely fixed up properly. This can then better drive IDE features to do things like insert the appropriate subtype cast. Tagging @jcouv to see if this is something that could be picked up.

@RikkiGibson RikkiGibson added Area-Compilers Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. labels Oct 8, 2020
@jcouv
Copy link
Member

jcouv commented Oct 8, 2020

@YairHalberstadt Can you provide some scenario(s) to illustrate current (misleading) behavior with CS0266?
For int to string, I get "error CS0029: Cannot implicitly convert type 'string' to 'int'" (which is appropriate), not CS0266...

@YairHalberstadt
Copy link
Contributor Author

@jcouv you get it when casting pretty much any non sealed reference type to another non sealed reference type (including interfaces)

@jcouv jcouv added this to the Compiler.Next milestone Mar 17, 2022
@jcouv jcouv self-assigned this Mar 17, 2022
@jaredpar jaredpar modified the milestones: Compiler.Next, Backlog Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings.
Projects
Development

No branches or pull requests

6 participants