Add separate error for disallowed members in global namespace#81221
Add separate error for disallowed members in global namespace#81221AlekseyTs merged 20 commits intodotnet:mainfrom
Conversation
|
I know this is in draft, but i took a quick look. It's unclear why all teh changes are needed, versus just chnaging the resource string. |
|
@CyrusNajmabadi I could not think of a error message that would fit both situations properly. The best thing i could come with would be something similar to (that's why i went with two seperate errors in the first way) |
src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol.cs
Outdated
Show resolved
Hide resolved
|
Those new labels are probably wrong, i just totally screwed up merging |
|
@CyrusNajmabadi Would you maybe have a moment for a quick review? Or should i resolve the conflict first? |
| { | ||
| var parentSyntax = member.Parent; | ||
| var errorCode = parentSyntax.IsKind(SyntaxKind.CompilationUnit) | ||
| && parentSyntax.ChildNodes().Any(node => node.IsKind(SyntaxKind.GlobalStatement)) |
There was a problem hiding this comment.
this will do a linear walk on the compilation unit each time. instead of this, i would pass in the parent as the preceding argument (before 'members') in AddNonTypeMembers. Then compute and store if the comp-unit has global statements. (can just do parent is CompilationUnit compilationUnit && compilationUnit.Members.Any(m => m is GlobalStatementSyntax)).
| int[] exclude = new int[] { (int)ErrorCode.ERR_NamespaceUnexpected }; | ||
|
|
||
| compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( | ||
| // (3,21): error CS9344: The global namespace cannot directly contain members such as fields or properties |
There was a problem hiding this comment.
instead of this, add ERR_GlobalNamespaceUnexpected to exclude as well.
| [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/81141")] | ||
| public void TestFieldInTopLevelStatement() | ||
| { | ||
| var source = """ |
There was a problem hiding this comment.
for all new tessts, use raw-strings.
Looking good. Just a few things to adjust. |
| @@ -31248,12 +31248,30 @@ public static System.Action TakeOutParam<T>(T y, out T x) | |||
| int[] exclude = new int[] { (int)ErrorCode.ERR_NamespaceUnexpected }; | |||
| @@ -31421,12 +31439,30 @@ public static System.Action TakeOutParam<T>(T y, out T x) | |||
| int[] exclude = new int[] { (int)ErrorCode.ERR_NamespaceUnexpected }; | |||
| // (3,12): error CS0214: Pointers and fixed size buffers may only be used in an unsafe context | ||
| // fixed bool a[2], b[H.TakeOutParam(1, out var x1)]; | ||
| Diagnostic(ErrorCode.ERR_UnsafeNeeded, "a[2]").WithLocation(3, 12), | ||
| // (3,20): error CS0133: The expression being assigned to '<invalid-global-code>.b' must be constant |
| } | ||
|
|
||
| [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/81141")] | ||
| public void TestFieldInNamespace() |
There was a problem hiding this comment.
Was a test that reported ERR_NamespaceUnexpected before the latest adaptions. Fixed it
|
@stbau04 It looks like some tests are failing #Closed |
|
Done with review pass (commit 15) #Closed |
|
@dotnet/roslyn-compiler For a second review on a community PR |
| ERR_InequalityOperatorInPatternNotSupported = 9345, | ||
| ERR_EncUpdateRequiresEmittingExplicitInterfaceImplementationNotSupportedByTheRuntime = 9346, | ||
| ERR_ExtensionParameterInStaticContext = 9347, | ||
| ERR_CompilationUnitUnexpected = 9348, |
There was a problem hiding this comment.
It would be good to adjust the number in base-lines as well
There was a problem hiding this comment.
You were talking about those, right? 11195ac
…://github.com/stbau04/roslyn into feature/81141-enhance-properties-on-top-level
|
@stbau04 Thank you for the contribution. |
Fixes #81141
Adds a dedicated error, for when members are defined next to statements in the global namespace. The current error
error CS0116: A namespace cannot directly contain members such as fields, methods or statementscan be confusing, as statements (including local functions) can be defined as top level statements, but not any other members. I could not figure any suitable single message for both global statements and a namespace, therefore i split the message into two separate ones.