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

Question: is extern_alias_directive misclassification instead of error intentional? #199

Open
am11 opened this issue Jul 27, 2021 · 2 comments

Comments

@am11
Copy link

am11 commented Jul 27, 2021

According to compiler error CS0439, extern_alias_directive must precede all other elements defined in the namespace. Meaning, only comments, preprocessor directives and extern alias can precede another extern alias .

e.g. this valid case

extern alias A;
extern Foo B;

using AT = A::NS.T;

becomes invalid if we swap the order of extern and extern alias:

extern Foo B;
extern alias A;

using AT = A::NS.T;

tree-sitter-c-sharp however does not error out in this case but instead changes:

  extern_alias_directive [0, 0] - [0, 15]
    identifier [0, 13] - [0, 14]

to

  global_statement [1, 0] - [1, 15]
    local_declaration_statement [1, 0] - [1, 15]
      modifier [1, 0] - [1, 6]
      variable_declaration [1, 7] - [1, 14]
        type: identifier [1, 7] - [1, 12]
        variable_declarator [1, 13] - [1, 14]
          identifier [1, 13] - [1, 14]

after the swap (i.e. alias becomes a separate type identifier).

Is this an intentional behavior?

@damieng
Copy link
Contributor

damieng commented Jul 27, 2021

tree-sitter-c-sharp is not as exact as a compiler and is more forgiving as it has a different role. We (and other tree-sitter grammars) intentionally wrote the grammars to support snippets of code - so for example we supported top-level statements such as just writing

int c = 0;

Long before C#/Roslyn actually supported that itself. In this way tree-sitter-c-sharp and other tree-sitter grammars could be used in more places than an actual compiler/strict parser.

It looks to me that this is one of those cases where it is doing just that. There are other places as well where we do not force strict order - this is also intentional.

You can find out a bit more about the goals of tree-sitter at https://tree-sitter.github.io/tree-sitter/

@am11
Copy link
Author

am11 commented Jul 27, 2021

I agree that the strictness of parser not necessarily match roslyn.

However, in this case, emitting extern_alias_directive in first case, but not the second (and treating alias as a separate directive) makes an interesting edge case. i.e. it looks like we do respond to CS0439 rules to certain degree in a sense that we seemingly do classify when it is preceded by another statement vs. when it isn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants