54

I noticed that if I write something like:

static void Main(string[] args)
{
    const const const bool flag = true;
}

The compiler doesn't warn me of the multiple consts. So this seems to mimic C modifiers, as they are idempotent.

However, if I write:

private readonly readonly int a;

The compiler does warn me of the duplicated readonly.

So what's going on here? Are modifiers idempotent or not?


csc version 1.0.0.50618

DavidG
  • 113,891
  • 12
  • 217
  • 223
xdevel2000
  • 20,780
  • 41
  • 129
  • 196
  • 13
    That the first line compiles is a bug in whichever compiler you're using - which you haven't told us. It doesn't compile with Roslyn... – Jon Skeet Nov 13 '15 at 14:28
  • I'm not sure I see what your examples have to do with being idempotent. – Bradley Uffner Nov 13 '15 at 14:28
  • 1
    This doesn't compile with C#5 either. – DavidG Nov 13 '15 at 14:28
  • 1
    Visual Studio 2010 gives out an error as well. – waka Nov 13 '15 at 14:29
  • 2
    @JonSkeet It doesn't compile if you try to put it as field. It does compile inside a method `static void Main(string[] args) { const const const bool flag = true; }` – Jakub Lortz Nov 13 '15 at 14:30
  • 2
    @JakubLortz: Nope, still creates an error (`Type expected`) in VS2010. – waka Nov 13 '15 at 14:31
  • As @JakubLortz points out, this WILL compile with Roslyn only if it's in a method. – DavidG Nov 13 '15 at 14:31
  • 1
    Monodevelop doesn't allow it either (even in static method) – Tomislav Muic Nov 13 '15 at 14:34
  • 3
    @JakubLortz: Ah, thanks for that. This is why short but *complete* examples are important. Will double check that it's invalid according to the spec, but it does look like it's just a compiler bug. – Jon Skeet Nov 13 '15 at 14:40
  • @SteffenWinkler This really didn't need the Visual Studio tag adding, please don't suggest edits that add nothing to the question. – DavidG Nov 13 '15 at 17:00
  • @DavidG uh yeah it did. It's only relevant to VS 2015. – Steffen Winkler Nov 13 '15 at 17:43
  • 7
    @SteffenWinkler No, it applies only to C# version 6, it's nothing to do with the version of Visual Studio. I could have written that code with Notepad/Sublime and compiled it that way. – DavidG Nov 13 '15 at 17:45
  • 2
    `C# version 6` which you only get with Visual Studio 2015. Mono doesn't show the same problem, and the .NET compiler (roslyn) is bundled with Visual Studio. Also the amount of people that would do what you just described can probably be counted on one hand for the entire world, which means for most people this is only relevant if they use Visual Studio 2015. – Steffen Winkler Nov 13 '15 at 17:50
  • 2
    @SteffenWinkler That handful of people are the most likely to actually try to repeat a `const` qualifier multiple times on a local variable. =) Or to care when it actually succeeds to compile. – jpmc26 Nov 14 '15 at 02:53
  • hm, you may have a point there. @jpmc26 – Steffen Winkler Nov 14 '15 at 20:33

1 Answers1

69

It's a bug in the compiler - at least in Roslyn version 1.0.0.50618. From section 8.5.2 of the C# 5 specification:

A local-constant-declaration declares one or more local constants.

local-constant-declaration:
   const type constant-declarators

constant-declarators:
  constant-declarator
  constant-declarators , constant-declarator

constant-declarator:   identifier = constant-expression

As you can see, that grammar doesn't allow for const const const bool flag = true;.

I've filed a bug against Roslyn so that it can get fixed.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194