Skip to content

Compiler errors for duplicate modifiers are inconsistent #66077

@DoctorKrolic

Description

@DoctorKrolic

Compiler error CS1004: Duplicate '{0}' modifier is reported during binding, so if we just parse a syntax tree like below, lets say, via CSharpSyntaxTree.ParseText...

public public class C
{
}

... it wouldn't contain any diagnostics at all.

The behaviour is different for statements though. If a stetement contains duplicate modifiers, error CS1031: Type expected is reported during parsing on every duplicate modifier. So code like:

class C
{
    void M()
    {
        const const const int a = 0;
    }
}

... produces 2 CS1031: Type expected errors on 2 const modifiers:
devenv_owYMsyShh7
For whatever reason sharplab doesn't properly render diagnostic locations for this case, but if you want to see it threre - here is the link
No additional errors are reported at the binding stage for statements.

This two mechanisms are mixed together for local functions. Local finctions are statements, so CS1031: Type expected is reported during parsing. However, at the binding stage error CS1004: Duplicate '{0}' modifier is reported as well, so we have 2 diagnostics on the same token:

class C
{
    void M()
    {
        static static void M() {}
    }
}

Sharplab link

Over all, there are several flaws with the current design, described above:

  1. No errors about duplicate declaration modifiers after parsing even though parser has all required information to report such errors
  2. Not intuitive CS1031: Type expected error for statements with duplicate modifiers even when the statement type is specified
  3. If there are several duplicate modifiers in a row, for declarations only 1 error is reported on the first duplicate modifier. For statements, there are several errors reported on every duplicate modifier
  4. Errors are produced during parsing for statements, but during binding for declarations
  5. 2 errors instead of 1 are reported for local finctions. Ideally, it should only be CS1004: Duplicate '{0}' modifier (see Improve diagnostics for duplicate modifiers on local functions #32106)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions