Skip to content

Commit

Permalink
Address test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
alrz committed Jan 26, 2018
1 parent 1ff896d commit edc09fc
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2743,7 +2743,7 @@ private BoundExpression BindImplicitStackAllocArrayCreationExpression(ImplicitSt
return BindStackAllocWithInitializer(
node,
initializer,
type: GetStackAllocType(node, bestType, inLegalPosition, diagnostics),
type: inLegalPosition ? GetStackAllocType(node, bestType, diagnostics) : null,
elementType: bestType,
sizeOpt: null,
diagnostics,
Expand Down Expand Up @@ -3132,7 +3132,7 @@ private BoundExpression BindStackAllocArrayCreationExpression(
new PointerTypeSymbol(elementType));
}

TypeSymbol type = GetStackAllocType(node, elementType, inLegalPosition, diagnostics);
TypeSymbol type = inLegalPosition ? GetStackAllocType(node, elementType, diagnostics) : null;

ExpressionSyntax countSyntax = rankSpecifiers[0].Sizes[0];
BoundExpression count = null;
Expand Down Expand Up @@ -3178,9 +3178,9 @@ private bool ReportBadStackAllocPosition(SyntaxNode node, DiagnosticBag diagnost
return inLegalPosition;
}

private TypeSymbol GetStackAllocType(SyntaxNode node, TypeSymbol elementType, bool inLegalPosition, DiagnosticBag diagnostics)
private TypeSymbol GetStackAllocType(SyntaxNode node, TypeSymbol elementType, DiagnosticBag diagnostics)
{
if (inLegalPosition && !node.IsVariableDeclarationInitialization())
if (!node.IsVariableDeclarationInitialization())
{
CheckFeatureAvailability(node, MessageID.IDS_FeatureRefStructs, diagnostics);
GetWellKnownTypeMember(Compilation, WellKnownMember.System_Span_T__ctor, diagnostics, syntax: node);
Expand Down
6 changes: 2 additions & 4 deletions src/Compilers/CSharp/Portable/Parser/LanguageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10712,21 +10712,19 @@ private ExpressionSyntax ParseRegularStackAllocExpression()
{
var @stackalloc = this.EatToken(SyntaxKind.StackAllocKeyword);
var elementType = this.ParseType(expectSizes: true);
InitializerExpressionSyntax initializer;
InitializerExpressionSyntax initializer = null;
if (this.CurrentToken.Kind == SyntaxKind.OpenBraceToken)
{
@stackalloc = CheckFeatureAvailability(@stackalloc, MessageID.IDS_FeatureStackAllocInitializer);
initializer = this.ParseArrayInitializer();
}
else
else if (elementType.Kind == SyntaxKind.ArrayType)
{
var rankSpec = ((ArrayTypeSyntax)elementType).RankSpecifiers[0];
if (GetNumberOfNonOmittedArraySizes(rankSpec) == 0)
{
elementType = this.AddError(elementType, rankSpec, ErrorCode.ERR_MissingArraySize);
}

initializer = null;
}

return _syntaxFactory.StackAllocArrayCreationExpression(@stackalloc, elementType, initializer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ unsafe class Test
public void Method1()
{
var obj1 = stackalloc[] {{1}};
var obj2 = stackalloc int[] {{1}};
var obj3 = stackalloc int[1] {{1}};
}
}", TestOptions.UnsafeReleaseDll);

Expand All @@ -160,7 +162,49 @@ public void Method1()
Diagnostic(ErrorCode.ERR_ArrayInitInBadPlace, "{1}").WithLocation(6, 34),
// (6,34): error CS0623: Array initializers can only be used in a variable or field initializer. Try using a new expression instead.
// var obj1 = stackalloc[] {{1}};
Diagnostic(ErrorCode.ERR_ArrayInitInBadPlace, "{1}").WithLocation(6, 34)
Diagnostic(ErrorCode.ERR_ArrayInitInBadPlace, "{1}").WithLocation(6, 34),
// (7,38): error CS0623: Array initializers can only be used in a variable or field initializer. Try using a new expression instead.
// var obj2 = stackalloc int[] {{1}};
Diagnostic(ErrorCode.ERR_ArrayInitInBadPlace, "{1}").WithLocation(7, 38),
// (8,39): error CS0623: Array initializers can only be used in a variable or field initializer. Try using a new expression instead.
// var obj3 = stackalloc int[1] {{1}};
Diagnostic(ErrorCode.ERR_ArrayInitInBadPlace, "{1}").WithLocation(8, 39)
);
}

[Fact]
public void AsStatement()
{
var comp = CreateCompilationWithMscorlibAndSpan(@"
unsafe class Test
{
public void Method1()
{
stackalloc[] {1};
stackalloc int[] {1};
stackalloc int[1] {1};
}
}", TestOptions.UnsafeReleaseDll);

comp.VerifyDiagnostics(
// (6,9): error CS1525: Invalid expression term 'stackalloc'
// stackalloc[] {1};
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "stackalloc").WithArguments("stackalloc").WithLocation(6, 9),
// (6,9): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
// stackalloc[] {1};
Diagnostic(ErrorCode.ERR_IllegalStatement, "stackalloc[] {1}").WithLocation(6, 9),
// (7,9): error CS1525: Invalid expression term 'stackalloc'
// stackalloc int[] {1};
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "stackalloc").WithArguments("stackalloc").WithLocation(7, 9),
// (7,9): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
// stackalloc int[] {1};
Diagnostic(ErrorCode.ERR_IllegalStatement, "stackalloc int[] {1}").WithLocation(7, 9),
// (8,9): error CS1525: Invalid expression term 'stackalloc'
// stackalloc int[1] {1};
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "stackalloc").WithArguments("stackalloc").WithLocation(8, 9),
// (8,9): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
// stackalloc int[1] {1};
Diagnostic(ErrorCode.ERR_IllegalStatement, "stackalloc int[1] {1}").WithLocation(8, 9)
);
}

