Skip to content

Compiler should emit localinits when method body contains stackalloc #23951

@VSadov

Description

@VSadov

Currently the localinits is triggered by the presence of IL locals. However the flag has effect on everything that is allocated from local frame, including stackalloc.

NOTE: the absence of the flag results in stackallocated data containing nondeterministic garbage and as such emitting the flag is not compat breaking.

While the language spec does not specify or require that, stackallocated memory is nearly always zero-inited and users often take dependency on that. Suddenly not emitting the flag could result in bugs that are very hard to reproduce.
We should make sure that the flag is emitted in all cases and document the default behavior as such.

NOTE: the cases with opposite expectations are also known -
Some users use this bug as a way to avoid costs of zeroing out stack-allocated memory. We should provide a more stable and documented way to do that.

Relying on essentially a bug that is triggered by unstable condition such as absence of IL locals is not a maintainable strategy anyways.

Proposal: https://github.com/dotnet/csharplang/blob/master/proposals/skip-localsinit.md

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions