Allow stackalloc Span to return from local function for local consumption #8507
Replies: 6 comments 11 replies
-
Is the suggestion for local functions only ? |
Beta Was this translation helpful? Give feedback.
-
Local functions are functions. Returning stack allocated buffer out of functions is critical bug and will never really work. Only force-inline snippet may have stack allocated buffer escaped to containing function, because they would be allocated in caller function's scope. |
Beta Was this translation helpful? Give feedback.
-
The
The better pattern here is to have the calling method allocate and provide the buffer. |
Beta Was this translation helpful? Give feedback.
-
.NET 9 introduces some enforcement of the struct size of inline-array: https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/9.0/inlinearray-size Granted that inline-array is different from inline-method, but the similarity here is the size dependency. |
Beta Was this translation helpful? Give feedback.
-
This topic has become a whole discussion about method inlining, but that's not really the point. Even if you say "just for local functions", the point is, there's no such thing as a local function: it gets compiled as a regular method with an unspeakable name. And in some cases, the compiler also synthesizes an invisible To implement your proposal, it would be required that the compiler also detects any usage of Except, that you can just do that yourself: instead of performing |
Beta Was this translation helpful? Give feedback.
-
Motivation: Modular programming, Clean code, Performance
Sample: See MethodA and its local function GetNumbers below.
Issue:
Trying to return a stack-allocated collection from a local function results in Compiler Error CS8352.
If the local function is in unsafe context, Compiler Warning CS9080 is instead displayed.
Wish: The possibility to return a stack-allocated collection from a local function, for local consumption, in safe context.
Beta Was this translation helpful? Give feedback.
All reactions