Why are local variables definitely assigned in unreachable statements?

Why are local variables definitely assigned in unreachable statements?

Rate This
  • Comments 24

You're probably all familiar with the feature of C# which disallows reading from a local variable before it has been "definitely assigned":

void M()
{
  int x;
  if (Q())
    x = 123;
  if (R())
    Console.WriteLine(x); // illegal!
}

This is illegal because there is a path through the code which, if taken, results in the local variable being read from before it has been assigned; in this case, if Q() returns false and R() returns true.

The reason why we want to make this illegal is not, as many people believe, because the local variable is going to be initialized to garbage and we want to protect you from garbage. We do in fact automatically initialize locals to their default values. (Though the C and C++ programming languages do not, and will cheerfully allow you to read garbage from an uninitialized local.) Rather, it is because the existence of such a code path is probably a bug, and we want to throw you in the pit of quality; you should have to work hard to write that bug.

The way in which the compiler determines if there is any path through the code which causes x to be read before it is written is quite interesting, but that's a subject for another day. The question I want to consider today is: why are local variables considered to be definitely assigned inside unreachable statements?

void M()
{
  int x;
  if (Q())
    x = 123;
  if (false)
    Console.WriteLine(x); // legal!
}

First off, obviously the way I've described the feature immediately gives the intuition that this ought to be legal. Clearly there is no path through the code which results in the local variable being read before it is assigned. In fact, there is no path through the code that results in the local variable being read, period!

On the other hand: that code looks wrong. We do not allow syntax errors, or overload resolution errors, or convertibility errors, or any other kind of error, in an unreachable statement, so why should we allow definite assignment errors?

It's a subtle point, I admit. Here's the thing. You have to ask yourself "why is there unreachable code in the method in the first place?" Either that unreachable code is deliberate, or it is an error.

If it is an error, then something is deeply messed up here. The programmer did not intend the written control flow in the first place. It seems premature to guess at what the definite assignment errors are in the unreachable code, since the control flow that would be used to determine definite assignment state is wrong. We are going to give a warning about the unreachable code; the user can then notice the warning and fix the control flow. Once it is fixed, then we can consider whether there are definite assignment problems with the fixed control flow.

Now, why on earth would someone deliberately make unreachable code? It does in fact happen; actually it happens quite frequently when dealing with libraries made by another team that are not quite done yet:

// If we can resrov the frob into a glob, do that and then blorg the result.
// Even if the frob is not a glob, we know it is definitely a resrovable blob,
// so resrov it as a blob and then blorg the result. Finally, fribble
// the blorgable result, regardless of whether it was a glob or a blob.
void BlorgFrob(Frob frob)
{
  IBlorgable blorgable;
  // TODO: Glob.TryResrov has not been ported to C# yet.
  if (false /* Glob.TryResrov(out blorgable, frob) */)
  {
    BlorgGlob(blorgable);
  }
  else
  {
    blorgable = Blob.Resrov(frob)
    BlorgBlob(blorgable);
  }
  blorgable.Fribble(frob);
}

Should BlorgGlob(blorgable) be an error? It seems plausible that it should not be an error; after all, it's never going to read the local. But it is still nice that we get overload resolution errors reported inside the unreachable code, just in case there is something wrong there.

  • When I started using C#, I felt like I was being thrown into a pit of quality.

  • I guess there's also a question of "how far" the compiler should look when it comes to determining whether or not something is unreachable - in the general case this requires solving the halting problem, so there needs to be some limit there. And while "I can't determine whether this is reachable or not" is fine for issuing a warning (there's no major consequence if you choose "I can't determine" instead of "definitely unreachable"), it's a whole lot less fine if your build fails when something isn't definitely-unreachable.

    Indeed. See http://blogs.msdn.com/b/ericlippert/archive/2011/02/24/never-say-never-part-two.aspx for some thoughts on reachability and the halting problem -- Eric

  • "Should BlorgGlob(blorgable) be an error?"

    If prolonged exposure to the VS11 Beta UI has reduced Eric to spouting this kind of gibberish on his blog, don't I have bigger things to worry about? :)

    We're still using VS10 on the Roslyn team. We start dogfooding VS11 soon. I'm reserving judgment until I actually use the thing! :-) -- Eric

  • @J: All warnings are errors. (Except the ones that aren't, of course). How far the compiler *actually* looks is extremely clearly defined: control statement's controlled by compile time expressions* evaluating to false are unreachable, but I'm assuming you mean how far *should* it look. My position is that just because some code *happens* to always be false at the moment, it doesn't mean I want the compiler to assume it's just an expensive "false". I'm a big fan of compile-time evaluation** for detailed control of the compiler, which I consider having complex conditions*** on when something was unreachable to be.

    * If I remember right, exactly the expressions whose terms are all "const" members or literal numbers or strings.

    ** I would love a language that had the ability to run arbitrary code at compile-time, assuming it's designed well enough to not surprise you! Well, more often than regular code surprises you, I guess.

    *** Conditions involving control flow, data structures, maybe even IO!

    @Eric: How the heck did you keep that description straight in your head? Without real-domain nouns and verbs I can't keep English descriptions straight in my head past about 2 sentences.

  • That method description is one of the most entertaining things I have read in a while.

  • That is the greatest documentation comment I have ever seen. Can we make BlorgFrob the Lorem Ipsum of code (rather than the potentially offensive foobar)?

  • Wasn't the spells named rezrov and blorb? If I remember Enchanter correctly...

  • "If you can't explain it simply, you don't understand it well enough."

    You sir, deserve a pat on the shoulder for this nice work.

  • So answer to "Why are local variables definitely assigned in unreachable statements?" is

    Because we (c# designer) think that you are messing up with something.

    But one question, then what is the use of having "default value"  concept in c# programming for local variables.

  • Is the pit of quality the antithesis of the Bog of Eternal Stench?

  • Just the other day I changed something, and some unrelated code that I didn't touch started getting the definite assignment error. It was puzzling at first because I didn't touch the code that had an error, nor had I changed the semantics of the program.

    In the end I had changed something so that the compiler could no longer determine that the code was unreachable, so it started telling me about the error that had already existed in it. I was going to mention something about it, but apparently you have mental telepathy so I didn't have to.

    Thanks a lot!

  • "Either that unreachable code is deliberate, or it is an error."

    Anything that is not deliberate is an error.

  • frob (noun) A small object or device.

    frob (verb) To manipulate a frob.

    rezrov - Open a locked or enchanted object.

    blorg - a variant of blorb?

    blorb - Safely protect a small object as though in a strong box.

  • Thanks for an interesting post, Erik! The comments are simply too good to be true.

    I have often wondered about Microsoft's compilers (C, C++, C#) on this point. The example you give does produce a warning - which is often an error. However, the following example compiles with no warning:

    static void Main(string[] args)

    {

               var reachme = false;

               if (reachme)

                 Console.WriteLine("Hallo world!");

    }

    The code is unreachable at all times - just as in your example - so why doesn't this emit a warning? The Watcom compiler did that with code like this...

    Just a question spawned from your post.

  • "We do in fact automatically initialize locals to their default values."

    Hello! Why do you do that, if we need to "definitely assign" a local to read from it?

Page 1 of 2 (24 items) 12