geam 0.1.2

Experimental Rust-embedded execution runtime for typed Gleam programs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
use crate::plan::ValueType;
use ecow::EcoString;
use thiserror::Error;

#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum InvalidTypedAstReason {
    #[error("custom type {package}:{module}.{name}: {reason}")]
    CustomType {
        package: EcoString,
        module: EcoString,
        name: EcoString,
        reason: Box<InvalidCustomTypeReason>,
    },
    #[error("function shape {name}: {reason}")]
    FunctionShape {
        name: EcoString,
        reason: InvalidFunctionShapeReason,
    },
    #[error("generated assignment")]
    GeneratedAssignment,
    #[error("use statement")]
    UseStatement,
    #[error("invalid expression node")]
    InvalidExpressionNode,
    #[error("expression shape refinement: expected {expected:?}, got {actual:?}")]
    ExpressionShapeRefinement {
        expected: ValueType,
        actual: ValueType,
    },
    #[error("pattern shape: {reason}")]
    PatternShape { reason: InvalidPatternShapeReason },
    #[error("expression shape: {kind}")]
    ExpressionShape { kind: InvalidExpressionShapeKind },
    #[error("expression type: expected {expected}, got {actual}")]
    ExpressionType {
        expected: InvalidExpressionType,
        actual: InvalidExpressionType,
    },
    #[error("expression value type: expected {expected:?}, got {actual:?}")]
    ExpressionValueTypeMismatch {
        expected: ValueType,
        actual: ValueType,
    },
    #[error("expression type has no supported runtime family; expected {expected}")]
    UnsupportedExpressionType { expected: InvalidExpressionType },
    #[error("call shape: {reason}")]
    CallShape { reason: InvalidCallShapeReason },
    #[error("case shape: {reason}")]
    CaseShape { reason: InvalidCaseShapeReason },
    #[error("pipeline shape: {reason}")]
    PipelineShape { reason: InvalidPipelineShapeReason },
    #[error("record update shape: {reason}")]
    RecordUpdateShape {
        reason: InvalidRecordUpdateShapeReason,
    },
    #[error("module reference {module}.{name}: {reason}")]
    ModuleReference {
        module: EcoString,
        name: EcoString,
        reason: InvalidModuleReferenceReason,
    },
    #[error("use shape: {reason}")]
    UseShape { reason: InvalidUseShapeReason },
    #[error("unknown local variable: {name}")]
    UnknownLocal { name: EcoString },
}

#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum InvalidPatternShapeReason {
    #[error("pattern type: expected {expected:?}, got {actual:?}")]
    TypeMismatch {
        expected: ValueType,
        actual: ValueType,
    },
    #[error("pattern annotation has no supported runtime type")]
    UnsupportedType,
    #[error("{actual} pattern cannot represent {expected:?}")]
    KindMismatch {
        expected: ValueType,
        actual: PatternKind,
    },
    #[error("tuple pattern arity: expected {expected}, got {actual}")]
    TupleArity { expected: usize, actual: usize },
    #[error("list tail must bind or discard, got {actual}")]
    ListTailKind { actual: PatternKind },
    #[error("{actual} pattern cannot be used as a total binding")]
    BindingKind { actual: PatternKind },
    #[error("binding aliases cannot contain another alias")]
    NestedBindingAlias,
    #[error("{actual} binding cannot represent {expected:?}")]
    BindingShape {
        expected: ValueType,
        actual: PatternKind,
    },
    #[error("binding shapes for {type_:?} are incompatible")]
    BindingShapeConflict { type_: ValueType },
    #[error("binding constructor {expected} does not match {actual:?}")]
    BindingConstructorRefinement {
        expected: usize,
        actual: Option<usize>,
    },
    #[error("list binding must not contain elements, got {actual}")]
    ListBindingElements { actual: usize },
    #[error("list binding must contain a tail")]
    ListBindingTailMissing,
    #[error("constructor binding is refutable across {constructors} constructors")]
    RefutableBindingConstructor { constructors: usize },
    #[error("bit-array binding segment count: expected 1, got {actual}")]
    BitArrayBindingSegmentCount { actual: usize },
    #[error("bit-array binding segment must not have a size")]
    BitArrayBindingSegmentSize,
    #[error("bit-array binding segment must use the bits option")]
    BitArrayBindingSegmentOptions,
    #[error("unsized bit-array segment {index} is not the final segment of {count}")]
    BitArrayUnsizedSegment { index: usize, count: usize },
    #[error("bit-array segment options: {reason}")]
    BitArraySegmentOptions {
        reason: InvalidBitArraySegmentOptionsReason,
    },
    #[error("bit-array size must use a size node, got {actual}")]
    BitArraySizePattern { actual: PatternKind },
    #[error("bit-array size variable {name} has no constructor metadata")]
    BitArraySizeUnresolved { name: EcoString },
    #[error("bit-array size variable {name} has an unsupported source")]
    BitArraySizeSource { name: EcoString },
    #[error("bit-array size constant is not an integer expression")]
    BitArraySizeConstant,
    #[error("{actual:?} cannot be used as a refutable assertion subject")]
    AssertSubject { actual: ValueType },
    #[error("invalid pattern node")]
    InvalidNode,
    #[error("bit-array size node used as a pattern")]
    BitArraySizeNode,
    #[error("constructor metadata is unresolved")]
    UnresolvedConstructor,
    #[error("constructor module: expected {expected}, got {actual}")]
    ConstructorModule {
        expected: EcoString,
        actual: EcoString,
    },
    #[error("constructor name: expected {expected}, got {actual}")]
    ConstructorName {
        expected: EcoString,
        actual: EcoString,
    },
    #[error("constructor index: expected {expected}, got {actual}")]
    ConstructorIndex { expected: usize, actual: usize },
    #[error("constructor arity: expected {expected}, got {actual}")]
    ConstructorArity { expected: usize, actual: usize },
    #[error("constructor pattern cannot represent {type_:?}")]
    ConstructorType { type_: ValueType },
    #[error("constructor spread is invalid for {type_:?}")]
    ConstructorSpread { type_: ValueType },
}

#[derive(Debug, Error, Clone, Copy, PartialEq, Eq)]
pub enum InvalidBitArraySegmentOptionsReason {
    #[error("multiple segment kinds")]
    MultipleKinds,
    #[error("multiple signedness options")]
    MultipleSignedness,
    #[error("multiple endianness options")]
    MultipleEndianness,
    #[error("multiple size options")]
    MultipleSizes,
    #[error("multiple unit options")]
    MultipleUnits,
    #[error("unit option without a size")]
    UnitWithoutSize,
    #[error("zero unit")]
    ZeroUnit,
    #[error("options incompatible with the segment kind")]
    Incompatible,
}

#[derive(Debug, Error, Clone, Copy, PartialEq, Eq)]
pub enum PatternKind {
    #[error("integer")]
    Int,
    #[error("float")]
    Float,
    #[error("string")]
    String,
    #[error("variable")]
    Variable,
    #[error("bit-array size")]
    BitArraySize,
    #[error("alias")]
    Assign,
    #[error("discard")]
    Discard,
    #[error("list")]
    List,
    #[error("constructor")]
    Constructor,
    #[error("tuple")]
    Tuple,
    #[error("bit array")]
    BitArray,
    #[error("string prefix")]
    StringPrefix,
    #[error("invalid")]
    Invalid,
}

#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum InvalidCustomTypeReason {
    #[error("type parameter {index} is not a generic parameter")]
    DefinitionParameter { index: usize },
    #[error("field {field} of constructor {constructor} has no supported type template")]
    DefinitionField {
        constructor: EcoString,
        field: usize,
    },
    #[error("template parameter {index} is outside {available} type arguments")]
    TemplateParameterIndex { index: usize, available: usize },
    #[error("template shape expected {expected:?}, got {actual:?}")]
    TemplateShapeMismatch {
        expected: ValueType,
        actual: ValueType,
    },
    #[error("parameter {parameter} inferred incompatible shapes {previous:?} and {actual:?}")]
    ConflictingParameterShape {
        parameter: usize,
        previous: ValueType,
        actual: ValueType,
    },
    #[error("custom type definition is missing")]
    MissingDefinition,
    #[error("type argument count: expected {expected}, got {actual}")]
    TypeArgumentCount { expected: usize, actual: usize },
    #[error("constructor index {index} is outside {available} constructors")]
    ConstructorIndex { index: usize, available: usize },
    #[error("constructor {index} name: expected {expected}, got {actual}")]
    ConstructorName {
        index: usize,
        expected: EcoString,
        actual: EcoString,
    },
    #[error("constructor module: expected {expected}, got {actual}")]
    ConstructorModule {
        expected: EcoString,
        actual: EcoString,
    },
    #[error("constructor arity: expected {expected}, got {actual}")]
    ConstructorArity { expected: usize, actual: usize },
    #[error("constructor result is {actual:?}, not a custom type")]
    ConstructorType { actual: ValueType },
    #[error("field index {index} is outside {available} fields")]
    FieldIndex { index: usize, available: usize },
    #[error("field {index} label: expected {expected:?}, got {actual:?}")]
    FieldLabel {
        index: usize,
        expected: Option<EcoString>,
        actual: Option<EcoString>,
    },
    #[error("field {index} type: expected {expected:?}, got {actual:?}")]
    FieldType {
        index: usize,
        expected: ValueType,
        actual: ValueType,
    },
    #[error("field {index} has incompatible result shape for {type_:?}")]
    FieldShapeConflict { index: usize, type_: ValueType },
}

#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum InvalidFunctionShapeReason {
    #[error("function argument types do not match signature")]
    ArgumentTypeMismatch,
    #[error("anonymous functions are not module functions")]
    Anonymous,
    #[error("empty function bodies are not supported")]
    EmptyBody,
    #[error("labelled function argument")]
    LabelledArgument,
    #[error("function expression has type {actual:?}")]
    ExpressionType { actual: ValueType },
    #[error("function return type does not match body")]
    ReturnTypeMismatch,
}

