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

Implement parsing for Global Using Directive. #51426

Conversation

AlekseyTs
Copy link
Contributor

@AlekseyTs AlekseyTs commented Feb 23, 2021

Test plan: #51307

@AlekseyTs AlekseyTs requested a review from a team as a code owner February 23, 2021 22:29
break;

case SyntaxKind.IdentifierToken:
if (this.CurrentToken.ContextualKind != SyntaxKind.GlobalKeyword || this.PeekToken(1).Kind != SyntaxKind.UsingKeyword)
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi Feb 23, 2021

Choose a reason for hiding this comment

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

fwiw, i think tihs would be much clearer if hte if was flipped and the condition was just if (CurrentToken == Global && PeekToken(1) == Using). #WontFix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fwiw, i think tihs would be much clearer if hte if was flipped and the condition was just if (CurrentToken == Global && PeekToken(1) == Using).

I prefer to structure the code in this case section and in the previous section as close as possible.


In reply to: 581474923 [](ancestors = 581474923)

@AlekseyTs
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@@ -324,6 +325,7 @@ internal static LanguageVersion RequiredVersion(this MessageID feature)
{
// C# preview features.
case MessageID.IDS_FeatureMixedDeclarationsAndExpressionsInDeconstruction:
case MessageID.IDS_FeatureUsingGlobal:
Copy link
Member

@jcouv jcouv Feb 24, 2021

Choose a reason for hiding this comment

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

IDS_FeatureUsingGlobal [](start = 31, length = 22)

nit: "GlobalUsing" #Closed

if (staticToken != null)
{
usingDirective = CheckFeatureAvailability(usingDirective, MessageID.IDS_FeatureUsingStatic);
}

if (globalToken != null)
{
usingDirective = CheckFeatureAvailability(usingDirective, MessageID.IDS_FeatureUsingGlobal);
Copy link
Member

@jcouv jcouv Feb 24, 2021

Choose a reason for hiding this comment

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

nit: why put the diagnostic on the whole directive? I expected we'd check the LangVer when we eat the token above (line 756) and put the diagnostic on globalToken (it's a little more specific) #ByDesign

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nit: why put the diagnostic on the whole directive? I expected we'd check the LangVer when we eat the token above (line 756) and put the diagnostic on globalToken (it's a little more specific)

I am following the pattern already established for the static keyword.


In reply to: 581664800 [](ancestors = 581664800)

