Skip to content

Commit 2970397

Browse files
authored
fix(jsii-pacmak): collection-of-union arguments in static functions break compilation (#5012)
In some specific cases, jsii generates some fancy types for unions. For example, in static functions only, it will generate the following signature: ```java public static void method(java.util.Map<java.lang.String, ? extends java.lang.Object> arg) { // ^^^^ check this out! } ``` I'm not sure what the value is of doing this; I suspect it doesn't actually provide any additional typing value. However, I *do* know that when we combine this with union validation, which generates the following code: ```java for (final Entry<String, Object> __item_fc3b34: arg.entrySet()) { // ... } ``` It leads to a compilation failure, because apparently `? extends Object` is not assignable to `Object` (I feel like it should be, but who am I to argue with the Java compiler? I'm sure there are good reasons for it to be this way). Rather than remove the `? extends Object` type from the signature, which I'm not sure what it's for, I'm adjusting the type checker to: ```java for (final Entry<String, ? extends Object> __item_fc3b34: arg.entrySet()) { // ... } ``` --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
1 parent 0a515b5 commit 2970397

File tree

10 files changed

+683
-215
lines changed

10 files changed

+683
-215
lines changed

packages/jsii-calc/lib/compliance.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3067,9 +3067,19 @@ export class TwoMethodsWithSimilarCapitalization {
30673067
}
30683068

30693069
export class ClassWithCollectionOfUnions {
3070+
public static staticMethodWithMapOfUnionsParam(
3071+
param: Record<string, StructA | StructB>,
3072+
) {
3073+
void param;
3074+
}
3075+
30703076
public constructor(
30713077
public unionProperty: Array<Record<string, StructA | StructB>>,
30723078
) {}
3079+
3080+
public methodWithMapOfUnionsParam(param: Record<string, StructA | StructB>) {
3081+
void param;
3082+
}
30733083
}
30743084

30753085
export interface StructWithCollectionOfUnionts {

packages/jsii-calc/test/assembly.jsii

Lines changed: 91 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,7 +1582,7 @@
15821582
"kind": "class",
15831583
"locationInModule": {
15841584
"filename": "lib/compliance.ts",
1585-
"line": 3146
1585+
"line": 3156
15861586
},
15871587
"methods": [
15881588
{
@@ -1592,7 +1592,7 @@
15921592
},
15931593
"locationInModule": {
15941594
"filename": "lib/compliance.ts",
1595-
"line": 3155
1595+
"line": 3165
15961596
},
15971597
"name": "mutateProperties",
15981598
"parameters": [
@@ -2606,7 +2606,7 @@
26062606
},
26072607
"locationInModule": {
26082608
"filename": "lib/compliance.ts",
2609-
"line": 3070
2609+
"line": 3076
26102610
},
26112611
"parameters": [
26122612
{
@@ -2641,6 +2641,73 @@
26412641
"filename": "lib/compliance.ts",
26422642
"line": 3069
26432643
},
2644+
"methods": [
2645+
{
2646+
"docs": {
2647+
"stability": "stable"
2648+
},
2649+
"locationInModule": {
2650+
"filename": "lib/compliance.ts",
2651+
"line": 3070
2652+
},
2653+
"name": "staticMethodWithMapOfUnionsParam",
2654+
"parameters": [
2655+
{
2656+
"name": "param",
2657+
"type": {
2658+
"collection": {
2659+
"elementtype": {
2660+
"union": {
2661+
"types": [
2662+
{
2663+
"fqn": "jsii-calc.StructA"
2664+
},
2665+
{
2666+
"fqn": "jsii-calc.StructB"
2667+
}
2668+
]
2669+
}
2670+
},
2671+
"kind": "map"
2672+
}
2673+
}
2674+
}
2675+
],
2676+
"static": true
2677+
},
2678+
{
2679+
"docs": {
2680+
"stability": "stable"
2681+
},
2682+
"locationInModule": {
2683+
"filename": "lib/compliance.ts",
2684+
"line": 3080
2685+
},
2686+
"name": "methodWithMapOfUnionsParam",
2687+
"parameters": [
2688+
{
2689+
"name": "param",
2690+
"type": {
2691+
"collection": {
2692+
"elementtype": {
2693+
"union": {
2694+
"types": [
2695+
{
2696+
"fqn": "jsii-calc.StructA"
2697+
},
2698+
{
2699+
"fqn": "jsii-calc.StructB"
2700+
}
2701+
]
2702+
}
2703+
},
2704+
"kind": "map"
2705+
}
2706+
}
2707+
}
2708+
]
2709+
}
2710+
],
26442711
"name": "ClassWithCollectionOfUnions",
26452712
"properties": [
26462713
{
@@ -2649,7 +2716,7 @@
26492716
},
26502717
"locationInModule": {
26512718
"filename": "lib/compliance.ts",
2652-
"line": 3071
2719+
"line": 3077
26532720
},
26542721
"name": "unionProperty",
26552722
"type": {
@@ -3128,7 +3195,7 @@
31283195
},
31293196
"locationInModule": {
31303197
"filename": "lib/compliance.ts",
3131-
"line": 3080
3198+
"line": 3090
31323199
},
31333200
"parameters": [
31343201
{
@@ -3184,7 +3251,7 @@
31843251
"kind": "class",
31853252
"locationInModule": {
31863253
"filename": "lib/compliance.ts",
3187-
"line": 3079
3254+
"line": 3089
31883255
},
31893256
"name": "ClassWithNestedUnion",
31903257
"properties": [
@@ -3194,7 +3261,7 @@
31943261
},
31953262
"locationInModule": {
31963263
"filename": "lib/compliance.ts",
3197-
"line": 3081
3264+
"line": 3091
31983265
},
31993266
"name": "unionProperty",
32003267
"type": {
@@ -11594,7 +11661,7 @@
1159411661
},
1159511662
"locationInModule": {
1159611663
"filename": "lib/compliance.ts",
11597-
"line": 3118
11664+
"line": 3128
1159811665
},
1159911666
"parameters": [
1160011667
{
@@ -11629,7 +11696,7 @@
1162911696
"kind": "class",
1163011697
"locationInModule": {
1163111698
"filename": "lib/compliance.ts",
11632-
"line": 3112
11699+
"line": 3122
1163311700
},
1163411701
"name": "ParamShadowsBuiltins",
1163511702
"symbolId": "lib/compliance:ParamShadowsBuiltins"
@@ -11644,7 +11711,7 @@
1164411711
"kind": "interface",
1164511712
"locationInModule": {
1164611713
"filename": "lib/compliance.ts",
11647-
"line": 3130
11714+
"line": 3140
1164811715
},
1164911716
"name": "ParamShadowsBuiltinsProps",
1165011717
"properties": [
@@ -11656,7 +11723,7 @@
1165611723
"immutable": true,
1165711724
"locationInModule": {
1165811725
"filename": "lib/compliance.ts",
11659-
"line": 3132
11726+
"line": 3142
1166011727
},
1166111728
"name": "booleanProperty",
1166211729
"type": {
@@ -11671,7 +11738,7 @@
1167111738
"immutable": true,
1167211739
"locationInModule": {
1167311740
"filename": "lib/compliance.ts",
11674-
"line": 3131
11741+
"line": 3141
1167511742
},
1167611743
"name": "stringProperty",
1167711744
"type": {
@@ -11686,7 +11753,7 @@
1168611753
"immutable": true,
1168711754
"locationInModule": {
1168811755
"filename": "lib/compliance.ts",
11689-
"line": 3133
11756+
"line": 3143
1169011757
},
1169111758
"name": "structProperty",
1169211759
"type": {
@@ -11712,7 +11779,7 @@
1171211779
"kind": "class",
1171311780
"locationInModule": {
1171411781
"filename": "lib/compliance.ts",
11715-
"line": 3101
11782+
"line": 3111
1171611783
},
1171711784
"methods": [
1171811785
{
@@ -11721,7 +11788,7 @@
1172111788
},
1172211789
"locationInModule": {
1172311790
"filename": "lib/compliance.ts",
11724-
"line": 3103
11791+
"line": 3113
1172511792
},
1172611793
"name": "useScope",
1172711794
"parameters": [
@@ -11986,7 +12053,7 @@
1198612053
"kind": "class",
1198712054
"locationInModule": {
1198812055
"filename": "lib/compliance.ts",
11989-
"line": 3136
12056+
"line": 3146
1199012057
},
1199112058
"methods": [
1199212059
{
@@ -11996,7 +12063,7 @@
1199612063
},
1199712064
"locationInModule": {
1199812065
"filename": "lib/compliance.ts",
11999-
"line": 3137
12066+
"line": 3147
1200012067
},
1200112068
"name": "promiseIt",
1200212069
"static": true
@@ -12008,7 +12075,7 @@
1200812075
},
1200912076
"locationInModule": {
1201012077
"filename": "lib/compliance.ts",
12011-
"line": 3141
12078+
"line": 3151
1201212079
},
1201312080
"name": "instancePromiseIt"
1201412081
}
@@ -14346,7 +14413,7 @@
1434614413
"kind": "interface",
1434714414
"locationInModule": {
1434814415
"filename": "lib/compliance.ts",
14349-
"line": 3075
14416+
"line": 3085
1435014417
},
1435114418
"name": "StructWithCollectionOfUnionts",
1435214419
"properties": [
@@ -14358,7 +14425,7 @@
1435814425
"immutable": true,
1435914426
"locationInModule": {
1436014427
"filename": "lib/compliance.ts",
14361-
"line": 3076
14428+
"line": 3086
1436214429
},
1436314430
"name": "unionProperty",
1436414431
"type": {
@@ -15973,7 +16040,7 @@
1597316040
},
1597416041
"locationInModule": {
1597516042
"filename": "lib/compliance.ts",
15976-
"line": 3090
16043+
"line": 3100
1597716044
},
1597816045
"parameters": [
1597916046
{
@@ -15998,7 +16065,7 @@
1599816065
"kind": "class",
1599916066
"locationInModule": {
1600016067
"filename": "lib/compliance.ts",
16001-
"line": 3087
16068+
"line": 3097
1600216069
},
1600316070
"name": "VariadicTypeUnion",
1600416071
"properties": [
@@ -16008,7 +16075,7 @@
1600816075
},
1600916076
"locationInModule": {
1601016077
"filename": "lib/compliance.ts",
16011-
"line": 3088
16078+
"line": 3098
1601216079
},
1601316080
"name": "union",
1601416081
"type": {
@@ -19893,5 +19960,5 @@
1989319960
"intersection-types"
1989419961
],
1989519962
"version": "3.20.120",
19896-
"fingerprint": "TNPI4Pzx1sCoo70P/RA0Nnp1IhHvJVLOQ+IOoracN3k="
19963+
"fingerprint": "vOdExLwYXggdf/TTFym/I+wkv5Ih6GlEJke+UiAjaXo="
1989719964
}

0 commit comments

Comments
 (0)