Expand Down
36 changes: 20 additions & 16 deletions src/Compilers/CSharp/Test/Semantic/Semantics/UnsafeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7463,33 +7463,37 @@ static void Main()
}
";
CreateStandardCompilation(text, options: TestOptions.UnsafeReleaseDll).VerifyDiagnostics(
// (6,31): error CS1575: A stackalloc expression requires [] after type
// (6,34): error CS1586: Array creation must have array size or array initializer
// { int* p = stackalloc int[]; }
Diagnostic(ErrorCode.ERR_BadStackAllocExpr, "int[]"),
Diagnostic(ErrorCode.ERR_MissingArraySize, "[]").WithLocation(6, 34),
// (8,34): error CS1586: Array creation must have array size or array initializer
// { int* p = stackalloc int[][]; }
Diagnostic(ErrorCode.ERR_MissingArraySize, "[]").WithLocation(8, 34),
// (9,34): error CS1586: Array creation must have array size or array initializer
// { int* p = stackalloc int[][1]; }
Diagnostic(ErrorCode.ERR_MissingArraySize, "[]").WithLocation(9, 34),
// (9,37): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
// { int* p = stackalloc int[][1]; }
Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "1").WithLocation(9, 37),
// (11,38): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
// { int* p = stackalloc int[1][1]; }
Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "1").WithLocation(11, 38),
// (7,31): error CS1575: A stackalloc expression requires [] after type
// { int* p = stackalloc int[1, 1]; }
Diagnostic(ErrorCode.ERR_BadStackAllocExpr, "int[1, 1]"),
Diagnostic(ErrorCode.ERR_BadStackAllocExpr, "int[1, 1]").WithLocation(7, 31),
// (8,31): error CS1575: A stackalloc expression requires [] after type
// { int* p = stackalloc int[][]; }
Diagnostic(ErrorCode.ERR_BadStackAllocExpr, "int[][]"),
Diagnostic(ErrorCode.ERR_BadStackAllocExpr, "int[][]").WithLocation(8, 31),
// (9,31): error CS1575: A stackalloc expression requires [] after type
// { int* p = stackalloc int[][1]; }
Diagnostic(ErrorCode.ERR_BadStackAllocExpr, "int[][1]"),
Diagnostic(ErrorCode.ERR_BadStackAllocExpr, "int[][1]").WithLocation(9, 31),
// (10,31): error CS1575: A stackalloc expression requires [] after type
// { int* p = stackalloc int[1][]; }
Diagnostic(ErrorCode.ERR_BadStackAllocExpr, "int[1][]"),
Diagnostic(ErrorCode.ERR_BadStackAllocExpr, "int[1][]").WithLocation(10, 31),
// (11,31): error CS1575: A stackalloc expression requires [] after type
// { int* p = stackalloc int[1][1]; }
Diagnostic(ErrorCode.ERR_BadStackAllocExpr, "int[1][1]"),

// CONSIDER: these are plausible, but not ideal.

// (9,37): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
// { int* p = stackalloc int[][1]; }
Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "1"),
// (11,38): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
// { int* p = stackalloc int[1][1]; }
Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "1"));
Diagnostic(ErrorCode.ERR_BadStackAllocExpr, "int[1][1]").WithLocation(11, 31)
);
}

[Fact]
Expand Down

0 comments on commit edc09fc

Please sign in to comment.