#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum InvalidExpressionShapeKind {
    #[error("bit-array constant size is not an integer")]
    BitArrayConstantSize,
    #[error("bit-array float size: got {actual}")]
    BitArrayFloatSize { actual: usize },
    #[error("bit-array segment has multiple kinds")]
    BitArrayMultipleKinds,
    #[error("bit-array segment cannot infer a kind from {actual:?}")]
    BitArrayImplicitKind { actual: ValueType },
    #[error("bit-array string segment has a size or non-default unit")]
    BitArrayStringOptions,
    #[error("bit-array codepoint segment has incompatible options")]
    BitArrayUtfCodepointOptions,
    #[error("unsupported bit-array segment option")]
    BitArrayUnsupportedOption,
    #[error("constant local variable")]
    ConstantLocalVariable,
    #[error("constant variable has no constructor metadata")]
    ConstantMissingConstructor,
    #[error("invalid constant node")]
    ConstantNode,
    #[error("constant record constructor kind")]
    ConstantRecordConstructorKind,
    #[error("constant record kind")]
    ConstantRecordKind,
    #[error("constant record constructor metadata is missing")]
    ConstantRecordMissingConstructor,
    #[error("constant record argument count: expected {expected}, got {actual}")]
    ConstantRecordArgumentCount { expected: usize, actual: usize },
    #[error("constant record argument {index} label: expected {expected:?}, got {actual:?}")]
    ConstantRecordArgumentLabel {
        index: usize,
        expected: Option<EcoString>,
        actual: Option<EcoString>,
    },
    #[error("constant record constructor type: got {actual:?}")]
    ConstantRecordConstructorType { actual: Option<ValueType> },
    #[error("constant prelude constructor {name}/{arity}: got {actual:?}")]
    ConstantPreludeConstructor {
        name: EcoString,
        arity: usize,
        actual: Option<ValueType>,
    },
    #[error("constant record update")]
    ConstantRecordUpdate,
    #[error("constant list element type")]
    ConstantListElementType,
    #[error("constant list type: got {actual:?}")]
    ConstantListType { actual: Option<ValueType> },
    #[error("constant tuple elements: expected {expected:?}, got {actual:?}")]
    ConstantTupleElements {
        expected: Vec<ValueType>,
        actual: Vec<ValueType>,
    },
    #[error("constant tuple type: got {actual:?}")]
    ConstantTupleType { actual: Option<ValueType> },
    #[error("echo expression is missing")]
    EchoExpressionMissing,
    #[error("function capture literal")]
    FunctionCaptureLiteral,
    #[error("function literal kind")]
    FunctionLiteralKind,
    #[error("generated todo message")]
    GeneratedTodoMessage,
    #[error("invalid guard node")]
    GuardNode,
    #[error("guard local shape")]
    GuardLocalShape,
    #[error("guard function local shape")]
    GuardFunctionLocalShape,
    #[error("list spread has no prefix elements")]
    ListSpreadEmptyPrefix,
    #[error("list expression type: got {actual:?}")]
    ListType { actual: ValueType },
    #[error("list index expression shape for {type_:?}")]
    ListIndexShape { type_: ValueType },
    #[error("local binding shape")]
    LocalBindingShape,
    #[error("invalid module constant node")]
    ModuleConstantNode,
    #[error("module constant bit-array float size: got {actual}")]
    ModuleConstantBitArrayFloatSize { actual: usize },
    #[error("module constant bit-array segment cannot infer a kind from {actual:?}")]
    ModuleConstantBitArrayImplicitKind { actual: ValueType },
    #[error("module constant bit-array segment has multiple kinds")]
    ModuleConstantBitArrayMultipleKinds,
    #[error("module constant bit-array size is not an integer")]
    ModuleConstantBitArraySize,
    #[error("module constant bit-array string segment has a size or non-default unit")]
    ModuleConstantBitArrayStringOptions,
    #[error("module constant bit-array codepoint option")]
    ModuleConstantBitArrayCodepointOption,
    #[error("unsupported module constant bit-array segment option")]
    ModuleConstantBitArrayUnsupportedOption,
    #[error("module constant list spread has no prefix elements")]
    ModuleConstantListSpreadEmptyPrefix,
    #[error("module constant list type: got {actual:?}")]
    ModuleConstantListType { actual: ValueType },
    #[error("module constant local variable")]
    ModuleConstantLocalVariable,
    #[error("module constant variable has no constructor metadata")]
    ModuleConstantMissingConstructor,
    #[error("module constant record kind")]
    ModuleConstantRecordKind,
    #[error("module constant record constructor metadata is missing")]
    ModuleConstantRecordMissingConstructor,
    #[error("module constant record argument count: expected {expected}, got {actual}")]
    ModuleConstantRecordArgumentCount { expected: usize, actual: usize },
    #[error("module constant record argument {index} label: expected {expected:?}, got {actual:?}")]
    ModuleConstantRecordArgumentLabel {
        index: usize,
        expected: Option<EcoString>,
        actual: Option<EcoString>,
    },
    #[error("module constant record value must be custom, got {actual:?}")]
    ModuleConstantRecordCustomShape { actual: ValueType },
    #[error("module constant record reference must be a function, got {actual:?}")]
    ModuleConstantRecordFunctionShape { actual: ValueType },
    #[error("module constant prelude constructor {name}/{arity}: got {actual:?}")]
    ModuleConstantPreludeConstructor {
        name: EcoString,
        arity: usize,
        actual: ValueType,
    },
    #[error("module constant record update")]
    ModuleConstantRecordUpdate,
    #[error("module constant storage shape")]
    ModuleConstantStorageShape,
    #[error("module constant tuple arity: expected {expected}, got {actual}")]
    ModuleConstantTupleArity { expected: usize, actual: usize },
    #[error("module constant tuple type: got {actual:?}")]
    ModuleConstantTupleType { actual: ValueType },
    #[error("positional access")]
    PositionalAccess,
    #[error("custom constructor metadata is not a record")]
    CustomConstructorKind,
    #[error("record access")]
    RecordAccess,
    #[error("tuple expression arity: expected {expected}, got {actual}")]
    TupleArity { expected: usize, actual: usize },
    #[error("tuple index {index} is outside {available} elements")]
    TupleIndex { index: usize, available: usize },
    #[error("tuple expression type: got {actual:?}")]
    TupleType { actual: ValueType },
    #[error("variable function local shape")]
    VariableFunctionLocalShape,
    #[error("variable prelude constructor {name}/{arity}: got {actual:?}")]
    VariablePreludeConstructor {
        name: EcoString,
        arity: usize,
        actual: ValueType,
    },
}

