-
Notifications
You must be signed in to change notification settings - Fork 94
Open
Description
Describe the bug
Consider the following program.
using System;
unsafe class Program
{
static void Main()
{
// Empty stackalloc initializer -- allocates a zero-length block.
// Valid C# (accepted by Roslyn); the grammar rule for
// stackalloc_initializer should allow an empty initializer list.
int* p = stackalloc int[] {};
Console.WriteLine("Empty stackalloc: length inferred as 0, pointer is non-null: " + ((IntPtr)p != IntPtr.Zero));
// Non-empty for contrast
int* q = stackalloc int[] { 1, 2, 3 };
Console.WriteLine("Non-empty stackalloc[0]: " + q[0]);
}
}This code compiles and runs fine in version 8 of the C# Roslyn compiler. In fact, it explicitly tests this for version 8 of the C# language.
However, the draft-v8 spec is wrong:
csharpstandard/standard/expressions.md
Lines 3411 to 3417 in 089fae9
| stackalloc_initializer | |
| : '{' stackalloc_initializer_element_list '}' | |
| ; | |
| stackalloc_initializer_element_list | |
| : stackalloc_element_initializer (',' stackalloc_element_initializer)* ','? | |
| ; |
The rule should be:
stackalloc_initializer
: '{' stackalloc_initializer_element_list? '}'
;
Or, stackalloc_initializer_element_list should allow for empty.
This feature was introduced in C# 7, so that EBNF is probably wrong as well.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels