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

Disallow modifiers in local_using_declaration #72589

Merged
merged 9 commits into from
Mar 22, 2024
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: 3 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -7902,4 +7902,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_ParamsCollectionMissingConstructor" xml:space="preserve">
<value>Non-array params collection type must have an applicable constructor that can be called with no arguments.</value>
</data>
<data name="ERR_NoModifiersOnUsing" xml:space="preserve">
<value>Modifiers cannot be placed on using declarations</value>
</data>
</root>
2 changes: 2 additions & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2300,6 +2300,8 @@ internal enum ErrorCode
ERR_ParamsCollectionExtensionAddMethod = 9227,
ERR_ParamsCollectionMissingConstructor = 9228,

ERR_NoModifiersOnUsing = 9229,

#endregion

// Note: you will need to do the following after adding warnings:
Expand Down
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2430,6 +2430,7 @@ internal static bool IsBuildOnlyDiagnostic(ErrorCode code)
case ErrorCode.ERR_ParamsCollectionExpressionTree:
case ErrorCode.ERR_ParamsCollectionExtensionAddMethod:
case ErrorCode.ERR_ParamsCollectionMissingConstructor:
case ErrorCode.ERR_NoModifiersOnUsing:
return false;
default:
// NOTE: All error codes must be explicitly handled in this switch statement
Expand Down
24 changes: 16 additions & 8 deletions src/Compilers/CSharp/Portable/Parser/LanguageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9575,7 +9575,7 @@ private StatementSyntax ParseLocalDeclarationStatement(SyntaxList<AttributeListS
}

var mods = _pool.Allocate();
this.ParseDeclarationModifiers(mods);
this.ParseDeclarationModifiers(mods, isUsingDeclaration: usingKeyword is not null);

var variables = _pool.AllocateSeparated<VariableDeclaratorSyntax>();
try
Expand Down Expand Up @@ -9623,13 +9623,17 @@ private StatementSyntax ParseLocalDeclarationStatement(SyntaxList<AttributeListS
type = _syntaxFactory.ScopedType(scopedKeyword, type);
}

for (int i = 0; i < mods.Count; i++)
// We've already reported all modifiers for local_using_declaration as errors
if (usingKeyword is null)
{
var mod = (SyntaxToken)mods[i];

if (IsAdditionalLocalFunctionModifier(mod.ContextualKind))
for (int i = 0; i < mods.Count; i++)
{
mods[i] = this.AddError(mod, ErrorCode.ERR_BadMemberFlag, mod.Text);
var mod = (SyntaxToken)mods[i];

if (IsAdditionalLocalFunctionModifier(mod.ContextualKind))
{
mods[i] = this.AddError(mod, ErrorCode.ERR_BadMemberFlag, mod.Text);
}
}
}

Expand Down Expand Up @@ -9824,7 +9828,7 @@ private bool IsEndOfDeclarationClause()
}
}

private void ParseDeclarationModifiers(SyntaxListBuilder list)
private void ParseDeclarationModifiers(SyntaxListBuilder list, bool isUsingDeclaration)
{
SyntaxKind k;
while (IsDeclarationModifier(k = this.CurrentToken.ContextualKind) || IsAdditionalLocalFunctionModifier(k))
Expand All @@ -9845,7 +9849,11 @@ private void ParseDeclarationModifiers(SyntaxListBuilder list)
mod = this.EatToken();
}

if (k is SyntaxKind.ReadOnlyKeyword or SyntaxKind.VolatileKeyword)
if (isUsingDeclaration)
{
mod = this.AddError(mod, ErrorCode.ERR_NoModifiersOnUsing);
}
else if (k is SyntaxKind.ReadOnlyKeyword or SyntaxKind.VolatileKeyword)
{
mod = this.AddError(mod, ErrorCode.ERR_BadMemberFlag, mod.Text);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading