Skip to content

Commit

Permalink
Merge pull request #20726 from zaytsev-victor/Fixed13158
Browse files Browse the repository at this point in the history
Fixed #13158 (IntelliSense does not display get-only abstract collection properties in object initializers).
  • Loading branch information
Pilchie authored Jul 16, 2017
2 parents 0cfe2b7 + 0e837e4 commit f03fded
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,58 @@ static void Main(string[] args)
await VerifyItemIsAbsentAsync(markup, "D");
}

[WorkItem(13158, "https://github.com/dotnet/roslyn/issues/13158")]
[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task CollectionInitializerForInterfaceType1()
{
var markup = @"
using System.Collections.Generic;
public class Foo
{
public IList<int> Items { get; } = new List<int>();
public int Bar;
}
class Program
{
static void Main(string[] args)
{
var y = new Foo { $$ };
}
}";

await VerifyItemExistsAsync(markup, "Items");
await VerifyItemExistsAsync(markup, "Bar");
}

[WorkItem(13158, "https://github.com/dotnet/roslyn/issues/13158")]
[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task CollectionInitializerForInterfaceType2()
{
var markup = @"
using System.Collections.Generic;
public interface ICustomCollection<T> : ICollection<T> { }
public class Foo
{
public ICustomCollection<int> Items { get; } = new List<int>();
public int Bar;
}
class Program
{
static void Main(string[] args)
{
var y = new Foo { $$ };
}
}";

await VerifyItemExistsAsync(markup, "Items");
await VerifyItemExistsAsync(markup, "Bar");
}

[WorkItem(4754, "https://github.com/dotnet/roslyn/issues/4754")]
[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task CollectionInitializerPatternFromBaseTypeAccessible()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,9 @@ public static bool CanSupportCollectionInitializer(this ITypeSymbol typeSymbol,
{
return
typeSymbol.AllInterfaces.Any(i => i.SpecialType == SpecialType.System_Collections_IEnumerable) &&
typeSymbol.GetAccessibleMembersInThisAndBaseTypes<IMethodSymbol>(within ?? typeSymbol).Where(s => s.Name == WellKnownMemberNames.CollectionInitializerAddMethodName)
typeSymbol.GetBaseTypesAndThis()
.Union(typeSymbol.GetOriginalInterfacesAndTheirBaseInterfaces())
.SelectAccessibleMembers<IMethodSymbol>(WellKnownMemberNames.CollectionInitializerAddMethodName, within ?? typeSymbol)
.OfType<IMethodSymbol>()
.Any(m => m.Parameters.Any());
}
Expand Down Expand Up @@ -682,9 +684,7 @@ public static ImmutableArray<T> GetAccessibleMembersInThisAndBaseTypes<T>(this I
return ImmutableArray<T>.Empty;
}

var types = containingType.GetBaseTypesAndThis();
return types.SelectMany(x => x.GetMembers().OfType<T>().Where(m => m.IsAccessibleWithin(within)))
.ToImmutableArray();
return containingType.GetBaseTypesAndThis().SelectAccessibleMembers<T>(within).ToImmutableArray();
}

public static bool? AreMoreSpecificThan(this IList<ITypeSymbol> t1, IList<ITypeSymbol> t2)
Expand Down Expand Up @@ -722,6 +722,26 @@ public static ImmutableArray<T> GetAccessibleMembersInThisAndBaseTypes<T>(this I
return result;
}

private static IEnumerable<T> SelectAccessibleMembers<T>(this IEnumerable<ITypeSymbol> types, ISymbol within) where T : class, ISymbol
{
if (types == null)
{
return ImmutableArray<T>.Empty;
}

return types.SelectMany(x => x.GetMembers().OfType<T>().Where(m => m.IsAccessibleWithin(within)));
}

private static IEnumerable<T> SelectAccessibleMembers<T>(this IEnumerable<ITypeSymbol> types, string memberName, ISymbol within) where T : class, ISymbol
{
if (types == null)
{
return ImmutableArray<T>.Empty;
}

return types.SelectMany(x => x.GetMembers(memberName).OfType<T>().Where(m => m.IsAccessibleWithin(within)));
}

private static bool? IsMoreSpecificThan(this ITypeSymbol t1, ITypeSymbol t2)
{
// SPEC: A type parameter is less specific than a non-type parameter.
Expand Down

0 comments on commit f03fded

Please sign in to comment.