提交 5e5e969c 编写于 作者: A AlekseyTs

A Declaration Expression should not be treated as a primary-expression by the...

A Declaration Expression should not be treated as a primary-expression by the language parser. Fixes https://roslyn.codeplex.com/workitem/18. This is a port of https://roslyn.codeplex.com/SourceControl/changeset/d5f905aefda0b465c8e32d990430f5da5ecdeb45. (changeset 1251223)
上级 e5f2525d
...@@ -7777,7 +7777,7 @@ private static bool IsDeclarationModifier(SyntaxKind kind) ...@@ -7777,7 +7777,7 @@ private static bool IsDeclarationModifier(SyntaxKind kind)
private ExpressionStatementSyntax ParseExpressionStatement() private ExpressionStatementSyntax ParseExpressionStatement()
{ {
return ParseExpressionStatement(this.ParseExpression(allowDeclarationExpressionAtTheBeginning: false)); return ParseExpressionStatement(this.ParseExpression(allowDeclarationExpression: false));
} }
private ExpressionStatementSyntax ParseExpressionStatement(ExpressionSyntax expression) private ExpressionStatementSyntax ParseExpressionStatement(ExpressionSyntax expression)
...@@ -7797,9 +7797,9 @@ private ExpressionStatementSyntax ParseExpressionStatement(ExpressionSyntax expr ...@@ -7797,9 +7797,9 @@ private ExpressionStatementSyntax ParseExpressionStatement(ExpressionSyntax expr
return syntaxFactory.ExpressionStatement(expression, semicolon); return syntaxFactory.ExpressionStatement(expression, semicolon);
} }
public ExpressionSyntax ParseExpression(bool allowDeclarationExpressionAtTheBeginning = true) public ExpressionSyntax ParseExpression(bool allowDeclarationExpression = true)
{ {
return this.ParseSubExpression(0, allowDeclarationExpressionAtTheBeginning); return this.ParseSubExpression(0, allowDeclarationExpression);
} }
private bool IsPossibleExpression() private bool IsPossibleExpression()
...@@ -8022,8 +8022,13 @@ private bool IsAwaitExpression() ...@@ -8022,8 +8022,13 @@ private bool IsAwaitExpression()
return false; return false;
} }
private ExpressionSyntax ParseSubExpression(uint precedence, bool allowDeclarationExpressionAtTheBeginning = true, bool contextRequiresVariable = false) private ExpressionSyntax ParseSubExpression(uint precedence, bool allowDeclarationExpression, bool contextRequiresVariable = false)
{ {
if (Options.LanguageVersion != LanguageVersion.Experimental)
{
allowDeclarationExpression = false;
}
ExpressionSyntax leftOperand = null; ExpressionSyntax leftOperand = null;
uint newPrecedence = 0; uint newPrecedence = 0;
SyntaxKind opKind = SyntaxKind.None; SyntaxKind opKind = SyntaxKind.None;
...@@ -8048,7 +8053,7 @@ private ExpressionSyntax ParseSubExpression(uint precedence, bool allowDeclarati ...@@ -8048,7 +8053,7 @@ private ExpressionSyntax ParseSubExpression(uint precedence, bool allowDeclarati
opKind = SyntaxFacts.GetPrefixUnaryExpression(tk); opKind = SyntaxFacts.GetPrefixUnaryExpression(tk);
newPrecedence = GetPrecedence(opKind); newPrecedence = GetPrecedence(opKind);
var opToken = this.EatToken(); var opToken = this.EatToken();
var operand = this.ParseSubExpression(newPrecedence); var operand = this.ParseSubExpression(newPrecedence, allowDeclarationExpression: false);
leftOperand = syntaxFactory.PrefixUnaryExpression(opKind, opToken, operand); leftOperand = syntaxFactory.PrefixUnaryExpression(opKind, opToken, operand);
} }
else if (IsAwaitExpression()) else if (IsAwaitExpression())
...@@ -8057,7 +8062,7 @@ private ExpressionSyntax ParseSubExpression(uint precedence, bool allowDeclarati ...@@ -8057,7 +8062,7 @@ private ExpressionSyntax ParseSubExpression(uint precedence, bool allowDeclarati
newPrecedence = GetPrecedence(opKind); newPrecedence = GetPrecedence(opKind);
var awaitToken = this.EatContextualToken(SyntaxKind.AwaitKeyword); var awaitToken = this.EatContextualToken(SyntaxKind.AwaitKeyword);
awaitToken = CheckFeatureAvailability(awaitToken, MessageID.IDS_FeatureAsync); awaitToken = CheckFeatureAvailability(awaitToken, MessageID.IDS_FeatureAsync);
var operand = this.ParseSubExpression(newPrecedence); var operand = this.ParseSubExpression(newPrecedence, allowDeclarationExpression: false);
leftOperand = syntaxFactory.PrefixUnaryExpression(opKind, awaitToken, operand); leftOperand = syntaxFactory.PrefixUnaryExpression(opKind, awaitToken, operand);
} }
else if (this.IsQueryExpression(mayBeVariableDeclaration: false, mayBeMemberDeclaration: false)) else if (this.IsQueryExpression(mayBeVariableDeclaration: false, mayBeMemberDeclaration: false))
...@@ -8073,10 +8078,21 @@ private ExpressionSyntax ParseSubExpression(uint precedence, bool allowDeclarati ...@@ -8073,10 +8078,21 @@ private ExpressionSyntax ParseSubExpression(uint precedence, bool allowDeclarati
skipped = this.AddError(skipped, ErrorCode.ERR_InvalidExprTerm, this.CurrentToken.Text); skipped = this.AddError(skipped, ErrorCode.ERR_InvalidExprTerm, this.CurrentToken.Text);
leftOperand = AddTrailingSkippedSyntax(this.CreateMissingIdentifierName(), skipped); leftOperand = AddTrailingSkippedSyntax(this.CreateMissingIdentifierName(), skipped);
} }
else if (allowDeclarationExpression &&
(IsPredefinedType(this.CurrentToken.Kind) || (this.CurrentToken.Kind == SyntaxKind.IdentifierToken &&
this.IsTrueIdentifier() &&
!(this.CurrentToken.ContextualKind == SyntaxKind.AsyncKeyword &&
this.PeekToken(1).Kind == SyntaxKind.DelegateKeyword) &&
!this.IsPossibleLambdaExpression(precedence))) &&
IsPossibleDeclarationExpression(contextRequiresVariable))
{
// According to the grammar, a declaration expression cannot be followed by a binary operator or be a condition for a '?' operator. Return.
return ParseDeclarationExpression();
}
else else
{ {
// Not a unary operator - get a primary expression. // Not a unary operator - get a primary expression.
leftOperand = this.ParseTerm(precedence, allowDeclarationExpressionAtTheBeginning, contextRequiresVariable); leftOperand = this.ParseTerm(precedence, contextRequiresVariable);
} }
while (true) while (true)
...@@ -8136,7 +8152,9 @@ private ExpressionSyntax ParseSubExpression(uint precedence, bool allowDeclarati ...@@ -8136,7 +8152,9 @@ private ExpressionSyntax ParseSubExpression(uint precedence, bool allowDeclarati
} }
else else
{ {
leftOperand = syntaxFactory.BinaryExpression(opKind, leftOperand, opToken, this.ParseSubExpression(newPrecedence)); leftOperand = syntaxFactory.BinaryExpression(opKind, leftOperand, opToken,
this.ParseSubExpression(newPrecedence,
allowDeclarationExpression: SyntaxFacts.IsAssignmentExpressionOperatorToken(opToken.Kind)));
} }
} }
...@@ -8154,23 +8172,18 @@ private ExpressionSyntax ParseSubExpression(uint precedence, bool allowDeclarati ...@@ -8154,23 +8172,18 @@ private ExpressionSyntax ParseSubExpression(uint precedence, bool allowDeclarati
{ {
var questionToken = this.EatToken(); var questionToken = this.EatToken();
var colonLeft = this.ParseSubExpression(nullCoalescingPrecedence - 1); var colonLeft = this.ParseSubExpression(nullCoalescingPrecedence - 1, allowDeclarationExpression: true);
var colon = this.EatToken(SyntaxKind.ColonToken); var colon = this.EatToken(SyntaxKind.ColonToken);
var colonRight = this.ParseSubExpression(nullCoalescingPrecedence - 1); var colonRight = this.ParseSubExpression(nullCoalescingPrecedence - 1, allowDeclarationExpression: true);
leftOperand = syntaxFactory.ConditionalExpression(leftOperand, questionToken, colonLeft, colon, colonRight); leftOperand = syntaxFactory.ConditionalExpression(leftOperand, questionToken, colonLeft, colon, colonRight);
} }
return leftOperand; return leftOperand;
} }
private ExpressionSyntax ParseTerm(uint precedence, bool allowDeclarationExpression, bool contextRequiresVariable) private ExpressionSyntax ParseTerm(uint precedence, bool contextRequiresVariable)
{ {
if (Options.LanguageVersion != LanguageVersion.Experimental)
{
allowDeclarationExpression = false;
}
ExpressionSyntax expr = null; ExpressionSyntax expr = null;
var tk = this.CurrentToken.Kind; var tk = this.CurrentToken.Kind;
...@@ -8214,10 +8227,6 @@ private ExpressionSyntax ParseTerm(uint precedence, bool allowDeclarationExpress ...@@ -8214,10 +8227,6 @@ private ExpressionSyntax ParseTerm(uint precedence, bool allowDeclarationExpress
{ {
expr = this.ParseLambdaExpression(); expr = this.ParseLambdaExpression();
} }
else if (allowDeclarationExpression && IsPossibleDeclarationExpression(contextRequiresVariable))
{
expr = ParseDeclarationExpression();
}
else else
{ {
expr = this.ParseAliasQualifiedName(NameOptions.InExpression); expr = this.ParseAliasQualifiedName(NameOptions.InExpression);
...@@ -8259,12 +8268,6 @@ private ExpressionSyntax ParseTerm(uint precedence, bool allowDeclarationExpress ...@@ -8259,12 +8268,6 @@ private ExpressionSyntax ParseTerm(uint precedence, bool allowDeclarationExpress
// check for intrinsic type followed by '.' // check for intrinsic type followed by '.'
if (IsPredefinedType(tk)) if (IsPredefinedType(tk))
{ {
if (allowDeclarationExpression && IsPossibleDeclarationExpression(contextRequiresVariable))
{
expr = ParseDeclarationExpression();
}
else
{
expr = syntaxFactory.PredefinedType(this.EatToken()); expr = syntaxFactory.PredefinedType(this.EatToken());
if (this.CurrentToken.Kind != SyntaxKind.DotToken || tk == SyntaxKind.VoidKeyword) if (this.CurrentToken.Kind != SyntaxKind.DotToken || tk == SyntaxKind.VoidKeyword)
...@@ -8272,7 +8275,6 @@ private ExpressionSyntax ParseTerm(uint precedence, bool allowDeclarationExpress ...@@ -8272,7 +8275,6 @@ private ExpressionSyntax ParseTerm(uint precedence, bool allowDeclarationExpress
expr = this.AddError(expr, ErrorCode.ERR_InvalidExprTerm, SyntaxFacts.GetText(tk)); expr = this.AddError(expr, ErrorCode.ERR_InvalidExprTerm, SyntaxFacts.GetText(tk));
} }
} }
}
else else
{ {
expr = this.CreateMissingIdentifierName(); expr = this.CreateMissingIdentifierName();
...@@ -8359,7 +8361,7 @@ private bool IsPossibleDeclarationExpression(bool contextRequiresVariable) ...@@ -8359,7 +8361,7 @@ private bool IsPossibleDeclarationExpression(bool contextRequiresVariable)
try try
{ {
var nullCoalescingPrecedence = GetPrecedence(SyntaxKind.CoalesceExpression); var nullCoalescingPrecedence = GetPrecedence(SyntaxKind.CoalesceExpression);
var colonLeft = this.ParseSubExpression(nullCoalescingPrecedence - 1); var colonLeft = this.ParseSubExpression(nullCoalescingPrecedence - 1, allowDeclarationExpression: true);
if (colonLeft.Kind != SyntaxKind.DeclarationExpression && this.CurrentToken.Kind == SyntaxKind.ColonToken) if (colonLeft.Kind != SyntaxKind.DeclarationExpression && this.CurrentToken.Kind == SyntaxKind.ColonToken)
{ {
...@@ -8694,7 +8696,7 @@ private ArgumentSyntax ParseArgumentExpression(bool isIndexer) ...@@ -8694,7 +8696,7 @@ private ArgumentSyntax ParseArgumentExpression(bool isIndexer)
} }
else else
{ {
expression = this.ParseSubExpression(0, contextRequiresVariable: refOrOutKeyword != null); expression = this.ParseSubExpression(0, allowDeclarationExpression: true, contextRequiresVariable: refOrOutKeyword != null);
} }
return syntaxFactory.Argument(nameColon, refOrOutKeyword, expression); return syntaxFactory.Argument(nameColon, refOrOutKeyword, expression);
...@@ -8736,7 +8738,7 @@ private MakeRefExpressionSyntax ParseMakeRefExpression() ...@@ -8736,7 +8738,7 @@ private MakeRefExpressionSyntax ParseMakeRefExpression()
{ {
var keyword = this.EatToken(); var keyword = this.EatToken();
var openParen = this.EatToken(SyntaxKind.OpenParenToken); var openParen = this.EatToken(SyntaxKind.OpenParenToken);
var expr = this.ParseSubExpression(0); var expr = this.ParseSubExpression(0, allowDeclarationExpression: true);
var closeParen = this.EatToken(SyntaxKind.CloseParenToken); var closeParen = this.EatToken(SyntaxKind.CloseParenToken);
return syntaxFactory.MakeRefExpression(keyword, openParen, expr, closeParen); return syntaxFactory.MakeRefExpression(keyword, openParen, expr, closeParen);
...@@ -8746,7 +8748,7 @@ private RefTypeExpressionSyntax ParseRefTypeExpression() ...@@ -8746,7 +8748,7 @@ private RefTypeExpressionSyntax ParseRefTypeExpression()
{ {
var keyword = this.EatToken(); var keyword = this.EatToken();
var openParen = this.EatToken(SyntaxKind.OpenParenToken); var openParen = this.EatToken(SyntaxKind.OpenParenToken);
var expr = this.ParseSubExpression(0); var expr = this.ParseSubExpression(0, allowDeclarationExpression: true);
var closeParen = this.EatToken(SyntaxKind.CloseParenToken); var closeParen = this.EatToken(SyntaxKind.CloseParenToken);
return syntaxFactory.RefTypeExpression(keyword, openParen, expr, closeParen); return syntaxFactory.RefTypeExpression(keyword, openParen, expr, closeParen);
...@@ -8759,7 +8761,7 @@ private CheckedExpressionSyntax ParseCheckedOrUncheckedExpression() ...@@ -8759,7 +8761,7 @@ private CheckedExpressionSyntax ParseCheckedOrUncheckedExpression()
var kind = (checkedOrUnchecked.Kind == SyntaxKind.CheckedKeyword) ? SyntaxKind.CheckedExpression : SyntaxKind.UncheckedExpression; var kind = (checkedOrUnchecked.Kind == SyntaxKind.CheckedKeyword) ? SyntaxKind.CheckedExpression : SyntaxKind.UncheckedExpression;
var openParen = this.EatToken(SyntaxKind.OpenParenToken); var openParen = this.EatToken(SyntaxKind.OpenParenToken);
var expr = this.ParseSubExpression(0); var expr = this.ParseSubExpression(0, allowDeclarationExpression: true);
var closeParen = this.EatToken(SyntaxKind.CloseParenToken); var closeParen = this.EatToken(SyntaxKind.CloseParenToken);
return syntaxFactory.CheckedExpression(kind, checkedOrUnchecked, openParen, expr, closeParen); return syntaxFactory.CheckedExpression(kind, checkedOrUnchecked, openParen, expr, closeParen);
...@@ -8769,7 +8771,7 @@ private RefValueExpressionSyntax ParseRefValueExpression() ...@@ -8769,7 +8771,7 @@ private RefValueExpressionSyntax ParseRefValueExpression()
{ {
var @refvalue = this.EatToken(SyntaxKind.RefValueKeyword); var @refvalue = this.EatToken(SyntaxKind.RefValueKeyword);
var openParen = this.EatToken(SyntaxKind.OpenParenToken); var openParen = this.EatToken(SyntaxKind.OpenParenToken);
var expr = this.ParseSubExpression(0); var expr = this.ParseSubExpression(0, allowDeclarationExpression: true);
var comma = this.EatToken(SyntaxKind.CommaToken); var comma = this.EatToken(SyntaxKind.CommaToken);
var type = this.ParseType(false); var type = this.ParseType(false);
var closeParen = this.EatToken(SyntaxKind.CloseParenToken); var closeParen = this.EatToken(SyntaxKind.CloseParenToken);
...@@ -8922,7 +8924,7 @@ private ExpressionSyntax ParseCastOrParenExpressionOrLambda(uint precedence, boo ...@@ -8922,7 +8924,7 @@ private ExpressionSyntax ParseCastOrParenExpressionOrLambda(uint precedence, boo
var openParen = this.EatToken(SyntaxKind.OpenParenToken); var openParen = this.EatToken(SyntaxKind.OpenParenToken);
var type = this.ParseType(false); var type = this.ParseType(false);
var closeParen = this.EatToken(SyntaxKind.CloseParenToken); var closeParen = this.EatToken(SyntaxKind.CloseParenToken);
var expr = this.ParseSubExpression(GetPrecedence(SyntaxKind.CastExpression)); var expr = this.ParseSubExpression(GetPrecedence(SyntaxKind.CastExpression), allowDeclarationExpression: false);
return syntaxFactory.CastExpression(openParen, type, closeParen, expr); return syntaxFactory.CastExpression(openParen, type, closeParen, expr);
} }
} }
...@@ -8937,7 +8939,7 @@ private ExpressionSyntax ParseCastOrParenExpressionOrLambda(uint precedence, boo ...@@ -8937,7 +8939,7 @@ private ExpressionSyntax ParseCastOrParenExpressionOrLambda(uint precedence, boo
{ {
this.Reset(ref resetPoint); this.Reset(ref resetPoint);
var openParen = this.EatToken(SyntaxKind.OpenParenToken); var openParen = this.EatToken(SyntaxKind.OpenParenToken);
var expression = this.ParseSubExpression(0, contextRequiresVariable: contextRequiresVariable); var expression = this.ParseSubExpression(0, allowDeclarationExpression: true, contextRequiresVariable: contextRequiresVariable);
var closeParen = this.EatToken(SyntaxKind.CloseParenToken); var closeParen = this.EatToken(SyntaxKind.CloseParenToken);
return syntaxFactory.ParenthesizedExpression(openParen, expression, closeParen); return syntaxFactory.ParenthesizedExpression(openParen, expression, closeParen);
} }
......
...@@ -280,16 +280,16 @@ public class Cls ...@@ -280,16 +280,16 @@ public class Cls
{ {
public static void Main() public static void Main()
{ {
for (int i = (int)double j = 0; (j = i) < 2; i=(int)j+1) for (int i = (int)(double j = 0); (j = i) < 2; i=(int)j+1)
{ {
System.Console.WriteLine(j); System.Console.WriteLine(j);
} }
for (int i = (int)double j = 10; (j = i) < 12; i=(int)j+1) for (int i = (int)(double j = 10); (j = i) < 12; i=(int)j+1)
System.Console.WriteLine(j + (int k = 5 + i) + k); System.Console.WriteLine(j + (int k = 5 + i) + k);
int ii; int ii;
for (ii = (int)double j = 10; (j = ii) < 12; ii=(int)j+1) for (ii = (int)(double j = 10); (j = ii) < 12; ii=(int)j+1)
System.Console.WriteLine(j + (int k = 5 + ii) + k); System.Console.WriteLine(j + (int k = 5 + ii) + k);
} }
}"; }";
...@@ -322,7 +322,7 @@ public static void Main() ...@@ -322,7 +322,7 @@ public static void Main()
j = 3; j = 3;
k = 4; k = 4;
for (int i = l; i < int l = 12; i++) {} for (int i = l; i < (int l = 12); i++) {}
for (int i = 0; i < m; i = (int m = 12) + m) {} for (int i = 0; i < m; i = (int m = 12) + m) {}
...@@ -4038,7 +4038,7 @@ Task M2() ...@@ -4038,7 +4038,7 @@ Task M2()
} }
[Fact] [Fact]
public void BugCodePlex_18_1() public void BugCodePlex_18_01()
{ {
var text = @" var text = @"
public class Cls public class Cls
...@@ -4073,42 +4073,30 @@ .maxstack 4 ...@@ -4073,42 +4073,30 @@ .maxstack 4
} }
[Fact] [Fact]
public void BugCodePlex_18_2() public void BugCodePlex_18_02()
{ {
var text = @" var text = @"
public class Cls public class Cls
{ {
public static void Main() public static void Main()
{ {
var x = int[] y = { } = new [] { 1 }; var x = int[] y = { } = null;
} }
}"; }";
var compilation = CreateCompilationWithMscorlib(text, compOptions: TestOptions.Exe, parseOptions: TestOptions.Regular.WithLanguageVersion(LanguageVersion.Experimental)); var compilation = CreateCompilationWithMscorlib(text, compOptions: TestOptions.Exe, parseOptions: TestOptions.Regular.WithLanguageVersion(LanguageVersion.Experimental));
CompileAndVerify(compilation).VerifyDiagnostics().VerifyIL("Cls.Main", compilation.VerifyDiagnostics(
@"{ // (6,31): error CS1002: ; expected
// Code size 21 (0x15) // var x = int[] y = { } = null;
.maxstack 4 Diagnostic(ErrorCode.ERR_SemicolonExpected, "=").WithLocation(6, 31),
.locals init (int[] V_0, //x // (6,31): error CS1525: Invalid expression term '='
int[] V_1) //y // var x = int[] y = { } = null;
IL_0000: ldc.i4.0 Diagnostic(ErrorCode.ERR_InvalidExprTerm, "=").WithArguments("=").WithLocation(6, 31)
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] [Fact]
public void BugCodePlex_18_3() public void BugCodePlex_18_03()
{ {
var text = @" var text = @"
public class Cls public class Cls
...@@ -4137,6 +4125,245 @@ .maxstack 1 ...@@ -4137,6 +4125,245 @@ .maxstack 1
}"); }");
} }
[Fact]
public void BugCodePlex_18_04()
{
var text = @"
public class Cls
{
public static void Main()
{
var x = (object) int[] y = { };
}
}";
var compilation = CreateCompilationWithMscorlib(text, compOptions: TestOptions.Exe, parseOptions: TestOptions.Regular.WithLanguageVersion(LanguageVersion.Experimental));
compilation.VerifyDiagnostics(
// (6,26): error CS1525: Invalid expression term 'int'
// var x = (object) int[] y = { };
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "int").WithArguments("int").WithLocation(6, 26),
// (6,30): error CS0443: Syntax error; value expected
// var x = (object) int[] y = { };
Diagnostic(ErrorCode.ERR_ValueExpected, "]").WithLocation(6, 30),
// (6,32): error CS1002: ; expected
// var x = (object) int[] y = { };
Diagnostic(ErrorCode.ERR_SemicolonExpected, "y").WithLocation(6, 32),
// (6,36): error CS1525: Invalid expression term '{'
// var x = (object) int[] y = { };
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "{").WithArguments("{").WithLocation(6, 36),
// (6,36): error CS1002: ; expected
// var x = (object) int[] y = { };
Diagnostic(ErrorCode.ERR_SemicolonExpected, "{").WithLocation(6, 36),
// (6,32): error CS0103: The name 'y' does not exist in the current context
// var x = (object) int[] y = { };
Diagnostic(ErrorCode.ERR_NameNotInContext, "y").WithArguments("y").WithLocation(6, 32)
);
}
[Fact]
public void BugCodePlex_18_05()
{
var text = @"
public class Cls
{
public static void Main()
{
var x = int[] y = { } ? 1 : 2;
}
}";
var compilation = CreateCompilationWithMscorlib(text, compOptions: TestOptions.Exe, parseOptions: TestOptions.Regular.WithLanguageVersion(LanguageVersion.Experimental));
compilation.VerifyDiagnostics(
// (6,31): error CS1003: Syntax error, ',' expected
// var x = int[] y = { } ? 1 : 2;
Diagnostic(ErrorCode.ERR_SyntaxError, "?").WithArguments(",", "?").WithLocation(6, 31),
// (6,33): error CS1002: ; expected
// var x = int[] y = { } ? 1 : 2;
Diagnostic(ErrorCode.ERR_SemicolonExpected, "1").WithLocation(6, 33),
// (6,35): error CS1002: ; expected
// var x = int[] y = { } ? 1 : 2;
Diagnostic(ErrorCode.ERR_SemicolonExpected, ":").WithLocation(6, 35),
// (6,35): error CS1513: } expected
// var x = int[] y = { } ? 1 : 2;
Diagnostic(ErrorCode.ERR_RbraceExpected, ":").WithLocation(6, 35),
// (6,37): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
// var x = int[] y = { } ? 1 : 2;
Diagnostic(ErrorCode.ERR_IllegalStatement, "2").WithLocation(6, 37)
);
}
[Fact]
public void BugCodePlex_18_06()
{
var text = @"
public class Cls
{
public static void Main()
{
var x = int[] y = { } ?? new int[] { 1 };
}
}";
var compilation = CreateCompilationWithMscorlib(text, compOptions: TestOptions.Exe, parseOptions: TestOptions.Regular.WithLanguageVersion(LanguageVersion.Experimental));
compilation.VerifyDiagnostics(
// (6,31): error CS1002: ; expected
// var x = int[] y = { } ?? new int[] { 1 };
Diagnostic(ErrorCode.ERR_SemicolonExpected, "??").WithLocation(6, 31),
// (6,31): error CS1525: Invalid expression term '??'
// var x = int[] y = { } ?? new int[] { 1 };
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "??").WithArguments("??").WithLocation(6, 31)
);
}
[Fact]
public void BugCodePlex_18_07()
{
var text = @"
public class Cls
{
public static void Main()
{
var x = int y++;
}
}";
var compilation = CreateCompilationWithMscorlib(text, compOptions: TestOptions.Exe, parseOptions: TestOptions.Regular.WithLanguageVersion(LanguageVersion.Experimental));
compilation.VerifyDiagnostics(
// (6,22): error CS1002: ; expected
// var x = int y++;
Diagnostic(ErrorCode.ERR_SemicolonExpected, "++").WithLocation(6, 22),
// (6,24): error CS1525: Invalid expression term ';'
// var x = int y++;
Diagnostic(ErrorCode.ERR_InvalidExprTerm, ";").WithArguments(";").WithLocation(6, 24),
// (6,17): error CS0165: Use of unassigned local variable 'y'
// var x = int y++;
Diagnostic(ErrorCode.ERR_UseDefViolation, "int y").WithArguments("y").WithLocation(6, 17)
);
}
[Fact]
public void BugCodePlex_18_08()
{
var text = @"
public class Cls
{
public static void Main()
{
var x = ++ int y;
}
}";
var compilation = CreateCompilationWithMscorlib(text, compOptions: TestOptions.Exe, parseOptions: TestOptions.Regular.WithLanguageVersion(LanguageVersion.Experimental));
compilation.VerifyDiagnostics(
// (6,20): error CS1525: Invalid expression term 'int'
// var x = ++ int y;
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "int").WithArguments("int").WithLocation(6, 20),
// (6,24): error CS1002: ; expected
// var x = ++ int y;
Diagnostic(ErrorCode.ERR_SemicolonExpected, "y").WithLocation(6, 24),
// (6,24): error CS0103: The name 'y' does not exist in the current context
// var x = ++ int y;
Diagnostic(ErrorCode.ERR_NameNotInContext, "y").WithArguments("y").WithLocation(6, 24),
// (6,24): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
// var x = ++ int y;
Diagnostic(ErrorCode.ERR_IllegalStatement, "y").WithLocation(6, 24)
);
}
[Fact]
public void BugCodePlex_18_09()
{
var text = @"
public class Cls
{
public static void Main()
{}
public async void Test()
{
var x = await int y = 2;
}
}";
var compilation = CreateCompilationWithMscorlib(text, compOptions: TestOptions.Exe, parseOptions: TestOptions.Regular.WithLanguageVersion(LanguageVersion.Experimental));
compilation.VerifyDiagnostics(
// (9,23): error CS1525: Invalid expression term 'int'
// var x = await int y = 2;
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "int").WithArguments("int").WithLocation(9, 23),
// (9,27): error CS1002: ; expected
// var x = await int y = 2;
Diagnostic(ErrorCode.ERR_SemicolonExpected, "y").WithLocation(9, 27),
// (9,27): error CS0103: The name 'y' does not exist in the current context
// var x = await int y = 2;
Diagnostic(ErrorCode.ERR_NameNotInContext, "y").WithArguments("y").WithLocation(9, 27)
);
}
[Fact]
public void BugCodePlex_18_10()
{
var text = @"
public class Cls
{
public static void Main()
{
var x = 3 + int y = 2;
}
}";
var compilation = CreateCompilationWithMscorlib(text, compOptions: TestOptions.Exe, parseOptions: TestOptions.Regular.WithLanguageVersion(LanguageVersion.Experimental));
compilation.VerifyDiagnostics(
// (6,21): error CS1525: Invalid expression term 'int'
// var x = 3 + int y = 2;
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "int").WithArguments("int").WithLocation(6, 21),
// (6,25): error CS1002: ; expected
// var x = 3 + int y = 2;
Diagnostic(ErrorCode.ERR_SemicolonExpected, "y").WithLocation(6, 25),
// (6,25): error CS0103: The name 'y' does not exist in the current context
// var x = 3 + int y = 2;
Diagnostic(ErrorCode.ERR_NameNotInContext, "y").WithArguments("y").WithLocation(6, 25)
);
}
[Fact]
public void BugCodePlex_18_11()
{
var text = @"
public class Cls
{
public static void Main()
{
int[] y = { };
var x = y ?? int[] z = { 1 };
}
}";
var compilation = CreateCompilationWithMscorlib(text, compOptions: TestOptions.Exe, parseOptions: TestOptions.Regular.WithLanguageVersion(LanguageVersion.Experimental));
compilation.VerifyDiagnostics(
// (7,22): error CS1525: Invalid expression term 'int'
// var x = y ?? int[] z = { 1 };
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "int").WithArguments("int").WithLocation(7, 22),
// (7,26): error CS0443: Syntax error; value expected
// var x = y ?? int[] z = { 1 };
Diagnostic(ErrorCode.ERR_ValueExpected, "]").WithLocation(7, 26),
// (7,28): error CS1002: ; expected
// var x = y ?? int[] z = { 1 };
Diagnostic(ErrorCode.ERR_SemicolonExpected, "z").WithLocation(7, 28),
// (7,32): error CS1525: Invalid expression term '{'
// var x = y ?? int[] z = { 1 };
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "{").WithArguments("{").WithLocation(7, 32),
// (7,32): error CS1002: ; expected
// var x = y ?? int[] z = { 1 };
Diagnostic(ErrorCode.ERR_SemicolonExpected, "{").WithLocation(7, 32),
// (7,36): error CS1002: ; expected
// var x = y ?? int[] z = { 1 };
Diagnostic(ErrorCode.ERR_SemicolonExpected, "}").WithLocation(7, 36),
// (7,28): error CS0103: The name 'z' does not exist in the current context
// var x = y ?? int[] z = { 1 };
Diagnostic(ErrorCode.ERR_NameNotInContext, "z").WithArguments("z").WithLocation(7, 28)
);
}
[Fact, WorkItem(2)] [Fact, WorkItem(2)]
public void InLambda_01() public void InLambda_01()
{ {
...@@ -4217,7 +4444,7 @@ static void Main() ...@@ -4217,7 +4444,7 @@ static void Main()
} }
[Fact, WorkItem(2)] [Fact, WorkItem(2)]
public void InALambda_04() public void InLambda_04()
{ {
var text = @" var text = @"
using System; using System;
......
...@@ -12786,6 +12786,12 @@ public void CS1525ERR_InvalidExprTerm() ...@@ -12786,6 +12786,12 @@ public void CS1525ERR_InvalidExprTerm()
// (4,25): error CS1001: Identifier expected // (4,25): error CS1001: Identifier expected
// bool b = string is string; // bool b = string is string;
Diagnostic(ErrorCode.ERR_IdentifierExpected, "is").WithLocation(4, 25), Diagnostic(ErrorCode.ERR_IdentifierExpected, "is").WithLocation(4, 25),
// (4,25): error CS1002: ; expected
// bool b = string is string;
Diagnostic(ErrorCode.ERR_SemicolonExpected, "is").WithLocation(4, 25),
// (4,25): error CS1525: Invalid expression term 'is'
// bool b = string is string;
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "is").WithArguments("is").WithLocation(4, 25),
// (4,18): error CS0165: Use of unassigned local variable '' // (4,18): error CS0165: Use of unassigned local variable ''
// bool b = string is string; // bool b = string is string;
Diagnostic(ErrorCode.ERR_UseDefViolation, "string ").WithArguments("").WithLocation(4, 18) Diagnostic(ErrorCode.ERR_UseDefViolation, "string ").WithArguments("").WithLocation(4, 18)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册