C# compiler bug

A spider in a web

Here’s one techie posting to alienate my regular readers (both of them) … I’ve encountered an apparent bug in the C# compiler in Visual Studio .Net 2003. Witness:

public interface ProductRow
{
	int Count { get ; }
	int Price { get ; }
}
public class ProductRowCollection
{
	public ProductRow this[int index]
	{
		get { return null; }
	}
	public int TotalPrice()
	{
		return ( this[0].Count * this[0].Price );
	}
}

(Yeah, I know the code is silly; I stripped it down for display purposes.)

This causes the compiler to bork with the following errors:

test.cs(14,20): error CS1513: } expected
test.cs(14,33): error CS1031: Type expected
test.cs(14,42): error CS1519: Invalid token ‘)’ in class, struct, or interface member declaration
test.cs(16,1): error CS1022: Type or namespace definition, or end-of-file expected

The first error is signalled on the reference to the Count property getter, a perfectly decent member of ProductRow. I am fairly sure the code is valid. The error seems to indicate something going awry in lexing.

It goes away if I put in a + instead of the * … maybe the compiler is confusing itself by mistaking the multiplication operator for a pointer indirection operator, for some reason? The C# language specification seems a little vague on the precedence and associativity of the pointer indirection operator; is it possible that this really is invalid code and the compiler is correctly rejecting it but just giving a shabby error message?

It also goes away if I comment it out and back in! This is really odd: I get this in Visual Studio, indicated with a red line under the Count reference, and I try to build and I get the compiler error … and then I comment the line out, then uncomment it again, and I don’t get a red line and it compiles! It makes sense that the syntax highlighting “snaps out of it” when I comment and uncomment … but the compiler too?

Of course that’s a shoddy, brittle, voodoo workaround, and sure enough, when I check it in it gets picked up by the automated build system and the same compiler error comes up there.

A sane workaround is to extract ProductRow row = this[0]; and write row.Count * row.Price … this way no error is raised. Probably judiciously applied parentheses would achieve the same.

I googled a little bit, but didn’t find anybody talking about this kind of glitch. So I hereby ceremoniously christen it The Marteinsson Mess, after my coworker who stumbled on it.

Update: this has been fixed in .Net Framework SDK v2.0 Beta 2.

3 Responses to “C# compiler bug”

  1. Kristín Says:

    zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz…………………….*!!

    Oh sorry… one of your TWO readers just dozed off…

  2. GÞB Says:

    Go ahead, try to claim I didn’t warn you! :)

    For the uninitiated but mildly curious, a compiler bug like this is for a programmer like a stone in a hiker’s boot … an annoyance that forces you to stop and work around the problem (take off the boot and clean it out). No biggie, just delays you a little bit.

    That’s this kind of compiler bug … the one that barfs an error message at you where there is no real error. There is another kind of compiler error that’s more like a scorpion in the boot … the kind that produces a program that does the wrong thing, and nobody knows it until you’ve sold the software to some huge corporation that then loses millions of dollars because of the bug and consequently sues your buttocks off.

    (Of course you deserve that for not testing your software properly before selling it. Always check your boots for scorpions before setting out.)

  3. Daniel Says:

    I am having the same problem running functions from my ASP.net C# code. I cannot imagine this being an unfixed bug. Is there some syntax issue that could cause this as well?