Span does not require local variable assignment. Is that a feature?

前端 未结 3 1770
北荒
北荒 2021-01-04 06:29

I notice that the following will compile and execute even though the local variables are not initialized. Is this a feature of Span?

void Uninitialized()
{
          


        
3条回答
  •  自闭症患者
    2021-01-04 07:17

    Marc has a great answer. I wanted to elaborate a bit on the history / context.

    First of all this is definitely a compiler bug. By rules of definite assignment this local is not definitely assigned and any usage should be an error. Unfortunately this bug is hard to fix for number of reasons:

    • This bug is old and goes back to at least C# 4.0. That gives customers 7+ years to inadvertently take a dependency on it
    • There are a number of structs in the BCL which have this basic structure. For example CancellationToken.

    Those taken together mean fixing this would likely break a large amount of existing code. Despite this the C# team attempted to fix the bug in C# 6.0 when the bug was much younger. But an attempt at compiling the Visual Studio source with this fix showed that the fears around customers taking a dependency on this bug were well founded: there were a number of build breaks. Enough to convince us it would have a negative impact on a significant amount of code. Hence the fix was undone.

    The second problem here is this bug wasn't known to all the compiler team members (before today at least). Been ~3 years since the fix was undone and had a bit of turn over since then. The team members who verified how we were generating reference assemblies for Span weren't aware of this bug and recommended the current design based on the language spec. I'm one of those developers :(

    Still discussing this but most likely we're going to update the reference assembly strategy for Span, and other types, so that it avoids this compiler bug.

    Thanks for reporting this. Sorry about the confusion caused :(

提交回复
热议问题