var @using = this.ParseUsingDirective();
if (seen > NamespaceParts.Usings)
{
@using = this.AddError(@using, ErrorCode.ERR_UsingAfterElements);
Copy link
Member

@jcouv jcouv Feb 24, 2021

Choose a reason for hiding this comment

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

If I recall correctly, adding an order error during parsing (as opposed to binding) has impact on incremental parsing that we generally want to avoid.
On the other hand, it looks like we already do it for top-level statements.
https://github.com/dotnet/roslyn/blob/master/docs/compilers/Design/Parser.md#do-not-use-ambient-context-to-direct-the-behavior-of-the-parser #Closed

Copy link
Member

@CyrusNajmabadi CyrusNajmabadi Feb 24, 2021

Choose a reason for hiding this comment

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

If I recall correctly, adding an order error during parsing (as opposed to binding) has impact on incremental parsing that we generally want to avoid.

this is true. however, in this case, this is the right thing to do. We effectively have no place to put this using-directive as we're now past the location in the CompliationUnitSyntax where we could store them. So we instead have to break this down and attach to whatever real construct we find.

If we had made CompilationUnitSyntax just have a list of generic member nodes, we could have potentially kept the using directive here and error'ed later. However, that would have come with the negative of not having a nice strongly typed API for CompilationUnit that explicitly breaks out the different types of children. #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I recall correctly, adding an order error during parsing (as opposed to binding) has impact on incremental parsing that we generally want to avoid.

This is a preexisting behavior of the parser. No change is introduced here. The code is simply extracted into a local function and reused.


In reply to: 581664827 [](ancestors = 581664827)

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense. Thanks


In reply to: 581718358 [](ancestors = 581718358)

Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

LGTM Thanks (iteration 1)

@jcouv jcouv self-assigned this Feb 24, 2021
@jcouv jcouv added this to the C# 10 milestone Feb 24, 2021
@@ -6600,4 +6600,13 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_FunctionPointerTypesInAttributeNotSupported" xml:space="preserve">
<value>Using a function pointer type in a 'typeof' in an attribute is not supported.</value>
</data>
<data name="IDS_FeatureUsingGlobal" xml:space="preserve">
<value>global using directive</value>
Copy link
Member

@Youssef1313 Youssef1313 Feb 24, 2021

Choose a reason for hiding this comment

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

Should this have a comment for localization team? #WontFix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should this have a comment for localization team?

Historically we do not do this.


In reply to: 581753713 [](ancestors = 581753713)

Copy link
Member

@Youssef1313 Youssef1313 Feb 24, 2021

Choose a reason for hiding this comment

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

@AlekseyTs There are few resources that add similar comments for loc team:

<comment>UnmanagedCallersOnly is not localizable.</comment>

This results in better translations. Was there a reason to not do this? (I see the resources I'm referring to are very very recent) #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Was there a reason to not do this?

In my opinion there was no a good reason to add those comments.


In reply to: 582073782 [](ancestors = 582073782)

@AlekseyTs
Copy link
Contributor Author

@dotnet/roslyn-compiler Please review, need a second sign-off.

@@ -53,6 +57,14 @@ public bool HasAssemblyAttributes
}
}

public override bool HasGlobalUsings
Copy link
Member

@Youssef1313 Youssef1313 Feb 24, 2021

Choose a reason for hiding this comment

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

Does SingleNamespaceDeclarationEx need to override this as well? I'm not sure what SingleNamespaceDeclarationEx is used for, so just noting about it. #ByDesign

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does SingleNamespaceDeclarationEx need to override this as well? I'm not sure what SingleNamespaceDeclarationEx is used for, so just noting about it.

Namespace declarations are not allowed to have Global Using directives. For the purpose of error recovery all usings there will be treated as non-global, therefore, there are no Global Usings there.


In reply to: 582082881 [](ancestors = 582082881)

@@ -10106,7 +10107,7 @@ private static ExternAliasDirectiveSyntax GenerateExternAliasDirective()
=> SyntaxFactory.ExternAliasDirective(SyntaxFactory.Token(SyntaxKind.ExternKeyword), SyntaxFactory.Token(SyntaxKind.AliasKeyword), SyntaxFactory.Identifier("Identifier"), SyntaxFactory.Token(SyntaxKind.SemicolonToken));

private static UsingDirectiveSyntax GenerateUsingDirective()
=> SyntaxFactory.UsingDirective(SyntaxFactory.Token(SyntaxKind.UsingKeyword), default(SyntaxToken), default(NameEqualsSyntax), GenerateIdentifierName(), SyntaxFactory.Token(SyntaxKind.SemicolonToken));
=> SyntaxFactory.UsingDirective(default(SyntaxToken), SyntaxFactory.Token(SyntaxKind.UsingKeyword), default(SyntaxToken), default(NameEqualsSyntax), GenerateIdentifierName(), SyntaxFactory.Token(SyntaxKind.SemicolonToken));
Copy link
Member

@Youssef1313 Youssef1313 Feb 24, 2021

Choose a reason for hiding this comment

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

nit: the old overload is now not tested. #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nit: the old overload is now not tested.

I find existing usage of that overload in the solution sufficient to provide coverage.


In reply to: 582088799 [](ancestors = 582088799)

@AlekseyTs AlekseyTs merged commit b0019ba into dotnet:features/GlobalUsingDirective Feb 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants