提交 6b88ea63 编写于 作者: V VSadov

CR feedback

上级 b4478d3d
......@@ -1577,8 +1577,6 @@ public override BoundNode VisitForEachStatement(BoundForEachStatement node)
// NOTE: iteration variables are not declared or assigned
// before the collection expression is evaluated
var result = base.VisitForEachStatement(node);
// NOTE: do not report unused iteration variables. They are always considered used.
return result;
}
......@@ -2188,6 +2186,7 @@ public override void VisitForEachIterationVariables(BoundForEachStatement node)
Debug.Assert((object)iterationVariable != null);
int slot = GetOrCreateSlot(iterationVariable);
if (slot > 0) SetSlotAssigned(slot);
// NOTE: do not report unused iteration variables. They are always considered used.
NoteWrite(iterationVariable, null, read: true);
}
}
......
......@@ -6192,5 +6192,56 @@ public static void Test5(IEnumerable<((int, int), string)> en)
CompileAndVerify(compilation, expectedOutput: "0");
}
[Fact]
[WorkItem(16106, "https://github.com/dotnet/roslyn/issues/16106")]
public void DefAssignmentsStruct002()
{
string source = @"
public class MyClass
{
public static void Main()
{
var data = new int[10];
var arr = new int[2];
foreach (arr[out int size] in data) {}
}
}
";
var compilation = CreateCompilationWithMscorlib(source, references: s_valueTupleRefs);
compilation.VerifyDiagnostics(
// (9,36): error CS0230: Type and identifier are both required in a foreach statement
// foreach (arr[out int size] in data) {}
Diagnostic(ErrorCode.ERR_BadForeachDecl, "in").WithLocation(9, 36)
);
}
[Fact]
[WorkItem(16106, "https://github.com/dotnet/roslyn/issues/16106")]
public void DefAssignmentsStruct003()
{
string source = @"
public class MyClass
{
public static void Main()
{
var data = new (int, int)[10];
var arr = new int[2];
foreach ((arr[out int size], int b) in data) {}
}
}
";
var compilation = CreateCompilationWithMscorlib(source, references: s_valueTupleRefs);
compilation.VerifyDiagnostics(
// (9,27): error CS1615: Argument 1 may not be passed with the 'out' keyword
// foreach ((arr[out int size], int b) in data) {}
Diagnostic(ErrorCode.ERR_BadArgExtraRef, "int size").WithArguments("1", "out").WithLocation(9, 27),
// (9,18): error CS8186: A foreach loop must declare its iteration variables.
// foreach ((arr[out int size], int b) in data) {}
Diagnostic(ErrorCode.ERR_MustDeclareForeachIteration, "(arr[out int size], int b)").WithLocation(9, 18)
);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册