Skip to content

Commit

Permalink
Fix error message when trying to use using-static with an invalid type (
Browse files Browse the repository at this point in the history
#68571)

* Fix error message when trying to use using-static with an invalid type

* Update

* comma

* suppress nested diagnostics
  • Loading branch information
CyrusNajmabadi authored Jun 13, 2023
1 parent 445f2c6 commit 2807574
Show file tree
Hide file tree
Showing 20 changed files with 465 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,18 @@
<data name="IDS_SK_TYVAR" xml:space="preserve">
<value>type parameter</value>
</data>
<data name="IDS_SK_ARRAY" xml:space="preserve">
<value>array</value>
</data>
<data name="IDS_SK_POINTER" xml:space="preserve">
<value>pointer</value>
</data>
<data name="IDS_SK_FUNCTION_POINTER" xml:space="preserve">
<value>function pointer</value>
</data>
<data name="IDS_SK_DYNAMIC" xml:space="preserve">
<value>dynamic</value>
</data>
<data name="IDS_SK_ALIAS" xml:space="preserve">
<value>using alias</value>
</data>
Expand Down Expand Up @@ -7595,4 +7607,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_InterceptorCannotUseUnmanagedCallersOnly" xml:space="preserve">
<value>An interceptor cannot be marked with 'UnmanagedCallersOnlyAttribute'.</value>
</data>
<data name="ERR_BadUsingStaticType" xml:space="preserve">
<value>'{0}' type is not valid for 'using static'. Only a class, struct, interface, enum, delegate, or namespace can be used.</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 @@ -2218,6 +2218,8 @@ internal enum ErrorCode
ERR_InterceptorCannotInterceptNameof = 9160,
ERR_InterceptorCannotUseUnmanagedCallersOnly = 9161,

ERR_BadUsingStaticType = 9162,

#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 @@ -2341,6 +2341,7 @@ internal static bool IsBuildOnlyDiagnostic(ErrorCode code)
case ErrorCode.ERR_ConstantValueOfTypeExpected:
case ErrorCode.ERR_UnsupportedPrimaryConstructorParameterCapturingRefAny:
case ErrorCode.ERR_InterceptorCannotUseUnmanagedCallersOnly:
case ErrorCode.ERR_BadUsingStaticType:
return false;
default:
// NOTE: All error codes must be explicitly handled in this switch statement
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/Errors/MessageID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ internal enum MessageID
IDS_FeatureAutoPropertyInitializer = MessageBase + 12649,

IDS_SK_TYPE_OR_NAMESPACE = MessageBase + 12652,
IDS_SK_ARRAY = MessageBase + 12653,
IDS_SK_POINTER = MessageBase + 12654,
IDS_SK_FUNCTION_POINTER = MessageBase + 12655,
IDS_SK_DYNAMIC = MessageBase + 12656,

IDS_Contravariant = MessageBase + 12659,
IDS_Contravariantly = MessageBase + 12660,
IDS_Covariant = MessageBase + 12661,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ UsingsAndDiagnostics buildUsings(

declarationBinder ??= compilation.GetBinderFactory(declarationSyntax.SyntaxTree).GetBinder(usingDirective.NamespaceOrType).WithAdditionalFlags(flags);
var imported = declarationBinder.BindNamespaceOrTypeSymbol(usingDirective.NamespaceOrType, directiveDiagnostics, basesBeingResolved).NamespaceOrTypeSymbol;
bool addDirectiveDiagnostics = true;

if (imported.Kind == SymbolKind.Namespace)
{
Expand Down Expand Up @@ -814,6 +815,15 @@ UsingsAndDiagnostics buildUsings(
}
}
}
else if (imported.Kind is SymbolKind.ArrayType or SymbolKind.PointerType or SymbolKind.FunctionPointerType or SymbolKind.DynamicType)
{
diagnostics.Add(ErrorCode.ERR_BadUsingStaticType, usingDirective.NamespaceOrType.Location, imported.GetKindText());

// Don't bother adding sub diagnostics (like that an unsafe type was referenced). The
// primary thing we want to report is simply that the using-static points to something
// entirely invalid.
addDirectiveDiagnostics = false;
}
else if (imported.Kind != SymbolKind.ErrorType)
{
// Do not report additional error if the symbol itself is erroneous.
Expand All @@ -825,7 +835,11 @@ UsingsAndDiagnostics buildUsings(
MessageID.IDS_SK_TYPE_OR_NAMESPACE.Localize());
}

diagnostics.AddRange(directiveDiagnostics.DiagnosticBag);
if (addDirectiveDiagnostics)
{
diagnostics.AddRange(directiveDiagnostics.DiagnosticBag);
}

directiveDiagnostics.Free();
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/Compilers/CSharp/Portable/Symbols/SymbolKindExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public static LocalizableErrorArgument Localize(this SymbolKind kind)
return MessageID.IDS_SK_TYPE.Localize();
case SymbolKind.TypeParameter:
return MessageID.IDS_SK_TYVAR.Localize();
case SymbolKind.ArrayType:
return MessageID.IDS_SK_ARRAY.Localize();
case SymbolKind.PointerType:
return MessageID.IDS_SK_POINTER.Localize();
case SymbolKind.FunctionPointerType:
return MessageID.IDS_SK_FUNCTION_POINTER.Localize();
case SymbolKind.DynamicType:
return MessageID.IDS_SK_DYNAMIC.Localize();
case SymbolKind.Method:
return MessageID.IDS_SK_METHOD.Localize();
case SymbolKind.Property:
Expand Down
25 changes: 25 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.

25 changes: 25 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.

25 changes: 25 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.

25 changes: 25 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.

25 changes: 25 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.

25 changes: 25 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.

25 changes: 25 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.

Loading

0 comments on commit 2807574

Please sign in to comment.