Skip to content

Commit

Permalink
Add a ref struct disposal test:
Browse files Browse the repository at this point in the history
- Check that we don't call Dispose on a ref struct enumerator in language versions lower than 8.0
  • Loading branch information
chsienki committed Feb 15, 2019
1 parent 7b57192 commit dffdb50
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/Compilers/CSharp/Test/Semantic/Semantics/ForEachTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1872,7 +1872,7 @@ public static void Main(string [] args)
}
}
";
var boundNode = GetBoundForEachStatement(text,
var boundNode = GetBoundForEachStatement(text, TestOptions.Regular,
// (6,13): error CS1525: Invalid expression term 'int'
// foreach(int; i < 5; i++)
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "int").WithArguments("int").WithLocation(6, 13),
Expand Down Expand Up @@ -3100,9 +3100,43 @@ public void Dispose() { }
Assert.Equal("void DisposableEnumerator.Dispose()", enumeratorInfo.DisposeMethod.ToTestDisplayString());
}

private static BoundForEachStatement GetBoundForEachStatement(string text, params DiagnosticDescription[] diagnostics)
[Fact]
public void TestPatternDisposeRefStruct_7_3()
{
var text = @"
class C
{
static void Main()
{
foreach (var x in new Enumerable1())
{
System.Console.WriteLine(x);
}
}
}
class Enumerable1
{
public DisposableEnumerator GetEnumerator() { return new DisposableEnumerator(); }
}
ref struct DisposableEnumerator
{
int x;
public int Current { get { return x; } }
public bool MoveNext() { return ++x < 4; }
public void Dispose() { }
}";

var boundNode = GetBoundForEachStatement(text, TestOptions.Regular7_3);
var enumeratorInfo = boundNode.EnumeratorInfoOpt;

Assert.Equal(null, enumeratorInfo.DisposeMethod);

This comment has been minimized.

Copy link
@jcouv

jcouv Feb 15, 2019

Member

nit: perhaps Assert.Null
Also nit, consider doing an execution test and semantic model, rather than just internal API.

}

private static BoundForEachStatement GetBoundForEachStatement(string text, CSharpParseOptions options = null, params DiagnosticDescription[] diagnostics)
{
var tree = Parse(text);
var tree = Parse(text, options: options);
var comp = CreateCompilationWithMscorlib40AndSystemCore(new[] { tree });

comp.VerifyDiagnostics(diagnostics);
Expand Down

0 comments on commit dffdb50

Please sign in to comment.