#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum InvalidRecordUpdateShapeReason {
    #[error("record constructor argument count: expected {expected}, got {actual}")]
    ArgumentCount { expected: usize, actual: usize },
    #[error("record constructor argument {index} label: expected {expected:?}, got {actual:?}")]
    ArgumentLabel {
        index: usize,
        expected: Option<EcoString>,
        actual: Option<EcoString>,
    },
    #[error(
        "record update base assignment: requires assignment {requires_assignment}, got {has_assignment}"
    )]
    BaseAssignment {
        requires_assignment: bool,
        has_assignment: bool,
    },
    #[error("record constructor expression")]
    ConstructorExpression,
    #[error("record constructor kind")]
    ConstructorKind,
    #[error("record constructor name: expected {expected}, got {actual}")]
    ConstructorName {
        expected: EcoString,
        actual: EcoString,
    },
    #[error("record constructor result type: expected {expected:?}, got {actual:?}")]
    ConstructorResultType {
        expected: ValueType,
        actual: ValueType,
    },
    #[error("record update implicit argument {index} origin: got {actual}")]
    ImplicitArgumentOrigin {
        index: usize,
        actual: RecordUpdateArgumentOrigin,
    },
    #[error("record update implicit argument {argument} is not a field access")]
    ImplicitFieldExpression { argument: usize },
    #[error(
        "record update implicit argument {argument} field index: expected {expected}, got {actual}"
    )]
    ImplicitFieldIndex {
        argument: usize,
        expected: usize,
        actual: u64,
    },
    #[error(
        "record update implicit argument {argument} field label: expected {expected:?}, got {actual:?}"
    )]
    ImplicitFieldLabel {
        argument: usize,
        expected: Option<EcoString>,
        actual: Option<EcoString>,
    },
    #[error(
        "record update implicit argument {argument} field type: expected {expected:?}, got {actual:?}"
    )]
    ImplicitFieldType {
        argument: usize,
        expected: ValueType,
        actual: ValueType,
    },
    #[error("record update implicit argument {argument} generated target kind")]
    ImplicitGeneratedTargetKind { argument: usize },
    #[error("record update implicit argument {argument} generated target origin")]
    ImplicitGeneratedTargetOrigin { argument: usize },
    #[error(
        "record update implicit argument {argument} generated target type: expected {expected:?}, got {actual:?}"
    )]
    ImplicitGeneratedTargetType {
        argument: usize,
        expected: ValueType,
        actual: ValueType,
    },
    #[error("record update implicit argument {argument} original target constructor")]
    ImplicitOriginalTargetConstructor { argument: usize },
    #[error("record update implicit argument {argument} target is not a variable")]
    ImplicitTargetExpression { argument: usize },
    #[error(
        "record update implicit argument {argument} target name: expected {expected}, got {actual}"
    )]
    ImplicitTargetName {
        argument: usize,
        expected: EcoString,
        actual: EcoString,
    },
    #[error("record update source is not a custom type: got {actual:?}")]
    UpdatedSourceFamily { actual: ValueType },
    #[error("record update source type: expected {expected:?}, got {actual:?}")]
    UpdatedSourceType {
        expected: ValueType,
        actual: ValueType,
    },
}

#[derive(Debug, Error, Clone, Copy, PartialEq, Eq)]
pub enum RecordUpdateArgumentOrigin {
    #[error("incorrect-arity use")]
    IncorrectArityUse,
    #[error("pattern field spread")]
    PatternFieldSpread,
    #[error("pipe")]
    Pipe,
    #[error("record update")]
    RecordUpdate,
    #[error("use")]
    Use,
}

