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

Give an error when a file type is used in a global using static #62258

Merged
merged 1 commit into from
Jun 30, 2022
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 @@ -7115,6 +7115,9 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_FileTypeNested" xml:space="preserve">
<value>File type '{0}' must be defined in a top level type; '{0}' is a nested type.</value>
</data>
<data name="ERR_GlobalUsingStaticFileType" xml:space="preserve">
<value>File type '{0}' cannot be used in a 'global using static' directive.</value>
</data>
<data name="IDS_FeatureUnsignedRightShift" xml:space="preserve">
<value>unsigned right shift</value>
</data>
Expand Down
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2096,6 +2096,7 @@ internal enum ErrorCode
ERR_FileTypeNoExplicitAccessibility = 9301,
ERR_FileTypeBase = 9302,
ERR_FileTypeNested = 9303,
ERR_GlobalUsingStaticFileType = 9304

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,11 @@ UsingsAndDiagnostics buildUsings(
else
{
var importedType = (NamedTypeSymbol)imported;
if (usingDirective.GlobalKeyword != default(SyntaxToken) && importedType.IsFileTypeOrUsesFileTypes())
{
diagnostics.Add(ErrorCode.ERR_GlobalUsingStaticFileType, usingDirective.Name.Location, imported);
}

if (!getOrCreateUniqueUsings(ref uniqueUsings, globalUsingNamespacesOrTypes).Add(importedType))
{
diagnostics.Add(!globalUsingNamespacesOrTypes.IsEmpty && getOrCreateUniqueGlobalUsingsNotInTree(ref uniqueGlobalUsings, globalUsingNamespacesOrTypes, declarationSyntax.SyntaxTree).Contains(imported) ?
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.

Original file line number Diff line number Diff line change
Expand Up @@ -2364,15 +2364,56 @@ public static void Main()
}
""";

// PROTOTYPE(ft): it should probably be an error to reference a file type in a global using static
var compilation = CreateCompilation(new[] { source, main });
compilation.VerifyDiagnostics(
// (1,1): hidden CS8019: Unnecessary using directive.
// global using static C;
Diagnostic(ErrorCode.HDN_UnusedUsingDirective, "global using static C;").WithLocation(1, 1),
// (5,9): error CS0103: The name 'M' does not exist in the current context
// M();
Diagnostic(ErrorCode.ERR_NameNotInContext, "M").WithArguments("M").WithLocation(5, 9));
// (1,1): hidden CS8019: Unnecessary using directive.
// global using static C;
Diagnostic(ErrorCode.HDN_UnusedUsingDirective, "global using static C;").WithLocation(1, 1),
// (1,21): error CS9304: File type 'C' cannot be used in a 'global using static' directive.
// global using static C;
Diagnostic(ErrorCode.ERR_GlobalUsingStaticFileType, "C").WithArguments("C").WithLocation(1, 21),
// (5,9): error CS0103: The name 'M' does not exist in the current context
// M();
Diagnostic(ErrorCode.ERR_NameNotInContext, "M").WithArguments("M").WithLocation(5, 9));
}

[Fact]
public void GlobalUsingStatic_02()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

note that "regular" UsingStatic tests are already present in this file.

{
var source = """
global using static Container<C>;

public class Container<T>
{
}

file class C
{
public static void M() { }
}
""";

var main = """
class Program
{
public static void Main()
{
M();
}
}
""";

var compilation = CreateCompilation(new[] { source, main });
compilation.VerifyDiagnostics(
// (1,1): hidden CS8019: Unnecessary using directive.
// global using static Container<C>;
Diagnostic(ErrorCode.HDN_UnusedUsingDirective, "global using static Container<C>;").WithLocation(1, 1),
// (1,21): error CS9304: File type 'Container<C>' cannot be used in a 'global using static' directive.
// global using static Container<C>;
Diagnostic(ErrorCode.ERR_GlobalUsingStaticFileType, "Container<C>").WithArguments("Container<C>").WithLocation(1, 21),
// (5,9): error CS0103: The name 'M' does not exist in the current context
// M();
Diagnostic(ErrorCode.ERR_NameNotInContext, "M").WithArguments("M").WithLocation(5, 9));
}

[Fact]
Expand Down