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

IDE0028 raised erroneously for collection type with Add(T) and IEnumerable<T[]> #72956

Open
stephentoub opened this issue Apr 9, 2024 · 7 comments
Labels
Area-IDE help wanted The issue is "up for grabs" - add a comment if you are interested in working on it
Milestone

Comments

@stephentoub
Copy link
Member

Version Used:
Version 17.11.0 Preview 1.0 [34804.211.main]

Steps to Reproduce:

using System.Collections;

MyCollection<object> c = new() { "Hello" };

class MyCollection<T> : IEnumerable<T[]>
{
    public void Add(T item) { }
    public IEnumerator<T[]> GetEnumerator() => throw new NotImplementedException();
    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

Diagnostic Id:
IDE0028

Expected Behavior:
No diagnostics.

Actual Behavior:
IDE0028 triggers and the fixer rewrites the new() { "Hello" } to be ["Hello"], which then fails to compile with error CS0029: Cannot implicitly convert type 'string' to 'object[]'.

cc: @CyrusNajmabadi

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Apr 9, 2024
@CyrusNajmabadi
Copy link
Member

CyrusNajmabadi commented Apr 9, 2024

@stephentoub do you have a real world example of this? We're aware of TheoryData, but we're trying to collect a list of all these types to help with future lang design. Thanks!

@stephentoub
Copy link
Member Author

TheoryData :)

@CyrusNajmabadi
Copy link
Member

@stephentoub Perfect. I'll let @cston dupe this as something known. We are working on this. If you run into other real-world apis, def let us know as we want any changes here to work for all of them. Thanks!

@cston
Copy link
Member

cston commented Apr 10, 2024

Collection expression conversions require the elements to be implicitly convertible to the collection iteration type. That requirement still holds in the latest proposal for target types that implement IEnumerable and do not use [CollectionBuilder] (see category 3 in dotnet/csharplang#8034). That will limit use of Xunit.TheoryData<T> with collection expressions to cases where T is implicitly convertible to object[].

@arkalyanms arkalyanms removed the untriaged Issues and PRs which have not yet been triaged by a lead label Apr 19, 2024
@cremor
Copy link

cremor commented May 27, 2024

#73655 seems to be related to this and contains a real world example with the FluentValidation library.

@cston
Copy link
Member

cston commented May 27, 2024

In the repro, the compiler is behaving as expected reporting an error for c = ["Hello"] since string is not implicitly convertible to the collection iteration type object[].

Reassigning to @CyrusNajmabadi since the original diagnostic IDE0028 is reported from the analyzer.

@cston cston assigned CyrusNajmabadi and unassigned cston May 27, 2024
@CyrusNajmabadi CyrusNajmabadi removed their assignment May 27, 2024
@CyrusNajmabadi CyrusNajmabadi added the help wanted The issue is "up for grabs" - add a comment if you are interested in working on it label May 27, 2024
@CyrusNajmabadi CyrusNajmabadi added this to the Backlog milestone May 27, 2024
@cremor
Copy link

cremor commented May 29, 2024

Should this be tagged with the label "New Feature - Collection Expressions"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE help wanted The issue is "up for grabs" - add a comment if you are interested in working on it
Projects
Status: InQueue
Development

No branches or pull requests

5 participants