Monthly Archives: November 2011

J’accuse

Sometimes things go wrong while you’re writing them. And it’s all the compiler’s fault – there absolutely has to be a problem with it. Oh wait, perhaps it’s actually just a bad bug in the library. No – maybe it’s the other guy on the team who did that weird checkin yesterday, and something’s gone wrong with that.

Actually, it’s probably just you. Almost all of the time, you’ll have a revelatory moment where you realize that you’ve missed something. It could be glaring, or it could be subtle. But there’s a facepalm moment, and then you move on with your life.

But not too long ago, I did enjoy independently discovering this little bug in the C# 4 compiler. It’s a very minor one, but its diminutive status did not decrease my immense enjoyment at its discovery. Behold: an incorrect compiler warning!

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication
{
    /// <summary>
    /// This example demonstrates a small bug in the C# compiler, which
    /// generates a CS0649 compiler warning saying that the "Field" field
    /// is not used.
    /// </summary>
    public class Program
    {
        // making this private triggers the compiler warning
        private class Example
        {
            public DateTime Field;
        }

        static void Main(string[] args)
        {
            var bug = new List<DateTime>()
                .AsQueryable()
                .Select(dt =>
                    new Example { Field = dt } // this object initializer isn't spotted by the compiler
                );
        }
    }
}

I reported this via Microsoft Connect, and I got a very polite response from a Microsoft PM, who (correctly) did not triage it with any great urgency. But it’s still a notch on the old programming belt.

This does of course mean that after years of conditioning myself to expect that I am the culprit, I am back to believing that everything is actually the fault of the compiler/library/etc. Such is life!