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

Get ArrayRankSpecifierSyntax where BracketedArgumentListSyntax is expected #31277

Open
TobiasBreuer opened this issue Nov 20, 2018 · 2 comments
Labels
Area-Compilers Bug Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings.
Milestone

Comments

@TobiasBreuer
Copy link

Version Used: 2.9.0

Steps to Reproduce:

  1. Create a Microsoft.CodeAnalysis.CSharp.Scripting.CSharpScript with following code:
public class TestMatrix
{
    private double dummy;
    public double this[int row, int col]
    {
        get
        {
            return 42;
        }
        set
        {
            dummy = value;
        }
    }
}

TestMatrix matrix = new TestMatrix();
// Writing the brackets here yields ArrayRankSpecifierSyntax
// actual statement should be matrix[0, 0] = 42;
matrix[] = 42;
// Writing the brackets here yields BracketedArgumentListSyntax 
// actual statement should be double test = matrix[0, 0]
double test = matrix[];
  1. Get the script compilation and the associated SyntaxTree
  2. Retrieve the Parent of the Token at position 680 (square brackets of the indexer assignment)

Expected Behavior:
The Token kind should be BracketedArgumentListSyntax

Actual Behavior:
The kind of the token is ArrayRankSpecifierSyntax

The kind changes if you start writing an index value into the squared brackets.
e.g.: Writing matrix[0] and then retrieving the token yields the BracketedArgumentListSyntax

The background is that I'm currently writing a completion provider for CSharp Scripts. When the user writes the opening bracket, the completion system shall provide with the required information e.g.:
image

Now the problem is that the token is only defined as BracketedArgumentListSyntax when the user already entered the first index and not when only the brackets are written.

Now the question is, is it expected (from a design point of view) that the token kind originally is ArrayRankSpecifierSyntax or is this a bug? It seems to make sense to me from the pure syntax-tree point of view, as it doesn't have the semantic context information to recognize the real type and recognize that it has an indexing operator. But on the other hand why does it then change when inserting a value into the brackets?
And why does it work for the second statement double test = matrix[] to immediately recognize this as the expected BracketedArgumentListSyntax when writing the brackets only?

@TobiasBreuer
Copy link
Author

Update As it turns out, the kind of the token is only ArrayRankSpecifierSyntax if the closing bracket is already inserted. When the code is only matrix[ without the closing bracket, then the token is also recognized as BracketedArgumentListSyntax.
So for me, this allows for a very hacky workaround to achieve what I'm after but it seems to be quite inconsistent to me (or I just don't get the point ;-))

@CyrusNajmabadi
Copy link
Member

but it seems to be quite inconsistent to me (or I just don't get the point ;-))

When code is syntactically correct, teh parser parses it appropriately. When it's syntactically incorrect, the parser makes a best effort to figure out what's going on and continues. Features that want to work while code is being typed need to understand and adapt to the forms the parser produces. Or, if the parser produces suboptimal forms in some error-recovery cases, then you can submit fixes to it to improve things. Cheers!

@gafter gafter added the Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. label Dec 3, 2018
@gafter gafter added this to the Backlog milestone Dec 3, 2018
@gafter gafter added the Bug label Sep 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Bug Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings.
Projects
None yet
Development

No branches or pull requests

4 participants