#[derive(Debug, Error, Clone, Copy, PartialEq, Eq)]
pub enum InvalidExpressionType {
    #[error("type parameter")]
    TypeParameter,
    #[error("Int")]
    Int,
    #[error("String")]
    String,
    #[error("BitArray")]
    BitArray,
    #[error("UtfCodepoint")]
    UtfCodepoint,
    #[error("custom type")]
    Custom,
    #[error("external type")]
    External,
    #[error("Float")]
    Float,
    #[error("Bool")]
    Bool,
    #[error("Nil")]
    Nil,
    #[error("Tuple")]
    Tuple,
    #[error("List")]
    List,
    #[error("Function")]
    Function,
}

impl InvalidExpressionType {
    pub(crate) fn from_value_type(type_: ValueType) -> Self {
        match type_ {
            ValueType::Parameter(_) => Self::TypeParameter,
            ValueType::Int => Self::Int,
            ValueType::String => Self::String,
            ValueType::BitArray => Self::BitArray,
            ValueType::UtfCodepoint => Self::UtfCodepoint,
            ValueType::Custom(_) => Self::Custom,
            ValueType::External(_) => Self::External,
            ValueType::Float => Self::Float,
            ValueType::Bool => Self::Bool,
            ValueType::Nil => Self::Nil,
            ValueType::Tuple(_) => Self::Tuple,
            ValueType::List(_) => Self::List,
            ValueType::Function(_) => Self::Function,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::InvalidExpressionType;
    use crate::plan::{ExternalType, ExternalTypeName, ValueType};

    #[test]
    fn classifies_external_value_types() {
        let type_ = ExternalType::new(
            ExternalTypeName::new("application".into(), "main".into(), "Token".into()),
            Vec::new(),
        );

        assert_eq!(
            InvalidExpressionType::from_value_type(ValueType::External(type_)),
            InvalidExpressionType::External,
        );
    }
}

#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum InvalidCallShapeReason {
    #[error("call argument count: expected {expected}, got {actual}")]
    ArgumentCount { expected: usize, actual: usize },
    #[error("call argument {index} label: expected {expected:?}, got {actual}")]
    ArgumentLabel {
        index: usize,
        expected: Option<EcoString>,
        actual: EcoString,
    },
    #[error("call argument {index} shape is incompatible with {type_:?}")]
    ArgumentShape { index: usize, type_: ValueType },
    #[error("call argument {index} type: expected {expected:?}, got {actual:?}")]
    ArgumentType {
        index: usize,
        expected: ValueType,
        actual: ValueType,
    },
    #[error("function instantiation argument count: expected {expected}, got {actual}")]
    FunctionInstantiationArgumentCount { expected: usize, actual: usize },
    #[error("function instantiation argument {index}: expected {expected:?}, got {actual:?}")]
    FunctionInstantiationArgumentShape {
        index: usize,
        expected: ValueType,
        actual: ValueType,
    },
    #[error("function instantiation return shape: expected {expected:?}, got {actual:?}")]
    FunctionInstantiationReturnShape {
        expected: ValueType,
        actual: ValueType,
    },
    #[error("function instantiation has an unresolved type parameter")]
    FunctionInstantiationUnresolvedParameter,
    #[error("function value return type: expected {expected:?}, got {actual:?}")]
    FunctionValueReturnType {
        expected: ValueType,
        actual: ValueType,
    },
    #[error("implicit call argument at index {index}")]
    ImplicitArgument { index: usize },
    #[error("record constructor argument count: expected {expected}, got {actual}")]
    RecordConstructorArgumentCount { expected: usize, actual: usize },
}

#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum InvalidModuleReferenceReason {
    #[error("module is not linked")]
    UnlinkedModule,
    #[error("function is missing from the linked registry")]
    MissingFunction,
    #[error("constant is missing from the linked registry")]
    MissingConstant,
    #[error("constant is not callable")]
    NonCallableConstant,
    #[error("function constructor module is {actual}")]
    FunctionModule { actual: EcoString },
    #[error("function constructor name is {actual}")]
    FunctionName { actual: EcoString },
    #[error("external function")]
    ExternalFunction,
    #[error("record constructor name is {actual}")]
    RecordConstructorName { actual: EcoString },
    #[error("record constructor result shape")]
    RecordConstructorResultShape,
    #[error("function value type")]
    FunctionType,
    #[error("function signature instantiation")]
    FunctionInstantiation,
    #[error("function reference result shape")]
    FunctionReferenceShape,
    #[error("constant signature instantiation")]
    ConstantInstantiation,
}

