-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Code fix for IDE0028 produces invalid collection expression #71240
Comments
@cston @RikkiGibson this seems strange. This is a legal collection initializer type. And we're not using the collection builder attribute. I would expect the to be a legal target, with the expressions validated against the add metgod. Do you know why this fails? |
The element expressions must be convertible to the enumerable element type, in this case to From Conversions:
|
That applies to All collections chuck? Not just Collection-Builder collections? I don't think i realized that :( I suppose it works for everything else, but does put us in this awkward spot with these sort of collection-initializers. Hrmm... Will have to noodle on this a bit. |
It's weird that collection expressions don't behave the same way as collection initializers here. For the above type, both of the following fail: Sample<string> a =
[
"test", // CS0029 Cannot implicitly convert type 'string' to 'object[]'
]; Sample<string> a =
[
["test"], // CS1950 The best overloaded Add method 'Sample<string>.Add(string)' for the collection initializer has some invalid arguments
]; 📝 Note that Xunit's |
Resolving as a dupe of 72098 |
Reopening since this is not a duplicate of #72098. |
Closing as by design since element expressions must be convertible to the enumerable element type as mentioned above. |
By design for collection expressions makes sense, but what about IDE0028? A code analysis rule shouldn't say to switch from valid code to invalid code. How it is now, I need to add a suppression every time I want to use Edit: At least for |
The invalid code is a bug in the compiler that is being fixed currently. |
The compiler bug that is being fixed will no longer require the |
Wait, why is that the case? That would break all sorts of collection-initializable types from being used with collection exprs. I thought that that's exactly what we were addressing just by looking for an "Add" method and no longer caring about convertibility. |
Reopening, the rule that "element expressions must be convertible to the enumerable element type as #71240 (comment)" is both undesirable and surprising. It goes against hte spirit of the collection-exprs design, and introduces arbitrary limitations that make normal collection-initializable types not work with collection exprs. |
I do not repro this. |
Version Used: '4.9.0-2.23578.7 (984df16)'. Language version: 12.0
Steps to Reproduce:
The actual use case is using
Xunit.TheoryData<T>
, but here's a small reproDiagnostic Id: IDE0028
Expected Behavior: The code is valid after applying the code fix
Actual Behavior: The build fails
The text was updated successfully, but these errors were encountered: