-
Couldn't load subscription status.
- Fork 4.2k
Description
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:

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() {}
}
}Over all, there are several flaws with the current design, described above:
- No errors about duplicate declaration modifiers after parsing even though parser has all required information to report such errors
- Not intuitive
CS1031: Type expectederror for statements with duplicate modifiers even when the statement type is specified - 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
- Errors are produced during parsing for statements, but during binding for declarations
- 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)