Skip to content

C#: Consolidate namespace and compilation unit model#6981

Merged
jkschneider merged 1 commit into
mainfrom
jkschneider/cs-model-consistency
Mar 13, 2026
Merged

C#: Consolidate namespace and compilation unit model#6981
jkschneider merged 1 commit into
mainfrom
jkschneider/cs-model-consistency

Conversation

@jkschneider

@jkschneider jkschneider commented Mar 13, 2026

Copy link
Copy Markdown
Member

Summary

Align the C# model, visitors, and RPC layer so that both sides (Java and C#/.NET) use identical shapes. This eliminates workarounds where the C# RPC sender/receiver had to split, merge, or fabricate fields to bridge structural differences.

Asymmetries fixed

Model consolidations (Cs.java)

  1. BlockScopeNamespaceDeclaration + FileScopeNamespaceDeclarationNamespaceDeclaration — Java had two separate types; C# had one NamespaceDeclaration. Merged into a single Cs.NamespaceDeclaration on the Java side. Removed the RegisterJavaTypeName override that mapped NamespaceDeclaration → Cs$BlockScopeNamespaceDeclaration.
  2. CompilationUnit.externs, .usings, .attributeLists removed — These were redundant top-level fields; externs, usings, and attribute lists are already represented as statements within CompilationUnit.members. The C# side never had these separate fields.
  3. NamespaceDeclaration.externs and .usings removed — Same redundancy as CompilationUnit: these were split out on the Java side but are just members on the C# side. The C# RPC sender/receiver had workaround code to filter UsingDirective vs non-UsingDirective members and merge them back — now removed.
  4. UsingDirective.unsafe field removed — Java modeled an unsafe left-padded boolean that the C# side never had. The C# sender fabricated a JLeftPadded<bool>(Space.Empty, false) dummy, and the receiver consumed and discarded it. Both workarounds removed.
  5. Cs.Try and Cs.Try.Catch removed — These were Java-only duplicates of J.Try / J.Try.Catch. The C# side uses the standard J.Try tree. Removed the redundant Cs types along with their visitor methods, RPC sender/receiver, and iso-visitor overrides.

J model alignment (J.cs on the C# side)

  1. J.Package.Expression: JRightPadded<Expression>Expression — Java side uses a plain Expression; C# had an extra right-padding wrapper. Parser, printer, visitor, and RPC updated.
  2. J.EnumValue.Initializer: JLeftPadded<Expression>?NewClass? — Java side uses a direct NewClass?; C# had an extra left-padding wrapper. Printer no longer manually prints = and spacing — delegates to the NewClass visit.
  3. J.Try.Resources: JContainer<NameTree>?JContainer<Try.Resource>? — Java side wraps try-with-resources in a Try.Resource node (holding variableDeclarations + terminatedWithSemicolon). Added the missing Try.Resource class to C# J.cs, plus VisitTryResource in both JavaSender.cs and JavaReceiver.cs.
  4. J.NewClass.Clazz: TypeTree?J? — Java side types this as J? (since the clazz can be a J.ParameterizedType which doesn't implement TypeTree on the C# side). Updated model, visitor, and RPC.

RPC alignment

  1. C# CSharpSender.VisitNamespaceDeclaration — Removed workaround that split members into separate externs/usings/non-using lists for transmission. Now sends name, members, end — matching the Java receiver.
  2. C# CSharpReceiver.VisitNamespaceDeclaration — Removed workaround that received separate externs/usings/members lists and merged them back into a single members list.
  3. C# CSharpSender.VisitUsingDirective — Removed fabricated JLeftPadded<bool>(false) for the unsafe field.
  4. C# CSharpReceiver.VisitUsingDirective — Removed consume-and-discard of the unsafe field.
  5. CSharpRpcTest timeout — Increased RPC timeout from 60s to 120s to fix transient flake in parsePragmaWarningMultipleCodes.

Location enum cleanup

  1. Removed CsSpace.Location / CsRightPadded.Location / CsLeftPadded.Location entries for deleted types: BLOCK_SCOPE_NAMESPACE_DECLARATION_*, FILE_SCOPE_NAMESPACE_DECLARATION_*, COMPILATION_UNIT_EXTERNS_*, COMPILATION_UNIT_USINGS_*, USING_DIRECTIVE_UNSAFE. Added NAMESPACE_DECLARATION_* replacements.

Test plan

  • Existing CSharpRpcTest tests pass (including parsePragmaWarningMultipleCodes with increased timeout)
  • CSharpRecipeTest passes with updated model references
  • C# side compiles and RPC round-trips correctly

@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Mar 13, 2026
@jkschneider jkschneider force-pushed the jkschneider/cs-model-consistency branch 3 times, most recently from e30ad07 to ed2565a Compare March 13, 2026 16:12
Merge `BlockScopeNamespaceDeclaration` and `FileScopeNamespaceDeclaration`
into a single `NamespaceDeclaration` type. Remove redundant `externs`,
`usings`, and `attributeLists` fields from `CompilationUnit` since these
are already represented as members. Update visitor, printer, RPC
sender/receiver, and tests on both Java and C# sides.

Also increase `CSharpRpcTest` RPC timeout from 60s to 120s to fix a
transient timeout flake in `parsePragmaWarningMultipleCodes`.
@jkschneider jkschneider force-pushed the jkschneider/cs-model-consistency branch from ed2565a to 77bf237 Compare March 13, 2026 16:40
@jkschneider jkschneider merged commit 4c5ec0c into main Mar 13, 2026
1 check passed
@jkschneider jkschneider deleted the jkschneider/cs-model-consistency branch March 13, 2026 16:55
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Mar 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant