From b01555ee48205664d901e0c50cf9c204ceed00f5 Mon Sep 17 00:00:00 2001 From: Rikki Gibson Date: Fri, 24 Jun 2022 17:48:49 -0700 Subject: [PATCH] Give an error when a file type is used in a global using static --- .../CSharp/Portable/CSharpResources.resx | 3 + .../CSharp/Portable/Errors/ErrorCode.cs | 1 + .../SourceNamespaceSymbol.AliasesAndUsings.cs | 5 ++ .../Portable/xlf/CSharpResources.cs.xlf | 5 ++ .../Portable/xlf/CSharpResources.de.xlf | 5 ++ .../Portable/xlf/CSharpResources.es.xlf | 5 ++ .../Portable/xlf/CSharpResources.fr.xlf | 5 ++ .../Portable/xlf/CSharpResources.it.xlf | 5 ++ .../Portable/xlf/CSharpResources.ja.xlf | 5 ++ .../Portable/xlf/CSharpResources.ko.xlf | 5 ++ .../Portable/xlf/CSharpResources.pl.xlf | 5 ++ .../Portable/xlf/CSharpResources.pt-BR.xlf | 5 ++ .../Portable/xlf/CSharpResources.ru.xlf | 5 ++ .../Portable/xlf/CSharpResources.tr.xlf | 5 ++ .../Portable/xlf/CSharpResources.zh-Hans.xlf | 5 ++ .../Portable/xlf/CSharpResources.zh-Hant.xlf | 5 ++ .../Symbols/Source/FileModifierTests.cs | 55 ++++++++++++++++--- 17 files changed, 122 insertions(+), 7 deletions(-) diff --git a/src/Compilers/CSharp/Portable/CSharpResources.resx b/src/Compilers/CSharp/Portable/CSharpResources.resx index 139e9ce8ccbf4..941de7567e043 100644 --- a/src/Compilers/CSharp/Portable/CSharpResources.resx +++ b/src/Compilers/CSharp/Portable/CSharpResources.resx @@ -7115,6 +7115,9 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ File type '{0}' must be defined in a top level type; '{0}' is a nested type. + + File type '{0}' cannot be used in a 'global using static' directive. + unsigned right shift diff --git a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs index 255f4583dd594..f91117d77cb88 100644 --- a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs +++ b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs @@ -2096,6 +2096,7 @@ internal enum ErrorCode ERR_FileTypeNoExplicitAccessibility = 9301, ERR_FileTypeBase = 9302, ERR_FileTypeNested = 9303, + ERR_GlobalUsingStaticFileType = 9304 #endregion diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceNamespaceSymbol.AliasesAndUsings.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceNamespaceSymbol.AliasesAndUsings.cs index a3d202bebc10e..b1098e3ea273f 100644 --- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceNamespaceSymbol.AliasesAndUsings.cs +++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceNamespaceSymbol.AliasesAndUsings.cs @@ -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) ? diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf index 4edff182eda01..553ae7c029879 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf @@ -627,6 +627,11 @@ Globální direktiva using musí předcházet všem direktivám using, které nejsou globální. + + File type '{0}' cannot be used in a 'global using static' directive. + File type '{0}' cannot be used in a 'global using static' directive. + + A goto cannot jump to a location before a using declaration within the same block. Příkaz goto nemůže přejít na místo před deklarací using ve stejném bloku. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf index 0d5dbb6ca6d5b..541c2e0bd60d5 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf @@ -627,6 +627,11 @@ Eine globale using-Anweisung muss allen nicht globalen using-Anweisungen vorangehen. + + File type '{0}' cannot be used in a 'global using static' directive. + File type '{0}' cannot be used in a 'global using static' directive. + + A goto cannot jump to a location before a using declaration within the same block. Mit "goto" kann nicht an eine Position vor einer using-Deklaration im selben Block gesprungen werden. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf index c146398c4d563..371a0abd07553 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf @@ -627,6 +627,11 @@ Una directiva de uso global debe ser anterior a todas las directivas no globales que no son de uso. + + File type '{0}' cannot be used in a 'global using static' directive. + File type '{0}' cannot be used in a 'global using static' directive. + + A goto cannot jump to a location before a using declaration within the same block. Una instrucción goto no puede saltar a una ubicación antes que una declaración using dentro del mismo bloque. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf index 52798620fbaf0..f9c476fe63e8e 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf @@ -627,6 +627,11 @@ Une directive using globale doit précéder toutes les directives using non globales. + + File type '{0}' cannot be used in a 'global using static' directive. + File type '{0}' cannot be used in a 'global using static' directive. + + A goto cannot jump to a location before a using declaration within the same block. Un goto ne peut pas accéder à un emplacement avant une déclaration using dans le même bloc. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf index 177e269e704dc..9b21120e04b0f 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf @@ -627,6 +627,11 @@ Una direttiva sull'utilizzo globale deve precedere tutte le direttiva non sull'uso non globale. + + File type '{0}' cannot be used in a 'global using static' directive. + File type '{0}' cannot be used in a 'global using static' directive. + + A goto cannot jump to a location before a using declaration within the same block. Un'istruzione goto non può passare a una posizione che precede una dichiarazione using all'interno dello stesso blocco. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf index 49343625d8876..69f89a4cc4585 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf @@ -627,6 +627,11 @@ グローバル using ディレクティブは、すべての非グローバル using ディレクティブの前に指定する必要があります。 + + File type '{0}' cannot be used in a 'global using static' directive. + File type '{0}' cannot be used in a 'global using static' directive. + + A goto cannot jump to a location before a using declaration within the same block. goto は同じブロック内の using 宣言より前の位置にはジャンプできません。 diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf index 0199e0ab379a3..fe1ad3bc7b2cc 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf @@ -627,6 +627,11 @@ 전역 using 지시문은 전역이 아닌 모든 using 지시문 앞에 있어야 합니다. + + File type '{0}' cannot be used in a 'global using static' directive. + File type '{0}' cannot be used in a 'global using static' directive. + + A goto cannot jump to a location before a using declaration within the same block. goto는 동일한 블록 내의 using 선언 앞 위치로 이동할 수 없습니다. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf index bfe3edd993e2e..c8f32f240de6e 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf @@ -627,6 +627,11 @@ Globalne używające dyrektywy muszą poprzedzać wszystkie nieglobalne używające dyrektywy. + + File type '{0}' cannot be used in a 'global using static' directive. + File type '{0}' cannot be used in a 'global using static' directive. + + A goto cannot jump to a location before a using declaration within the same block. Instrukcja goto nie może przechodzić do lokalizacji występującej przed deklaracją using w tym samym bloku. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf index 51ef2f7c6d194..19a4209e27893 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf @@ -627,6 +627,11 @@ Uma diretiva de uso global deve preceder todas as diretivas de uso não global. + + File type '{0}' cannot be used in a 'global using static' directive. + File type '{0}' cannot be used in a 'global using static' directive. + + A goto cannot jump to a location before a using declaration within the same block. Um goto não pode saltar para um local antes de uma declaração using no mesmo bloco. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf index 49fe2ce705ee8..610c0398dd0f6 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf @@ -627,6 +627,11 @@ Глобальная директива using должна предшествовать всем неглобальным директивам using. + + File type '{0}' cannot be used in a 'global using static' directive. + File type '{0}' cannot be used in a 'global using static' directive. + + A goto cannot jump to a location before a using declaration within the same block. Оператор goto не может переходить к расположению раньше объявления using в том же блоке. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf index 9daaaba095219..f0dc2c30b3dfd 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf @@ -627,6 +627,11 @@ Genel bir using yönergesi genel olmayan tüm using yönergelerinden önce gelmelidir. + + File type '{0}' cannot be used in a 'global using static' directive. + File type '{0}' cannot be used in a 'global using static' directive. + + A goto cannot jump to a location before a using declaration within the same block. Bir goto, aynı blok içinde yer alan using bildiriminden önceki bir konuma atlayamaz. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf index 211e53063cd85..0252b3abb7b06 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf @@ -627,6 +627,11 @@ 全局 using 指令必须位于所有非全局 using 指令之前。 + + File type '{0}' cannot be used in a 'global using static' directive. + File type '{0}' cannot be used in a 'global using static' directive. + + A goto cannot jump to a location before a using declaration within the same block. goto 无法跳转到同一块中 using 声明之前的某个位置。 diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf index 9ac1bddbf4c14..d8d561655ec9e 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf @@ -627,6 +627,11 @@ 全域 using 指示詞必須在所有非全域 using 指示詞之前。 + + File type '{0}' cannot be used in a 'global using static' directive. + File type '{0}' cannot be used in a 'global using static' directive. + + A goto cannot jump to a location before a using declaration within the same block. 在相同區塊內,goto 不可跳到 using 宣告前的位置。 diff --git a/src/Compilers/CSharp/Test/Symbol/Symbols/Source/FileModifierTests.cs b/src/Compilers/CSharp/Test/Symbol/Symbols/Source/FileModifierTests.cs index 0fbb15b774fbb..dfc93da7eedfc 100644 --- a/src/Compilers/CSharp/Test/Symbol/Symbols/Source/FileModifierTests.cs +++ b/src/Compilers/CSharp/Test/Symbol/Symbols/Source/FileModifierTests.cs @@ -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() + { + var source = """ + global using static Container; + + public class Container + { + } + + 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; + Diagnostic(ErrorCode.HDN_UnusedUsingDirective, "global using static Container;").WithLocation(1, 1), + // (1,21): error CS9304: File type 'Container' cannot be used in a 'global using static' directive. + // global using static Container; + Diagnostic(ErrorCode.ERR_GlobalUsingStaticFileType, "Container").WithArguments("Container").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]