提交 db82d34e 编写于 作者: A AlekseyTs

Enable assignment to a declaration expression. Fixes...

Enable assignment to a declaration expression. Fixes https://roslyn.codeplex.com/workitem/18. This is a port of https://roslyn.codeplex.com/SourceControl/changeset/0d51dfd01a7ff479cdae0bb90837dfa3770e6562. (changeset 1250584)
上级 28bbcbfc
......@@ -1795,6 +1795,51 @@ private bool EmitAssignmentPreamble(BoundAssignmentOperator assignmentOperator)
}
break;
case BoundKind.Sequence:
{
var sequence = (BoundSequence)assignmentOperator.Left;
var hasLocals = !sequence.Locals.IsEmpty;
if (hasLocals)
{
builder.OpenLocalScope();
foreach (var local in sequence.Locals)
{
DefineLocal(local, sequence.Syntax);
}
}
EmitSideEffects(sequence);
BoundLocal referencedLocal = DigForLocal(sequence.Value);
LocalSymbol doNotRelease = null;
if (referencedLocal != null)
{
doNotRelease = referencedLocal.LocalSymbol;
}
lhsUsesStack = EmitAssignmentPreamble(assignmentOperator.Update(sequence.Value, assignmentOperator.Right, assignmentOperator.RefKind, assignmentOperator.Type));
if (hasLocals)
{
builder.CloseLocalScope();
foreach (var local in sequence.Locals)
{
if (local != doNotRelease)
{
FreeLocal(local);
}
else
{
throw ExceptionUtilities.Unreachable;
}
}
}
}
break;
case BoundKind.PropertyAccess:
case BoundKind.IndexerAccess:
// Property access should have been rewritten.
......@@ -1924,6 +1969,13 @@ private void EmitStore(BoundAssignmentOperator assignment)
EmitIndirectStore(expression.Type, expression.Syntax);
break;
case BoundKind.Sequence:
{
var sequence = (BoundSequence)expression;
EmitStore(assignment.Update(sequence.Value, assignment.Right, assignment.RefKind, assignment.Type));
}
break;
case BoundKind.PreviousSubmissionReference:
// Script references are lowered to a this reference and a field access.
default:
......
......@@ -4037,5 +4037,105 @@ Task M2()
);
}
[Fact]
public void BugCodePlex_18_1()
{
var text = @"
public class Cls
{
public static void Main()
{
var x = (int[] y = { }) = new [] { 1 };
}
}";
var compilation = CreateCompilationWithMscorlib(text, compOptions: TestOptions.Exe, parseOptions: TestOptions.Regular.WithLanguageVersion(LanguageVersion.Experimental));
CompileAndVerify(compilation).VerifyDiagnostics().VerifyIL("Cls.Main",
@"{
// Code size 21 (0x15)
.maxstack 4
.locals init (int[] V_0, //x
int[] V_1) //y
IL_0000: ldc.i4.0
IL_0001: newarr ""int""
IL_0006: stloc.1
IL_0007: ldc.i4.1
IL_0008: newarr ""int""
IL_000d: dup
IL_000e: ldc.i4.0
IL_000f: ldc.i4.1
IL_0010: stelem.i4
IL_0011: dup
IL_0012: stloc.1
IL_0013: stloc.0
IL_0014: ret
}");
}
[Fact]
public void BugCodePlex_18_2()
{
var text = @"
public class Cls
{
public static void Main()
{
var x = int[] y = { } = new [] { 1 };
}
}";
var compilation = CreateCompilationWithMscorlib(text, compOptions: TestOptions.Exe, parseOptions: TestOptions.Regular.WithLanguageVersion(LanguageVersion.Experimental));
CompileAndVerify(compilation).VerifyDiagnostics().VerifyIL("Cls.Main",
@"{
// Code size 21 (0x15)
.maxstack 4
.locals init (int[] V_0, //x
int[] V_1) //y
IL_0000: ldc.i4.0
IL_0001: newarr ""int""
IL_0006: stloc.1
IL_0007: ldc.i4.1
IL_0008: newarr ""int""
IL_000d: dup
IL_000e: ldc.i4.0
IL_000f: ldc.i4.1
IL_0010: stelem.i4
IL_0011: dup
IL_0012: stloc.1
IL_0013: stloc.0
IL_0014: ret
}");
}
[Fact]
public void BugCodePlex_18_3()
{
var text = @"
public class Cls
{
public static void Main()
{
var x = var y = var z = 1;
}
}";
var compilation = CreateCompilationWithMscorlib(text, compOptions: TestOptions.Exe, parseOptions: TestOptions.Regular.WithLanguageVersion(LanguageVersion.Experimental));
CompileAndVerify(compilation).VerifyDiagnostics().VerifyIL("Cls.Main",
@"{
// Code size 7 (0x7)
.maxstack 1
.locals init (int V_0, //x
int V_1, //y
int V_2) //z
IL_0000: ldc.i4.1
IL_0001: stloc.2
IL_0002: ldloc.2
IL_0003: stloc.1
IL_0004: ldloc.1
IL_0005: stloc.0
IL_0006: ret
}");
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册