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,1078 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// ============================================================================
|
|
3
|
+
// Annotation validation — sumtype variant fields, variant method
|
|
4
|
+
// implementations, annotation type/arity checking, unknown constructors
|
|
5
|
+
// (split from annotations.ts; verbatim moves)
|
|
6
|
+
//
|
|
7
|
+
// Plain language: before annotations are trusted, they are proof-read. This
|
|
8
|
+
// file checks that annotations only mention type names that exist, apply
|
|
9
|
+
// each type to the right number of arguments, and that sum type variants and
|
|
10
|
+
// their method implementations line up with their declarations. Mistakes
|
|
11
|
+
// become diagnostics pointing at the annotation itself.
|
|
12
|
+
// ============================================================================
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.validateDataVariantFields = validateDataVariantFields;
|
|
15
|
+
exports.validateVariantMethodAnnotations = validateVariantMethodAnnotations;
|
|
16
|
+
exports.validateVariantMethodImplementations = validateVariantMethodImplementations;
|
|
17
|
+
exports.buildTypeArities = buildTypeArities;
|
|
18
|
+
exports.applyArityHKGrouping = applyArityHKGrouping;
|
|
19
|
+
exports.validateAnnotationTypes = validateAnnotationTypes;
|
|
20
|
+
exports.findArityMismatches = findArityMismatches;
|
|
21
|
+
exports.hasStructuralMismatch = hasStructuralMismatch;
|
|
22
|
+
exports.hasParamShapeMismatch = hasParamShapeMismatch;
|
|
23
|
+
exports.annotationNameLoc = annotationNameLoc;
|
|
24
|
+
const types_1 = require("../hm/types");
|
|
25
|
+
const commentParser_1 = require("../commentParser");
|
|
26
|
+
const methodRegistry_1 = require("../registries/methodRegistry");
|
|
27
|
+
const constraintRegistry_1 = require("../registries/constraintRegistry");
|
|
28
|
+
const dataTypeRegistry_1 = require("../registries/dataTypeRegistry");
|
|
29
|
+
const typeUtils_1 = require("../hm/typeUtils");
|
|
30
|
+
const typeParser_1 = require("../hm/typeParser");
|
|
31
|
+
const commentParser_2 = require("../commentParser");
|
|
32
|
+
const parsing_1 = require("../parsing");
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
// Context interface for methods that need engine state
|
|
35
|
+
const domGlobals_1 = require("../registries/domGlobals");
|
|
36
|
+
const annotations_1 = require("./annotations");
|
|
37
|
+
/**
|
|
38
|
+
* Validate the field types written directly in the `|` shorthand of a sumtype
|
|
39
|
+
* declaration. Each bare uppercase field must be a known type (built-in,
|
|
40
|
+
* alias, data type, local constructor, or imported / `::types`-loaded), and
|
|
41
|
+
* arity-bearing built-ins such as `Object`/`Function` cannot appear bare
|
|
42
|
+
* because the shorthand has no way to supply their mandatory type arguments.
|
|
43
|
+
*
|
|
44
|
+
* Runs SEPARATELY from `processDataDirectives` (which registers the sumtype
|
|
45
|
+
* early) so it can execute AFTER `::types` directives and imports have been
|
|
46
|
+
* resolved — otherwise imported field types would be reported as unknown.
|
|
47
|
+
* Parenthesized (`(Object String a)`) and record (`{ x :: T }`) fields are
|
|
48
|
+
* skipped here: their inner types are validated through the variant's explicit
|
|
49
|
+
* constructor annotation via `validateAnnotationTypes`.
|
|
50
|
+
*/
|
|
51
|
+
function validateDataVariantFields(ctx, source) {
|
|
52
|
+
const directives = (0, commentParser_1.extractDataDirectives)(source);
|
|
53
|
+
if (directives.length === 0)
|
|
54
|
+
return;
|
|
55
|
+
const knownTypes = buildKnownTypeNames(ctx.annotationMap, ctx.typeAliases, source);
|
|
56
|
+
for (const decl of directives) {
|
|
57
|
+
const commentText = source.slice(decl.commentStart, decl.commentEnd);
|
|
58
|
+
const bodyStart = commentText.indexOf('::');
|
|
59
|
+
const bodyOffset = bodyStart >= 0 ? bodyStart : 0;
|
|
60
|
+
let fieldSearch = bodyOffset;
|
|
61
|
+
for (const variant of decl.variants) {
|
|
62
|
+
// Advance the cursor past the variant name so field positions are local.
|
|
63
|
+
const nameAt = commentText.indexOf(variant.name, fieldSearch);
|
|
64
|
+
if (nameAt >= 0)
|
|
65
|
+
fieldSearch = nameAt + variant.name.length;
|
|
66
|
+
for (const field of variant.fields) {
|
|
67
|
+
const fieldAt = commentText.indexOf(field, fieldSearch);
|
|
68
|
+
const fieldLoc = fieldAt >= 0
|
|
69
|
+
? { start: decl.commentStart + fieldAt, end: decl.commentStart + fieldAt + field.length }
|
|
70
|
+
: { start: decl.commentStart, end: decl.commentEnd };
|
|
71
|
+
if (fieldAt >= 0)
|
|
72
|
+
fieldSearch = fieldAt + field.length;
|
|
73
|
+
if (!/^[A-Z]/.test(field))
|
|
74
|
+
continue; // type variable or paren/record field — fine
|
|
75
|
+
const reqArity = annotations_1.BUILTIN_TYPE_ARITIES.get(field);
|
|
76
|
+
if (reqArity !== undefined) {
|
|
77
|
+
ctx.addDiag(`Type '${field}' requires ${reqArity} type argument${reqArity === 1 ? '' : 's'}; the '|' shorthand can't express them. Declare '${variant.name}' with an explicit constructor annotation (e.g. // ${variant.name} :: … ${field} k a … -> ${decl.name}).`, fieldLoc);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (field === decl.name)
|
|
81
|
+
continue; // recursive self-reference
|
|
82
|
+
if (!knownTypes.has(field)) {
|
|
83
|
+
ctx.addDiag(`Unknown type '${field}' in variant '${variant.name}'. Import it or declare it (e.g. add an import or a '// ${field} :: …' definition).`, fieldLoc);
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
// Known type used bare in the shorthand. If it carries type
|
|
87
|
+
// parameters (e.g. an imported `Body :: * -> *`), the space-separated
|
|
88
|
+
// shorthand can't supply them — a bare `Body` is parsed as one field
|
|
89
|
+
// and the following word as a *separate* field, which silently changes
|
|
90
|
+
// the variant's arity. Flag it the same way the annotation context
|
|
91
|
+
// rejects `Future Any Response a`; the fix is to parenthesize, e.g.
|
|
92
|
+
// `(Body a)`.
|
|
93
|
+
const knownDecl = (0, dataTypeRegistry_1.getDataType)(field);
|
|
94
|
+
const knownArity = knownDecl ? knownDecl.decl.typeVars.length : 0;
|
|
95
|
+
if (knownArity > 0) {
|
|
96
|
+
const sampleArgs = Array.from({ length: knownArity }, (_, i) => String.fromCharCode(97 + i)).join(' ');
|
|
97
|
+
ctx.addDiag(`Type '${field}' requires ${knownArity} type argument${knownArity === 1 ? '' : 's'}; a bare '${field}' in the '|' shorthand is read as a field on its own. Wrap it in parentheses, e.g. (${field} ${sampleArgs}).`, fieldLoc);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function validateVariantMethodAnnotations(annotationMap, addDiag) {
|
|
104
|
+
for (const [name, ann] of annotationMap) {
|
|
105
|
+
if (!name.includes('#'))
|
|
106
|
+
continue;
|
|
107
|
+
const [typeName, methodName] = name.split('#');
|
|
108
|
+
if (!typeName || !methodName)
|
|
109
|
+
continue;
|
|
110
|
+
const parentName = (0, dataTypeRegistry_1.getParentTypeName)(typeName);
|
|
111
|
+
if (parentName) {
|
|
112
|
+
addDiag(`'${typeName}' is a variant of '${parentName}'. Use '${parentName}#${methodName}' instead of '${typeName}#${methodName}'.`, annotationNameLoc(ann));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
// ---------------------------------------------------------------------------
|
|
117
|
+
// Missing variant method implementation detection
|
|
118
|
+
// ---------------------------------------------------------------------------
|
|
119
|
+
/**
|
|
120
|
+
* Collect all method implementations from the AST.
|
|
121
|
+
* Returns a Set of `"ClassName:methodName"` strings.
|
|
122
|
+
*/
|
|
123
|
+
function collectImplementedMethods(source) {
|
|
124
|
+
const implemented = new Set();
|
|
125
|
+
let ast;
|
|
126
|
+
try {
|
|
127
|
+
ast = (0, parsing_1.parseStrict)(source);
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
return implemented;
|
|
131
|
+
}
|
|
132
|
+
for (const stmt of ast.body) {
|
|
133
|
+
// Unwrap export wrappers
|
|
134
|
+
const decl = (stmt.type === 'ExportNamedDeclaration' || stmt.type === 'ExportDefaultDeclaration')
|
|
135
|
+
&& stmt.declaration
|
|
136
|
+
? stmt.declaration
|
|
137
|
+
: stmt;
|
|
138
|
+
// Class declarations: class Left { isLeft() { ... } }
|
|
139
|
+
if (decl.type === 'ClassDeclaration' && decl.id?.name) {
|
|
140
|
+
const className = decl.id.name;
|
|
141
|
+
for (const member of (decl.body?.body ?? [])) {
|
|
142
|
+
if (member.type !== 'MethodDefinition')
|
|
143
|
+
continue;
|
|
144
|
+
if (member.key?.type !== 'Identifier')
|
|
145
|
+
continue;
|
|
146
|
+
if (member.kind === 'constructor')
|
|
147
|
+
continue;
|
|
148
|
+
implemented.add(`${className}:${member.key.name}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
// Prototype assignments: Left.prototype.isLeft = function() { ... }
|
|
152
|
+
if (decl.type === 'ExpressionStatement'
|
|
153
|
+
&& decl.expression?.type === 'AssignmentExpression'
|
|
154
|
+
&& decl.expression.left?.type === 'MemberExpression') {
|
|
155
|
+
const proto = (0, commentParser_2.extractPrototypeMethod)(decl.expression.left);
|
|
156
|
+
if (proto) {
|
|
157
|
+
implemented.add(`${proto.typeName}:${proto.methodName}`);
|
|
158
|
+
}
|
|
159
|
+
// Object literal on prototype: Left.prototype = { isLeft: function() { ... } }
|
|
160
|
+
const left = decl.expression.left;
|
|
161
|
+
if (!left.computed
|
|
162
|
+
&& left.object?.type === 'Identifier'
|
|
163
|
+
&& left.property?.type === 'Identifier'
|
|
164
|
+
&& left.property.name === 'prototype'
|
|
165
|
+
&& decl.expression.right?.type === 'ObjectExpression') {
|
|
166
|
+
const className = left.object.name;
|
|
167
|
+
for (const prop of decl.expression.right.properties) {
|
|
168
|
+
if (prop.type === 'Property' && prop.key?.type === 'Identifier') {
|
|
169
|
+
implemented.add(`${className}:${prop.key.name}`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return implemented;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Detect sum type variants that are missing method implementations.
|
|
179
|
+
*
|
|
180
|
+
* For each parent type method annotation (e.g. `Either#isLeft :: ...`),
|
|
181
|
+
* checks whether every variant of that sum type has an implementation
|
|
182
|
+
* (class method or prototype assignment). Emits an informational diagnostic
|
|
183
|
+
* on the variant name in the sum type definition when missing.
|
|
184
|
+
*/
|
|
185
|
+
function validateVariantMethodImplementations(annotationMap, source, addDiag) {
|
|
186
|
+
const dataTypeNames = (0, dataTypeRegistry_1.getDataTypeNames)();
|
|
187
|
+
if (dataTypeNames.size === 0)
|
|
188
|
+
return;
|
|
189
|
+
// Collect parent method annotations: Map<parentTypeName, Set<methodName>>
|
|
190
|
+
// Only include annotations where the method type is a function (TFun) or
|
|
191
|
+
// Boolean (getter). Property annotations like `$value :: (e | a)` are set
|
|
192
|
+
// in constructors and don't need separate variant implementations.
|
|
193
|
+
const parentMethods = new Map();
|
|
194
|
+
for (const [name, ann] of annotationMap) {
|
|
195
|
+
if (!name.includes('#'))
|
|
196
|
+
continue;
|
|
197
|
+
const [typeName, methodName] = name.split('#');
|
|
198
|
+
if (!typeName || !methodName)
|
|
199
|
+
continue;
|
|
200
|
+
if (!dataTypeNames.has(typeName))
|
|
201
|
+
continue;
|
|
202
|
+
if (!ann.methodType)
|
|
203
|
+
continue;
|
|
204
|
+
if (ann.methodType.tag !== 'TFun' && !(ann.methodType.tag === 'TCon' && ann.methodType.name === 'Boolean'))
|
|
205
|
+
continue;
|
|
206
|
+
const methods = parentMethods.get(typeName) ?? new Set();
|
|
207
|
+
methods.add(methodName);
|
|
208
|
+
parentMethods.set(typeName, methods);
|
|
209
|
+
}
|
|
210
|
+
if (parentMethods.size === 0)
|
|
211
|
+
return;
|
|
212
|
+
const implemented = collectImplementedMethods(source);
|
|
213
|
+
for (const [parentName, methods] of parentMethods) {
|
|
214
|
+
const dt = (0, dataTypeRegistry_1.getDataType)(parentName);
|
|
215
|
+
if (!dt)
|
|
216
|
+
continue;
|
|
217
|
+
for (const methodName of methods) {
|
|
218
|
+
// Only flag missing implementations when at least one variant has
|
|
219
|
+
// implemented the method. When no variant has an implementation,
|
|
220
|
+
// the signature is likely a pure declaration (or work hasn't started).
|
|
221
|
+
const variantsWithImpl = dt.decl.variants.filter(v => implemented.has(`${v.name}:${methodName}`));
|
|
222
|
+
if (variantsWithImpl.length === 0)
|
|
223
|
+
continue;
|
|
224
|
+
for (const variant of dt.decl.variants) {
|
|
225
|
+
if (implemented.has(`${variant.name}:${methodName}`))
|
|
226
|
+
continue;
|
|
227
|
+
// Find the variant name position within the sumtype comment
|
|
228
|
+
const commentText = source.slice(dt.decl.commentStart, dt.decl.commentEnd);
|
|
229
|
+
const bodyStart = commentText.indexOf('::');
|
|
230
|
+
const offset = bodyStart >= 0 ? bodyStart : 0;
|
|
231
|
+
const searchText = commentText.slice(offset);
|
|
232
|
+
const variantMatch = new RegExp(`\\b${variant.name}\\b`).exec(searchText);
|
|
233
|
+
const variantLoc = variantMatch
|
|
234
|
+
? { start: dt.decl.commentStart + offset + variantMatch.index, end: dt.decl.commentStart + offset + variantMatch.index + variant.name.length }
|
|
235
|
+
: { start: dt.decl.commentStart, end: dt.decl.commentEnd };
|
|
236
|
+
const ann = annotationMap.get(`${parentName}#${methodName}`);
|
|
237
|
+
const related = ann
|
|
238
|
+
? [{ message: `${parentName}#${methodName} :: ${ann.raw}`, start: ann.commentStart, end: ann.commentEnd }]
|
|
239
|
+
: undefined;
|
|
240
|
+
// DiagnosticSeverity.Information = 3 (blue underline)
|
|
241
|
+
addDiag(`'${variant.name}' does not implement '${parentName}#${methodName}'`, variantLoc, related, 3);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
/**
|
|
248
|
+
* Build a map of known type constructor arities from all available sources:
|
|
249
|
+
* - sumtype declarations (via dataTypeRegistry)
|
|
250
|
+
* - Constructor annotations (e.g. `Pair :: (a, b) -> Pair a b` → arity 2)
|
|
251
|
+
* - Method registry (Array=1, Map=2, etc.)
|
|
252
|
+
* - Built-in hardcoded arities (StrMap=1)
|
|
253
|
+
*/
|
|
254
|
+
function buildTypeArities(annotationMap) {
|
|
255
|
+
const arities = new Map();
|
|
256
|
+
// 1. From sumtype declarations
|
|
257
|
+
for (const name of (0, dataTypeRegistry_1.getDataTypeNames)()) {
|
|
258
|
+
const dt = (0, dataTypeRegistry_1.getDataType)(name);
|
|
259
|
+
if (dt && dt.decl.typeVars.length > 0) {
|
|
260
|
+
arities.set(name, dt.decl.typeVars.length);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
// 2. From constructor annotations: name :: ... -> Name a b → arity = args.length
|
|
264
|
+
for (const [name, ann] of annotationMap) {
|
|
265
|
+
if (!ann.scheme || (0, annotations_1.isMethodAnnotationName)(name))
|
|
266
|
+
continue;
|
|
267
|
+
const returnType = (0, types_1.getReturnType)(ann.scheme.type);
|
|
268
|
+
if (returnType.tag === 'TCon' && returnType.name === name && returnType.args.length > 0) {
|
|
269
|
+
arities.set(name, returnType.args.length);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
// 3. From method registry (built-in types with registered arities)
|
|
273
|
+
for (const typeName of (0, methodRegistry_1.getRegisteredTypeNames)()) {
|
|
274
|
+
if (!arities.has(typeName)) {
|
|
275
|
+
const arity = (0, methodRegistry_1.getTypeArity)(typeName);
|
|
276
|
+
if (arity > 0)
|
|
277
|
+
arities.set(typeName, arity);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
// 4. From hardcoded built-in arities
|
|
281
|
+
for (const [name, arity] of annotations_1.BUILTIN_TYPE_ARITIES) {
|
|
282
|
+
if (!arities.has(name))
|
|
283
|
+
arities.set(name, arity);
|
|
284
|
+
}
|
|
285
|
+
return arities;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Apply arity-aware HK grouping to annotations that reference type constructors
|
|
289
|
+
* with known arities and have constraint variables.
|
|
290
|
+
*
|
|
291
|
+
* Instead of re-parsing (which would create new TVar IDs), this does a
|
|
292
|
+
* structural transform on the existing type tree: when a TCon has more args
|
|
293
|
+
* than its known arity and the excess args start with a constraint variable,
|
|
294
|
+
* group those trailing args into a $kv:name:id TCon.
|
|
295
|
+
*
|
|
296
|
+
* Example: `Pair(Boolean, TVar:f, TVar:m)` with arity 2 and constraint var `f`
|
|
297
|
+
* → `Pair(Boolean, $kv:f:42(TVar:m))`
|
|
298
|
+
*/
|
|
299
|
+
function applyArityHKGrouping(annotationMap, annotationByOffset, typeArities) {
|
|
300
|
+
if (typeArities.size === 0)
|
|
301
|
+
return;
|
|
302
|
+
const processAnnotation = (ann) => {
|
|
303
|
+
if (!ann.scheme)
|
|
304
|
+
return;
|
|
305
|
+
const constraints = ann.scheme.constraints;
|
|
306
|
+
if (!constraints || constraints.length === 0)
|
|
307
|
+
return;
|
|
308
|
+
// Build a set of constraint variable names for this annotation
|
|
309
|
+
const constraintVarNames = new Set(constraints.map(c => c.typeVarName));
|
|
310
|
+
// Build a map from constraint variable name → TVar id
|
|
311
|
+
// collectVarNames returns Map<id, name>
|
|
312
|
+
const idToName = new Map();
|
|
313
|
+
(0, types_1.collectVarNames)(ann.scheme.type, idToName);
|
|
314
|
+
if (ann.receiver)
|
|
315
|
+
(0, types_1.collectVarNames)(ann.receiver, idToName);
|
|
316
|
+
if (ann.methodType)
|
|
317
|
+
(0, types_1.collectVarNames)(ann.methodType, idToName);
|
|
318
|
+
const nameToId = new Map();
|
|
319
|
+
for (const [id, name] of idToName) {
|
|
320
|
+
if (constraintVarNames.has(name))
|
|
321
|
+
nameToId.set(name, id);
|
|
322
|
+
}
|
|
323
|
+
if (nameToId.size === 0)
|
|
324
|
+
return;
|
|
325
|
+
const transform = (t) => {
|
|
326
|
+
if (t.tag === 'TCon') {
|
|
327
|
+
// First recursively transform args
|
|
328
|
+
const newArgs = t.args.map(transform);
|
|
329
|
+
const arity = typeArities.get(t.name);
|
|
330
|
+
if (arity !== undefined && newArgs.length > arity) {
|
|
331
|
+
// Try to group excess args starting at position `arity - 1`
|
|
332
|
+
// The arg at `arity - 1` must be a TVar that is a constraint variable
|
|
333
|
+
const lastNormalIdx = arity - 1;
|
|
334
|
+
const candidate = newArgs[lastNormalIdx];
|
|
335
|
+
if (candidate.tag === 'TVar' && candidate.name && nameToId.has(candidate.name)) {
|
|
336
|
+
const hvArgs = newArgs.slice(lastNormalIdx + 1);
|
|
337
|
+
const kvCon = (0, types_1.tCon)((0, types_1.kvName)(candidate.name, candidate.id), hvArgs);
|
|
338
|
+
return (0, types_1.tCon)(t.name, [...newArgs.slice(0, lastNormalIdx), kvCon]);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
// If args changed, return new TCon
|
|
342
|
+
if (newArgs.some((a, i) => a !== t.args[i])) {
|
|
343
|
+
return (0, types_1.tCon)(t.name, newArgs);
|
|
344
|
+
}
|
|
345
|
+
return t;
|
|
346
|
+
}
|
|
347
|
+
if (t.tag === 'TFun') {
|
|
348
|
+
const newParam = transform(t.param);
|
|
349
|
+
const newResult = transform(t.result);
|
|
350
|
+
if (newParam !== t.param || newResult !== t.result) {
|
|
351
|
+
return (0, types_1.tFun)(newParam, newResult);
|
|
352
|
+
}
|
|
353
|
+
return t;
|
|
354
|
+
}
|
|
355
|
+
return t;
|
|
356
|
+
};
|
|
357
|
+
ann.scheme = {
|
|
358
|
+
...ann.scheme,
|
|
359
|
+
type: transform(ann.scheme.type),
|
|
360
|
+
};
|
|
361
|
+
if (ann.receiver)
|
|
362
|
+
ann.receiver = transform(ann.receiver);
|
|
363
|
+
if (ann.methodType)
|
|
364
|
+
ann.methodType = transform(ann.methodType);
|
|
365
|
+
};
|
|
366
|
+
for (const [, ann] of annotationMap) {
|
|
367
|
+
processAnnotation(ann);
|
|
368
|
+
}
|
|
369
|
+
for (const [, ann] of annotationByOffset) {
|
|
370
|
+
processAnnotation(ann);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Validate that all type constructors referenced in annotations are known.
|
|
375
|
+
* A type constructor is "known" if it is:
|
|
376
|
+
* - A built-in type (Number, String, Boolean, Array, etc.)
|
|
377
|
+
* - Registered in the method registry (via local or ::types annotations)
|
|
378
|
+
* - Defined as a constructor in the current file's annotations (e.g. `// Box :: a -> Box a`)
|
|
379
|
+
*/
|
|
380
|
+
function validateAnnotationTypes(annotations, source, annotationMap, typeAliases, addDiag) {
|
|
381
|
+
const knownTypes = buildKnownTypeNames(annotationMap, typeAliases, source);
|
|
382
|
+
const constructorArities = collectConstructorArities(annotationMap, typeAliases, knownTypes);
|
|
383
|
+
const aliasNames = new Set(typeAliases.keys());
|
|
384
|
+
validateConstructorReturnVars(annotations, typeAliases, addDiag);
|
|
385
|
+
checkUnknownTypeConstructors(annotations, annotationMap, knownTypes, addDiag);
|
|
386
|
+
// Check type constructor arity
|
|
387
|
+
for (const ann of annotations) {
|
|
388
|
+
if (!ann.scheme)
|
|
389
|
+
continue;
|
|
390
|
+
const mapAnn = ann.name ? annotationMap.get(ann.name) : undefined;
|
|
391
|
+
const useAnn = (mapAnn?.scheme) ? mapAnn : ann;
|
|
392
|
+
for (const t of (0, annotations_1.annotationTypeParts)(useAnn)) {
|
|
393
|
+
for (const { name, actual, expected } of findArityMismatches(t, constructorArities, aliasNames)) {
|
|
394
|
+
addDiag(`Type constructor '${name}' expects ${expected} type argument${expected === 1 ? '' : 's'} but got ${actual}`, annotationNameLoc(ann));
|
|
395
|
+
}
|
|
396
|
+
for (const loc of findBareTypeReps(t)) {
|
|
397
|
+
addDiag(`'TypeRep' requires a type argument, e.g. 'TypeRep m'`, annotationNameLoc(ann));
|
|
398
|
+
}
|
|
399
|
+
for (const { display } of findFunctionParamListErrors(t)) {
|
|
400
|
+
addDiag(`The first child of 'Function' must be a parameter-list array (e.g. 'Function [a] b' or 'Function [] b'), but got '${display}'`, annotationNameLoc(ann));
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
// A `::narrow <InstanceType>` target may UNDER-apply a parametric
|
|
404
|
+
// constructor (`::narrow Array` denotes an instance, saturated with fresh
|
|
405
|
+
// vars at narrowing time), but OVER-application (`::narrow Array a a`) is an
|
|
406
|
+
// error — the user should write `::narrow Array a`.
|
|
407
|
+
const narrowedType = useAnn.scheme?.typePredicate?.narrowedType;
|
|
408
|
+
if (narrowedType) {
|
|
409
|
+
for (const { name, actual, expected } of findArityMismatches(narrowedType, constructorArities, aliasNames)) {
|
|
410
|
+
if (actual > expected) {
|
|
411
|
+
addDiag(`Type constructor '${name}' expects ${expected} type argument${expected === 1 ? '' : 's'} but got ${actual}`, annotationNameLoc(ann));
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
// Validate type-constructor arity *inside* `::constraint` definitions.
|
|
417
|
+
// A bare parametric constructor that appears in a constraint body — whether
|
|
418
|
+
// in a method-record signature, a structural self-clause, or misused in an
|
|
419
|
+
// `extends`/super-constraint position (e.g. `{ f :: Array }`) — must carry
|
|
420
|
+
// its mandatory children (`Array a`), or use a `TypeRep Array` witness.
|
|
421
|
+
// This reuses the same arity diagnostic as ordinary annotation positions.
|
|
422
|
+
for (const directive of (0, commentParser_1.extractConstraintDirectives)(source)) {
|
|
423
|
+
const def = (0, constraintRegistry_1.parseConstraintDirective)(directive.text);
|
|
424
|
+
if (!def)
|
|
425
|
+
continue;
|
|
426
|
+
const cvars = def.headerParams ?? [def.typeVar];
|
|
427
|
+
const reportType = (t) => {
|
|
428
|
+
for (const { name, actual, expected } of findArityMismatches(t, constructorArities, aliasNames)) {
|
|
429
|
+
addDiag(`Type constructor '${name}' expects ${expected} type argument${expected === 1 ? '' : 's'} but got ${actual}`, directive);
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
// (a) method-record signatures (`{ map :: f a ~> (a -> b) -> f b }`)
|
|
433
|
+
for (const method of def.methods) {
|
|
434
|
+
try {
|
|
435
|
+
if (/~>/.test(method.signature)) {
|
|
436
|
+
const sig = (0, typeParser_1.parseMethodTypeSignature)(method.signature, cvars);
|
|
437
|
+
reportType(sig.receiver);
|
|
438
|
+
reportType(sig.methodType);
|
|
439
|
+
}
|
|
440
|
+
else {
|
|
441
|
+
reportType((0, typeParser_1.parseTypeSignature)(method.signature, cvars).type);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
catch {
|
|
445
|
+
/* malformed signatures are surfaced by other diagnostics */
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
// (b) structural self-clause bodies (`f :: Array String`)
|
|
449
|
+
for (const sc of def.selfClauses ?? []) {
|
|
450
|
+
reportType(sc.body);
|
|
451
|
+
}
|
|
452
|
+
// (c) `extends` names that are actually parametric type constructors
|
|
453
|
+
// misused as a structural body (`{ f :: Array }`). A genuine
|
|
454
|
+
// super-constraint carries no type-constructor arity, so this only
|
|
455
|
+
// fires for real type constructors that demand children.
|
|
456
|
+
for (const ext of def.extends ?? []) {
|
|
457
|
+
if ((0, constraintRegistry_1.lookupConstraint)(ext))
|
|
458
|
+
continue;
|
|
459
|
+
const dt = (0, dataTypeRegistry_1.getDataType)(ext);
|
|
460
|
+
const expected = dt
|
|
461
|
+
? dt.decl.typeVars.length
|
|
462
|
+
: ((0, methodRegistry_1.getTypeArity)(ext) || annotations_1.BUILTIN_TYPE_ARITIES.get(ext) || 0);
|
|
463
|
+
if (expected > 0) {
|
|
464
|
+
addDiag(`Type constructor '${ext}' expects ${expected} type argument${expected === 1 ? '' : 's'} but got 0`, directive);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
// Validate applied constraints
|
|
469
|
+
for (const ann of annotations) {
|
|
470
|
+
if (!ann.scheme?.constraints)
|
|
471
|
+
continue;
|
|
472
|
+
for (const constraint of ann.scheme.constraints) {
|
|
473
|
+
if (!constraint.isApplied)
|
|
474
|
+
continue;
|
|
475
|
+
const def = (0, constraintRegistry_1.lookupConstraint)(constraint.className);
|
|
476
|
+
if (!def)
|
|
477
|
+
continue;
|
|
478
|
+
const methods = (0, constraintRegistry_1.getEffectiveMethods)(constraint.className);
|
|
479
|
+
const typeArgNames = new Set();
|
|
480
|
+
const varPattern = new RegExp(`\\b${def.typeVar}\\s+([a-z])\\b`, 'g');
|
|
481
|
+
for (const method of methods) {
|
|
482
|
+
let match;
|
|
483
|
+
while ((match = varPattern.exec(method.signature)) !== null) {
|
|
484
|
+
typeArgNames.add(match[1]);
|
|
485
|
+
}
|
|
486
|
+
varPattern.lastIndex = 0;
|
|
487
|
+
}
|
|
488
|
+
if (typeArgNames.size > 1) {
|
|
489
|
+
addDiag(`Constraint '${constraint.className}' has methods that vary the type argument (${[...typeArgNames].map(n => `${def.typeVar} ${n}`).join(', ')}). Use '${constraint.className} ${constraint.typeVarName}' instead of '${constraint.className} (${constraint.typeVarName} ...)'`, annotationNameLoc(ann));
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
// Validate unconstrained kind-variable usages
|
|
494
|
+
for (const ann of annotations) {
|
|
495
|
+
if (!ann.scheme)
|
|
496
|
+
continue;
|
|
497
|
+
const ownConstraintVars = new Set((ann.scheme.constraints ?? []).map(c => c.typeVarName));
|
|
498
|
+
for (const t of (0, annotations_1.annotationTypeParts)(ann)) {
|
|
499
|
+
for (const varName of findUnconstrainedKvUsages(t, ownConstraintVars)) {
|
|
500
|
+
addDiag(`Type variable '${varName}' is used as a type constructor (${varName} ...) but has no constraint. Add a constraint like 'Functor ${varName} =>' to enable higher-kinded usage`, annotationNameLoc(ann));
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
// Validate bare-TVar value annotations on uppercase names (e.g. `Tag :: a`).
|
|
505
|
+
// Such an annotation lets the value `Tag` be used as ANY type, which is
|
|
506
|
+
// unsound for a name that is also used as a type constructor. The
|
|
507
|
+
// engineering-correct form is a constructor signature like `a -> Tag a`.
|
|
508
|
+
validateBareTVarValueAnnotations(annotations, annotationMap, addDiag);
|
|
509
|
+
// Validate record-spread / Merge over bare type-variable parameters. An
|
|
510
|
+
// annotation like `(a, b) -> { ...a, ...b }` (or its desugaring
|
|
511
|
+
// `(a, b) -> Merge a b`) spreads/merges parameters `a`/`b` that are bare
|
|
512
|
+
// type variables — they are not known to be records, so the merge is
|
|
513
|
+
// meaningless. The user must declare each parameter with a record form.
|
|
514
|
+
for (const ann of annotations) {
|
|
515
|
+
if (!ann.scheme)
|
|
516
|
+
continue;
|
|
517
|
+
for (const branch of unionBranches(ann.scheme.type)) {
|
|
518
|
+
for (const name of findBareParamMergeNames(branch)) {
|
|
519
|
+
addDiag(`Cannot merge bare type variable '${name}' — it is not known to be a record. Declare the parameter with a record form: '{${name}}', '{[k] :: ${name}}', or '{...${name}}'.`, annotationNameLoc(ann));
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Diagnostic 4a/4b: detect uppercase value annotations whose merged scheme
|
|
526
|
+
* contains a bare-TVar branch (e.g. `Tag :: a`). Emits a warning on the
|
|
527
|
+
* annotation itself, and a second warning on every `Tag#method` annotation
|
|
528
|
+
* that depends on `Tag` being a real type constructor.
|
|
529
|
+
*
|
|
530
|
+
* Both diagnostics are suppressed when the same name also has a constructor
|
|
531
|
+
* branch (`Tag :: ... -> Tag ...`), because the bare branch is then inert
|
|
532
|
+
* and has already been overridden by a sound declaration.
|
|
533
|
+
*/
|
|
534
|
+
function validateBareTVarValueAnnotations(annotations, annotationMap, addDiag) {
|
|
535
|
+
// Identify uppercase value names whose scheme contains a bare-TVar branch
|
|
536
|
+
// and NO constructor branch returning the same TCon.
|
|
537
|
+
const offenders = new Set();
|
|
538
|
+
for (const [name, ann] of annotationMap) {
|
|
539
|
+
if (!ann.scheme)
|
|
540
|
+
continue;
|
|
541
|
+
if ((0, annotations_1.isMethodAnnotationName)(name))
|
|
542
|
+
continue;
|
|
543
|
+
if (!/^[A-Z]/.test(name))
|
|
544
|
+
continue;
|
|
545
|
+
if ((0, dataTypeRegistry_1.isVariant)(name))
|
|
546
|
+
continue;
|
|
547
|
+
if ((0, dataTypeRegistry_1.isDataType)(name))
|
|
548
|
+
continue;
|
|
549
|
+
const branches = unionBranches(ann.scheme.type);
|
|
550
|
+
const hasBareTVar = branches.some(b => b.tag === 'TVar');
|
|
551
|
+
if (!hasBareTVar)
|
|
552
|
+
continue;
|
|
553
|
+
const hasOwnConstructor = branches.some(b => {
|
|
554
|
+
const ret = (0, types_1.getReturnType)(b);
|
|
555
|
+
return ret.tag === 'TCon' && ret.name === name;
|
|
556
|
+
});
|
|
557
|
+
if (hasOwnConstructor)
|
|
558
|
+
continue;
|
|
559
|
+
offenders.add(name);
|
|
560
|
+
}
|
|
561
|
+
if (offenders.size === 0)
|
|
562
|
+
return;
|
|
563
|
+
// 4a — warn on the annotation site itself. Emit one warning per
|
|
564
|
+
// annotation occurrence in the source so the user sees it on the
|
|
565
|
+
// exact `Name :: a` line they wrote.
|
|
566
|
+
// DiagnosticSeverity.Warning = 2.
|
|
567
|
+
for (const ann of annotations) {
|
|
568
|
+
if (!ann.name || !offenders.has(ann.name))
|
|
569
|
+
continue;
|
|
570
|
+
if (!ann.scheme)
|
|
571
|
+
continue;
|
|
572
|
+
if (ann.scheme.type.tag !== 'TVar')
|
|
573
|
+
continue;
|
|
574
|
+
addDiag(`'${ann.name}' is annotated as an unconstrained type variable, which lets it be used as any type. Did you mean a constructor signature like 'a -> ${ann.name} a'?`, annotationNameLoc(ann), 2);
|
|
575
|
+
}
|
|
576
|
+
// 4b — warn on each `Name#method` (or `Name.method`) annotation when
|
|
577
|
+
// `Name` is an offender. Such methods presuppose that `Name` is a real
|
|
578
|
+
// type constructor with values of type `Name a`, which the bare value
|
|
579
|
+
// annotation defeats.
|
|
580
|
+
for (const ann of annotations) {
|
|
581
|
+
if (!ann.name)
|
|
582
|
+
continue;
|
|
583
|
+
const sep = ann.name.includes('#') ? '#' : (ann.name.includes('.') ? '.' : null);
|
|
584
|
+
if (!sep)
|
|
585
|
+
continue;
|
|
586
|
+
const typeName = ann.name.split(sep)[0];
|
|
587
|
+
if (!offenders.has(typeName))
|
|
588
|
+
continue;
|
|
589
|
+
addDiag(`Method '${ann.name}' is declared on type '${typeName}', but '${typeName} :: a' annotates '${typeName}' as a plain type variable rather than a type constructor. Change the value annotation to a constructor form (e.g. 'a -> ${typeName} a') so instances of '${typeName}' have a proper type.`, annotationNameLoc(ann), 2);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
/** Split a type into union branches, or return [t] for non-union types. */
|
|
593
|
+
function unionBranches(t) {
|
|
594
|
+
if (t.tag === 'TCon' && t.name === types_1.T_UNION)
|
|
595
|
+
return t.args;
|
|
596
|
+
return [t];
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Detect `Merge` markers in a function annotation's result that reference a
|
|
600
|
+
* parameter declared as a *bare* type variable (rather than a record form).
|
|
601
|
+
*
|
|
602
|
+
* In `(a, b) -> { ...a, ...b }` (parsed as `Merge(TRow a, TRow b)`) or its
|
|
603
|
+
* desugaring `(a, b) -> Merge a b`, the parameters `a` and `b` are bare
|
|
604
|
+
* `TVar`s — nothing constrains them to be records, so spreading/merging them
|
|
605
|
+
* is meaningless. By contrast `({a}, {...b}) -> { ...a, ...b }` binds `a`/`b`
|
|
606
|
+
* as record row variables, which is valid.
|
|
607
|
+
*
|
|
608
|
+
* Returns the distinct names of the offending bare-variable parameters.
|
|
609
|
+
*/
|
|
610
|
+
function findBareParamMergeNames(fnType) {
|
|
611
|
+
if (fnType.tag !== 'TFun')
|
|
612
|
+
return [];
|
|
613
|
+
// Walk the curried/tupled parameter spine, classifying each parameter as a
|
|
614
|
+
// bare type variable or a record row variable.
|
|
615
|
+
const bareParamNames = new Set();
|
|
616
|
+
const recordRowNames = new Set();
|
|
617
|
+
let cur = fnType;
|
|
618
|
+
while (cur.tag === 'TFun') {
|
|
619
|
+
const params = (0, types_1.isTuple)(cur.param) ? cur.param.args : [cur.param];
|
|
620
|
+
for (const p of params) {
|
|
621
|
+
if (p.tag === 'TVar')
|
|
622
|
+
bareParamNames.add(p.name);
|
|
623
|
+
else if (p.tag === 'TRecord' && p.rest && p.rest.tag === 'TRow') {
|
|
624
|
+
recordRowNames.add(p.rest.name);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
cur = cur.result;
|
|
628
|
+
}
|
|
629
|
+
if (bareParamNames.size === 0)
|
|
630
|
+
return [];
|
|
631
|
+
// Collect every Merge marker reachable in the (final) result type and check
|
|
632
|
+
// its operands against the bare-parameter set.
|
|
633
|
+
const offenders = new Set();
|
|
634
|
+
const visit = (t) => {
|
|
635
|
+
if (t.tag === 'TCon') {
|
|
636
|
+
if (t.name === types_1.T_MERGE) {
|
|
637
|
+
for (const arg of t.args) {
|
|
638
|
+
const argName = arg.tag === 'TVar' ? arg.name : arg.tag === 'TRow' ? arg.name : undefined;
|
|
639
|
+
if (argName && bareParamNames.has(argName) && !recordRowNames.has(argName)) {
|
|
640
|
+
offenders.add(argName);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
for (const arg of t.args)
|
|
645
|
+
visit(arg);
|
|
646
|
+
}
|
|
647
|
+
else if (t.tag === 'TFun') {
|
|
648
|
+
visit(t.param);
|
|
649
|
+
visit(t.result);
|
|
650
|
+
}
|
|
651
|
+
else if (t.tag === 'TRecord') {
|
|
652
|
+
for (const f of Object.values(t.fields))
|
|
653
|
+
visit(f);
|
|
654
|
+
}
|
|
655
|
+
};
|
|
656
|
+
visit(cur);
|
|
657
|
+
return [...offenders];
|
|
658
|
+
}
|
|
659
|
+
/** Collect all known type constructor names from built-ins, registries, aliases,
|
|
660
|
+
* imports, and constructor annotations (excluding method-annotation-only types). */
|
|
661
|
+
function buildKnownTypeNames(annotationMap, typeAliases, source) {
|
|
662
|
+
const knownTypes = new Set(annotations_1.BUILTIN_TYPE_NAMES);
|
|
663
|
+
// DOM/Fetch nominal types (Element, Event, Response, …) — valid in
|
|
664
|
+
// annotations when typed globals are on; Response always (fetch API).
|
|
665
|
+
for (const name of (0, domGlobals_1.activeDomAnnotationTypeNames)())
|
|
666
|
+
knownTypes.add(name);
|
|
667
|
+
for (const name of typeAliases.keys()) {
|
|
668
|
+
knownTypes.add(name);
|
|
669
|
+
}
|
|
670
|
+
// Uppercase names with a callable-value annotation (e.g.
|
|
671
|
+
// `Tag :: a -> Wrapped a` paired with `Tag :: a`) are deliberately
|
|
672
|
+
// excluded from `typeAliases` so the constructor-as-callable bridge
|
|
673
|
+
// can keep `tagged :: Tag a` as a TCon at the value site. Register
|
|
674
|
+
// the bare name here so other annotations referring to `Tag a` don't
|
|
675
|
+
// emit a spurious "unknown type constructor" diagnostic.
|
|
676
|
+
for (const [name, ann] of annotationMap) {
|
|
677
|
+
if (!ann.scheme)
|
|
678
|
+
continue;
|
|
679
|
+
if (name.includes('#') || name.includes('.'))
|
|
680
|
+
continue;
|
|
681
|
+
if (!/^[A-Z]/.test(name))
|
|
682
|
+
continue;
|
|
683
|
+
knownTypes.add(name);
|
|
684
|
+
}
|
|
685
|
+
// Type names introduced by LOCAL method annotations (e.g. Either2#map → Either2)
|
|
686
|
+
// should NOT auto-mark the type as known.
|
|
687
|
+
const localMethodTypeNames = new Set();
|
|
688
|
+
for (const name of annotationMap.keys()) {
|
|
689
|
+
if (name.includes('#'))
|
|
690
|
+
localMethodTypeNames.add(name.split('#')[0]);
|
|
691
|
+
if (name.includes('.'))
|
|
692
|
+
localMethodTypeNames.add(name.split('.')[0]);
|
|
693
|
+
}
|
|
694
|
+
for (const name of (0, methodRegistry_1.getRegisteredTypeNames)()) {
|
|
695
|
+
if (!localMethodTypeNames.has(name)) {
|
|
696
|
+
knownTypes.add(name);
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
for (const cName of (0, constraintRegistry_1.getConstraintNames)()) {
|
|
700
|
+
knownTypes.add(cName);
|
|
701
|
+
}
|
|
702
|
+
for (const name of (0, dataTypeRegistry_1.getDataTypeNames)()) {
|
|
703
|
+
knownTypes.add(name);
|
|
704
|
+
const dt = (0, dataTypeRegistry_1.getDataType)(name);
|
|
705
|
+
if (dt) {
|
|
706
|
+
for (const v of dt.decl.variants) {
|
|
707
|
+
knownTypes.add(v.name);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
for (const name of (0, commentParser_1.extractImportedNames)(source)) {
|
|
712
|
+
knownTypes.add(name);
|
|
713
|
+
}
|
|
714
|
+
return knownTypes;
|
|
715
|
+
}
|
|
716
|
+
/** Collect arity information from constructor annotations and type aliases.
|
|
717
|
+
* Also adds constructor return-type names to `knownTypes`. */
|
|
718
|
+
function collectConstructorArities(annotationMap, typeAliases, knownTypes) {
|
|
719
|
+
const constructorArities = new Map();
|
|
720
|
+
for (const [name, ann] of annotationMap) {
|
|
721
|
+
if (!ann.scheme)
|
|
722
|
+
continue;
|
|
723
|
+
if ((0, annotations_1.isMethodAnnotationName)(name))
|
|
724
|
+
continue;
|
|
725
|
+
const returnType = (0, types_1.getReturnType)(ann.scheme.type);
|
|
726
|
+
if (returnType.tag === 'TCon' && returnType.name === name) {
|
|
727
|
+
knownTypes.add(name);
|
|
728
|
+
if (returnType.args.length > 0) {
|
|
729
|
+
constructorArities.set(name, returnType.args.length);
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
if (returnType.tag === 'TCon' && returnType.name !== name && /^[A-Z]/.test(name)) {
|
|
733
|
+
if ((0, dataTypeRegistry_1.getParentTypeName)(name) === returnType.name) {
|
|
734
|
+
knownTypes.add(returnType.name);
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
// Add type alias arities
|
|
739
|
+
for (const [name, type] of typeAliases) {
|
|
740
|
+
if (constructorArities.has(name))
|
|
741
|
+
continue;
|
|
742
|
+
const ann = annotationMap.get(name);
|
|
743
|
+
if (ann?.scheme) {
|
|
744
|
+
const vars = (0, annotations_1.collectFreeVarNames)(type);
|
|
745
|
+
constructorArities.set(name, vars.length);
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
return constructorArities;
|
|
749
|
+
}
|
|
750
|
+
/** Validate that type vars in constructor return types appear in the parameter types.
|
|
751
|
+
* Only walks annotations parsed from the CURRENT file — never annotations
|
|
752
|
+
* pulled in via `// ::types <pkg>` (those live in the merged annotationMap
|
|
753
|
+
* but originate from a different source file and have offsets that don't
|
|
754
|
+
* belong to this document). */
|
|
755
|
+
function validateConstructorReturnVars(annotations, typeAliases, addDiag) {
|
|
756
|
+
for (const ann of annotations) {
|
|
757
|
+
const name = ann.name;
|
|
758
|
+
if (!name)
|
|
759
|
+
continue;
|
|
760
|
+
if (!ann.scheme)
|
|
761
|
+
continue;
|
|
762
|
+
if ((0, annotations_1.isMethodAnnotationName)(name))
|
|
763
|
+
continue;
|
|
764
|
+
if (!/^[A-Z]/.test(name))
|
|
765
|
+
continue;
|
|
766
|
+
const returnType = (0, types_1.getReturnType)(ann.scheme.type);
|
|
767
|
+
if (returnType.tag !== 'TCon' || returnType.name !== name)
|
|
768
|
+
continue;
|
|
769
|
+
if (returnType.args.length === 0)
|
|
770
|
+
continue;
|
|
771
|
+
const returnVarNames = new Map();
|
|
772
|
+
for (const arg of returnType.args)
|
|
773
|
+
(0, types_1.collectVarNames)(arg, returnVarNames);
|
|
774
|
+
if (returnVarNames.size === 0)
|
|
775
|
+
continue;
|
|
776
|
+
// Expand aliases in the parameter positions before collecting their
|
|
777
|
+
// type vars. A parameter written as an alias (e.g. `Rej` where
|
|
778
|
+
// `Rej :: e -> ()`) contributes the alias body's free vars; those
|
|
779
|
+
// whose name matches a return var are captured to the same outer var
|
|
780
|
+
// (see `expandAliases` capture-by-name), so `Future :: (Rej -> Res
|
|
781
|
+
// -> ()) -> Future e a` correctly derives `e`/`a` from the params.
|
|
782
|
+
const outerVars = new Map();
|
|
783
|
+
(0, typeUtils_1.eachType)(ann.scheme.type, n => {
|
|
784
|
+
if (n.tag === 'TVar' && !outerVars.has(n.name))
|
|
785
|
+
outerVars.set(n.name, n);
|
|
786
|
+
});
|
|
787
|
+
const expandedType = typeAliases.size > 0
|
|
788
|
+
? (0, annotations_1.expandAliases)(typeAliases, ann.scheme.type, undefined, undefined, undefined, outerVars)
|
|
789
|
+
: ann.scheme.type;
|
|
790
|
+
const paramVarNames = new Set();
|
|
791
|
+
let cur = expandedType;
|
|
792
|
+
while (cur.tag === 'TFun') {
|
|
793
|
+
const paramNames = new Map();
|
|
794
|
+
(0, types_1.collectVarNames)(cur.param, paramNames);
|
|
795
|
+
for (const n of paramNames.values())
|
|
796
|
+
paramVarNames.add(n);
|
|
797
|
+
cur = cur.result;
|
|
798
|
+
}
|
|
799
|
+
for (const varName of returnVarNames.values()) {
|
|
800
|
+
if (!paramVarNames.has(varName)) {
|
|
801
|
+
addDiag(`Type variable '${varName}' in return type '${name}' is not derivable from the parameter types. Each type variable in the return type must appear in at least one parameter`, annotationNameLoc(ann));
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
/** Check all annotations for unknown type constructors and constraint-as-type misuse. */
|
|
807
|
+
function checkUnknownTypeConstructors(annotations, annotationMap, knownTypes, addDiag) {
|
|
808
|
+
const constraintNames = new Set((0, constraintRegistry_1.getConstraintNames)());
|
|
809
|
+
for (const ann of annotations) {
|
|
810
|
+
if (!ann.scheme)
|
|
811
|
+
continue;
|
|
812
|
+
// Validate method annotation type name matches receiver type
|
|
813
|
+
if (ann.name?.includes('#') && ann.receiver?.tag === 'TCon') {
|
|
814
|
+
const typeName = ann.name.split('#')[0];
|
|
815
|
+
const receiverName = ann.receiver.name;
|
|
816
|
+
if (typeName && typeName !== receiverName) {
|
|
817
|
+
const ctorAnn = annotationMap.get(typeName);
|
|
818
|
+
const ctorReturnType = ctorAnn?.scheme ? (0, types_1.getReturnType)(ctorAnn.scheme.type) : null;
|
|
819
|
+
const isSubtype = ctorReturnType?.tag === 'TCon' && ctorReturnType.name === receiverName;
|
|
820
|
+
// A named transformer INSTANCE (`StateFuture :: TypeRep (StateT
|
|
821
|
+
// (Future e) s)`) may override a method for that instance only:
|
|
822
|
+
// `StateFuture#ap :: StateT (Future e) s a ~> …`.
|
|
823
|
+
const ctorType = ctorAnn?.scheme?.type;
|
|
824
|
+
const isInstance = ctorType?.tag === 'TCon' && ctorType.name === 'TypeRep'
|
|
825
|
+
&& ctorType.args[0]?.tag === 'TCon' && ctorType.args[0].name === receiverName;
|
|
826
|
+
if (!isSubtype && !isInstance) {
|
|
827
|
+
addDiag(`Type name mismatch in method annotation: '${typeName}' does not match receiver type '${receiverName}'. Did you mean '${receiverName}#${ann.name.split('#')[1]}'?`, annotationNameLoc(ann));
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
// Method/static annotations: validate type constructors in full scheme + receiver + method type
|
|
832
|
+
if ((0, annotations_1.isMethodAnnotationName)(ann.name)) {
|
|
833
|
+
const unknownMethodTypes = [];
|
|
834
|
+
for (const t of (0, annotations_1.annotationTypeParts)(ann)) {
|
|
835
|
+
unknownMethodTypes.push(...(0, types_1.findUnknownTCons)(t, knownTypes));
|
|
836
|
+
}
|
|
837
|
+
for (const unknown of new Set(unknownMethodTypes)) {
|
|
838
|
+
addDiag(`Unknown type constructor '${unknown}' in method annotation. Define it with a sum type declaration or \`// ::types\``, annotationNameLoc(ann));
|
|
839
|
+
}
|
|
840
|
+
continue;
|
|
841
|
+
}
|
|
842
|
+
const unknownTypes = [];
|
|
843
|
+
for (const t of (0, annotations_1.annotationTypeParts)(ann)) {
|
|
844
|
+
unknownTypes.push(...(0, types_1.findUnknownTCons)(t, knownTypes));
|
|
845
|
+
}
|
|
846
|
+
for (const unknown of new Set(unknownTypes)) {
|
|
847
|
+
addDiag(`Unknown type constructor '${unknown}'. Use \`// ::types\` to load its definition`, annotationNameLoc(ann));
|
|
848
|
+
}
|
|
849
|
+
// Check for constraint names used as type constructors
|
|
850
|
+
if (constraintNames.size > 0) {
|
|
851
|
+
const tconNames = new Set();
|
|
852
|
+
(0, typeUtils_1.eachType)(ann.scheme.type, n => {
|
|
853
|
+
if (n.tag === 'TCon' && /^[A-Z]/.test(n.name))
|
|
854
|
+
tconNames.add(n.name);
|
|
855
|
+
});
|
|
856
|
+
for (const name of tconNames) {
|
|
857
|
+
if (constraintNames.has(name)) {
|
|
858
|
+
addDiag(`'${name}' is a type class constraint, not a type constructor. Use it in the constraint section (e.g. '${name} f => ... f a ...'), not as a type.`, annotationNameLoc(ann));
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
/** Walk a type tree and find TCon nodes whose arg count mismatches the sumtype declaration. */
|
|
865
|
+
function findArityMismatches(t, constructorArities, aliasNames) {
|
|
866
|
+
const out = [];
|
|
867
|
+
// A type constructor that is the direct argument of `TypeRep` is a *type
|
|
868
|
+
// representative witness* (e.g. `TypeRep Function`, `TypeRep Either`), not an
|
|
869
|
+
// instantiation, so its mandatory-arity requirement does not apply — the
|
|
870
|
+
// witness may be a bare or partially-applied constructor.
|
|
871
|
+
const witnessExempt = new WeakSet();
|
|
872
|
+
(0, typeUtils_1.eachType)(t, n => {
|
|
873
|
+
if (n.tag === 'TCon' && n.name === types_1.T_TYPEREP) {
|
|
874
|
+
for (const a of n.args)
|
|
875
|
+
witnessExempt.add(a);
|
|
876
|
+
}
|
|
877
|
+
});
|
|
878
|
+
// A constructor that is a direct argument of a USER-defined type
|
|
879
|
+
// constructor may be under-applied by exactly one: it fills a
|
|
880
|
+
// higher-kinded parameter (`StateT (Future e) s a`, where StateT's `m` is
|
|
881
|
+
// used as `m (Pair a s)`). Kinds of user parameters are not tracked, so
|
|
882
|
+
// this is not checked further here; a wrong use still fails unification.
|
|
883
|
+
const hkArgSlots = new WeakSet();
|
|
884
|
+
(0, typeUtils_1.eachType)(t, n => {
|
|
885
|
+
if (n.tag !== 'TCon' || n.args.length === 0)
|
|
886
|
+
return;
|
|
887
|
+
if ((0, methodRegistry_1.isBuiltinType)(n.name) || annotations_1.BUILTIN_TYPE_ARITIES.has(n.name) || n.name === types_1.T_TYPEREP)
|
|
888
|
+
return;
|
|
889
|
+
for (const a of n.args)
|
|
890
|
+
if (a.tag === 'TCon')
|
|
891
|
+
hkArgSlots.add(a);
|
|
892
|
+
});
|
|
893
|
+
(0, typeUtils_1.eachType)(t, n => {
|
|
894
|
+
if (n.tag !== 'TCon')
|
|
895
|
+
return;
|
|
896
|
+
if (witnessExempt.has(n))
|
|
897
|
+
return;
|
|
898
|
+
if (hkArgSlots.has(n)) {
|
|
899
|
+
const full = (0, dataTypeRegistry_1.getDataType)(n.name)?.decl.typeVars.length ?? constructorArities.get(n.name) ?? (0, methodRegistry_1.getTypeArity)(n.name);
|
|
900
|
+
if (full !== undefined && full > 0 && n.args.length === full - 1)
|
|
901
|
+
return;
|
|
902
|
+
}
|
|
903
|
+
const dt = (0, dataTypeRegistry_1.getDataType)(n.name);
|
|
904
|
+
if (dt && n.args.length !== dt.decl.typeVars.length) {
|
|
905
|
+
out.push({ name: n.name, actual: n.args.length, expected: dt.decl.typeVars.length });
|
|
906
|
+
}
|
|
907
|
+
else if (!dt) {
|
|
908
|
+
// Check constructor annotation arities (e.g. Pair :: (a, b) -> Pair a b → arity 2)
|
|
909
|
+
const ctorArity = constructorArities.get(n.name);
|
|
910
|
+
if (ctorArity !== undefined && n.args.length !== ctorArity) {
|
|
911
|
+
// Aliases support partial application: any unprovided trailing
|
|
912
|
+
// parameters are freshened at expansion time (see `expandAliases`).
|
|
913
|
+
// So `[Selector]` and `GenericSelector UserTag` both type-check
|
|
914
|
+
// against a multi-param alias just like `Selector a b` does.
|
|
915
|
+
// Only over-application is reported. Real constructors (named in
|
|
916
|
+
// `aliasNames`'s complement) still require exact arity.
|
|
917
|
+
const isAlias = aliasNames
|
|
918
|
+
? aliasNames.has(n.name)
|
|
919
|
+
: !(0, dataTypeRegistry_1.getDataType)(n.name);
|
|
920
|
+
if (isAlias && n.args.length <= ctorArity)
|
|
921
|
+
return;
|
|
922
|
+
out.push({ name: n.name, actual: n.args.length, expected: ctorArity });
|
|
923
|
+
}
|
|
924
|
+
else if (ctorArity === undefined) {
|
|
925
|
+
// Check method registry arities (Array=1, Map=2, Set=1, Promise=1)
|
|
926
|
+
const regArity = (0, methodRegistry_1.getTypeArity)(n.name);
|
|
927
|
+
if (regArity > 0 && n.args.length !== regArity) {
|
|
928
|
+
out.push({ name: n.name, actual: n.args.length, expected: regArity });
|
|
929
|
+
}
|
|
930
|
+
else if (regArity === 0) {
|
|
931
|
+
// Fallback: hardcoded built-in arities
|
|
932
|
+
const expected = annotations_1.BUILTIN_TYPE_ARITIES.get(n.name);
|
|
933
|
+
if (expected !== undefined && n.args.length !== expected) {
|
|
934
|
+
out.push({ name: n.name, actual: n.args.length, expected });
|
|
935
|
+
}
|
|
936
|
+
else if (expected === undefined && n.args.length > 0 && (0, methodRegistry_1.isBuiltinType)(n.name)) {
|
|
937
|
+
// Nullary built-in types (Object, String, Number, Boolean, …) take
|
|
938
|
+
// no type arguments — `Object a b c` or `String a` is an error.
|
|
939
|
+
out.push({ name: n.name, actual: n.args.length, expected: 0 });
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
});
|
|
945
|
+
return out;
|
|
946
|
+
}
|
|
947
|
+
/** Walk a type tree and find applied `Function` nodes whose first child is not
|
|
948
|
+
* an array-like parameter list (`[x]` / `[x, y]` / `[]`). The first child of
|
|
949
|
+
* `Function` MUST be such an array (`Function [params] result`). */
|
|
950
|
+
function findFunctionParamListErrors(t) {
|
|
951
|
+
const out = [];
|
|
952
|
+
(0, typeUtils_1.eachType)(t, n => {
|
|
953
|
+
if (n.tag === 'TCon' && n.name === types_1.T_FUNCTION && n.args.length === 2 &&
|
|
954
|
+
(0, types_1.functionParamList)(n.args[0]) === null) {
|
|
955
|
+
out.push({ display: (0, types_1.typeToString)(n.args[0]) });
|
|
956
|
+
}
|
|
957
|
+
});
|
|
958
|
+
return out;
|
|
959
|
+
}
|
|
960
|
+
/** Walk a type tree and find bare `TypeRep` TCon nodes with no type arguments. */
|
|
961
|
+
function findBareTypeReps(t) {
|
|
962
|
+
const out = [];
|
|
963
|
+
(0, typeUtils_1.eachType)(t, n => {
|
|
964
|
+
if (n.tag === 'TCon' && n.name === types_1.T_TYPEREP && n.args.length === 0)
|
|
965
|
+
out.push(true);
|
|
966
|
+
});
|
|
967
|
+
return out;
|
|
968
|
+
}
|
|
969
|
+
/**
|
|
970
|
+
* Walk a type tree and collect kind-variable names ($kv:name:id) that are
|
|
971
|
+
* NOT in the given set of constraint variable names. Returns a deduplicated
|
|
972
|
+
* Set of unconstrained variable names used in HK position.
|
|
973
|
+
*/
|
|
974
|
+
function findUnconstrainedKvUsages(t, constraintVarNames) {
|
|
975
|
+
const out = new Set();
|
|
976
|
+
// Rank-N: constraints scoped inside a `forall` (e.g.
|
|
977
|
+
// `(forall f. Functor f => f a -> f a)`) also constrain their binder —
|
|
978
|
+
// collect them up-front so kv usages inside the forall body are covered.
|
|
979
|
+
const inScope = new Set(constraintVarNames);
|
|
980
|
+
(0, typeUtils_1.eachType)(t, n => {
|
|
981
|
+
if (n.tag === 'TForall' && n.constraints) {
|
|
982
|
+
for (const c of n.constraints)
|
|
983
|
+
inScope.add(c.typeVarName);
|
|
984
|
+
}
|
|
985
|
+
});
|
|
986
|
+
(0, typeUtils_1.eachType)(t, n => {
|
|
987
|
+
if (n.tag !== 'TCon')
|
|
988
|
+
return;
|
|
989
|
+
const kv = (0, types_1.parseKvName)(n.name);
|
|
990
|
+
if (kv && !inScope.has(kv.varName))
|
|
991
|
+
out.add(kv.varName);
|
|
992
|
+
});
|
|
993
|
+
return out;
|
|
994
|
+
}
|
|
995
|
+
// ---------------------------------------------------------------------------
|
|
996
|
+
// Rigid annotation checking
|
|
997
|
+
// ---------------------------------------------------------------------------
|
|
998
|
+
/**
|
|
999
|
+
* Detect whether the annotation type and the inferred type have a clear
|
|
1000
|
+
* structural mismatch — different parameter count/shape or incompatible
|
|
1001
|
+
* return-type constructors — that the rigid check can reliably catch even
|
|
1002
|
+
* when the annotation's return type contains quantified variables and
|
|
1003
|
+
* wildcards make identity-tracking unreliable.
|
|
1004
|
+
*
|
|
1005
|
+
* Covered mismatches:
|
|
1006
|
+
* - One side uses a tuple param, the other doesn't
|
|
1007
|
+
* - Both use tuples but with different arity
|
|
1008
|
+
* - Return types are distinct TCons (e.g. Undefined vs Array)
|
|
1009
|
+
*/
|
|
1010
|
+
function hasStructuralMismatch(annotationType, inferredType) {
|
|
1011
|
+
if (annotationType.tag !== 'TFun' || inferredType.tag !== 'TFun')
|
|
1012
|
+
return false;
|
|
1013
|
+
const annParam = annotationType.param;
|
|
1014
|
+
const infParam = inferredType.param;
|
|
1015
|
+
const annIsTuple = (0, types_1.isTuple)(annParam);
|
|
1016
|
+
const infIsTuple = (0, types_1.isTuple)(infParam);
|
|
1017
|
+
// Param structure: one is tuple, other is not
|
|
1018
|
+
if (annIsTuple !== infIsTuple)
|
|
1019
|
+
return true;
|
|
1020
|
+
// Both tuples but different arity
|
|
1021
|
+
if (annIsTuple && infIsTuple && annParam.args.length !== infParam.args.length)
|
|
1022
|
+
return true;
|
|
1023
|
+
// Return type top-level constructor mismatch
|
|
1024
|
+
const annReturn = (0, types_1.getReturnType)(annotationType);
|
|
1025
|
+
const infReturn = (0, types_1.getReturnType)(inferredType);
|
|
1026
|
+
if (annReturn.tag === 'TCon' && infReturn.tag === 'TCon'
|
|
1027
|
+
&& !(0, types_1.isWildcard)(annReturn) && !(0, types_1.isWildcard)(infReturn)
|
|
1028
|
+
&& !(0, types_1.isAnyType)(infReturn)
|
|
1029
|
+
&& annReturn.name !== infReturn.name) {
|
|
1030
|
+
return true;
|
|
1031
|
+
}
|
|
1032
|
+
// Record-vs-non-record return mismatch: the annotation's return is a record
|
|
1033
|
+
// shape (`{ x::T }`, `{ [k]::a }`, `{ ...a }`, or a `Merge` marker) but the
|
|
1034
|
+
// body returns a concrete NON-record value (`String`, `Number`, `[a]`, …).
|
|
1035
|
+
// The plain TCon-vs-TCon test above misses this because a record-shaped
|
|
1036
|
+
// annotation return is a `TRecord`, not a `TCon`. This only GATES whether
|
|
1037
|
+
// the rigid check runs; the rigid check itself (with the merge-shape
|
|
1038
|
+
// acceptance in `unify`) makes the final decision, so a record-ish body is
|
|
1039
|
+
// still accepted there.
|
|
1040
|
+
const annReturnIsRecordShape = annReturn.tag === 'TRecord' ||
|
|
1041
|
+
(annReturn.tag === 'TCon' && annReturn.name === types_1.T_MERGE);
|
|
1042
|
+
const infReturnIsConcreteNonRecord = infReturn.tag === 'TCon' && infReturn.name !== types_1.T_MERGE
|
|
1043
|
+
&& !(0, types_1.isWildcard)(infReturn) && !(0, types_1.isAnyType)(infReturn);
|
|
1044
|
+
if (annReturnIsRecordShape && infReturnIsConcreteNonRecord) {
|
|
1045
|
+
return true;
|
|
1046
|
+
}
|
|
1047
|
+
return false;
|
|
1048
|
+
}
|
|
1049
|
+
/**
|
|
1050
|
+
* Detect whether the annotation type and the inferred type have different
|
|
1051
|
+
* parameter shapes. A curried annotation (`a -> b -> c -> d`) paired with
|
|
1052
|
+
* a multi-param (tuple) implementation (`(a, b, c) -> d`) — or vice versa —
|
|
1053
|
+
* is a structural mismatch that the rigid check should always catch,
|
|
1054
|
+
* regardless of whether the return type contains quantified variables.
|
|
1055
|
+
*/
|
|
1056
|
+
function hasParamShapeMismatch(annotationType, inferredType) {
|
|
1057
|
+
if (annotationType.tag !== 'TFun' || inferredType.tag !== 'TFun')
|
|
1058
|
+
return false;
|
|
1059
|
+
const annIsTuple = (0, types_1.isTuple)(annotationType.param);
|
|
1060
|
+
const annIsCurriedMulti = !annIsTuple && annotationType.result.tag === 'TFun';
|
|
1061
|
+
const infIsTuple = (0, types_1.isTuple)(inferredType.param);
|
|
1062
|
+
const infIsCurriedMulti = !infIsTuple && inferredType.result.tag === 'TFun';
|
|
1063
|
+
// Mismatch when one side is curried and the other uses a tuple param
|
|
1064
|
+
return (annIsCurriedMulti && infIsTuple) || (annIsTuple && infIsCurriedMulti);
|
|
1065
|
+
}
|
|
1066
|
+
/**
|
|
1067
|
+
* Get the declaration-name location for an annotation. Prefers the JS
|
|
1068
|
+
* identifier range (`nameStart`/`nameEnd`) so that only the function or
|
|
1069
|
+
* variable name is underlined, not the entire `// :: signature` comment.
|
|
1070
|
+
* Falls back to the full comment range when no declaration name is available.
|
|
1071
|
+
*/
|
|
1072
|
+
function annotationNameLoc(ann) {
|
|
1073
|
+
if (typeof ann.signatureNameStart === 'number' && typeof ann.signatureNameEnd === 'number') {
|
|
1074
|
+
return { start: ann.signatureNameStart, end: ann.signatureNameEnd };
|
|
1075
|
+
}
|
|
1076
|
+
return { start: ann.commentStart, end: ann.commentEnd };
|
|
1077
|
+
}
|
|
1078
|
+
//# sourceMappingURL=annotation-validation.js.map
|