Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8.2.5 Grammar ambiguities #180

Open
gafter opened this issue Jun 7, 2019 · 4 comments
Open

8.2.5 Grammar ambiguities #180

gafter opened this issue Jun 7, 2019 · 4 comments
Labels
type: bug The Standard does not describe the language as intended or implemented

Comments

@gafter
Copy link
Member

gafter commented Jun 7, 2019

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

x = y is C<T> && z;

does not trigger disambiguation: "the tokens C<T> 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<T> 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<T> rather than the type C followed by less-than operator. The following program demonstrates that this could be a valid parse:

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 G<A, B>7).

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

@gafter
Copy link
Member Author

gafter commented Jun 7, 2019

While existing compilers cannot parse

        return y is C < t > z;

they can parse

        return y is C > t > z;

The addition of pattern-matching in C# 7 takes advantage of this to give a new meaning to the former.

@jskeet
Copy link
Contributor

jskeet commented Oct 13, 2020

This is probably worth including in our discussion of ANTLR grammars tomorrow.

@jskeet jskeet transferred this issue from another repository Jan 12, 2021
@jskeet jskeet added meeting: discuss This issue should be discussed at the next TC49-TG2 meeting type: bug The Standard does not describe the language as intended or implemented labels Jan 12, 2021
@RexJaeschke
Copy link
Contributor

Just an FYI, we'll also touch this section to address a separate issue re adding support for out variables in V7. See #44, which might or might not impact how we deal with this issue now.

@jskeet jskeet removed the meeting: discuss This issue should be discussed at the next TC49-TG2 meeting label Jun 10, 2021
@gafter
Copy link
Member Author

gafter commented May 11, 2022

Disambiguating these kind of examples is explicitly handled in the PR for pattern-matching.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug The Standard does not describe the language as intended or implemented
Projects
None yet
Development

No branches or pull requests

3 participants