Commit 5c4736e
authored
C#: Fix typed capture type resolution and improve type system (#7126)
* C#: Remove RewriteRule.Rewrite(TemplateStringHandler) shorthand
The `Rewrite(TemplateStringHandler, TemplateStringHandler)` overload
created patterns without `usings`, so typed captures with generic types
(e.g., `IDictionary<TKey, TValue>`) silently failed — the scaffold had
no semantic model and type constraints fell back to string matching.
Removing the shorthand forces callers through the explicit
`CSharpPattern`/`CSharpTemplate` API where `usings` is visible.
Also adds two new tests exercising typed captures with `typeParameters`
through `ToVisitor()`: `UseContainsKeyRecipe` (IDictionary) and
`UseElementAtRecipe` (IEnumerable).
* C#: Add implicit usings to scaffold compilations
DependencyWorkspace.CreateSemanticModel now includes a synthetic
GlobalUsings source file mirroring the .NET 6+ implicit usings.
Without this, types like Dictionary<,> didn't resolve in scaffold
compilations unless the caller explicitly added using directives.
Also adds a test verifying Dictionary<string, int> resolves without
an explicit using System.Collections.Generic directive.
* C#: Map System.String as JavaType.Class instead of JavaType.Primitive
System.String is a reference type with interfaces (IEnumerable<char>,
IComparable<string>, IEquatable<string>, etc.) that
TypeUtils.IsAssignableTo needs to walk. Mapping it as Primitive lost
this hierarchy information.
Now MapPrimitive skips System_String, so the type mapper builds a full
JavaType.Class with supertypes and interfaces. J.Literal and J.Primitive
(the syntax node for the `string` keyword) still use
JavaType.Primitive(String) directly and are unaffected.
* C#: Add test that string type argument is mapped as JavaType.Class
Verifies that List<string>'s type parameter is JavaType.Class("System.String"),
not JavaType.Primitive(String), confirming the type mapper preserves the full
type hierarchy for string.
* C#: Handle array type assignability in TypeUtils
T[] implements a known set of non-generic (System.Array, IList, ICloneable,
etc.) and generic (IEnumerable<T>, IList<T>, IReadOnlyList<T>, etc.)
supertypes. TypeUtils.IsAssignableTo now recognizes these by synthesizing
the parameterized interfaces on the fly when encountering a JavaType.Array.
* C#: Move TypeUtils and MethodMatcher to OpenRewrite.CSharp namespace
Both contain .NET-specific logic (array interface knowledge, string alias
resolution, C# method matching) rather than Java concepts.
* C#: Always create semantic model for scaffold compilations
Previously the semantic model was only created when explicit usings or
dependencies were provided. This meant scaffolds without usings got no
type attribution at all, even though DependencyWorkspace now includes
implicit usings. Always creating the semantic model ensures types resolve
consistently. Reference resolution is cached so the cost is negligible.
* C#: Fix MethodMatcher short name matching after String type change
MethodMatcher patterns like "Aes Create(String)" failed to match when
the parameter type is JavaType.Class("System.String") because the regex
only matched the full FQN or keyword aliases. Now TypeArgMatcher also
tries matching the simple name (last segment of the FQN), so "String"
matches "System.String".1 parent 84de4cf commit 5c4736e
File tree
10 files changed
+459
-52
lines changed- rewrite-csharp/csharp/OpenRewrite
- CSharp
- Template
- Tests
- CSharp
- Template
10 files changed
+459
-52
lines changedLines changed: 5 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
321 | 321 | | |
322 | 322 | | |
323 | 323 | | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
324 | 329 | | |
325 | 330 | | |
326 | 331 | | |
| |||
332 | 337 | | |
333 | 338 | | |
334 | 339 | | |
335 | | - | |
336 | 340 | | |
337 | 341 | | |
338 | 342 | | |
| |||
Lines changed: 11 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | | - | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
323 | 324 | | |
324 | 325 | | |
325 | 326 | | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
326 | 336 | | |
327 | 337 | | |
328 | 338 | | |
| |||
Lines changed: 21 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
35 | 53 | | |
36 | 54 | | |
37 | 55 | | |
| |||
46 | 64 | | |
47 | 65 | | |
48 | 66 | | |
| 67 | + | |
| 68 | + | |
49 | 69 | | |
50 | 70 | | |
51 | 71 | | |
| |||
55 | 75 | | |
56 | 76 | | |
57 | 77 | | |
58 | | - | |
| 78 | + | |
59 | 79 | | |
60 | 80 | | |
61 | 81 | | |
| |||
Lines changed: 14 additions & 21 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
25 | | - | |
| 24 | + | |
26 | 25 | | |
27 | 26 | | |
28 | 27 | | |
29 | 28 | | |
30 | | - | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| |||
59 | 60 | | |
60 | 61 | | |
61 | 62 | | |
62 | | - | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
63 | 66 | | |
64 | 67 | | |
65 | 68 | | |
| |||
72 | 75 | | |
73 | 76 | | |
74 | 77 | | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | 78 | | |
92 | 79 | | |
93 | | - | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
94 | 87 | | |
95 | 88 | | |
96 | 89 | | |
| |||
Lines changed: 7 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
115 | 110 | | |
116 | 111 | | |
117 | 112 | | |
| |||
Lines changed: 69 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
17 | 19 | | |
18 | 20 | | |
19 | 21 | | |
| |||
46 | 48 | | |
47 | 49 | | |
48 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
49 | 55 | | |
50 | 56 | | |
51 | 57 | | |
| |||
354 | 360 | | |
355 | 361 | | |
356 | 362 | | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
357 | 369 | | |
358 | 370 | | |
359 | 371 | | |
| |||
501 | 513 | | |
502 | 514 | | |
503 | 515 | | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
504 | 572 | | |
505 | 573 | | |
506 | 574 | | |
| |||
Lines changed: 64 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
33 | 45 | | |
34 | 46 | | |
35 | 47 | | |
| |||
38 | 50 | | |
39 | 51 | | |
40 | 52 | | |
41 | | - | |
| 53 | + | |
42 | 54 | | |
43 | 55 | | |
44 | 56 | | |
| |||
194 | 206 | | |
195 | 207 | | |
196 | 208 | | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
197 | 235 | | |
198 | 236 | | |
199 | 237 | | |
| |||
227 | 265 | | |
228 | 266 | | |
229 | 267 | | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
230 | 293 | | |
0 commit comments