Skip to content

Commit

Permalink
Ensure obsolete diagnostics about required constructor or Add method …
Browse files Browse the repository at this point in the history
…is suppressible. (#72426)
  • Loading branch information
AlekseyTs authored Mar 8, 2024
1 parent 91815cb commit 512187f
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1559,7 +1559,7 @@ void validateParamsType(BindingDiagnosticBag diagnostics)
case CollectionExpressionTypeKind.ImplementsIEnumerable:
{
var syntax = ParameterSyntax;
var binder = GetDefaultParameterValueBinder(syntax); // this binder is good for our purpose
var binder = GetDefaultParameterValueBinder(syntax).WithContainingMemberOrLambda(ContainingSymbol); // this binder is good for our purpose

binder.TryGetCollectionIterationType(syntax, Type, out elementTypeWithAnnotations);
elementType = elementTypeWithAnnotations.Type;
Expand Down Expand Up @@ -1618,7 +1618,7 @@ void validateParamsType(BindingDiagnosticBag diagnostics)
case CollectionExpressionTypeKind.CollectionBuilder:
{
var syntax = ParameterSyntax;
var binder = GetDefaultParameterValueBinder(syntax); // this binder is good for our purpose
var binder = GetDefaultParameterValueBinder(syntax).WithContainingMemberOrLambda(ContainingSymbol); // this binder is good for our purpose

binder.TryGetCollectionIterationType(syntax, Type, out elementTypeWithAnnotations);
elementType = elementTypeWithAnnotations.Type;
Expand Down
117 changes: 117 additions & 0 deletions src/Compilers/CSharp/Test/Emit2/Semantics/ParamsCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2158,6 +2158,123 @@ static void Test2()
);
}

[Fact]
public void ImplementsIEnumerableT_22_ObsoleteConstructor()
{
var src = """
using System.Collections;
using System.Collections.Generic;

class MyCollection : IEnumerable<long>
{
[System.Obsolete()]
public MyCollection(){}
public List<long> Array = new List<long>();
IEnumerator<long> IEnumerable<long>.GetEnumerator() => throw null;
IEnumerator IEnumerable.GetEnumerator() => throw null;

public void Add(long l) => Array.Add(l);
}

class Program
{
static void Main()
{
#line 100
Test();
Test(1);
Test(2, 3);
}

static void Test(params MyCollection a)
{
}

[System.Obsolete()]
static void Test2(params MyCollection a)
{
Test2();
Test2(1);
Test2(2, 3);
}
}
""";
var comp = CreateCompilation(src, options: TestOptions.ReleaseExe);

comp.VerifyEmitDiagnostics(
// (100,9): warning CS0612: 'MyCollection.MyCollection()' is obsolete
// Test();
Diagnostic(ErrorCode.WRN_DeprecatedSymbol, "Test()").WithArguments("MyCollection.MyCollection()").WithLocation(100, 9),
// (101,9): warning CS0612: 'MyCollection.MyCollection()' is obsolete
// Test(1);
Diagnostic(ErrorCode.WRN_DeprecatedSymbol, "Test(1)").WithArguments("MyCollection.MyCollection()").WithLocation(101, 9),
// (102,9): warning CS0612: 'MyCollection.MyCollection()' is obsolete
// Test(2, 3);
Diagnostic(ErrorCode.WRN_DeprecatedSymbol, "Test(2, 3)").WithArguments("MyCollection.MyCollection()").WithLocation(102, 9),
// (105,22): warning CS0612: 'MyCollection.MyCollection()' is obsolete
// static void Test(params MyCollection a)
Diagnostic(ErrorCode.WRN_DeprecatedSymbol, "params MyCollection a").WithArguments("MyCollection.MyCollection()").WithLocation(105, 22)
);
}

[Fact]
public void ImplementsIEnumerableT_23_ObsoleteAdd()
{
var src = """
using System.Collections;
using System.Collections.Generic;

class MyCollection : IEnumerable<long>
{
public List<long> Array = new List<long>();
IEnumerator<long> IEnumerable<long>.GetEnumerator() => throw null;
IEnumerator IEnumerable.GetEnumerator() => throw null;

[System.Obsolete()]
public void Add(long l) => Array.Add(l);
}

class Program
{
static void Main()
{
#line 100
Test();
Test(1);
Test(2, 3);
}

static void Test(params MyCollection a)
{
}

[System.Obsolete()]
static void Test2(params MyCollection a)
{
Test2();
Test2(1);
Test2(2, 3);
}
}
""";
var comp = CreateCompilation(src, options: TestOptions.ReleaseExe);

comp.VerifyEmitDiagnostics(
// (101,14): warning CS1064: The best overloaded Add method 'MyCollection.Add(long)' for the collection initializer element is obsolete.
// Test(1);
Diagnostic(ErrorCode.WRN_DeprecatedCollectionInitAdd, "1").WithArguments("MyCollection.Add(long)").WithLocation(101, 14),
// (102,14): warning CS1064: The best overloaded Add method 'MyCollection.Add(long)' for the collection initializer element is obsolete.
// Test(2, 3);
Diagnostic(ErrorCode.WRN_DeprecatedCollectionInitAdd, "2").WithArguments("MyCollection.Add(long)").WithLocation(102, 14),
// (102,17): warning CS1064: The best overloaded Add method 'MyCollection.Add(long)' for the collection initializer element is obsolete.
// Test(2, 3);
Diagnostic(ErrorCode.WRN_DeprecatedCollectionInitAdd, "3").WithArguments("MyCollection.Add(long)").WithLocation(102, 17),
// (105,22): warning CS1064: The best overloaded Add method 'MyCollection.Add(long)' for the collection initializer element is obsolete.
// static void Test(params MyCollection a)
Diagnostic(ErrorCode.WRN_DeprecatedCollectionInitAdd, "params MyCollection a").WithArguments("MyCollection.Add(long)").WithLocation(105, 22)
);
}

[Fact]
public void ImplementsIEnumerable_01()
{
Expand Down

0 comments on commit 512187f

Please sign in to comment.