dotnet/roslyn

Cannot use index initializer with fixed sized buffer

Offen

#39.632 geöffnet am 01.11.2019

 (9 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)C# (4.257 Forks)batch import
Area-CompilersBughelp wanted

Repository-Metriken

Stars
 (20.414 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (256 gemergte PRs in 30 T)

Beschreibung

You currently cannot use an index initializer to populate a fixed-sized buffer. For example:

struct Body
{
    public fixed double V[3];
}

new Body
{
	V = 
	{
		[0] = 1,
		[1] = 2,
		[2] = 3
	}
};

emits on V = an error:

error CS1666: You cannot use fixed size buffers contained in unfixed expressions. Try using the fixed statement.

Firstly, if this is intended behavior, I would at least improve the error message, especially since trying to pin Body.V emits

error CS0213: You cannot use the fixed statement to take the address of an already fixed expression

so you cannot even resolve this issue in the way the error message suggests. However, I think that it should work as expected (and I believe that it can be supported within the current parameters of the language).

Contributor Guide