-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Parse partial events and constructors #76860
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
Merged
jjonescz
merged 10 commits into
dotnet:features/PartialEventsCtors
from
jjonescz:PartialEventsCtors-01-Parsing
Jan 30, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ba8ca9d
Parse partial events and constructors
jjonescz 941b824
Update pre-existing tests
jjonescz ced9913
Explain why partial ctors are sometimes disallowed
jjonescz 504f58d
Parse `partial` constructors unconditionally
jjonescz b541347
Gate parsing on LangVersion
jjonescz 12a3225
Extend tests and docs
jjonescz a339c79
Fix indentation
jjonescz c89acca
Rename combinatorial values
jjonescz 02af237
Test more lang versions
jjonescz 4c61a35
Parse partial events unconditionally
jjonescz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/Compilers/CSharp/Test/Emit3/PartialEventsAndConstructorsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using Microsoft.CodeAnalysis.CSharp.Test.Utilities; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.CSharp.UnitTests; | ||
|
|
||
| public sealed class PartialEventsAndConstructorsTests : CSharpTestBase | ||
| { | ||
| [Fact] | ||
| public void ReturningPartialType_LocalFunction_InMethod() | ||
| { | ||
| var source = """ | ||
| class @partial | ||
| { | ||
| static void Main() | ||
| { | ||
| System.Console.Write(F().GetType().Name); | ||
| partial F() => new(); | ||
| } | ||
| } | ||
| """; | ||
| CompileAndVerify(source, parseOptions: TestOptions.Regular13, expectedOutput: "partial").VerifyDiagnostics(); | ||
jjonescz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| var expectedDiagnostics = new[] | ||
| { | ||
| // (5,30): error CS0103: The name 'F' does not exist in the current context | ||
| // System.Console.Write(F().GetType().Name); | ||
| Diagnostic(ErrorCode.ERR_NameNotInContext, "F").WithArguments("F").WithLocation(5, 30), | ||
| // (5,50): error CS1513: } expected | ||
| // System.Console.Write(F().GetType().Name); | ||
| Diagnostic(ErrorCode.ERR_RbraceExpected, "").WithLocation(5, 50), | ||
| // (6,9): error CS0267: The 'partial' modifier can only appear immediately before 'class', 'record', 'struct', 'interface', or a method or property return type. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it will be in the next PR (I already have that locally so I would skip adding a prototype comment for that if that's okay) |
||
| // partial F() => new(); | ||
| Diagnostic(ErrorCode.ERR_PartialMisplaced, "partial").WithLocation(6, 9), | ||
| // (6,17): error CS1520: Method must have a return type | ||
| // partial F() => new(); | ||
| Diagnostic(ErrorCode.ERR_MemberNeedsType, "F").WithLocation(6, 17), | ||
| // (6,24): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement | ||
| // partial F() => new(); | ||
| Diagnostic(ErrorCode.ERR_IllegalStatement, "new()").WithLocation(6, 24), | ||
| // (8,1): error CS1022: Type or namespace definition, or end-of-file expected | ||
| // } | ||
| Diagnostic(ErrorCode.ERR_EOFExpected, "}").WithLocation(8, 1) | ||
| }; | ||
|
|
||
| CreateCompilation(source, parseOptions: TestOptions.RegularNext).VerifyDiagnostics(expectedDiagnostics); | ||
| CreateCompilation(source).VerifyDiagnostics(expectedDiagnostics); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ReturningPartialType_LocalFunction_TopLevel() | ||
| { | ||
| var source = """ | ||
| System.Console.Write(F().GetType().Name); | ||
| partial F() => new(); | ||
| class @partial; | ||
| """; | ||
| CompileAndVerify(source, parseOptions: TestOptions.Regular13, expectedOutput: "partial").VerifyDiagnostics(); | ||
|
|
||
| var expectedDiagnostics = new[] | ||
| { | ||
| // (1,22): error CS0103: The name 'F' does not exist in the current context | ||
| // System.Console.Write(F().GetType().Name); | ||
| Diagnostic(ErrorCode.ERR_NameNotInContext, "F").WithArguments("F").WithLocation(1, 22), | ||
| // (2,9): error CS0116: A namespace cannot directly contain members such as fields, methods or statements | ||
| // partial F() => new(); | ||
| Diagnostic(ErrorCode.ERR_NamespaceUnexpected, "F").WithLocation(2, 9), | ||
| // (2,10): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement | ||
| // partial F() => new(); | ||
| Diagnostic(ErrorCode.ERR_IllegalStatement, "() => new()").WithLocation(2, 10) | ||
| }; | ||
|
|
||
| CreateCompilation(source, parseOptions: TestOptions.RegularNext).VerifyDiagnostics(expectedDiagnostics); | ||
| CreateCompilation(source).VerifyDiagnostics(expectedDiagnostics); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ReturningPartialType_Method() | ||
| { | ||
| var source = """ | ||
| class C | ||
| { | ||
| partial F() => new(); | ||
| static void Main() | ||
| { | ||
| System.Console.Write(new C().F().GetType().Name); | ||
| } | ||
| } | ||
| class @partial; | ||
| """; | ||
| CompileAndVerify(source, parseOptions: TestOptions.Regular13, expectedOutput: "partial").VerifyDiagnostics(); | ||
|
|
||
| var expectedDiagnostics = new[] | ||
| { | ||
| // (3,5): error CS0267: The 'partial' modifier can only appear immediately before 'class', 'record', 'struct', 'interface', or a method or property return type. | ||
| // partial F() => new(); | ||
| Diagnostic(ErrorCode.ERR_PartialMisplaced, "partial").WithLocation(3, 5), | ||
| // (3,13): error CS1520: Method must have a return type | ||
| // partial F() => new(); | ||
| Diagnostic(ErrorCode.ERR_MemberNeedsType, "F").WithLocation(3, 13), | ||
| // (3,20): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement | ||
| // partial F() => new(); | ||
| Diagnostic(ErrorCode.ERR_IllegalStatement, "new()").WithLocation(3, 20), | ||
| // (6,38): error CS1061: 'C' does not contain a definition for 'F' and no accessible extension method 'F' accepting a first argument of type 'C' could be found (are you missing a using directive or an assembly reference?) | ||
| // System.Console.Write(new C().F().GetType().Name); | ||
| Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "F").WithArguments("C", "F").WithLocation(6, 38) | ||
| }; | ||
|
|
||
| CreateCompilation(source, parseOptions: TestOptions.RegularNext).VerifyDiagnostics(expectedDiagnostics); | ||
| CreateCompilation(source).VerifyDiagnostics(expectedDiagnostics); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.