@markw65/monkeyc-optimizer 1.1.81 → 1.1.83

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,8 +26,8 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
26
26
  mod
27
27
  ));
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var chunk_W4QVHCFN_exports = {};
30
- __export(chunk_W4QVHCFN_exports, {
29
+ var chunk_UNU55JJ3_exports = {};
30
+ __export(chunk_UNU55JJ3_exports, {
31
31
  EnumTagsConst: () => EnumTagsConst,
32
32
  LastTypeTag: () => LastTypeTag,
33
33
  ObjectLikeTagsConst: () => ObjectLikeTagsConst,
@@ -139,7 +139,7 @@ __export(chunk_W4QVHCFN_exports, {
139
139
  visitorNode: () => visitorNode,
140
140
  xml_util_exports: () => xml_util_exports
141
141
  });
142
- module.exports = __toCommonJS(chunk_W4QVHCFN_exports);
142
+ module.exports = __toCommonJS(chunk_UNU55JJ3_exports);
143
143
  var import_chunk_X7QCZR3F = require("./chunk-X7QCZR3F.cjs");
144
144
  var import_chunk_JDC43A3I = require("./chunk-JDC43A3I.cjs");
145
145
  var import_chunk_ABYVSU2C = require("./chunk-ABYVSU2C.cjs");
@@ -5726,6 +5726,12 @@ var init_build_config = (0, import_chunk_ABYVSU2C.__esm)({
5726
5726
  default: "YES",
5727
5727
  scope: "resource"
5728
5728
  },
5729
+ extraReferenceTypeChecks: {
5730
+ type: "boolean",
5731
+ description: "Whether to warn about [unsafe operations on Arrays and Dictionaries](https://github.com/markw65/monkeyc-optimizer/wiki/Extra-Reference-Type-Checks-(prettierMonkeyC.extraReferenceTypeChecks)) that Garmin's compiler allows",
5732
+ default: true,
5733
+ scope: "resource"
5734
+ },
5729
5735
  compilerLookupRules: {
5730
5736
  type: "string",
5731
5737
  description: "Whether to perform lookups as compiler1, compiler2, or based on the currently selected sdk",
@@ -7613,6 +7619,9 @@ var init_control_flow = (0, import_chunk_ABYVSU2C.__esm)({
7613
7619
  }
7614
7620
  });
7615
7621
  function unionInto(to, from) {
7622
+ return unionHelper(to, from, -Infinity);
7623
+ }
7624
+ function unionHelper(to, from, widenDepth) {
7616
7625
  if (to == null || from == null) {
7617
7626
  throw new Error("Null");
7618
7627
  }
@@ -7638,14 +7647,14 @@ function unionInto(to, from) {
7638
7647
  clearValuesUnder(to, from.type);
7639
7648
  return true;
7640
7649
  }
7641
- return mergeMultiple(to, from);
7650
+ return mergeMultiple(to, from, widenDepth);
7642
7651
  }
7643
7652
  if (to.value != null) {
7644
7653
  if (from.value == null) {
7645
7654
  clearValuesUnder(to, from.type);
7646
7655
  return true;
7647
7656
  }
7648
- mergeMultiple(to, from);
7657
+ mergeMultiple(to, from, widenDepth);
7649
7658
  return true;
7650
7659
  }
7651
7660
  if (from.value == null) {
@@ -7658,7 +7667,7 @@ function unionInto(to, from) {
7658
7667
  to.value = tmp.value;
7659
7668
  return true;
7660
7669
  }
7661
- function mergeMultiple(to, from) {
7670
+ function mergeMultiple(to, from, widenDepth) {
7662
7671
  const newTags = to.type | from.type;
7663
7672
  let anyChanged = newTags !== to.type;
7664
7673
  let mask = 0;
@@ -7667,11 +7676,14 @@ function mergeMultiple(to, from) {
7667
7676
  const fromv = getUnionComponent(from, ac.type);
7668
7677
  if (ac.value != null) {
7669
7678
  if (fromv != null) {
7670
- const [value2, changed] = mergeSingle({
7671
- type: ac.type,
7672
- avalue: ac.value,
7673
- bvalue: fromv
7674
- });
7679
+ const [value2, changed] = ac.value === fromv ? [fromv, false] : mergeSingle(
7680
+ {
7681
+ type: ac.type,
7682
+ avalue: ac.value,
7683
+ bvalue: fromv
7684
+ },
7685
+ widenDepth
7686
+ );
7675
7687
  if (changed)
7676
7688
  anyChanged = true;
7677
7689
  if (value2) {
@@ -7708,13 +7720,13 @@ function mergeMultiple(to, from) {
7708
7720
  to.value = result[mask];
7709
7721
  return true;
7710
7722
  }
7711
- function tryUnion(to, from) {
7712
- to = cloneType(to);
7713
- if (unionInto(to, from))
7714
- return to;
7715
- return null;
7716
- }
7717
- function mergeSingle(pair) {
7723
+ function mergeSingle(pair, widenDepth) {
7724
+ function tryUnion(to, from) {
7725
+ to = cloneType(to);
7726
+ if (unionHelper(to, from, widenDepth + 1))
7727
+ return to;
7728
+ return null;
7729
+ }
7718
7730
  switch (pair.type) {
7719
7731
  case 1:
7720
7732
  case 2:
@@ -7727,31 +7739,58 @@ function mergeSingle(pair) {
7727
7739
  case 128:
7728
7740
  case 256:
7729
7741
  case 131072:
7730
- if (pair.avalue === pair.bvalue) {
7731
- return [pair.avalue, false];
7732
- }
7733
7742
  return [null, true];
7734
7743
  case 512: {
7735
- if (Array.isArray(pair.avalue)) {
7736
- const bv = pair.bvalue;
7737
- if (Array.isArray(bv)) {
7738
- if (pair.avalue.length === bv.length) {
7739
- let changed = false;
7740
- const u = pair.avalue.map((a, i) => {
7741
- const merged2 = tryUnion(a, bv[i]);
7742
- if (merged2)
7743
- changed = true;
7744
- return merged2 || a;
7745
- });
7746
- return [u, changed];
7744
+ if (widenDepth > MaxWidenDepth) {
7745
+ return [null, true];
7746
+ }
7747
+ const tupleTypes = /* @__PURE__ */ new Map();
7748
+ const arrayTypes = [];
7749
+ tupleForEach(
7750
+ pair.avalue,
7751
+ (v) => {
7752
+ const t = tupleTypes.get(v.length);
7753
+ if (t) {
7754
+ t.push(v);
7755
+ } else {
7756
+ tupleTypes.set(v.length, [v]);
7757
+ }
7758
+ },
7759
+ (v) => arrayTypes.push(v)
7760
+ );
7761
+ const extraTypes = [];
7762
+ tupleForEach(
7763
+ pair.bvalue,
7764
+ (v) => {
7765
+ const tuples = tupleTypes.get(v.length);
7766
+ if (!tuples?.some(
7767
+ (t) => t.every((at, i) => {
7768
+ const bt = v[i];
7769
+ return subtypeOf(at, bt) && subtypeOf(bt, at);
7770
+ })
7771
+ )) {
7772
+ extraTypes.push(v);
7773
+ }
7774
+ },
7775
+ (v) => {
7776
+ if (!arrayTypes.some((at) => subtypeOf(at, v) && subtypeOf(v, at))) {
7777
+ extraTypes.push(v);
7747
7778
  }
7748
7779
  }
7780
+ );
7781
+ if (extraTypes.length) {
7782
+ if (widenDepth >= 0) {
7783
+ return [null, true];
7784
+ }
7785
+ const allTypes = (pair.avalue instanceof Set ? Array.from(pair.avalue) : [pair.avalue]).concat(extraTypes);
7786
+ return [tupleReduce(allTypes), true];
7749
7787
  }
7750
- const av = reducedType(pair.avalue);
7751
- const merged = tryUnion(av, reducedType(pair.bvalue));
7752
- return [merged || av, merged != null];
7788
+ return [pair.avalue, false];
7753
7789
  }
7754
7790
  case 1024: {
7791
+ if (widenDepth > MaxWidenDepth) {
7792
+ return [null, true];
7793
+ }
7755
7794
  const avalue = pair.avalue;
7756
7795
  const bvalue = pair.bvalue;
7757
7796
  if (!avalue.value) {
@@ -7766,7 +7805,7 @@ function mergeSingle(pair) {
7766
7805
  return;
7767
7806
  }
7768
7807
  av = cloneType(av);
7769
- if (unionInto(av, bv))
7808
+ if (unionHelper(av, bv, widenDepth + 1))
7770
7809
  merged = true;
7771
7810
  result.set(key2, av);
7772
7811
  });
@@ -7842,7 +7881,8 @@ function mergeSingle(pair) {
7842
7881
  let klass = pair.avalue.klass;
7843
7882
  const [obj, objChanged] = mergeObjectValues(
7844
7883
  pair.avalue.obj,
7845
- pair.bvalue.obj
7884
+ pair.bvalue.obj,
7885
+ widenDepth
7846
7886
  );
7847
7887
  const klassChanged = tryUnion(klass, pair.bvalue.klass);
7848
7888
  if (klassChanged || objChanged) {
@@ -7857,34 +7897,39 @@ function mergeSingle(pair) {
7857
7897
  case 65536: {
7858
7898
  const toE = pair.avalue;
7859
7899
  const fromE = pair.bvalue;
7860
- if (toE.enum !== fromE.enum) {
7861
- if (toE.value && fromE.value) {
7862
- const result = cloneType(toE.value);
7863
- unionInto(result, fromE.value);
7864
- const e = { value: result };
7865
- return [e, true];
7900
+ let changed = false;
7901
+ let resultEnum = toE.enum;
7902
+ const s = new Set(
7903
+ Array.isArray(toE.enum) ? toE.enum : [toE.enum]
7904
+ );
7905
+ const size = s.size;
7906
+ (0, import_chunk_X7QCZR3F.forEach)(fromE.enum, (e) => s.add(e));
7907
+ if (size !== s.size) {
7908
+ resultEnum = Array.from(s);
7909
+ changed = true;
7910
+ }
7911
+ let resultValue = toE.value;
7912
+ if (resultValue) {
7913
+ if (fromE.value) {
7914
+ resultValue = cloneType(resultValue);
7915
+ if (unionHelper(resultValue, fromE.value, widenDepth + 1)) {
7916
+ changed = true;
7917
+ }
7918
+ } else {
7919
+ resultValue = void 0;
7920
+ changed = true;
7866
7921
  }
7867
- return [null, true];
7868
7922
  }
7869
- if (!toE.value) {
7923
+ if (!changed) {
7870
7924
  return [toE, false];
7871
7925
  }
7872
- if (!fromE.value) {
7873
- delete toE.value;
7874
- return [toE, true];
7875
- }
7876
- const toValue = tryUnion(toE.value, fromE.value);
7877
- if (toValue) {
7878
- const e = { enum: toE.enum, value: toValue };
7879
- return [e, true];
7880
- }
7881
- return [toE, false];
7926
+ return resultValue ? [{ enum: resultEnum, value: resultValue }, changed] : [{ enum: resultEnum }, changed];
7882
7927
  }
7883
7928
  default:
7884
7929
  unhandledType(pair);
7885
7930
  }
7886
7931
  }
7887
- function mergeObjectValues(to, from) {
7932
+ function mergeObjectValues(to, from, widenDepth) {
7888
7933
  if (!to) {
7889
7934
  return [to, false];
7890
7935
  }
@@ -7901,7 +7946,7 @@ function mergeObjectValues(to, from) {
7901
7946
  return;
7902
7947
  }
7903
7948
  const rep = cloneType(value2);
7904
- if (unionInto(rep, from[key])) {
7949
+ if (unionHelper(rep, from[key], widenDepth + 1)) {
7905
7950
  if (result === to)
7906
7951
  result = { ...result };
7907
7952
  result[key] = rep;
@@ -7991,94 +8036,7 @@ function clearValuesUnder(v, tag, clearTag = false) {
7991
8036
  v.value = newData;
7992
8037
  }
7993
8038
  }
7994
- function widenTypeHelper(t, depth2) {
7995
- let result = null;
7996
- forEachUnionComponent(
7997
- t,
7998
- t.type & (512 | 1024),
7999
- (ac) => {
8000
- if (ac.value == null)
8001
- return;
8002
- switch (ac.type) {
8003
- case 512:
8004
- if (depth2 > 4) {
8005
- if (!result)
8006
- result = cloneType(t);
8007
- clearValuesUnder(result, ac.type);
8008
- } else {
8009
- if (Array.isArray(ac.value)) {
8010
- let newAData = ac.value;
8011
- ac.value.forEach((avalue, index) => {
8012
- const data = widenTypeHelper(avalue, depth2 + 1);
8013
- if (data) {
8014
- if (newAData === ac.value) {
8015
- newAData = newAData.slice();
8016
- }
8017
- newAData[index] = data;
8018
- }
8019
- });
8020
- if (newAData !== ac.value) {
8021
- if (!result)
8022
- result = cloneType(t);
8023
- setUnionComponent(result, ac.type, newAData);
8024
- }
8025
- } else {
8026
- const v = widenTypeHelper(ac.value, depth2 + 1);
8027
- if (v) {
8028
- if (!result)
8029
- result = cloneType(t);
8030
- setUnionComponent(result, ac.type, v);
8031
- }
8032
- }
8033
- }
8034
- return;
8035
- case 1024:
8036
- if (depth2 > 4) {
8037
- if (!result)
8038
- result = cloneType(t);
8039
- clearValuesUnder(result, ac.type);
8040
- } else {
8041
- const ddata = ac.value;
8042
- if (ddata.value) {
8043
- const key = widenTypeHelper(ddata.key, depth2 + 1);
8044
- const data = widenTypeHelper(ddata.value, depth2 + 1);
8045
- if (key || data) {
8046
- if (!result)
8047
- result = cloneType(t);
8048
- const newDData = { ...ddata };
8049
- if (key)
8050
- newDData.key = key;
8051
- if (data)
8052
- newDData.value = data;
8053
- setUnionComponent(result, ac.type, newDData);
8054
- }
8055
- } else {
8056
- let newDData = ddata;
8057
- ddata.forEach((dvalue, dkey) => {
8058
- const data = widenTypeHelper(dvalue, depth2 + 1);
8059
- if (data) {
8060
- if (newDData === ddata) {
8061
- newDData = new Map(newDData);
8062
- }
8063
- newDData.set(dkey, data);
8064
- }
8065
- });
8066
- if (newDData !== ddata) {
8067
- if (!result)
8068
- result = cloneType(t);
8069
- setUnionComponent(result, ac.type, newDData);
8070
- }
8071
- }
8072
- }
8073
- return;
8074
- }
8075
- }
8076
- );
8077
- return result;
8078
- }
8079
- function widenType(t) {
8080
- return widenTypeHelper(t, 0);
8081
- }
8039
+ var MaxWidenDepth;
8082
8040
  var init_union_type = (0, import_chunk_ABYVSU2C.__esm)({
8083
8041
  "src/type-flow/union-type.ts"() {
8084
8042
  "use strict";
@@ -8089,6 +8047,7 @@ var init_union_type = (0, import_chunk_ABYVSU2C.__esm)({
8089
8047
  init_intersection_type();
8090
8048
  init_sub_type();
8091
8049
  init_types();
8050
+ MaxWidenDepth = 4;
8092
8051
  }
8093
8052
  });
8094
8053
  function expandTypedef(t) {
@@ -8109,10 +8068,7 @@ function intersectEnum(t, e) {
8109
8068
  /* Enum */
8110
8069
  );
8111
8070
  const e2 = cloneType(e);
8112
- const i = intersection(
8113
- t,
8114
- !enumData ? { type: EnumTagsConst } : enumData.value ? enumData.value : enumData.enum?.resolvedType || { type: EnumTagsConst }
8115
- );
8071
+ const i = intersection(t, typeFromEnumValue(enumData));
8116
8072
  if (e2.value != null) {
8117
8073
  clearValuesUnder(e2, 65536, true);
8118
8074
  } else {
@@ -8221,35 +8177,49 @@ function intersectionValue(pair) {
8221
8177
  case 128:
8222
8178
  case 131072:
8223
8179
  return pair.avalue === pair.bvalue ? pair.avalue : null;
8224
- case 512: {
8225
- if (Array.isArray(pair.avalue)) {
8226
- const bv = pair.bvalue;
8227
- if (Array.isArray(bv)) {
8228
- if (pair.avalue.length !== bv.length) {
8229
- return null;
8230
- }
8231
- const isect2 = pair.avalue.map((t, i) => intersection(t, bv[i]));
8232
- return isect2.some(
8233
- (t) => t.type === 0
8234
- /* Never */
8235
- ) ? null : isect2;
8180
+ case 512:
8181
+ return tupleMap(
8182
+ pair.avalue,
8183
+ (av) => tupleMap(
8184
+ pair.bvalue,
8185
+ (bv) => {
8186
+ if (av.length !== bv.length)
8187
+ return null;
8188
+ const isect = av.map((t, i) => intersection(t, bv[i]));
8189
+ return isect.some(
8190
+ (t) => t.type === 0
8191
+ /* Never */
8192
+ ) ? null : isect;
8193
+ },
8194
+ (bv) => {
8195
+ const isect = av.map((t) => intersection(bv, t));
8196
+ return isect.some(
8197
+ (t) => t.type === 0
8198
+ /* Never */
8199
+ ) ? null : isect;
8200
+ },
8201
+ (bv) => bv.length === 0 ? null : bv
8202
+ ),
8203
+ (av) => tupleMap(
8204
+ pair.bvalue,
8205
+ (bv) => {
8206
+ const isect = bv.map((t) => intersection(av, t));
8207
+ return isect.some(
8208
+ (t) => t.type === 0
8209
+ /* Never */
8210
+ ) ? null : isect;
8211
+ },
8212
+ (bv) => {
8213
+ const atype = intersection(av, bv);
8214
+ return atype.type === 0 ? null : atype;
8215
+ },
8216
+ (bv) => bv.length === 0 ? null : bv
8217
+ ),
8218
+ (av) => {
8219
+ const result = av.flat(1);
8220
+ return result.length === 0 ? null : tupleReduce(result);
8236
8221
  }
8237
- const isect = pair.avalue.map((t) => intersection(t, bv));
8238
- return isect.some(
8239
- (t) => t.type === 0
8240
- /* Never */
8241
- ) ? null : isect;
8242
- } else if (Array.isArray(pair.bvalue)) {
8243
- const av = pair.avalue;
8244
- const isect = pair.bvalue.map((t) => intersection(av, t));
8245
- return isect.some(
8246
- (t) => t.type === 0
8247
- /* Never */
8248
- ) ? null : isect;
8249
- }
8250
- const atype = intersection(pair.avalue, pair.bvalue);
8251
- return atype.type === 0 ? null : atype;
8252
- }
8222
+ );
8253
8223
  case 1024: {
8254
8224
  if (!pair.avalue.value) {
8255
8225
  if (!pair.bvalue.value) {
@@ -8327,19 +8297,21 @@ function intersectionValue(pair) {
8327
8297
  return klass.type !== 16384 || klass.value == null ? null : obj ? { klass, obj } : { klass };
8328
8298
  }
8329
8299
  case 65536: {
8330
- if (pair.avalue.enum !== pair.bvalue.enum && pair.avalue.enum && pair.bvalue.enum) {
8331
- return null;
8332
- }
8333
- const enumDecl = pair.avalue.enum || pair.bvalue.enum;
8334
- if (pair.avalue.value != null) {
8335
- if (pair.bvalue.value != null) {
8336
- const value2 = intersection(pair.avalue.value, pair.bvalue.value);
8337
- const e = { enum: enumDecl, value: value2 };
8338
- return e;
8300
+ let enumDecl;
8301
+ if (Array.isArray(pair.avalue.enum)) {
8302
+ const s = new Set(pair.avalue.enum);
8303
+ const enums = [];
8304
+ (0, import_chunk_X7QCZR3F.forEach)(pair.bvalue.enum, (b) => s.has(b) && enums.push(b));
8305
+ if (enums.length) {
8306
+ enumDecl = enums.length === 1 ? enums[0] : enums;
8339
8307
  }
8340
- return pair.avalue.value;
8308
+ } else {
8309
+ (0, import_chunk_X7QCZR3F.some)(pair.bvalue.enum, (b) => b === pair.avalue.enum) && (enumDecl = pair.bvalue.enum);
8341
8310
  }
8342
- return pair.bvalue;
8311
+ if (!enumDecl)
8312
+ return null;
8313
+ const enumValue = pair.avalue.value != null ? pair.bvalue.value != null ? intersection(pair.avalue.value, pair.bvalue.value) : pair.avalue.value : pair.bvalue.value;
8314
+ return { enum: enumDecl, value: enumValue };
8343
8315
  }
8344
8316
  default:
8345
8317
  unhandledType(pair);
@@ -8371,12 +8343,7 @@ function fixupEnum(a, b_restricted, b) {
8371
8343
  65536
8372
8344
  /* Enum */
8373
8345
  );
8374
- const br = restrictByEquality(
8375
- a,
8376
- bvalue && (bvalue.value || bvalue.enum?.resolvedType) || {
8377
- type: EnumTagsConst
8378
- }
8379
- );
8346
+ const br = restrictByEquality(a, typeFromEnumValue(bvalue));
8380
8347
  if (br.type) {
8381
8348
  b_restricted = cloneType(b_restricted);
8382
8349
  unionInto(
@@ -8544,12 +8511,7 @@ function restrictExactTypesByEquality(a, b) {
8544
8511
  case 131072:
8545
8512
  return intersection(a, b);
8546
8513
  case 65536: {
8547
- return restrictByEquality(
8548
- a.value && (a.value.value || a.value.enum?.resolvedType) || {
8549
- type: EnumTagsConst
8550
- },
8551
- b
8552
- );
8514
+ return restrictByEquality(typeFromEnumValue(a.value), b);
8553
8515
  }
8554
8516
  case 262144:
8555
8517
  return restrictByEquality(
@@ -8640,12 +8602,7 @@ function subtypeOf(a, b) {
8640
8602
  65536
8641
8603
  /* Enum */
8642
8604
  );
8643
- if (!subtypeOf(
8644
- value2 != null && (value2.value || value2.enum?.resolvedType) || {
8645
- type: EnumTagsConst
8646
- },
8647
- b
8648
- )) {
8605
+ if (!subtypeOf(typeFromEnumValue(value2), b)) {
8649
8606
  return false;
8650
8607
  }
8651
8608
  if (a.type === 65536)
@@ -8693,19 +8650,34 @@ function subtypeOfValue(pair) {
8693
8650
  case 131072:
8694
8651
  return pair.avalue === pair.bvalue;
8695
8652
  case 512: {
8696
- if (Array.isArray(pair.avalue)) {
8697
- const bv = pair.bvalue;
8698
- if (Array.isArray(bv)) {
8699
- if (pair.avalue.length !== bv.length) {
8700
- return false;
8701
- }
8702
- return pair.avalue.every((t, i) => subtypeOf(t, bv[i]));
8653
+ let result = true;
8654
+ tupleForEach(
8655
+ pair.avalue,
8656
+ (av) => {
8657
+ let some2 = false;
8658
+ tupleForEach(
8659
+ pair.bvalue,
8660
+ (bv) => {
8661
+ some2 = av.length === bv.length && bv.every((b, i) => subtypeOf(av[i], b));
8662
+ return some2 === false;
8663
+ },
8664
+ (bv) => (some2 = av.every((a) => subtypeOf(a, bv))) === false
8665
+ );
8666
+ return result = some2;
8667
+ },
8668
+ (av) => {
8669
+ let some2 = false;
8670
+ tupleForEach(
8671
+ pair.bvalue,
8672
+ () => {
8673
+ return true;
8674
+ },
8675
+ (bv) => (some2 = subtypeOf(av, bv)) === false
8676
+ );
8677
+ return result = some2;
8703
8678
  }
8704
- return pair.avalue.every((t) => subtypeOf(t, bv));
8705
- } else if (Array.isArray(pair.bvalue)) {
8706
- return false;
8707
- }
8708
- return subtypeOf(pair.avalue, pair.bvalue);
8679
+ );
8680
+ return result;
8709
8681
  }
8710
8682
  case 1024: {
8711
8683
  const adict = pair.avalue;
@@ -8757,7 +8729,12 @@ function subtypeOfValue(pair) {
8757
8729
  case 65536: {
8758
8730
  const aenum = pair.avalue;
8759
8731
  const benum = pair.bvalue;
8760
- return aenum.enum === benum.enum && (!aenum.value || !benum.value || subtypeOf(aenum.value, benum.value));
8732
+ if (benum.value) {
8733
+ if (!aenum.value || !subtypeOf(aenum.value, benum.value)) {
8734
+ return false;
8735
+ }
8736
+ }
8737
+ return (0, import_chunk_X7QCZR3F.every)(aenum.enum, (ea) => (0, import_chunk_X7QCZR3F.some)(benum.enum, (eb) => ea === eb));
8761
8738
  }
8762
8739
  default:
8763
8740
  unhandledType(pair);
@@ -9472,7 +9449,7 @@ function checkCallArgs(istate, node, callees, args) {
9472
9449
  if (object) {
9473
9450
  const info = sysCallInfo(istate.state, cur);
9474
9451
  if (info) {
9475
- const result2 = info(istate.state, cur, object, () => args);
9452
+ const result2 = info(istate, cur, object, () => args);
9476
9453
  if (result2.argTypes)
9477
9454
  argTypes = result2.argTypes;
9478
9455
  if (result2.returnType)
@@ -9481,6 +9458,8 @@ function checkCallArgs(istate, node, callees, args) {
9481
9458
  effects = false;
9482
9459
  if (!result2.argEffects)
9483
9460
  argEffects = false;
9461
+ if (result2.diagnostic)
9462
+ curDiags.push([node, result2.diagnostic]);
9484
9463
  }
9485
9464
  }
9486
9465
  if (cur.info === false) {
@@ -9514,7 +9493,7 @@ function checkCallArgs(istate, node, callees, args) {
9514
9493
  paramType = typeFromTypespec(istate.state, param.right, cur.stack);
9515
9494
  }
9516
9495
  if (checker(arg, paramType)) {
9517
- if (istate.state.config?.covarianceWarnings && effects && argEffects) {
9496
+ if (istate.state.config?.extraReferenceTypeChecks && effects && argEffects && !safeReferenceArg(node.arguments[i])) {
9518
9497
  if (arg.type & 512) {
9519
9498
  const atype = getUnionComponent(
9520
9499
  arg,
@@ -9527,7 +9506,7 @@ function checkCallArgs(istate, node, callees, args) {
9527
9506
  512
9528
9507
  /* Array */
9529
9508
  );
9530
- if (!ptype || Array.isArray(ptype) || Array.isArray(atype) || !subtypeOf(ptype, atype)) {
9509
+ if (!ptype || !checkArrayCovariance(atype, ptype)) {
9531
9510
  curDiags.push([
9532
9511
  node.arguments[i],
9533
9512
  `Argument ${i + 1} to ${cur.fullName}: passing ${display(
@@ -9709,7 +9688,7 @@ function getSystemCallTable(state) {
9709
9688
  }
9710
9689
  return ret;
9711
9690
  };
9712
- const arrayAdd = (state2, callee, calleeObj, getArgs) => {
9691
+ const arrayAdd = (istate, callee, calleeObj, getArgs) => {
9713
9692
  const ret = {};
9714
9693
  if (calleeObj.type & 512) {
9715
9694
  const adata = getUnionComponent(
@@ -9722,54 +9701,97 @@ function getSystemCallTable(state) {
9722
9701
  const args = getArgs();
9723
9702
  if (args.length === 1) {
9724
9703
  const arg = args[0];
9704
+ let hasTuple = false;
9725
9705
  if (callee.name === "add") {
9726
- if (Array.isArray(adata)) {
9727
- const relaxed = relaxType(arg);
9728
- const newAData = [...adata, relaxed];
9729
- ret.returnType.value = newAData;
9730
- ret.argTypes = [relaxed];
9731
- ret.calleeObj = ret.returnType;
9732
- } else if (!subtypeOf(arg, adata)) {
9733
- const newAData = cloneType(adata);
9734
- unionInto(newAData, arg);
9735
- const newObj = cloneType(calleeObj);
9736
- setUnionComponent(newObj, 512, newAData);
9737
- ret.calleeObj = newObj;
9738
- ret.argTypes = [adata];
9739
- }
9706
+ const relaxed = relaxType(arg);
9707
+ tupleMap(
9708
+ adata,
9709
+ (v) => {
9710
+ hasTuple = true;
9711
+ return [...v, relaxed];
9712
+ },
9713
+ (v) => {
9714
+ if (subtypeOf(arg, v))
9715
+ return v;
9716
+ const newAData = cloneType(v);
9717
+ unionInto(newAData, arg);
9718
+ if (!ret.argTypes) {
9719
+ ret.argTypes = [v];
9720
+ } else {
9721
+ ret.argTypes = [intersection(ret.argTypes[0], v)];
9722
+ }
9723
+ return newAData;
9724
+ },
9725
+ (v) => {
9726
+ const newAData = tupleReduce(v);
9727
+ ret.returnType.value = newAData;
9728
+ const newObj = cloneType(calleeObj);
9729
+ setUnionComponent(newObj, 512, newAData);
9730
+ ret.calleeObj = newObj;
9731
+ if (!ret.argTypes) {
9732
+ ret.argTypes = [relaxed];
9733
+ }
9734
+ }
9735
+ );
9740
9736
  } else {
9741
- if (Array.isArray(adata)) {
9742
- if (arg.type & 512) {
9743
- const argSubtypes = getUnionComponent(
9744
- arg,
9745
- 512
9746
- /* Array */
9747
- );
9748
- if (argSubtypes) {
9749
- if (Array.isArray(argSubtypes)) {
9750
- const newAData2 = [...adata, ...argSubtypes];
9751
- ret.returnType.value = newAData2;
9752
- ret.argTypes = [arg];
9753
- ret.calleeObj = ret.returnType;
9754
- return ret;
9737
+ const argSubtypes = arg.type & 512 ? getUnionComponent(
9738
+ arg,
9739
+ 512
9740
+ /* Array */
9741
+ ) : null;
9742
+ if (argSubtypes) {
9743
+ tupleMap(
9744
+ adata,
9745
+ (v) => {
9746
+ hasTuple = true;
9747
+ return tupleMap(
9748
+ argSubtypes,
9749
+ (a) => [...v, ...a],
9750
+ (a) => {
9751
+ const at = cloneType(reducedType(v));
9752
+ unionInto(at, a);
9753
+ return at;
9754
+ },
9755
+ (a) => a
9756
+ );
9757
+ },
9758
+ (v) => {
9759
+ const addedType = reducedArrayType(argSubtypes);
9760
+ if (subtypeOf(addedType, v))
9761
+ return [v];
9762
+ if (!ret.argTypes) {
9763
+ ret.argTypes = [v];
9764
+ } else {
9765
+ ret.argTypes = [intersection(ret.argTypes[0], v)];
9755
9766
  }
9756
- const newAData = reducedType(adata);
9757
- unionInto(newAData, argSubtypes);
9767
+ v = cloneType(v);
9768
+ unionInto(v, addedType);
9769
+ return [v];
9770
+ },
9771
+ (va) => {
9772
+ const newAData = tupleReduce(va.flat());
9758
9773
  ret.returnType.value = newAData;
9759
- ret.argTypes = [arg];
9760
- ret.calleeObj = ret.returnType;
9761
- return ret;
9774
+ const newObj = cloneType(calleeObj);
9775
+ setUnionComponent(newObj, 512, newAData);
9776
+ ret.calleeObj = newObj;
9777
+ if (!ret.argTypes) {
9778
+ ret.argTypes = [
9779
+ { type: 512, value: argSubtypes }
9780
+ ];
9781
+ }
9762
9782
  }
9763
- }
9764
- ret.returnType.value = reducedType(adata);
9765
- }
9766
- if (!subtypeOf(arg, ret.returnType)) {
9767
- ret.argTypes = [ret.returnType];
9768
- const newObj = cloneType(calleeObj);
9769
- unionInto(newObj, arg);
9770
- ret.calleeObj = newObj;
9783
+ );
9784
+ } else {
9785
+ tupleForEach(
9786
+ adata,
9787
+ () => hasTuple = true,
9788
+ () => false
9789
+ );
9771
9790
  }
9772
9791
  }
9792
+ if (hasTuple && istate.typeChecker && istate.state.config?.extraReferenceTypeChecks !== false) {
9793
+ ret.diagnostic = "Adding to a tuple would change its type";
9794
+ }
9773
9795
  }
9774
9796
  }
9775
9797
  }
@@ -9959,7 +9981,7 @@ function getSystemCallTable(state) {
9959
9981
  ret.argTypes = getArgs();
9960
9982
  return ret;
9961
9983
  };
9962
- const method = (state2, callee, calleeObj, getArgs) => {
9984
+ const method = (istate, callee, calleeObj, getArgs) => {
9963
9985
  const ret = {};
9964
9986
  const args = getArgs();
9965
9987
  if (args.length === 1 && hasValue(args[0]) && args[0].type === 131072) {
@@ -9974,7 +9996,7 @@ function getSystemCallTable(state) {
9974
9996
  computed: false
9975
9997
  };
9976
9998
  const [, trueDecls] = findObjectDeclsByProperty(
9977
- state2,
9999
+ istate.state,
9978
10000
  calleeObj,
9979
10001
  next.property
9980
10002
  );
@@ -9988,7 +10010,7 @@ function getSystemCallTable(state) {
9988
10010
  ret.returnType = callees.reduce(
9989
10011
  (type, callee2) => {
9990
10012
  const result = callee2.node.returnType ? typeFromTypespec(
9991
- state2,
10013
+ istate.state,
9992
10014
  callee2.node.returnType.argument,
9993
10015
  callee2.stack
9994
10016
  ) : {
@@ -9996,7 +10018,7 @@ function getSystemCallTable(state) {
9996
10018
  /* Any */
9997
10019
  };
9998
10020
  const args2 = callee2.node.params.map(
9999
- (param) => param.type === "BinaryExpression" ? typeFromTypespec(state2, param.right, callee2.stack) : {
10021
+ (param) => param.type === "BinaryExpression" ? typeFromTypespec(istate.state, param.right, callee2.stack) : {
10000
10022
  type: 524287
10001
10023
  /* Any */
10002
10024
  }
@@ -10017,7 +10039,7 @@ function getSystemCallTable(state) {
10017
10039
  };
10018
10040
  const nop = () => ({ effectFree: true });
10019
10041
  const mod = () => ({});
10020
- const rounder = (state2, callee, calleeObj, getArgs) => {
10042
+ const rounder = (istate, callee, calleeObj, getArgs) => {
10021
10043
  const results = {};
10022
10044
  const fn = Math[callee.name];
10023
10045
  results.effectFree = true;
@@ -10039,7 +10061,7 @@ function getSystemCallTable(state) {
10039
10061
  };
10040
10062
  return results;
10041
10063
  };
10042
- const mathHelper = (state2, callee, calleeObj, getArgs, helper) => {
10064
+ const mathHelper = (istate, callee, calleeObj, getArgs, helper) => {
10043
10065
  const results = {};
10044
10066
  const fn = helper && typeof helper === "function" ? helper : (0, import_chunk_JDC43A3I.hasProperty)(Math, helper || callee.name) && Math[helper || callee.name];
10045
10067
  if (fn && typeof fn === "function") {
@@ -10188,6 +10210,7 @@ var init_interp_call = (0, import_chunk_ABYVSU2C.__esm)({
10188
10210
  init_optimizer_types();
10189
10211
  (0, import_chunk_X7QCZR3F.init_util)();
10190
10212
  init_interp();
10213
+ init_intersection_type();
10191
10214
  init_sub_type();
10192
10215
  init_type_flow_util();
10193
10216
  init_types();
@@ -10447,32 +10470,31 @@ function deEnumerate(t) {
10447
10470
  );
10448
10471
  t = cloneType(t);
10449
10472
  clearValuesUnder(t, 65536, true);
10450
- unionInto(
10451
- t,
10452
- data && (data.value || data.enum?.resolvedType) || {
10453
- type: EnumTagsConst
10454
- }
10455
- );
10473
+ unionInto(t, typeFromEnumValue(data));
10456
10474
  }
10457
10475
  return t;
10458
10476
  }
10459
- function arrayTypeAtIndex(arr, elemType) {
10460
- if (Array.isArray(arr)) {
10461
- if (elemType && isExact(elemType) && elemType.type === 8 && elemType.value != null && arr[elemType.value] != null) {
10462
- return arr[elemType.value];
10477
+ function arrayTypeAtIndex(arr, elemType, forStrictAssign, enforceTuples) {
10478
+ const reduce2 = (v) => forStrictAssign ? v.reduce(
10479
+ (t, c) => t ? intersection(t, c) : c,
10480
+ null
10481
+ ) ?? {
10482
+ type: 0
10483
+ /* Never */
10484
+ } : reducedType(v);
10485
+ const key = elemType && isExact(elemType) && elemType.type === 8 ? elemType.value : null;
10486
+ return tupleMap(
10487
+ arr,
10488
+ (v) => key != null ? v[key] ?? null : reduce2(v),
10489
+ (v) => v,
10490
+ (v) => v.length ? reduce2(v) : enforceTuples ? {
10491
+ type: 0
10492
+ /* Never */
10493
+ } : {
10494
+ type: 524287
10495
+ /* Any */
10463
10496
  }
10464
- return arr.reduce(
10465
- (p, v) => {
10466
- unionInto(p, v);
10467
- return p;
10468
- },
10469
- {
10470
- type: 0
10471
- /* Never */
10472
- }
10473
- );
10474
- }
10475
- return arr;
10497
+ );
10476
10498
  }
10477
10499
  function getLhsConstraint(istate, node) {
10478
10500
  if (istate.localLvals?.has(node)) {
@@ -10502,7 +10524,15 @@ function getLhsConstraint(istate, node) {
10502
10524
  /* Array */
10503
10525
  );
10504
10526
  if (arr) {
10505
- result = arrayTypeAtIndex(arr, istate.typeMap.get(node.property));
10527
+ result = arrayTypeAtIndex(
10528
+ arr,
10529
+ istate.typeMap.get(node.property) ?? {
10530
+ type: 8,
10531
+ value: arrayLiteralKeyFromExpr(node.property) ?? void 0
10532
+ },
10533
+ strict,
10534
+ istate.state.config?.extraReferenceTypeChecks !== false
10535
+ );
10506
10536
  }
10507
10537
  }
10508
10538
  const updateResult = (value2) => {
@@ -10968,7 +10998,7 @@ function evaluateNode(istate, node) {
10968
10998
  type: 524287
10969
10999
  /* Any */
10970
11000
  };
10971
- const atype = arrayTypeAtIndex(avalue, property.value);
11001
+ const atype = arrayTypeAtIndex(avalue, property.value, false, true);
10972
11002
  if (result) {
10973
11003
  unionInto(result = cloneType(result), atype);
10974
11004
  } else {
@@ -11983,11 +12013,8 @@ function mcExprFromType(type) {
11983
12013
  if (type.value.value && hasValue(type.value.value)) {
11984
12014
  const left = mcExprFromType(type.value.value);
11985
12015
  if (left) {
11986
- if (!type.value.enum) {
11987
- return left;
11988
- }
11989
- const enumStr = type.value.enum.fullName.slice(2);
11990
- if (enumStr === "Toybox.Graphics.ColorValue" && left.type === "Literal" && typeof left.value === "number" && left.value >= 0 && left.value <= 16777215 && /^\d+$/.test(left.raw)) {
12016
+ const enumStrs = (0, import_chunk_X7QCZR3F.map)(type.value.enum, (e) => e.fullName.slice(2));
12017
+ if (enumStrs.length === 1 && enumStrs[0] === "Toybox.Graphics.ColorValue" && left.type === "Literal" && typeof left.value === "number" && left.value >= 0 && left.value <= 16777215 && /^\d+$/.test(left.raw)) {
11991
12018
  left.raw = "0x" + `00000${left.value.toString(16)}`.slice(-6);
11992
12019
  }
11993
12020
  return {
@@ -11996,7 +12023,7 @@ function mcExprFromType(type) {
11996
12023
  left,
11997
12024
  right: {
11998
12025
  type: "TypeSpecList",
11999
- ts: [type.value.enum.fullName.slice(2)]
12026
+ ts: enumStrs
12000
12027
  }
12001
12028
  };
12002
12029
  }
@@ -12085,7 +12112,7 @@ function reducedType(elems) {
12085
12112
  if (!Array.isArray(elems)) {
12086
12113
  return elems;
12087
12114
  }
12088
- return elems.reduce(
12115
+ return elems.flat().reduce(
12089
12116
  (p, t) => {
12090
12117
  unionInto(p, t);
12091
12118
  return p;
@@ -12121,7 +12148,12 @@ function display(type) {
12121
12148
  case 256:
12122
12149
  return JSON.stringify(tv.value);
12123
12150
  case 512:
12124
- return Array.isArray(tv.value) ? `[${tv.value.map((t) => display(t)).join(", ")}]` : `Array<${display(tv.value)}>`;
12151
+ return tupleMap(
12152
+ tv.value,
12153
+ (v) => `[${v.map((t) => display(t)).join(", ")}]`,
12154
+ (v) => `Array<${display(v)}>`,
12155
+ (v) => v.join(" or ")
12156
+ );
12125
12157
  case 1024:
12126
12158
  return tv.value.value ? `Dictionary<${display(tv.value.key)}, ${display(tv.value.value)}>` : `{ ${Array.from(tv.value).map(
12127
12159
  ([key, value2]) => `${display(typeFromObjectLiteralKey(key))} as ${display(
@@ -12145,7 +12177,8 @@ function display(type) {
12145
12177
  }
12146
12178
  case 65536: {
12147
12179
  const v = tv.value;
12148
- return v.enum != null ? v.value != null ? `${display(v.value)} as ${v.enum.fullName.slice(2)}` : v.enum.fullName.slice(2) : v.value != null ? `enum<${display(v.value)}>` : `enum`;
12180
+ const name = v.enum ? (0, import_chunk_X7QCZR3F.map)(v.enum, (e) => e.fullName.slice(2)).join(" or ") : "";
12181
+ return v.value != null ? `${display(v.value)} as ${name}` : name;
12149
12182
  }
12150
12183
  case 131072:
12151
12184
  return `:${tv.value}`;
@@ -12295,6 +12328,91 @@ function getStateNodeDeclsWithExactFromType(state, object) {
12295
12328
  }
12296
12329
  return decls;
12297
12330
  }
12331
+ function tupleForEach(t, tupleFn, arrayFn) {
12332
+ if (t instanceof Set) {
12333
+ for (const v of t) {
12334
+ if ((Array.isArray(v) ? tupleFn(v) : arrayFn(v)) === false) {
12335
+ break;
12336
+ }
12337
+ }
12338
+ } else {
12339
+ if (Array.isArray(t)) {
12340
+ tupleFn(t);
12341
+ } else {
12342
+ arrayFn(t);
12343
+ }
12344
+ }
12345
+ }
12346
+ function tupleMap(t, tupleFn, arrayFn, reduceFn) {
12347
+ const result = t instanceof Set ? Array.from(t).map((v) => Array.isArray(v) ? tupleFn(v) : arrayFn(v)) : [Array.isArray(t) ? tupleFn(t) : arrayFn(t)];
12348
+ return reduceFn(result.filter((v) => v != null));
12349
+ }
12350
+ function tupleReduce(v) {
12351
+ return v.length === 1 ? v[0] : new Set(v);
12352
+ }
12353
+ function reducedArrayType(t) {
12354
+ if (t instanceof Set) {
12355
+ return reducedType(Array.from(t).map((v) => reducedType(v)));
12356
+ }
12357
+ return reducedType(t);
12358
+ }
12359
+ function checkArrayCovariance(arg, param) {
12360
+ let ok = true;
12361
+ tupleForEach(
12362
+ arg,
12363
+ (av) => {
12364
+ tupleForEach(
12365
+ param,
12366
+ (pv) => {
12367
+ if (av.length !== pv.length)
12368
+ return true;
12369
+ if (!av.every((a, i) => subtypeOf(a, pv[i])))
12370
+ return true;
12371
+ if (!av.every((a, i) => subtypeOf(pv[i], a))) {
12372
+ ok = false;
12373
+ }
12374
+ return ok !== false;
12375
+ },
12376
+ () => {
12377
+ ok = false;
12378
+ return false;
12379
+ }
12380
+ );
12381
+ return ok !== false;
12382
+ },
12383
+ (av) => {
12384
+ tupleForEach(
12385
+ param,
12386
+ () => {
12387
+ return ok !== false;
12388
+ },
12389
+ (pv) => {
12390
+ if (subtypeOf(av, pv) && !subtypeOf(pv, av)) {
12391
+ ok = false;
12392
+ }
12393
+ return ok !== false;
12394
+ }
12395
+ );
12396
+ return ok !== false;
12397
+ }
12398
+ );
12399
+ return ok;
12400
+ }
12401
+ function safeReferenceArg(arg) {
12402
+ return arg.type === "ArrayExpression" || arg.type === "ObjectExpression" || arg.type === "NewExpression";
12403
+ }
12404
+ function typeFromEnumValue(arg) {
12405
+ return arg?.value ?? (arg && reducedType(
12406
+ (0, import_chunk_X7QCZR3F.map)(
12407
+ arg.enum,
12408
+ (e) => e.resolvedType ?? {
12409
+ type: EnumTagsConst
12410
+ }
12411
+ )
12412
+ )) ?? {
12413
+ type: EnumTagsConst
12414
+ };
12415
+ }
12298
12416
  var LastTypeTag, SingletonTypeTagsConst, UnionDataTypeTagsConst, ValueTypeTagsConst, ObjectLikeTagsConst, EnumTagsConst, TruthyTypes;
12299
12417
  var init_types = (0, import_chunk_ABYVSU2C.__esm)({
12300
12418
  "src/type-flow/types.ts"() {
@@ -12306,6 +12424,7 @@ var init_types = (0, import_chunk_ABYVSU2C.__esm)({
12306
12424
  init_interp();
12307
12425
  init_intersection_type();
12308
12426
  init_union_type();
12427
+ init_sub_type();
12309
12428
  LastTypeTag = 262144;
12310
12429
  SingletonTypeTagsConst = 1 | 2 | 4;
12311
12430
  UnionDataTypeTagsConst = 512 | 1024 | 2048 | 4096 | 8192 | 16384 | 32768 | 65536 | 262144;
@@ -12403,24 +12522,40 @@ function couldBeValue(pair, shallow) {
12403
12522
  case 128:
12404
12523
  case 131072:
12405
12524
  return pair.avalue === pair.bvalue;
12406
- case 512:
12525
+ case 512: {
12407
12526
  if (shallow)
12408
12527
  return true;
12409
- if (Array.isArray(pair.avalue)) {
12410
- const bv = pair.bvalue;
12411
- if (Array.isArray(bv)) {
12412
- if (pair.avalue.length !== bv.length) {
12413
- return false;
12414
- }
12415
- return pair.avalue.every((a, i) => couldBe(a, bv[i]));
12528
+ let result = false;
12529
+ tupleForEach(
12530
+ pair.avalue,
12531
+ (av) => {
12532
+ tupleForEach(
12533
+ pair.bvalue,
12534
+ (bv) => {
12535
+ result = av.length === bv.length && bv.every((b, i) => couldBe(av[i], b));
12536
+ return result === false;
12537
+ },
12538
+ (bv) => {
12539
+ result = av.every((a) => couldBe(a, bv));
12540
+ return result === false;
12541
+ }
12542
+ );
12543
+ return result === false;
12544
+ },
12545
+ (av) => {
12546
+ tupleForEach(
12547
+ pair.bvalue,
12548
+ (bv) => {
12549
+ result = bv.every((b) => couldBe(av, b));
12550
+ return result === false;
12551
+ },
12552
+ (bv) => (result = couldBe(av, bv)) === false
12553
+ );
12554
+ return result === false;
12416
12555
  }
12417
- return pair.avalue.every((a) => couldBe(a, bv));
12418
- }
12419
- if (Array.isArray(pair.bvalue)) {
12420
- const av = pair.avalue;
12421
- return pair.bvalue.every((b) => couldBe(av, b));
12422
- }
12423
- return couldBe(pair.avalue, pair.bvalue);
12556
+ );
12557
+ return result;
12558
+ }
12424
12559
  case 1024: {
12425
12560
  if (shallow)
12426
12561
  return true;
@@ -12455,7 +12590,10 @@ function couldBeValue(pair, shallow) {
12455
12590
  return couldBe(pair.avalue.klass, pair.bvalue.klass) && (shallow || couldBeObj(pair.avalue.obj, pair.bvalue.obj));
12456
12591
  }
12457
12592
  case 65536: {
12458
- return pair.avalue.enum === pair.bvalue.enum && (!pair.avalue.value || !pair.bvalue.value || couldBe(pair.avalue.value, pair.bvalue.value));
12593
+ return (!pair.avalue.value || !pair.bvalue.value || couldBe(pair.avalue.value, pair.bvalue.value)) && (0, import_chunk_X7QCZR3F.some)(
12594
+ pair.avalue.enum,
12595
+ (sna) => (0, import_chunk_X7QCZR3F.some)(pair.bvalue.enum, (snb) => sna === snb)
12596
+ );
12459
12597
  }
12460
12598
  default:
12461
12599
  unhandledType(pair);
@@ -13160,14 +13298,9 @@ function mergeTypeState(blockStates, index, from, nodeCopyProp) {
13160
13298
  return;
13161
13299
  }
13162
13300
  }
13163
- let result = cloneType(tov.curType);
13164
- if (!unionInto(result, fromv.curType))
13301
+ const result = cloneType(tov.curType);
13302
+ if (!unionHelper(result, fromv.curType, widen ? 0 : -Infinity))
13165
13303
  return;
13166
- if (widen) {
13167
- const wide = widenType(result);
13168
- if (wide)
13169
- result = wide;
13170
- }
13171
13304
  tov.curType = result;
13172
13305
  changes = true;
13173
13306
  });
@@ -13238,11 +13371,11 @@ function updateAffected(blockState, objectType, baseDecl, assignedPath, affected
13238
13371
  /* Array */
13239
13372
  );
13240
13373
  if (atype) {
13241
- if (Array.isArray(atype)) {
13242
- atype.forEach((value2) => unionInto(newType, value2));
13243
- } else {
13244
- unionInto(newType, atype);
13245
- }
13374
+ tupleForEach(
13375
+ atype,
13376
+ (v) => v.forEach((value2) => unionInto(newType, value2)),
13377
+ (v) => unionInto(newType, v)
13378
+ );
13246
13379
  }
13247
13380
  }
13248
13381
  if (type.type & 1024) {
@@ -13370,7 +13503,7 @@ function propagateTypes(state, root, graph, optimizeEquivalencies, copyPropStore
13370
13503
  next = t;
13371
13504
  }
13372
13505
  if (cur.type & 512) {
13373
- let avalue = getUnionComponent(
13506
+ const avalue = getUnionComponent(
13374
13507
  cur,
13375
13508
  512
13376
13509
  /* Array */
@@ -13378,29 +13511,41 @@ function propagateTypes(state, root, graph, optimizeEquivalencies, copyPropStore
13378
13511
  type: 524287
13379
13512
  /* Any */
13380
13513
  };
13381
- if (Array.isArray(avalue)) {
13382
- const index = arrayLiteralKeyFromExpr(me.property);
13383
- if (index != null && avalue[index] != null) {
13384
- if (!next) {
13385
- assocValue[i].keyStr = index;
13514
+ const index = arrayLiteralKeyFromExpr(me.property);
13515
+ tupleForEach(
13516
+ avalue,
13517
+ (av) => {
13518
+ let possibleValues = av;
13519
+ if (index != null) {
13520
+ if (av[index] != null) {
13521
+ if (!next) {
13522
+ assocValue[i].keyStr = index;
13523
+ }
13524
+ possibleValues = av[index];
13525
+ } else {
13526
+ possibleValues = {
13527
+ type: 0
13528
+ /* Never */
13529
+ };
13530
+ }
13531
+ }
13532
+ if (next || i !== l || !newValue) {
13533
+ const n = next ? cloneType(next) : {
13534
+ type: 0
13535
+ /* Never */
13536
+ };
13537
+ (0, import_chunk_X7QCZR3F.forEach)(possibleValues, (v) => unionInto(n, v));
13538
+ next = n;
13539
+ }
13540
+ },
13541
+ (av) => {
13542
+ if (next) {
13543
+ unionInto(next = cloneType(next), av);
13544
+ } else {
13545
+ next = av;
13386
13546
  }
13387
- avalue = avalue[index];
13388
- }
13389
- if (next || i !== l || !newValue) {
13390
- const n = next ? cloneType(next) : {
13391
- type: 0
13392
- /* Never */
13393
- };
13394
- (0, import_chunk_X7QCZR3F.forEach)(avalue, (v) => unionInto(n, v));
13395
- next = n;
13396
- }
13397
- } else {
13398
- if (next) {
13399
- unionInto(next = cloneType(next), avalue);
13400
- } else {
13401
- next = avalue;
13402
13547
  }
13403
- }
13548
+ );
13404
13549
  }
13405
13550
  let isExact2 = false;
13406
13551
  if (cur.type & 1024) {
@@ -13679,7 +13824,7 @@ function propagateTypes(state, root, graph, optimizeEquivalencies, copyPropStore
13679
13824
  return;
13680
13825
  }
13681
13826
  const result = info(
13682
- istate.state,
13827
+ istate,
13683
13828
  callee,
13684
13829
  calleeObj,
13685
13830
  () => node.arguments.map((arg) => evaluateExpr(state, arg, typeMap).value)
@@ -14765,24 +14910,39 @@ function updateByAssocPath(path7, property, update) {
14765
14910
  512
14766
14911
  /* Array */
14767
14912
  );
14768
- if (Array.isArray(avalue)) {
14769
- const key = pathElem.keyStr;
14770
- const relaxed = relaxType(property);
14771
- if (typeof key === "number" && key >= 0 && key < avalue.length) {
14772
- avalue = avalue.slice();
14773
- avalue[key] = relaxed;
14774
- } else {
14775
- avalue = avalue.map((v) => {
14776
- v = cloneType(v);
14777
- unionInto(v, relaxed);
14778
- return v;
14779
- });
14780
- }
14781
- } else {
14782
- avalue = valueToStore(avalue);
14913
+ const key = pathElem.keyStr;
14914
+ const relaxed = relaxType(property);
14915
+ if (avalue) {
14916
+ avalue = tupleMap(
14917
+ avalue,
14918
+ (v) => {
14919
+ if (typeof key === "number") {
14920
+ if (key < 0 || key >= v.length) {
14921
+ return null;
14922
+ }
14923
+ v = v.slice();
14924
+ v[key] = relaxed;
14925
+ return v;
14926
+ }
14927
+ return v.map((v2) => {
14928
+ v2 = cloneType(v2);
14929
+ unionInto(v2, relaxed);
14930
+ return v2;
14931
+ });
14932
+ },
14933
+ (v) => valueToStore(v),
14934
+ tupleReduce
14935
+ );
14783
14936
  }
14784
14937
  object = cloneType(object);
14785
- setUnionComponent(object, 512, avalue);
14938
+ setUnionComponent(
14939
+ object,
14940
+ 512,
14941
+ avalue ?? {
14942
+ type: 524287
14943
+ /* Any */
14944
+ }
14945
+ );
14786
14946
  }
14787
14947
  if (object.type & 1024) {
14788
14948
  const dvalue = getUnionComponent(
@@ -17024,6 +17184,7 @@ async function analyze(fnMap, resourcesMap, manifestXML, config, allowParseError
17024
17184
  allClasses: [],
17025
17185
  nestedClasses: {},
17026
17186
  allModules: /* @__PURE__ */ new Set(),
17187
+ allTypedefs: /* @__PURE__ */ new Set(),
17027
17188
  shouldExclude(node) {
17028
17189
  if ("attrs" in node && node.attrs && "attributes" in node.attrs && node.attrs.attributes && node.loc?.source) {
17029
17190
  const excludeAnnotations = fnMap[node.loc.source].excludeAnnotations;
@@ -17716,7 +17877,7 @@ async function optimizeMonkeyCHelper(fnMap, resourcesMap, manifestXML, config) {
17716
17877
  changes |= 1;
17717
17878
  this.removeNodeComments(node, f.ast);
17718
17879
  } else if (ret) {
17719
- if (node.type === "EnumDeclaration" && ret.type === "TypedefDeclaration" && ret.ts.argument.ts.length > 1) {
17880
+ if (node.type === "EnumDeclaration" && ret.type === "TypedefDeclaration") {
17720
17881
  changes |= 2;
17721
17882
  } else {
17722
17883
  changes |= 1;
@@ -17725,6 +17886,9 @@ async function optimizeMonkeyCHelper(fnMap, resourcesMap, manifestXML, config) {
17725
17886
  return ret;
17726
17887
  };
17727
17888
  collectNamespaces(f.ast, state2);
17889
+ if (changes & 2) {
17890
+ state2.allTypedefs?.forEach((t) => delete t.resolvedType);
17891
+ }
17728
17892
  return changes;
17729
17893
  },
17730
17894
  0
@@ -17916,14 +18080,10 @@ function cleanup(state, node, ast, usedNodes) {
17916
18080
  (d) => d.type === "EnumDeclaration" && d.node === node
17917
18081
  );
17918
18082
  if (i >= 0) {
17919
- const old = decls[i];
17920
- const rep = {
17921
- ...old,
17922
- type: "TypedefDeclaration",
17923
- node: typedefDecl
17924
- };
18083
+ const rep = decls[i];
18084
+ rep.type = "TypedefDeclaration";
18085
+ rep.node = typedefDecl;
17925
18086
  delete rep.resolvedType;
17926
- decls.splice(i, 1, rep);
17927
18087
  }
17928
18088
  }
17929
18089
  return typedefDecl;
@@ -18383,8 +18543,11 @@ function visit_resource_refs(state, doc, e, barrelNames) {
18383
18543
  loc.start.offset -= name.length;
18384
18544
  }
18385
18545
  } else {
18386
- const base = (0, import_chunk_JDC43A3I.makeScopedName)(`Rez.Styles`);
18387
- const idLoc = (0, import_chunk_JDC43A3I.adjustLoc)(loc, 0, 0);
18546
+ const colonPos = name.indexOf(":");
18547
+ const barrel = colonPos < 0 ? "" : name.slice(0, colonPos) + ".";
18548
+ name = name.slice(colonPos + 1);
18549
+ const base = (0, import_chunk_JDC43A3I.makeScopedName)(`${barrel}Rez.Styles`);
18550
+ const idLoc = (0, import_chunk_JDC43A3I.adjustLoc)(loc, colonPos + 1, 0);
18388
18551
  idLoc.end = { ...idLoc.start };
18389
18552
  idLoc.end.column += name.length;
18390
18553
  idLoc.end.offset += name.length;
@@ -18478,35 +18641,41 @@ function add_one_resource(state, doc, module2, e, barrelNames) {
18478
18641
  let func;
18479
18642
  const makeVarDecl = (id2, outer) => {
18480
18643
  const loc = id2 && (0, import_chunk_JDC43A3I.adjustLoc)(id2.value.loc);
18644
+ const declarations = [];
18645
+ if (id2) {
18646
+ declarations.push({
18647
+ type: "VariableDeclarator",
18648
+ kind: "var",
18649
+ id: {
18650
+ type: "BinaryExpression",
18651
+ operator: "as",
18652
+ left: (0, import_chunk_JDC43A3I.makeIdentifier)(id2.value.value, loc),
18653
+ right: {
18654
+ type: "TypeSpecList",
18655
+ ts: [
18656
+ {
18657
+ type: "TypeSpecPart",
18658
+ name: (0, import_chunk_JDC43A3I.makeScopedName)(
18659
+ (state?.sdkVersion ?? 0) >= 7e6 ? "Toybox.Lang.ResourceId" : "Toybox.Lang.Symbol"
18660
+ )
18661
+ }
18662
+ ]
18663
+ }
18664
+ }
18665
+ });
18666
+ }
18667
+ if (init) {
18668
+ declarations.push({
18669
+ type: "VariableDeclarator",
18670
+ kind: "var",
18671
+ id: (0, import_chunk_JDC43A3I.makeIdentifier)("*invalid*"),
18672
+ init
18673
+ });
18674
+ }
18481
18675
  return (0, import_chunk_JDC43A3I.wrap)(
18482
18676
  {
18483
18677
  type: "VariableDeclaration",
18484
- declarations: [
18485
- (0, import_chunk_JDC43A3I.wrap)(
18486
- {
18487
- type: "VariableDeclarator",
18488
- kind: "var",
18489
- id: id2 && !init ? {
18490
- type: "BinaryExpression",
18491
- operator: "as",
18492
- left: (0, import_chunk_JDC43A3I.makeIdentifier)(id2.value.value, loc),
18493
- right: {
18494
- type: "TypeSpecList",
18495
- ts: [
18496
- {
18497
- type: "TypeSpecPart",
18498
- name: (0, import_chunk_JDC43A3I.makeScopedName)(
18499
- (state?.sdkVersion ?? 0) >= 7e6 ? "Toybox.Lang.ResourceId" : "Toybox.Lang.Symbol"
18500
- )
18501
- }
18502
- ]
18503
- }
18504
- } : (0, import_chunk_JDC43A3I.makeIdentifier)(id2 ? id2.value.value : "*invalid*", loc),
18505
- init
18506
- },
18507
- outer
18508
- )
18509
- ],
18678
+ declarations: declarations.map((d) => (0, import_chunk_JDC43A3I.wrap)(d, outer)),
18510
18679
  kind: "var"
18511
18680
  },
18512
18681
  outer
@@ -19659,6 +19828,8 @@ function stateFuncs() {
19659
19828
  parent.type_decls[name].push(decl);
19660
19829
  if (decl.type === "EnumDeclaration") {
19661
19830
  currentEnum = decl;
19831
+ } else {
19832
+ this.allTypedefs?.add(decl);
19662
19833
  }
19663
19834
  break;
19664
19835
  }
@@ -30242,7 +30413,7 @@ async function generateOneConfig(buildConfig, manifestXML, dependencyFiles, conf
30242
30413
  const opt_time = await (0, import_chunk_X7QCZR3F.first_modified)(
30243
30414
  Object.values(fnMap).map((v) => v.output)
30244
30415
  );
30245
- if (source_time < opt_time && 1732126457866 < opt_time) {
30416
+ if (source_time < opt_time && 1732680967178 < opt_time) {
30246
30417
  return {
30247
30418
  hasTests,
30248
30419
  diagnostics: prevDiagnostics,
@@ -30280,7 +30451,7 @@ async function generateOneConfig(buildConfig, manifestXML, dependencyFiles, conf
30280
30451
  hasTests: hasTests2,
30281
30452
  diagnostics,
30282
30453
  sdkVersion,
30283
- optimizerVersion: "1.1.81",
30454
+ optimizerVersion: "1.1.83",
30284
30455
  ...Object.fromEntries(
30285
30456
  configOptionsToCheck.map((option) => [option, config[option]])
30286
30457
  )
@@ -30446,7 +30617,7 @@ var init_optimizer = (0, import_chunk_ABYVSU2C.__esm)({
30446
30617
  "minimizeModules",
30447
30618
  "extensionVersion",
30448
30619
  "typeCheckLevel",
30449
- "covarianceWarnings",
30620
+ "extraReferenceTypeChecks",
30450
30621
  "iterateOptimizer",
30451
30622
  "extraExcludes"
30452
30623
  ];