Skip to content

Ambiguous description of ambiguous syntax #5638

@Joe-K-Sewell

Description

@Joe-K-Sewell

This page notes that because casting uses an identifier surrounded by parentheses, some expressions may be ambiguous. It's not clear, though, what implication ambiguous syntax has for a programmer. Is there a possibility of the compiler interpreting it one way, but later being updated to interpret it another way? Should the developer take care when using third-party compilers, such as that for Mono? Or is the interpretation well-specified for any ECMA-334 compliant compiler?

For the example given, (x)-y, it looks like the current behavior (VS 2017 15.5.5, C# 7.2) actually does disambiguate the cases:

  • If identifiers x and y both denote variables, there is no compiler error or warning, and the expression is treated as x - y (y subtracted from x).
  • If identifier x denotes a type and y denotes a variable, the compiler emits error CS0075 (even if an explicit conversion exists). This error cites the language specification section on cast expressions, which repeats this example and notes that there is a disambiguation rule that the compiler is following.

Given that there is a disambiguation rule in the spec, should this page even mention that there could be an ambiguity? It seems like "ambiguous grammar" is an implementation detail resolved by an additional rule on the compiler. Or is there another reason to caution developers when using the cast operator?


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions