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

Filter out abstract types from the list of collection-initializable types #69546

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ bool IsConstructibleCollectionType(ITypeSymbol type)
return true;

// At this point, all that is left are collection-initializer types. These need to derive from
// System.Collections.IEnumerable, and have an accessible no-arg constructor.
// System.Collections.IEnumerable, and have an invokable no-arg constructor.

// Abstract type don't have invokable constructors at all.
if (namedType.IsAbstract)
return false;

if (namedType.AllInterfaces.Contains(compilation.IEnumerableType()!))
{
// If they have an accessible `public C(int capacity)` constructor, the lang prefers calling that.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1028,4 +1028,99 @@ void M()
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
}.RunAsync();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/69507")]
public async Task NotForAbstractClassWithoutCollectionBuilderAttribute()
{
var collectionDefinition = """

abstract class V<T> : IEnumerable<T>
{
public static readonly V<T> Empty = null;

public V() { }

public IEnumerator<T> GetEnumerator() => default;
IEnumerator IEnumerable.GetEnumerator() => default;

public void Add(T x) { }
}
""";

await new VerifyCS.Test
{
TestCode = """
using System;
using System.Collections;
using System.Collections.Generic;

class C
{
void M()
{
V<int> v = V<int>.Empty;
}
}
""" + collectionDefinition,
LanguageVersion = LanguageVersion.CSharp12,
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
}.RunAsync();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/69507")]
public async Task ForAbstractClassWithCollectionBuilderAttribute()
{
var collectionDefinition = """

[System.Runtime.CompilerServices.CollectionBuilder(typeof(V), "Create")]
abstract class V<T> : IEnumerable<T>
{
public static readonly V<T> Empty = null;

public V() { }

public IEnumerator<T> GetEnumerator() => default;
IEnumerator IEnumerable.GetEnumerator() => default;

public void Add(T x) { }
}

static class V
{
public static V<T> Create<T>(ReadOnlySpan<T> values) => default;
}
""";

await new VerifyCS.Test
{
TestCode = """
using System;
using System.Collections;
using System.Collections.Generic;

class C
{
void M()
{
V<int> v = V<int>.[|Empty|];
}
}
""" + collectionDefinition,
FixedCode = """
using System;
using System.Collections;
using System.Collections.Generic;

class C
{
void M()
{
V<int> v = [];
}
}
""" + collectionDefinition,
LanguageVersion = LanguageVersion.CSharp12,
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
}.RunAsync();
}
}
Loading