#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum InvalidCaseShapeReason {
    #[error("branch annotation type: expected {expected:?}, got {actual:?}")]
    BranchAnnotatedTypeMismatch {
        expected: ValueType,
        actual: ValueType,
    },
    #[error("branch shapes are incompatible: expected {expected:?}, got {actual:?}")]
    BranchShapeIncompatibility {
        expected: ValueType,
        actual: ValueType,
    },
    #[error("branch family assembly: expected {expected:?}, got {actual:?}")]
    BranchFamilyAssemblyMismatch {
        expected: ValueType,
        actual: ValueType,
    },
    #[error("empty clauses")]
    EmptyClauses,
    #[error("empty subjects")]
    EmptySubjects,
    #[error("compiled case clause index is out of bounds")]
    CompiledCaseClauseIndex,
    #[error("compiled case contains a reachable failure")]
    CompiledCaseFailure,
    #[error("compiled case guard index is out of bounds")]
    CompiledCaseGuardIndex,
    #[error("compiled case guard does not match its clause")]
    CompiledCaseGuard,
    #[error("compiled case subject count does not match case subjects")]
    CompiledCaseSubjectCountMismatch,
    #[error("missing false pattern")]
    MissingFalsePattern,
    #[error("missing fallback pattern")]
    MissingFallbackPattern,
    #[error("missing true pattern")]
    MissingTruePattern,
    #[error("case pattern count: expected {expected}, got {actual}")]
    PatternSubjectCountMismatch { expected: usize, actual: usize },
}

#[derive(Debug, Error, Clone, Copy, PartialEq, Eq)]
pub enum InvalidPipelineShapeReason {
    #[error("invalid echo step")]
    EchoStep,
    #[error("hole capture argument is not a variable binding")]
    HoleCaptureBinding,
    #[error("hole capture argument count: expected 1, got {actual}")]
    HoleCaptureArgumentCount { actual: usize },
    #[error("hole capture is not a function literal")]
    HoleCaptureFunction,
    #[error("hole capture function has the wrong literal kind")]
    HoleCaptureLiteralKind,
    #[error("hole capture use count: expected 1, got {actual}")]
    HoleCaptureUseCount { actual: usize },
    #[error("hole body has an implicit argument at index {index}")]
    HoleBodyImplicitArgument { index: usize },
    #[error("hole body is not a call")]
    HoleBodyNotCall,
    #[error("hole body statement count: expected 1, got {actual}")]
    HoleBodyStatementCount { actual: usize },
    #[error("hole wrapper argument count: expected 1, got {actual}")]
    HoleWrapperArgumentCount { actual: usize },
    #[error("missing pipe argument")]
    MissingPipeArgument,
    #[error("multiple pipe arguments at indices {first} and {second}")]
    MultiplePipeArguments { first: usize, second: usize },
    #[error("non-call pipeline step")]
    NonCallStep,
    #[error("unsupported pipe argument at index {index}")]
    UnsupportedPipeArgument { index: usize },
}

#[derive(Debug, Error, Clone, Copy, PartialEq, Eq)]
pub enum InvalidUseShapeReason {
    #[error("callback literal kind is not use")]
    CallbackLiteralKindNotUse { index: usize },
    #[error("callback is not the last argument")]
    CallbackNotLast { index: usize, arguments: usize },
    #[error("callback argument is not a function literal")]
    CallbackNotFunctionLiteral { index: usize },
    #[error("invalid generated assignment")]
    InvalidGeneratedAssignment { index: usize },
    #[error("missing callback")]
    MissingCallback,
    #[error("multiple callbacks at indices {first} and {second}")]
    MultipleCallbacks { first: usize, second: usize },
    #[error("non-call use right hand side")]
    NonCallRhs,
    #[error("unexpected variable use assignment at index {index}")]
    UnexpectedVariableAssignment { index: usize },
    #[error("unsupported implicit use argument at index {index}")]
    UnsupportedImplicitArgument { index: usize },
}