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

TokenType: Tests and support for scoped type declarations #8061

Merged
merged 3 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion analyzers/src/SonarAnalyzer.CFG/ShimLayer/SyntaxKindEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static class SyntaxKindEx
public const SyntaxKind Utf8StringLiteralToken = (SyntaxKind)8520;
public const SyntaxKind Utf8SingleLineRawStringLiteralToken = (SyntaxKind)8521;
public const SyntaxKind Utf8MultiLineRawStringLiteralToken = (SyntaxKind)8522;
public const SyntaxKind PragmaChecksumDirectiveTrivia = (SyntaxKind)8560;
public const SyntaxKind ConflictMarkerTrivia = (SyntaxKind)8564;
public const SyntaxKind IsPatternExpression = (SyntaxKind)8657;
public const SyntaxKind RangeExpression = (SyntaxKind)8658;
Expand Down Expand Up @@ -87,6 +88,6 @@ public static class SyntaxKindEx
public const SyntaxKind InterpolatedSingleLineRawStringStartToken = (SyntaxKind)9072;
public const SyntaxKind InterpolatedMultiLineRawStringStartToken = (SyntaxKind)9073;
public const SyntaxKind InterpolatedRawStringEndToken = (SyntaxKind)9074;
public const SyntaxKind PragmaChecksumDirectiveTrivia = (SyntaxKind)8560;
public const SyntaxKind ScopedType = (SyntaxKind)9075;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ private static bool IsInTypeContext(SimpleNameSyntax name) =>
{ RawKind: (int)SyntaxKindEx.FileScopedNamespaceDeclaration } x => ((FileScopedNamespaceDeclarationSyntaxWrapper)x).Name == name,
{ RawKind: (int)SyntaxKindEx.TupleElement } x => ((TupleElementSyntaxWrapper)x).Type == name,
{ RawKind: (int)SyntaxKindEx.RefType } x => ((RefTypeSyntaxWrapper)x).Type == name,
{ RawKind: (int)SyntaxKindEx.ScopedType } x => ((ScopedTypeSyntaxWrapper)x).Type == name,
_ => false,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1286,4 +1286,47 @@ public void M(object o)
}
}
""", allowSemanticModel);

[DataTestMethod]
[DataRow("[k:scoped] ref S s2 = ref s1;", false)]
[DataRow("scoped ref [t:S] s2 = ref s1;", false)]
[DataRow("ref [t:S] s2 = ref s1;", false)]
[DataRow("scoped [t:S] s2 = s1;", false)]
[DataRow("[k:scoped] [k:ref] [k:readonly] [t:S] [u:s2] = [k:ref] [u:s1];", false)]
[DataRow("[k:int] [u:scoped] = 1;", false)]
public void IdentifierToken_Scoped_Local(string localDeclaration, bool allowSemanticModel) =>
ClassifierTestHarness.AssertTokenTypes($$"""
using System;

public ref struct S { }

public class C
{
public void M(ref S s1)
{
{{localDeclaration}}
}
}
""", allowSemanticModel);

[DataTestMethod]
[DataRow("[k:scoped] ref S s", false)]
[DataRow("scoped ref [t:S] s", false)]
[DataRow("ref [t:S] s", false)]
[DataRow("scoped [t:S] s", false)]
[DataRow("[k:scoped] [k:ref] [t:S] [u:s]", false)]
[DataRow("[k:int] [u:scoped]", false)]
public void IdentifierToken_Scoped_Parameter(string parameterDeclaration, bool allowSemanticModel) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: We could add a test combining scoped ref with readonly, when the reference is one to a readonly ref struct, just to make sure that readonly is interpreted as keyword, when following ref.

E.g. here, where GitHub fails at properly coloring it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not compile (readonly parameter are at implemented proposal stage dotnet/csharplang#6010 for C#12). But for locals it works and I added a test case.

ClassifierTestHarness.AssertTokenTypes($$"""
using System;

public ref struct S { }

public class C
{
public void M({{parameterDeclaration}})
{
}
}
""", allowSemanticModel);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: I would also add tests with a parameter and/or variable named scope. Unlike other keyword, that require escaping with @, scope has been introduced recently, and as such, is not tokenized as a keyword when used after the type:

int scoped = 3;

I think it would make sense to assert that scoped is of type u, and not k.

}