dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.02k stars 4.03k forks source link

Parse error in not-quite-ambiguity around "as" #13839

Open gafter opened 8 years ago

gafter commented 8 years ago

The following should parse without a problem

        M(e as A < B, C > D);

and mean the same thing as this

        M((e as A) < B, C > D);

but Roslyn gives a syntax error.

Here is a more complete context in which there should be no syntactic or semantic errors

class Program
{
    const int B = 2;
    const int C = 3;
    const int D = 4;
    static void Main(string[] args)
    {
        object e = null;
        M((e as A) < B, C > D);
        M(e as A<B, C> D);
    }
    static void M(bool b, bool c)
    {
    }
}

class A
{
    public static bool operator <(A a, int B) => true;
    public static bool operator >(A a, int B) => false;
}
Youssef1313 commented 3 years ago

Per spec, < has higher precedence than as. so isn't the current behavior By Design?

gafter commented 3 years ago

@Youssef1313 The right-hand operand of as is not an expression, so the precedence rules don't really apply. Use the grammar for more precision.