dotnet / dotnet/csharpstandard

8.2.5 Grammar ambiguities

Open
#180 4 comments 0 reactions 0 assignees View on GitHub
type: bug
Dominant language
C#
Stars
815
Forks
99
Avg merge
1d 14h
Merged PRs (30d)
16

Description

The resolution we took in https://github.com/ECMA-TC49-TG2/spec/issues/880 does not appear to be correct. The current spec now says that the example
``` c#
x = y is C && z;
```
does not trigger disambiguation: "the tokens `C` are interpreted as a _namespace-or-type-name_ with a _type-argument-list_ due to being on the right-hand side of the `is` operator (§13.11.1). Because `C` parses as a _namespace-or-type-name_, not a _simple-name_, _member-access_, or _pointer-member-access_, the above rule does not apply, and it is considered to have a _type-argument-list_ regardless of the token that follows."

As I show below, the token that follows needs to matter.

It is true that the disambiguation rules don't handle this case, but that still leaves the question as to why the right-hand-side of the `is` expression is the type `C` rather than the type `C` followed by less-than operator. The following program demonstrates that this could be a valid parse:
``` c#
public class Example
{
public static void Main()
{
}
static bool M(object y, C c, T t, Z z)
{
// return ((y is C) < t) > z;
return y is C < t > z;
}
}

class C
{
}

class T
{
public static bool operator <(bool left, T right) => true;
public static bool operator >(bool left, T right) => false;
}

class Z
{
public static bool operator <(bool left, Z right) => true;
public static bool operator >(bool left, Z right) => false;
}
```
Similarly, we may need to handle an expression such as `F(e is G7)`.

We might consider modifying the precedence of the `is` and `as` operators to forbid this. I believe existing compilers won't parse these examples.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.