hinmil 0.0.0-stage → 0.2.0
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.
- package/LICENSE +21 -0
- package/README.md +2306 -2
- package/dist/cli.d.ts +15 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +338 -0
- package/dist/cli.js.map +1 -0
- package/dist/commentParser.d.ts +384 -0
- package/dist/commentParser.d.ts.map +1 -0
- package/dist/commentParser.js +2487 -0
- package/dist/commentParser.js.map +1 -0
- package/dist/config.d.ts +203 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +375 -0
- package/dist/config.js.map +1 -0
- package/dist/debugLog.d.ts +13 -0
- package/dist/debugLog.d.ts.map +1 -0
- package/dist/debugLog.js +44 -0
- package/dist/debugLog.js.map +1 -0
- package/dist/estree.d.ts +100 -0
- package/dist/estree.d.ts.map +1 -0
- package/dist/estree.js +34 -0
- package/dist/estree.js.map +1 -0
- package/dist/hm/env.d.ts +134 -0
- package/dist/hm/env.d.ts.map +1 -0
- package/dist/hm/env.js +939 -0
- package/dist/hm/env.js.map +1 -0
- package/dist/hm/index.d.ts +7 -0
- package/dist/hm/index.d.ts.map +1 -0
- package/dist/hm/index.js +26 -0
- package/dist/hm/index.js.map +1 -0
- package/dist/hm/substitution.d.ts +57 -0
- package/dist/hm/substitution.d.ts.map +1 -0
- package/dist/hm/substitution.js +546 -0
- package/dist/hm/substitution.js.map +1 -0
- package/dist/hm/typeParser.d.ts +39 -0
- package/dist/hm/typeParser.d.ts.map +1 -0
- package/dist/hm/typeParser.js +1285 -0
- package/dist/hm/typeParser.js.map +1 -0
- package/dist/hm/typeUtils.d.ts +248 -0
- package/dist/hm/typeUtils.d.ts.map +1 -0
- package/dist/hm/typeUtils.js +905 -0
- package/dist/hm/typeUtils.js.map +1 -0
- package/dist/hm/types.d.ts +738 -0
- package/dist/hm/types.d.ts.map +1 -0
- package/dist/hm/types.js +1853 -0
- package/dist/hm/types.js.map +1 -0
- package/dist/hm/unify.d.ts +59 -0
- package/dist/hm/unify.d.ts.map +1 -0
- package/dist/hm/unify.js +1700 -0
- package/dist/hm/unify.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +196 -0
- package/dist/index.js.map +1 -0
- package/dist/infer/annotation-rigid.d.ts +76 -0
- package/dist/infer/annotation-rigid.d.ts.map +1 -0
- package/dist/infer/annotation-rigid.js +542 -0
- package/dist/infer/annotation-rigid.js.map +1 -0
- package/dist/infer/annotation-validation.d.ts +109 -0
- package/dist/infer/annotation-validation.d.ts.map +1 -0
- package/dist/infer/annotation-validation.js +1078 -0
- package/dist/infer/annotation-validation.js.map +1 -0
- package/dist/infer/annotations.d.ts +277 -0
- package/dist/infer/annotations.d.ts.map +1 -0
- package/dist/infer/annotations.js +1343 -0
- package/dist/infer/annotations.js.map +1 -0
- package/dist/infer/call-overloads.d.ts +24 -0
- package/dist/infer/call-overloads.d.ts.map +1 -0
- package/dist/infer/call-overloads.js +900 -0
- package/dist/infer/call-overloads.js.map +1 -0
- package/dist/infer/call-rows.d.ts +154 -0
- package/dist/infer/call-rows.d.ts.map +1 -0
- package/dist/infer/call-rows.js +2162 -0
- package/dist/infer/call-rows.js.map +1 -0
- package/dist/infer/calls.d.ts +20 -0
- package/dist/infer/calls.d.ts.map +1 -0
- package/dist/infer/calls.js +1468 -0
- package/dist/infer/calls.js.map +1 -0
- package/dist/infer/constraints.d.ts +99 -0
- package/dist/infer/constraints.d.ts.map +1 -0
- package/dist/infer/constraints.js +1208 -0
- package/dist/infer/constraints.js.map +1 -0
- package/dist/infer/expr-assignment.d.ts +20 -0
- package/dist/infer/expr-assignment.d.ts.map +1 -0
- package/dist/infer/expr-assignment.js +620 -0
- package/dist/infer/expr-assignment.js.map +1 -0
- package/dist/infer/expr-constraint-lookup.d.ts +44 -0
- package/dist/infer/expr-constraint-lookup.d.ts.map +1 -0
- package/dist/infer/expr-constraint-lookup.js +207 -0
- package/dist/infer/expr-constraint-lookup.js.map +1 -0
- package/dist/infer/expr-functions.d.ts +20 -0
- package/dist/infer/expr-functions.d.ts.map +1 -0
- package/dist/infer/expr-functions.js +1359 -0
- package/dist/infer/expr-functions.js.map +1 -0
- package/dist/infer/expr-jsx.d.ts +19 -0
- package/dist/infer/expr-jsx.d.ts.map +1 -0
- package/dist/infer/expr-jsx.js +310 -0
- package/dist/infer/expr-jsx.js.map +1 -0
- package/dist/infer/expr-member.d.ts +40 -0
- package/dist/infer/expr-member.d.ts.map +1 -0
- package/dist/infer/expr-member.js +1154 -0
- package/dist/infer/expr-member.js.map +1 -0
- package/dist/infer/expr-prototype.d.ts +11 -0
- package/dist/infer/expr-prototype.d.ts.map +1 -0
- package/dist/infer/expr-prototype.js +202 -0
- package/dist/infer/expr-prototype.js.map +1 -0
- package/dist/infer/expressions.d.ts +40 -0
- package/dist/infer/expressions.d.ts.map +1 -0
- package/dist/infer/expressions.js +1497 -0
- package/dist/infer/expressions.js.map +1 -0
- package/dist/infer/functionPatterns.d.ts +229 -0
- package/dist/infer/functionPatterns.d.ts.map +1 -0
- package/dist/infer/functionPatterns.js +1035 -0
- package/dist/infer/functionPatterns.js.map +1 -0
- package/dist/infer/helpers.d.ts +73 -0
- package/dist/infer/helpers.d.ts.map +1 -0
- package/dist/infer/helpers.js +187 -0
- package/dist/infer/helpers.js.map +1 -0
- package/dist/infer/index.d.ts +551 -0
- package/dist/infer/index.d.ts.map +1 -0
- package/dist/infer/index.js +1127 -0
- package/dist/infer/index.js.map +1 -0
- package/dist/infer/jsx-dom.d.ts +26 -0
- package/dist/infer/jsx-dom.d.ts.map +1 -0
- package/dist/infer/jsx-dom.js +303 -0
- package/dist/infer/jsx-dom.js.map +1 -0
- package/dist/infer/moduleReps.d.ts +7 -0
- package/dist/infer/moduleReps.d.ts.map +1 -0
- package/dist/infer/moduleReps.js +48 -0
- package/dist/infer/moduleReps.js.map +1 -0
- package/dist/infer/modules.d.ts +10 -0
- package/dist/infer/modules.d.ts.map +1 -0
- package/dist/infer/modules.js +775 -0
- package/dist/infer/modules.js.map +1 -0
- package/dist/infer/narrowing.d.ts +199 -0
- package/dist/infer/narrowing.d.ts.map +1 -0
- package/dist/infer/narrowing.js +1428 -0
- package/dist/infer/narrowing.js.map +1 -0
- package/dist/infer/results.d.ts +24 -0
- package/dist/infer/results.d.ts.map +1 -0
- package/dist/infer/results.js +611 -0
- package/dist/infer/results.js.map +1 -0
- package/dist/infer/setupPass.d.ts +11 -0
- package/dist/infer/setupPass.d.ts.map +1 -0
- package/dist/infer/setupPass.js +524 -0
- package/dist/infer/setupPass.js.map +1 -0
- package/dist/infer/statements.d.ts +26 -0
- package/dist/infer/statements.d.ts.map +1 -0
- package/dist/infer/statements.js +750 -0
- package/dist/infer/statements.js.map +1 -0
- package/dist/infer/stmt-bindings.d.ts +22 -0
- package/dist/infer/stmt-bindings.d.ts.map +1 -0
- package/dist/infer/stmt-bindings.js +1482 -0
- package/dist/infer/stmt-bindings.js.map +1 -0
- package/dist/infer/stmt-destructure.d.ts +29 -0
- package/dist/infer/stmt-destructure.d.ts.map +1 -0
- package/dist/infer/stmt-destructure.js +490 -0
- package/dist/infer/stmt-destructure.js.map +1 -0
- package/dist/infer/types.d.ts +220 -0
- package/dist/infer/types.d.ts.map +1 -0
- package/dist/infer/types.js +46 -0
- package/dist/infer/types.js.map +1 -0
- package/dist/infer/unifyDiag.d.ts +46 -0
- package/dist/infer/unifyDiag.d.ts.map +1 -0
- package/dist/infer/unifyDiag.js +642 -0
- package/dist/infer/unifyDiag.js.map +1 -0
- package/dist/parsing-babel.d.ts +3 -0
- package/dist/parsing-babel.d.ts.map +1 -0
- package/dist/parsing-babel.js +353 -0
- package/dist/parsing-babel.js.map +1 -0
- package/dist/parsing.d.ts +69 -0
- package/dist/parsing.d.ts.map +1 -0
- package/dist/parsing.js +288 -0
- package/dist/parsing.js.map +1 -0
- package/dist/registries/constraintRegistry.d.ts +233 -0
- package/dist/registries/constraintRegistry.d.ts.map +1 -0
- package/dist/registries/constraintRegistry.js +803 -0
- package/dist/registries/constraintRegistry.js.map +1 -0
- package/dist/registries/dataTypeRegistry.d.ts +63 -0
- package/dist/registries/dataTypeRegistry.d.ts.map +1 -0
- package/dist/registries/dataTypeRegistry.js +180 -0
- package/dist/registries/dataTypeRegistry.js.map +1 -0
- package/dist/registries/domGlobals.d.ts +33 -0
- package/dist/registries/domGlobals.d.ts.map +1 -0
- package/dist/registries/domGlobals.js +309 -0
- package/dist/registries/domGlobals.js.map +1 -0
- package/dist/registries/methodRegistry.d.ts +277 -0
- package/dist/registries/methodRegistry.d.ts.map +1 -0
- package/dist/registries/methodRegistry.js +2074 -0
- package/dist/registries/methodRegistry.js.map +1 -0
- package/dist/registries/session.d.ts +38 -0
- package/dist/registries/session.d.ts.map +1 -0
- package/dist/registries/session.js +75 -0
- package/dist/registries/session.js.map +1 -0
- package/dist/server.d.ts +2 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +1896 -0
- package/dist/server.js.map +1 -0
- package/dist/services/codeActions.d.ts +22 -0
- package/dist/services/codeActions.d.ts.map +1 -0
- package/dist/services/codeActions.js +465 -0
- package/dist/services/codeActions.js.map +1 -0
- package/dist/services/completion.d.ts +259 -0
- package/dist/services/completion.d.ts.map +1 -0
- package/dist/services/completion.js +2794 -0
- package/dist/services/completion.js.map +1 -0
- package/dist/services/definitions.d.ts +37 -0
- package/dist/services/definitions.d.ts.map +1 -0
- package/dist/services/definitions.js +605 -0
- package/dist/services/definitions.js.map +1 -0
- package/dist/services/diagnosticCodes.d.ts +70 -0
- package/dist/services/diagnosticCodes.d.ts.map +1 -0
- package/dist/services/diagnosticCodes.js +239 -0
- package/dist/services/diagnosticCodes.js.map +1 -0
- package/dist/services/diagnostics.d.ts +86 -0
- package/dist/services/diagnostics.d.ts.map +1 -0
- package/dist/services/diagnostics.js +665 -0
- package/dist/services/diagnostics.js.map +1 -0
- package/dist/services/documentCache.d.ts +88 -0
- package/dist/services/documentCache.d.ts.map +1 -0
- package/dist/services/documentCache.js +108 -0
- package/dist/services/documentCache.js.map +1 -0
- package/dist/services/documentSymbols.d.ts +20 -0
- package/dist/services/documentSymbols.d.ts.map +1 -0
- package/dist/services/documentSymbols.js +381 -0
- package/dist/services/documentSymbols.js.map +1 -0
- package/dist/services/fileSelection.d.ts +24 -0
- package/dist/services/fileSelection.d.ts.map +1 -0
- package/dist/services/fileSelection.js +154 -0
- package/dist/services/fileSelection.js.map +1 -0
- package/dist/services/fileSelectionRefresh.d.ts +26 -0
- package/dist/services/fileSelectionRefresh.d.ts.map +1 -0
- package/dist/services/fileSelectionRefresh.js +77 -0
- package/dist/services/fileSelectionRefresh.js.map +1 -0
- package/dist/services/files.d.ts +342 -0
- package/dist/services/files.d.ts.map +1 -0
- package/dist/services/files.js +1712 -0
- package/dist/services/files.js.map +1 -0
- package/dist/services/fsProbe.d.ts +14 -0
- package/dist/services/fsProbe.d.ts.map +1 -0
- package/dist/services/fsProbe.js +112 -0
- package/dist/services/fsProbe.js.map +1 -0
- package/dist/services/globalsLoader.d.ts +20 -0
- package/dist/services/globalsLoader.d.ts.map +1 -0
- package/dist/services/globalsLoader.js +122 -0
- package/dist/services/globalsLoader.js.map +1 -0
- package/dist/services/hmFileServices.d.ts +113 -0
- package/dist/services/hmFileServices.d.ts.map +1 -0
- package/dist/services/hmFileServices.js +686 -0
- package/dist/services/hmFileServices.js.map +1 -0
- package/dist/services/hover.d.ts +280 -0
- package/dist/services/hover.d.ts.map +1 -0
- package/dist/services/hover.js +1687 -0
- package/dist/services/hover.js.map +1 -0
- package/dist/services/importContext.d.ts +396 -0
- package/dist/services/importContext.d.ts.map +1 -0
- package/dist/services/importContext.js +1999 -0
- package/dist/services/importContext.js.map +1 -0
- package/dist/services/projectChecker.d.ts +50 -0
- package/dist/services/projectChecker.d.ts.map +1 -0
- package/dist/services/projectChecker.js +228 -0
- package/dist/services/projectChecker.js.map +1 -0
- package/dist/services/references.d.ts +41 -0
- package/dist/services/references.d.ts.map +1 -0
- package/dist/services/references.js +757 -0
- package/dist/services/references.js.map +1 -0
- package/dist/services/rename.d.ts +22 -0
- package/dist/services/rename.d.ts.map +1 -0
- package/dist/services/rename.js +101 -0
- package/dist/services/rename.js.map +1 -0
- package/dist/services/shortCircuit.d.ts +27 -0
- package/dist/services/shortCircuit.d.ts.map +1 -0
- package/dist/services/shortCircuit.js +28 -0
- package/dist/services/shortCircuit.js.map +1 -0
- package/dist/services/sidecarPerf.d.ts +19 -0
- package/dist/services/sidecarPerf.d.ts.map +1 -0
- package/dist/services/sidecarPerf.js +59 -0
- package/dist/services/sidecarPerf.js.map +1 -0
- package/dist/services/signatureHelp.d.ts +27 -0
- package/dist/services/signatureHelp.d.ts.map +1 -0
- package/dist/services/signatureHelp.js +984 -0
- package/dist/services/signatureHelp.js.map +1 -0
- package/dist/walking.d.ts +25 -0
- package/dist/walking.d.ts.map +1 -0
- package/dist/walking.js +107 -0
- package/dist/walking.js.map +1 -0
- package/hm-types-builtin/assert.hm +20 -0
- package/hm-types-builtin/buffer.hm +8 -0
- package/hm-types-builtin/child_process.hm +16 -0
- package/hm-types-builtin/crypto.hm +17 -0
- package/hm-types-builtin/fs.hm +15 -0
- package/hm-types-builtin/http.hm +14 -0
- package/hm-types-builtin/os.hm +19 -0
- package/hm-types-builtin/path.hm +18 -0
- package/hm-types-builtin/react-dom.hm +27 -0
- package/hm-types-builtin/react.hm +113 -0
- package/hm-types-builtin/url.hm +7 -0
- package/hm-types-builtin/zlib.hm +12 -0
- package/package.json +53 -4
- package/schemas/hinmil.config.schema.json +261 -0
- package/schemas/package-hinmil.schema.json +59 -0
|
@@ -0,0 +1,642 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// ============================================================================
|
|
3
|
+
// Unification with diagnostics — error reporting, recovery and bridging
|
|
4
|
+
//
|
|
5
|
+
// Plain language: the raw puzzle solver (unify.ts) just says "doesn't fit".
|
|
6
|
+
// This wrapper turns that into a helpful, human-readable error at the right
|
|
7
|
+
// source location — and then *recovers* so one mistake doesn't set off a
|
|
8
|
+
// chain of follow-up errors (the offending placeholders are poisoned with a
|
|
9
|
+
// silent `$error` marker that suppresses further complaints).
|
|
10
|
+
//
|
|
11
|
+
// Extracted from the InferenceEngine class. These functions implement the
|
|
12
|
+
// diagnostic-producing unification path: pretty error messages, alias-expand
|
|
13
|
+
// retry, infinite-type reporting, auto-union bridging, constructor-callable
|
|
14
|
+
// bridging and `$error` poisoning for cascade suppression.
|
|
15
|
+
// ============================================================================
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.unifyWithDiagnostic = unifyWithDiagnostic;
|
|
18
|
+
exports.unifyArgWithDiagnostic = unifyArgWithDiagnostic;
|
|
19
|
+
exports.argumentOutsideParameter = argumentOutsideParameter;
|
|
20
|
+
exports.poisonFreeVars = poisonFreeVars;
|
|
21
|
+
exports.tryAutoUnion = tryAutoUnion;
|
|
22
|
+
exports.tryConstructorCallable = tryConstructorCallable;
|
|
23
|
+
exports.findRootTVarId = findRootTVarId;
|
|
24
|
+
const types_1 = require("../hm/types");
|
|
25
|
+
const env_1 = require("../hm/env");
|
|
26
|
+
const unify_1 = require("../hm/unify");
|
|
27
|
+
const typeUtils_1 = require("../hm/typeUtils");
|
|
28
|
+
const annotations_1 = require("./annotations");
|
|
29
|
+
const narrowing_1 = require("./narrowing");
|
|
30
|
+
function unifyWithDiagnostic(engine, t1, t2, node) {
|
|
31
|
+
try {
|
|
32
|
+
const s = (0, unify_1.unify)(engine.resolve(t1), engine.resolve(t2));
|
|
33
|
+
engine.subst.mergeFrom(s);
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
if (!(e instanceof unify_1.UnificationError))
|
|
37
|
+
throw e;
|
|
38
|
+
// Retry once with both sides expanded through `maybeExpandAlias`.
|
|
39
|
+
// This catches cases where one side is a zero-arg TCon alias for
|
|
40
|
+
// a TRecord (e.g. `GameRow :: { id :: Number, name :: String }`)
|
|
41
|
+
// and the other side is a TRecord literal. Without this fallback,
|
|
42
|
+
// `insert({ id: 1, name: "x" })` against `insert :: GameRow -> ...`
|
|
43
|
+
// would wrongly diagnose a TCon-vs-TRecord mismatch. Aliases are
|
|
44
|
+
// intentionally NOT eagerly expanded everywhere — many call paths
|
|
45
|
+
// need to keep the alias name visible — so we only expand here on
|
|
46
|
+
// the failure path.
|
|
47
|
+
const e1 = engine.maybeExpandAlias(engine.resolve(t1));
|
|
48
|
+
const e2 = engine.maybeExpandAlias(engine.resolve(t2));
|
|
49
|
+
if (e1 !== engine.resolve(t1) || e2 !== engine.resolve(t2)) {
|
|
50
|
+
try {
|
|
51
|
+
const s = (0, unify_1.unify)(e1, e2);
|
|
52
|
+
engine.subst.mergeFrom(s);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
catch (e2err) {
|
|
56
|
+
if (!(e2err instanceof unify_1.UnificationError))
|
|
57
|
+
throw e2err;
|
|
58
|
+
// fall through to normal diagnostic path
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// Suppress "Infinite type" during annotated constrained self-recursion
|
|
62
|
+
if (engine.suppressInfiniteType && e.message.startsWith('Infinite type:')) {
|
|
63
|
+
engine.infiniteTypeSuppressedDuringAnnotation = true;
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
// Fold-completion leniency: a TOP-LEVEL spread arrow chain
|
|
67
|
+
// (`a -> b -> ... -> z`) reconciled against a NON-function type stands for
|
|
68
|
+
// "the chain once fully applied" — a variadic chain only arises from an
|
|
69
|
+
// explicit `...` annotation, and the heterogeneous fold that produced it
|
|
70
|
+
// consumes all of its parameters at runtime. E.g. FP-library `combine`:
|
|
71
|
+
// `reduce (reducer) (liftN (n - 1) (hd)) (tl)` yields the accumulator
|
|
72
|
+
// CHAIN, while the annotated return is the terminal `m z`. Retry with
|
|
73
|
+
// the chain's terminal result before surfacing a mismatch.
|
|
74
|
+
{
|
|
75
|
+
const rr1 = engine.resolve(t1);
|
|
76
|
+
const rr2 = engine.resolve(t2);
|
|
77
|
+
const pair = rr1.tag === 'TFun' && (0, typeUtils_1.hasTopLevelSpreadChain)(rr1) && rr2.tag === 'TCon' ? [rr1, rr2] :
|
|
78
|
+
rr2.tag === 'TFun' && (0, typeUtils_1.hasTopLevelSpreadChain)(rr2) && rr1.tag === 'TCon' ? [rr2, rr1] :
|
|
79
|
+
null;
|
|
80
|
+
if (pair) {
|
|
81
|
+
try {
|
|
82
|
+
const s = (0, unify_1.unify)((0, types_1.flattenCurriedType)(pair[0]).result, pair[1]);
|
|
83
|
+
engine.subst.mergeFrom(s);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
catch (e3) {
|
|
87
|
+
if (!(e3 instanceof unify_1.UnificationError))
|
|
88
|
+
throw e3;
|
|
89
|
+
// fall through to normal diagnostic path
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
// Infinite type (occurs-check): emit a clear, actionable diagnostic
|
|
94
|
+
// attributed to the enclosing binding. A binding SIGNATURE overrules
|
|
95
|
+
// this — the post-pass in server.ts drops HM_INFINITE_TYPE diagnostics
|
|
96
|
+
// located inside an annotated binding extent.
|
|
97
|
+
if (e instanceof unify_1.OccursCheckError || e.message.startsWith('Infinite type:')) {
|
|
98
|
+
engine.occursCheckCount++;
|
|
99
|
+
const subject = engine.currentBindingName ?? 'Value';
|
|
100
|
+
let detail = '';
|
|
101
|
+
if (e instanceof unify_1.OccursCheckError) {
|
|
102
|
+
const [occStr, varStr] = (0, types_1.prettyTypeToStringPair)(e.occursInType, e.tvType);
|
|
103
|
+
detail = ` (${varStr} occurs in ${occStr})`;
|
|
104
|
+
}
|
|
105
|
+
engine.addDiag(`${subject} has an infinite type${detail}. Add a type signature.`, (0, annotations_1.narrowToIdentifier)(node));
|
|
106
|
+
poisonFreeVars(engine, engine.resolve(t1), engine.resolve(t2));
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
// Suppress unification errors involving `$error` — these come from
|
|
110
|
+
// property accesses on types where the property doesn't exist OR
|
|
111
|
+
// from a previous failed unification whose free TVars were poisoned
|
|
112
|
+
// (see `poisonFreeVars` below). A specific diagnostic has already
|
|
113
|
+
// been emitted at the originating site; surfacing further mismatches
|
|
114
|
+
// that mention `$error` would only add noise. Check the FULL type
|
|
115
|
+
// (not just the outermost tag) so cases like `Prop $error -> ...`
|
|
116
|
+
// are also suppressed.
|
|
117
|
+
const r1 = engine.resolve(t1);
|
|
118
|
+
const r2 = engine.resolve(t2);
|
|
119
|
+
if ((0, types_1.containsErrorType)(r1) || (0, types_1.containsErrorType)(r2))
|
|
120
|
+
return;
|
|
121
|
+
if (tryAutoUnion(engine, t1, r1, t2, r2))
|
|
122
|
+
return;
|
|
123
|
+
if (tryConstructorCallable(engine, t1, r1, t2, r2))
|
|
124
|
+
return;
|
|
125
|
+
// Pretty-print the types so internal variable IDs (t429, t430)
|
|
126
|
+
// are replaced with readable letters (a, b, c, ...).
|
|
127
|
+
const [pretty1, pretty2] = (0, types_1.prettyTypeToStringPair)(e.left, e.right);
|
|
128
|
+
// When unify produced a specialised arity-mismatch message, surface it
|
|
129
|
+
// verbatim so it gets classified as HM_TYPE_ARITY_MISMATCH (2008)
|
|
130
|
+
// instead of the generic HM_TYPE_MISMATCH (1001).
|
|
131
|
+
if (e.message.startsWith("Type constructor '") && e.message.includes('applied to')) {
|
|
132
|
+
const ctorName = e.left.tag === 'TCon' ? e.left.name : '?';
|
|
133
|
+
const got = e.left.tag === 'TCon' ? e.left.args.length : 0;
|
|
134
|
+
const exp = e.right.tag === 'TCon' ? e.right.args.length : 0;
|
|
135
|
+
engine.addDiag(`Type constructor '${ctorName}' expects ${exp} type argument${exp === 1 ? '' : 's'} but got ${got}`, (0, annotations_1.narrowToIdentifier)(node));
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
// When unify produced a specialised "is not a valid relation here"
|
|
139
|
+
// message (Picked-union mismatch), surface it verbatim instead of
|
|
140
|
+
// dumping the entire union.
|
|
141
|
+
if (/ is not a valid relation here\b/.test(e.message)) {
|
|
142
|
+
engine.addDiag(e.message, (0, annotations_1.narrowToIdentifier)(node));
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
engine.addDiag(`Type mismatch: Cannot unify '${pretty1}' with '${pretty2}'`, (0, annotations_1.narrowToIdentifier)(node));
|
|
146
|
+
// Error recovery: poison free type variables involved in the failed
|
|
147
|
+
// unification by binding them to the `$error` sentinel. Subsequent
|
|
148
|
+
// unifications that touch these variables are silently absorbed by the
|
|
149
|
+
// `isErrorType` check above (and at other unify sites), preventing one
|
|
150
|
+
// root-cause mismatch from cascading into multiple noisy diagnostics.
|
|
151
|
+
poisonFreeVars(engine, r1, r2);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/** Directional variant of `unifyWithDiagnostic` for the call-argument
|
|
155
|
+
* position. Adds a pre-check that surfaces missing required fields on
|
|
156
|
+
* a closed-record parameter — the standard `unify` is symmetric and
|
|
157
|
+
* historically lenient for closed-vs-closed records, so passing an
|
|
158
|
+
* empty `{}` against a parameter type `{ @@type :: String }` would
|
|
159
|
+
* otherwise silently succeed. Surplus fields on the argument side
|
|
160
|
+
* remain accepted (width subtyping). Falls through to the normal
|
|
161
|
+
* unification when no missing fields are detected. */
|
|
162
|
+
function unifyArgWithDiagnostic(engine, argType, paramType, node) {
|
|
163
|
+
const excess = findExcessRecordFields(engine, node, paramType);
|
|
164
|
+
if (excess)
|
|
165
|
+
engine.addDiag(excess, node);
|
|
166
|
+
const missing = findMissingRecordFields(engine, argType, paramType);
|
|
167
|
+
if (missing) {
|
|
168
|
+
engine.addDiag(missing, node);
|
|
169
|
+
// Still run the normal unify so downstream inference sees the binding
|
|
170
|
+
// — but the diagnostic above is the user-facing message.
|
|
171
|
+
}
|
|
172
|
+
const before = engine.diagnostics.length;
|
|
173
|
+
unifyWithDiagnostic(engine, argType, paramType, node);
|
|
174
|
+
if (engine.diagnostics.length === before) {
|
|
175
|
+
const outside = argumentOutsideParameter(engine, argType, paramType);
|
|
176
|
+
if (outside)
|
|
177
|
+
engine.addDiag(outside, (0, annotations_1.narrowToIdentifier)(node));
|
|
178
|
+
else
|
|
179
|
+
checkObjectLiteralFields(engine, node, argType, paramType);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* The fields of an object-literal argument (also the props of a JSX
|
|
184
|
+
* element) in the same direction as an argument: `<Text intent={level} />`
|
|
185
|
+
* with `level :: String` does not fit `intent :: Intent | Undefined`. The
|
|
186
|
+
* field types come from the inferred record of the argument (the type
|
|
187
|
+
* recorded at a value's start offset can belong to a sub-expression, such
|
|
188
|
+
* as `best` in `best == null ? null : best.loss`).
|
|
189
|
+
*/
|
|
190
|
+
function checkObjectLiteralFields(engine, node, argType, paramType) {
|
|
191
|
+
if (node.type !== 'ObjectExpression')
|
|
192
|
+
return;
|
|
193
|
+
const param = engine.maybeExpandAlias(engine.resolve(paramType));
|
|
194
|
+
const arg = engine.maybeExpandAlias(engine.resolve(argType));
|
|
195
|
+
if (param.tag !== 'TRecord' || arg.tag !== 'TRecord')
|
|
196
|
+
return;
|
|
197
|
+
for (const prop of node.properties) {
|
|
198
|
+
if (prop.type !== 'Property' || prop.computed)
|
|
199
|
+
continue;
|
|
200
|
+
const key = prop.key;
|
|
201
|
+
const name = key.type === 'Identifier'
|
|
202
|
+
? key.name
|
|
203
|
+
: key.type === 'Literal' ? String(key.value) : null;
|
|
204
|
+
if (name === null || !(name in param.fields) || !(name in arg.fields))
|
|
205
|
+
continue;
|
|
206
|
+
const outside = argumentOutsideParameter(engine, arg.fields[name], param.fields[name]);
|
|
207
|
+
if (outside)
|
|
208
|
+
engine.addDiag(outside.replace('Type mismatch:', `Type mismatch in field '${name}':`), prop.value);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/** The members of a type: the members of a (nested) union, or the type. */
|
|
212
|
+
function membersOf(engine, t) {
|
|
213
|
+
const out = [];
|
|
214
|
+
const collect = (u, depth) => {
|
|
215
|
+
const r = engine.maybeExpandAlias(engine.resolve(u));
|
|
216
|
+
if (depth < 6 && r.tag === 'TCon' && r.name === types_1.T_UNION) {
|
|
217
|
+
for (const a of r.args)
|
|
218
|
+
collect(a, depth + 1);
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
out.push(r);
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
collect(t, 0);
|
|
225
|
+
return out;
|
|
226
|
+
}
|
|
227
|
+
/** Is this member open (a type variable or a rigid one, a label variable,
|
|
228
|
+
* `Any`, the error sentinel)? */
|
|
229
|
+
function isOpenMember(t) {
|
|
230
|
+
return t.tag === 'TVar' || (0, types_1.isSkolem)(t) || (t.tag === 'TLabel' && t.kind !== 'lit')
|
|
231
|
+
|| (0, types_1.isAnyType)(t) || (0, types_1.isErrorType)(t) || (0, types_1.isWildcard)(t);
|
|
232
|
+
}
|
|
233
|
+
/** A union that describes ONE value with several faces rather than a choice:
|
|
234
|
+
* an overload set, or a value that is both callable and a record/table
|
|
235
|
+
* (`Table Row | ([Selector] -> QueryTag Row)`). */
|
|
236
|
+
function isFacetUnion(engine, t) {
|
|
237
|
+
const r = engine.resolve(t);
|
|
238
|
+
if (r.tag !== 'TCon' || r.name !== types_1.T_UNION)
|
|
239
|
+
return false;
|
|
240
|
+
if (r.overloadSet)
|
|
241
|
+
return true;
|
|
242
|
+
return r.args.some(a => engine.resolve(a).tag === 'TFun');
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Direction for a call argument: every member of the argument must fit a
|
|
246
|
+
* member of the parameter. The symmetric unifier accepts a union argument
|
|
247
|
+
* as soon as ONE member fits (`Number | Null` for `Number`, `Number | Null
|
|
248
|
+
* | Undefined` for `Number | Undefined`), and a plain `String` where a
|
|
249
|
+
* literal union is expected (`intent :: "none" | "danger"`), because a
|
|
250
|
+
* label and a string are interchangeable to it. Returns the diagnostic, or
|
|
251
|
+
* null when the argument fits.
|
|
252
|
+
*/
|
|
253
|
+
function argumentOutsideParameter(engine, argType, paramType) {
|
|
254
|
+
if (isFacetUnion(engine, argType))
|
|
255
|
+
return null;
|
|
256
|
+
// A list against a list: the same check on the elements
|
|
257
|
+
// (`Foo ([k])` with `k :: String` for `['id' | 'name']`).
|
|
258
|
+
const argList = engine.maybeExpandAlias(engine.resolve(argType));
|
|
259
|
+
const paramList = engine.maybeExpandAlias(engine.resolve(paramType));
|
|
260
|
+
if (argList.tag === 'TCon' && argList.name === 'Array' && argList.args.length === 1
|
|
261
|
+
&& paramList.tag === 'TCon' && paramList.name === 'Array' && paramList.args.length === 1) {
|
|
262
|
+
return argumentOutsideParameter(engine, argList.args[0], paramList.args[0]);
|
|
263
|
+
}
|
|
264
|
+
// Fast path for the common call: an argument that is not a union only
|
|
265
|
+
// matters against a parameter that can hold a literal (a union, a label,
|
|
266
|
+
// or an alias of one); a record, function, list or primitive cannot.
|
|
267
|
+
const rawArg = engine.resolve(argType);
|
|
268
|
+
const rawParam = engine.resolve(paramType);
|
|
269
|
+
const argMayBeUnion = (rawArg.tag === 'TCon' && rawArg.name === types_1.T_UNION)
|
|
270
|
+
|| (rawArg.tag === 'TCon' && rawArg.args.length === 0 && engine.typeAliases.has(rawArg.name));
|
|
271
|
+
const paramMayHoldLiteral = rawParam.tag === 'TLabel'
|
|
272
|
+
|| (rawParam.tag === 'TCon' && (rawParam.name === types_1.T_UNION || engine.typeAliases.has(rawParam.name)));
|
|
273
|
+
if (!argMayBeUnion && !paramMayHoldLiteral)
|
|
274
|
+
return null;
|
|
275
|
+
const params = membersOf(engine, paramType);
|
|
276
|
+
if (params.some(isOpenMember))
|
|
277
|
+
return null;
|
|
278
|
+
const args = membersOf(engine, argType);
|
|
279
|
+
// Only a union argument or a literal parameter: otherwise the unifier
|
|
280
|
+
// above is authoritative (and knows bindings a trial unify would miss,
|
|
281
|
+
// such as a higher-kinded `m`).
|
|
282
|
+
const literalParam = params.some(p => p.tag === 'TLabel' && p.kind === 'lit');
|
|
283
|
+
if (args.length < 2 && !literalParam)
|
|
284
|
+
return null;
|
|
285
|
+
// A literal parameter (`"danger"`) takes only that literal (or a label
|
|
286
|
+
// variable, or a `const` bound to it), not any string.
|
|
287
|
+
const fitsLiteral = (arg, value) => {
|
|
288
|
+
if (arg.tag === 'TCon' && arg.stringLiteral !== undefined)
|
|
289
|
+
return arg.stringLiteral === value;
|
|
290
|
+
return arg.tag === 'TLabel' && (arg.kind !== 'lit' || arg.value === value);
|
|
291
|
+
};
|
|
292
|
+
const literalValues = [];
|
|
293
|
+
for (const p of params)
|
|
294
|
+
if (p.tag === 'TLabel' && p.kind === 'lit')
|
|
295
|
+
literalValues.push(p.value);
|
|
296
|
+
const unifies = (arg, param) => {
|
|
297
|
+
try {
|
|
298
|
+
(0, unify_1.unify)(arg, param);
|
|
299
|
+
return true;
|
|
300
|
+
}
|
|
301
|
+
catch {
|
|
302
|
+
return false;
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
// Cheap first: a bare type (`Null`, `Number`, `Date`) by name, a compound
|
|
306
|
+
// one against the parameter members of the same shape (record against
|
|
307
|
+
// record, the same constructor); only without such a member every one.
|
|
308
|
+
const sameShape = (arg, param) => arg.tag === param.tag && (arg.tag !== 'TCon' || param.name === arg.name);
|
|
309
|
+
const fits = (arg) => {
|
|
310
|
+
if (isOpenMember(arg))
|
|
311
|
+
return true;
|
|
312
|
+
if (literalValues.some(v => fitsLiteral(arg, v)))
|
|
313
|
+
return true;
|
|
314
|
+
const others = params.filter(p => !(p.tag === 'TLabel' && p.kind === 'lit'));
|
|
315
|
+
if (arg.tag === 'TCon' && arg.args.length === 0
|
|
316
|
+
&& others.some(p => p.tag === 'TCon' && p.name === arg.name && p.args.length === 0)) {
|
|
317
|
+
return true;
|
|
318
|
+
}
|
|
319
|
+
const shaped = others.filter(p => sameShape(arg, p));
|
|
320
|
+
if (shaped.some(p => (0, types_1.structuralEquals)(arg, p)))
|
|
321
|
+
return true;
|
|
322
|
+
if (shaped.length > 0 && shaped.some(p => unifies(arg, p)))
|
|
323
|
+
return true;
|
|
324
|
+
const rest = others.filter(p => !sameShape(arg, p));
|
|
325
|
+
return rest.some(p => unifies(arg, p));
|
|
326
|
+
};
|
|
327
|
+
const stray = args.filter(a => !fits(a));
|
|
328
|
+
if (stray.length === 0)
|
|
329
|
+
return null;
|
|
330
|
+
const shown = (ts) => ts.length === 1 ? (0, types_1.prettyTypeToString)(ts[0]) : (0, types_1.prettyTypeToString)((0, types_1.tCon)(types_1.T_UNION, ts));
|
|
331
|
+
return `Type mismatch: Cannot unify '${shown(args)}' with '${shown(params)}' ('${shown(stray)}' does not fit)`;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Excess-property check (as in TypeScript, for "fresh" literals only): an
|
|
335
|
+
* object LITERAL passed where a NAMED closed record (a type alias) is
|
|
336
|
+
* expected may not have fields that record does not declare. Catches typos in optional fields —
|
|
337
|
+
* `{ form: d, to: d }` for `{ from :: Date | Undefined, to :: Date }` is
|
|
338
|
+
* otherwise accepted and `from` silently missing. Open records (`{ …,
|
|
339
|
+
* ...r }`), dictionaries, literals with a spread and computed keys are not
|
|
340
|
+
* checked.
|
|
341
|
+
*/
|
|
342
|
+
function findExcessRecordFields(engine, node, paramType) {
|
|
343
|
+
if (node.type !== 'ObjectExpression')
|
|
344
|
+
return null;
|
|
345
|
+
const literal = node;
|
|
346
|
+
const keys = [];
|
|
347
|
+
for (const prop of literal.properties) {
|
|
348
|
+
if (prop.type !== 'Property' || prop.computed)
|
|
349
|
+
return null;
|
|
350
|
+
const key = prop.key;
|
|
351
|
+
if (key.type === 'Identifier')
|
|
352
|
+
keys.push(key.name);
|
|
353
|
+
else if (key.type === 'Literal' && typeof key.value === 'string')
|
|
354
|
+
keys.push(String(key.value));
|
|
355
|
+
else
|
|
356
|
+
return null;
|
|
357
|
+
}
|
|
358
|
+
if (keys.length === 0)
|
|
359
|
+
return null;
|
|
360
|
+
let records = [engine.resolve(paramType)];
|
|
361
|
+
const first = records[0];
|
|
362
|
+
if (first.tag === 'TCon' && first.name === types_1.T_UNION) {
|
|
363
|
+
records = first.args.map(a => engine.resolve(a)).filter(a => !(a.tag === 'TCon' && a.name === types_1.tUndefined.name));
|
|
364
|
+
}
|
|
365
|
+
if (records.length !== 1)
|
|
366
|
+
return null;
|
|
367
|
+
const named = records[0];
|
|
368
|
+
const rec = engine.maybeExpandAlias(named);
|
|
369
|
+
if (rec.tag !== 'TRecord' || rec.rest || (rec.labelFields && rec.labelFields.length > 0))
|
|
370
|
+
return null;
|
|
371
|
+
// Only NAMED record types (`RangeParams :: { … }`) are exact: a record
|
|
372
|
+
// inferred from destructuring (`({ x }) => x`) or an inline annotation
|
|
373
|
+
// keeps JavaScript's lenience for extra fields.
|
|
374
|
+
const isNamed = (named.tag === 'TCon' && named.name !== types_1.T_UNION) || !!rec.alias;
|
|
375
|
+
if (!isNamed)
|
|
376
|
+
return null;
|
|
377
|
+
const known = Object.keys(rec.fields);
|
|
378
|
+
if (known.length === 0)
|
|
379
|
+
return null;
|
|
380
|
+
const unknown = keys.filter(k => !(k in rec.fields));
|
|
381
|
+
if (unknown.length === 0)
|
|
382
|
+
return null;
|
|
383
|
+
const hint = unknown.length === 1 ? closestName(unknown[0], known.filter(k => !keys.includes(k))) : null;
|
|
384
|
+
const names = unknown.map(k => `'${k}'`).join(', ');
|
|
385
|
+
return `Unknown field${unknown.length > 1 ? 's' : ''} ${names} on argument; expected '${(0, types_1.typeToString)(rec)}'${hint ? ` (did you mean '${hint}'?)` : ''}`;
|
|
386
|
+
}
|
|
387
|
+
/** The candidate within edit distance 2 of `name` (closest first), or null. */
|
|
388
|
+
function closestName(name, candidates) {
|
|
389
|
+
let best = null;
|
|
390
|
+
let bestDist = 3;
|
|
391
|
+
for (const c of candidates) {
|
|
392
|
+
const d = editDistance(name, c);
|
|
393
|
+
if (d < bestDist) {
|
|
394
|
+
best = c;
|
|
395
|
+
bestDist = d;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
return best;
|
|
399
|
+
}
|
|
400
|
+
/** Levenshtein distance with adjacent transpositions (`form` ↔ `from`). */
|
|
401
|
+
function editDistance(a, b) {
|
|
402
|
+
const d = Array.from({ length: a.length + 1 }, (_, i) => [i, ...Array(b.length).fill(0)]);
|
|
403
|
+
for (let j = 1; j <= b.length; j++)
|
|
404
|
+
d[0][j] = j;
|
|
405
|
+
for (let i = 1; i <= a.length; i++) {
|
|
406
|
+
for (let j = 1; j <= b.length; j++) {
|
|
407
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
408
|
+
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
|
|
409
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
|
|
410
|
+
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
return d[a.length][b.length];
|
|
415
|
+
}
|
|
416
|
+
/** Return a diagnostic message describing fields required by the
|
|
417
|
+
* parameter's closed record type that are absent from the argument's
|
|
418
|
+
* closed record type, or `null` if every required field is present. */
|
|
419
|
+
function findMissingRecordFields(engine, argType, paramType) {
|
|
420
|
+
const stripOptional = (t) => {
|
|
421
|
+
const r = engine.resolve(t);
|
|
422
|
+
if (r.tag === 'TCon' && r.name === types_1.T_UNION) {
|
|
423
|
+
return r.args
|
|
424
|
+
.map(a => engine.resolve(a))
|
|
425
|
+
.filter(a => !(a.tag === 'TCon' && a.name === types_1.tUndefined.name));
|
|
426
|
+
}
|
|
427
|
+
return [r];
|
|
428
|
+
};
|
|
429
|
+
const paramCands = stripOptional(paramType);
|
|
430
|
+
const argCands = stripOptional(argType);
|
|
431
|
+
for (const p of paramCands) {
|
|
432
|
+
const pExp = engine.maybeExpandAlias(p);
|
|
433
|
+
if (pExp.tag !== 'TRecord' || pExp.rest)
|
|
434
|
+
continue;
|
|
435
|
+
// Find at least one arg-side record that satisfies the param shape;
|
|
436
|
+
// if every arg-side record is missing required fields, report the
|
|
437
|
+
// gap against the first such candidate.
|
|
438
|
+
let firstMissing = null;
|
|
439
|
+
let anySatisfies = false;
|
|
440
|
+
for (const a of argCands) {
|
|
441
|
+
const aExp = engine.maybeExpandAlias(a);
|
|
442
|
+
if (aExp.tag !== 'TRecord' || aExp.rest) {
|
|
443
|
+
// Non-record arg candidate (e.g. TVar, TCon) — can't validate, skip.
|
|
444
|
+
anySatisfies = true;
|
|
445
|
+
break;
|
|
446
|
+
}
|
|
447
|
+
// A field is "missing" only when the argument lacks it AND the
|
|
448
|
+
// parameter's declared type for that field does NOT admit Undefined.
|
|
449
|
+
// A field typed as `T | Undefined` (or just `Undefined`) is implicitly
|
|
450
|
+
// optional — omitting it on the argument is equivalent to `undefined`.
|
|
451
|
+
const missing = Object.keys(pExp.fields).filter(k => {
|
|
452
|
+
if (k in aExp.fields)
|
|
453
|
+
return false;
|
|
454
|
+
return !(0, narrowing_1.typeAcceptsUndefined)(engine.subst, pExp.fields[k]);
|
|
455
|
+
});
|
|
456
|
+
if (missing.length === 0) {
|
|
457
|
+
anySatisfies = true;
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
if (!firstMissing)
|
|
461
|
+
firstMissing = { fields: missing, param: pExp };
|
|
462
|
+
}
|
|
463
|
+
if (!anySatisfies && firstMissing) {
|
|
464
|
+
const fields = firstMissing.fields.map(k => `'${k}'`).join(', ');
|
|
465
|
+
return `Missing required field${firstMissing.fields.length > 1 ? 's' : ''} ${fields} on argument; expected '${(0, types_1.typeToString)(firstMissing.param)}'`;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
return null;
|
|
469
|
+
}
|
|
470
|
+
/** Bind every free TVar reachable from the given types to the `$error`
|
|
471
|
+
* sentinel. Used to recover from a failed unification so downstream
|
|
472
|
+
* inference can continue without producing cascading noise. */
|
|
473
|
+
function poisonFreeVars(engine, ...types) {
|
|
474
|
+
if (engine.suppressPoisonDepth > 0)
|
|
475
|
+
return;
|
|
476
|
+
const errorType = (0, types_1.tCon)(types_1.T_ERROR, []);
|
|
477
|
+
const seen = new Set();
|
|
478
|
+
for (const t of types) {
|
|
479
|
+
for (const id of (0, env_1.freeTypeVarsInType)(t)) {
|
|
480
|
+
if (seen.has(id))
|
|
481
|
+
continue;
|
|
482
|
+
seen.add(id);
|
|
483
|
+
engine.subst.set(id, errorType);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Try to automatically create a union type when unification of two
|
|
489
|
+
* concrete types fails. Returns true if a union was created.
|
|
490
|
+
*/
|
|
491
|
+
function tryAutoUnion(engine, t1, r1, t2, r2) {
|
|
492
|
+
if (engine.suppressAutoUnion)
|
|
493
|
+
return false;
|
|
494
|
+
if ((0, typeUtils_1.isUnionable)(r1) && (0, typeUtils_1.isUnionable)(r2)) {
|
|
495
|
+
// Build the union from both sides
|
|
496
|
+
const union = (0, typeUtils_1.mergeUnion)(r1, r2);
|
|
497
|
+
// Find a TVar in the substitution chain from t1 or t2
|
|
498
|
+
const rootId = findRootTVarId(engine, t1) ?? findRootTVarId(engine, t2);
|
|
499
|
+
if (rootId === null)
|
|
500
|
+
return false;
|
|
501
|
+
engine.subst.set(rootId, union);
|
|
502
|
+
return true;
|
|
503
|
+
}
|
|
504
|
+
// Same-name parameterized types: union the differing args.
|
|
505
|
+
// e.g. Either String Number + Either String String → Either String (Number | String)
|
|
506
|
+
if (r1.tag === 'TCon' && r2.tag === 'TCon' &&
|
|
507
|
+
r1.name === r2.name && r1.args.length === r2.args.length && r1.args.length > 0) {
|
|
508
|
+
const unionArgs = [];
|
|
509
|
+
for (let i = 0; i < r1.args.length; i++) {
|
|
510
|
+
try {
|
|
511
|
+
const s = (0, unify_1.unify)(r1.args[i], r2.args[i]);
|
|
512
|
+
engine.subst.mergeFrom(s);
|
|
513
|
+
unionArgs.push(s.apply(r1.args[i]));
|
|
514
|
+
}
|
|
515
|
+
catch {
|
|
516
|
+
// Differing args: try unionable merge or recursive same-constructor merge
|
|
517
|
+
if ((0, typeUtils_1.isUnionable)(r1.args[i]) && (0, typeUtils_1.isUnionable)(r2.args[i])) {
|
|
518
|
+
unionArgs.push((0, typeUtils_1.mergeUnion)(r1.args[i], r2.args[i]));
|
|
519
|
+
}
|
|
520
|
+
else {
|
|
521
|
+
const nested = (0, typeUtils_1.mergeSameConstructor)(r1.args[i], r2.args[i]);
|
|
522
|
+
if (!nested)
|
|
523
|
+
return false;
|
|
524
|
+
unionArgs.push(nested);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
const merged = (0, types_1.tCon)(r1.name, unionArgs);
|
|
529
|
+
const rootId = findRootTVarId(engine, t1) ?? findRootTVarId(engine, t2);
|
|
530
|
+
if (rootId === null)
|
|
531
|
+
return false;
|
|
532
|
+
engine.subst.set(rootId, merged);
|
|
533
|
+
return true;
|
|
534
|
+
}
|
|
535
|
+
return false;
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Try to bridge a TCon ↔ TFun mismatch using the TCon name's constructor
|
|
539
|
+
* annotations. When a value of type `QueryTag b` is required to be a
|
|
540
|
+
* function (e.g. it's passed to `one(t => t(p).map(...))`), check the
|
|
541
|
+
* annotation map for `QueryTag :: a -> Promise [b]` and instantiate that
|
|
542
|
+
* signature into the TVar slot, so the constructor's call-overloads
|
|
543
|
+
* become usable as the value's type at this site.
|
|
544
|
+
*/
|
|
545
|
+
function tryConstructorCallable(engine, t1, r1, t2, r2) {
|
|
546
|
+
const tryOne = (tcon, fn, hostT) => {
|
|
547
|
+
if (tcon.tag !== 'TCon' || fn.tag !== 'TFun')
|
|
548
|
+
return false;
|
|
549
|
+
// Only `Name() :: T` (instance-callable) annotations can describe
|
|
550
|
+
// how a value of an uppercase TCon is invoked. The constructor
|
|
551
|
+
// scheme `Name :: ...` describes how to construct a `Name`, never
|
|
552
|
+
// how to call one — so it is intentionally NOT consulted here as
|
|
553
|
+
// a callable signature for an instance.
|
|
554
|
+
const localCallables = engine.instanceCallables.get(tcon.name);
|
|
555
|
+
if (!localCallables || localCallables.length === 0)
|
|
556
|
+
return false;
|
|
557
|
+
const candidates = localCallables.map((s) => (0, env_1.instantiate)(s)).filter((t) => t.tag === 'TFun');
|
|
558
|
+
// Expand bare polymorphic type aliases (e.g. `[Selector]` →
|
|
559
|
+
// `[String | Prop a' | …]` with fresh TVars) inside each candidate.
|
|
560
|
+
// The scheme came from an imported sidecar that was parsed before
|
|
561
|
+
// the consumer's alias map existed, so bare uppercase references
|
|
562
|
+
// need lazy instantiation here.
|
|
563
|
+
const expandBareAliases = (t) => (0, typeUtils_1.mapType)(t, n => {
|
|
564
|
+
if (n.tag !== 'TCon')
|
|
565
|
+
return undefined;
|
|
566
|
+
if (!engine.typeAliases.has(n.name))
|
|
567
|
+
return undefined;
|
|
568
|
+
if (n.args.length !== 0)
|
|
569
|
+
return undefined;
|
|
570
|
+
const vars = engine.typeAliasVars.get(n.name) ?? [];
|
|
571
|
+
if (vars.length === 0)
|
|
572
|
+
return undefined;
|
|
573
|
+
return engine.maybeExpandAlias(n);
|
|
574
|
+
});
|
|
575
|
+
const expandedCandidates = candidates.map(c => {
|
|
576
|
+
const ec = expandBareAliases(c);
|
|
577
|
+
return ec.tag === 'TFun' ? ec : c;
|
|
578
|
+
});
|
|
579
|
+
// When the host TCon (the value being called) carries a row arg —
|
|
580
|
+
// e.g. `QueryTag {projected}` from a Picked projection — specialise
|
|
581
|
+
// the carrier TVar of each candidate's result with that row. This
|
|
582
|
+
// turns `a -> RTask r e [b]` into `a -> RTask r e [{projected}]`,
|
|
583
|
+
// so the call's actual result type, signature help, and chained
|
|
584
|
+
// method completions all see the projected row instead of a bare
|
|
585
|
+
// type variable. When the host also carries a parameter arg (the
|
|
586
|
+
// `p` in `QueryTag p row`), substitute that into the candidate's
|
|
587
|
+
// parameter slot so e.g. `p -> RTask r e [{projected}]` is what
|
|
588
|
+
// unification sees.
|
|
589
|
+
const slots = (0, typeUtils_1.findHostSlots)(tcon);
|
|
590
|
+
// Display-only fallback: a host without a parameter slot in its
|
|
591
|
+
// declared arity may still carry a `paramHint` attached during
|
|
592
|
+
// call-site post-processing (see `bindParamSlot` in
|
|
593
|
+
// `src/infer/calls.ts`).
|
|
594
|
+
const paramArg = slots.paramArg ?? tcon.paramHint ?? null;
|
|
595
|
+
const specialisedCandidates = expandedCandidates.map(cand => {
|
|
596
|
+
if (cand.tag !== 'TFun')
|
|
597
|
+
return cand;
|
|
598
|
+
let out = cand;
|
|
599
|
+
const { paramTVarId, carrierTVarId } = (0, typeUtils_1.findOverloadSlots)(out);
|
|
600
|
+
if (slots.rowArg && carrierTVarId !== null) {
|
|
601
|
+
out = (0, typeUtils_1.substTVar)(out, carrierTVarId, slots.rowArg);
|
|
602
|
+
}
|
|
603
|
+
if (paramArg && paramTVarId !== null) {
|
|
604
|
+
out = (0, typeUtils_1.substTVar)(out, paramTVarId, paramArg);
|
|
605
|
+
}
|
|
606
|
+
return out;
|
|
607
|
+
});
|
|
608
|
+
for (const cand of specialisedCandidates) {
|
|
609
|
+
try {
|
|
610
|
+
const s = (0, unify_1.unify)(cand, fn);
|
|
611
|
+
// Bind the TCon-side TVar (if any in the chain) to the instantiated callable.
|
|
612
|
+
const rootId = findRootTVarId(engine, hostT);
|
|
613
|
+
if (rootId !== null)
|
|
614
|
+
engine.subst.set(rootId, cand);
|
|
615
|
+
engine.subst.mergeFrom(s);
|
|
616
|
+
return true;
|
|
617
|
+
}
|
|
618
|
+
catch { /* try next */ }
|
|
619
|
+
}
|
|
620
|
+
return false;
|
|
621
|
+
};
|
|
622
|
+
return tryOne(r1, r2, t1) || tryOne(r2, r1, t2);
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Follow a type through the substitution chain to find the deepest TVar
|
|
626
|
+
* that is bound to a concrete type. Returns that TVar's id, or null.
|
|
627
|
+
*/
|
|
628
|
+
function findRootTVarId(engine, t) {
|
|
629
|
+
if (t.tag !== 'TVar')
|
|
630
|
+
return null;
|
|
631
|
+
const bound = engine.subst.lookup(t.id);
|
|
632
|
+
if (!bound)
|
|
633
|
+
return null; // unbound TVar — no concrete binding
|
|
634
|
+
if (bound.tag === 'TVar') {
|
|
635
|
+
// Chain continues — follow it
|
|
636
|
+
const deeper = findRootTVarId(engine, bound);
|
|
637
|
+
return deeper ?? t.id;
|
|
638
|
+
}
|
|
639
|
+
// bound to a concrete type — this TVar is the root
|
|
640
|
+
return t.id;
|
|
641
|
+
}
|
|
642
|
+
//# sourceMappingURL=unifyDiag.js.map
|