Skip to main content

nash_can/
error.rs

1use nash_ast::ModuleName;
2use nash_ast::{Kind, QualifiedName};
3use nash_region::{Located, Region};
4use nash_source::Type as SourceType;
5
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub enum BadArityContext {
8    TypeArity,
9    PatternArity,
10}
11
12#[derive(Clone, Copy, Debug, PartialEq, Eq)]
13pub enum DuplicatePatternContext<'a> {
14    LambdaArgs,
15    FuncArgs(&'a str),
16    CaseBranch,
17    LetBinding,
18    Destruct,
19}
20
21#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22pub enum VarKind {
23    BadOp,
24    BadVar,
25    BadPattern,
26    BadType,
27}
28
29/// Mirrors Elm's `Error.PossibleNames`.
30#[derive(Clone, Copy, Debug, PartialEq, Eq)]
31pub struct PossibleNames<'a> {
32    pub locals: &'a [&'a str],
33    pub qualified: &'a [(&'a str, &'a [&'a str])],
34}
35
36#[derive(Clone, Debug)]
37pub enum Error<'a> {
38    DuplicateTest {
39        name: &'a str,
40        first: Region,
41        second: Region,
42    },
43    ValidatorMissingMain {
44        region: Region,
45        module: &'a str,
46    },
47    ValidatorMainNotExposed {
48        region: Region,
49        module: &'a str,
50    },
51    RecordLiteralNoAlias {
52        region: Region,
53        fields: &'a [&'a str],
54    },
55    RecordLiteralAmbiguous {
56        region: Region,
57        candidates: &'a [QualifiedName<'a>],
58    },
59    RecordTypeOutsideAlias {
60        region: Region,
61    },
62    /// Overlap or superclass search used up the compile's fuel.
63    OutOfFuel {
64        region: Region,
65    },
66    NegateWithoutNum {
67        region: Region,
68    },
69    DoWithoutMonad {
70        region: Region,
71    },
72    RefutableBindPattern {
73        region: Region,
74    },
75    StructuralEqOverride {
76        head: &'a Located<nash_ast::Type<'a>>,
77    },
78    ReflexiveLiftOverlap {
79        heads: &'a [&'a Located<nash_ast::Type<'a>>],
80    },
81    MissingSuperclass {
82        region: Region,
83        trait_: QualifiedName<'a>,
84        heads: &'a [Located<nash_ast::Head<'a>>],
85        superclass: &'a nash_ast::Pred<'a>,
86        index: u16,
87        reason: crate::entailment::Failure,
88    },
89    BadInstanceHead {
90        region: Region,
91        reason: BadHead,
92    },
93    ImplContextVarNotInHead {
94        region: Region,
95        name: &'a str,
96    },
97    MissingMethod {
98        region: Region,
99        trait_: &'a str,
100        name: &'a str,
101    },
102    UnknownMethod {
103        region: Region,
104        trait_: &'a str,
105        name: &'a str,
106    },
107    OrphanImpl {
108        region: Region,
109        trait_: QualifiedName<'a>,
110        heads: &'a [nash_ast::HeadCon<'a>],
111    },
112    OverlappingImpls {
113        key: &'a nash_ast::ImplKey<'a>,
114        first: Region,
115        second: Region,
116        first_home: ModuleName<'a>,
117        second_home: ModuleName<'a>,
118    },
119    ImportOpenTrait {
120        region: Region,
121        name: &'a str,
122    },
123    DuplicateTrait {
124        name: &'a str,
125        first: Region,
126        second: Region,
127    },
128    DuplicateMethod {
129        name: &'a str,
130        first: Region,
131        second: Region,
132    },
133    DuplicateTraitParameter {
134        name: &'a str,
135        first: Region,
136        second: Region,
137    },
138    SuperclassBadArg {
139        region: Region,
140        trait_: &'a str,
141    },
142    MethodMissingParameter {
143        region: Region,
144        method: &'a str,
145        parameter: &'a str,
146    },
147    RecursiveSuperclass {
148        names: &'a [&'a Located<&'a str>],
149    },
150    ExportOpenTrait {
151        region: Region,
152        name: &'a str,
153    },
154    NotFoundTrait {
155        region: Region,
156        prefix: Option<&'a str>,
157        name: &'a str,
158    },
159    AmbiguousTrait {
160        region: Region,
161        prefix: Option<&'a str>,
162        name: &'a str,
163        first_module: ModuleName<'a>,
164        other_modules: &'a [ModuleName<'a>],
165    },
166    TraitArity {
167        region: Region,
168        name: &'a str,
169        expected: usize,
170        actual: usize,
171    },
172    ContextVarNotInType {
173        region: Region,
174        name: &'a str,
175    },
176    // --- Kind errors (docs/kinds.md) ---
177    KindMismatch {
178        region: Region,
179        context: &'a KindContext<'a>,
180        expected: &'a Kind<'a>,
181        actual: &'a Kind<'a>,
182    },
183    KindInfinite {
184        region: Region,
185        context: &'a KindContext<'a>,
186    },
187    RepresentationMismatch {
188        region: Region,
189        context: &'a KindContext<'a>,
190        required: nash_ast::primitives::ReprTrait,
191        actual: nash_ast::primitives::Repr,
192    },
193    ContradictoryRepresentation {
194        region: Region,
195        /// A type variable or a type application such as `'f 'a`.
196        subject: &'a Located<nash_ast::Type<'a>>,
197    },
198    ImplOfBuiltinTrait {
199        region: Region,
200        trait_: QualifiedName<'a>,
201    },
202    IrregularRecursion {
203        region: Region,
204        constructor: QualifiedName<'a>,
205        parameter: &'a str,
206    },
207
208    Unsupported {
209        feature: &'static str,
210        region: Region,
211    },
212    MissingModuleHeader,
213    NotFoundType {
214        region: Region,
215        prefix: Option<&'a str>,
216        name: &'a str,
217        suggestions: PossibleNames<'a>,
218    },
219    ImportNotFound {
220        region: Region,
221        module: &'a str,
222    },
223    AmbiguousType {
224        region: Region,
225        prefix: Option<&'a str>,
226        name: &'a str,
227        first_module: ModuleName<'a>,
228        other_modules: &'a [ModuleName<'a>],
229    },
230    BadArity {
231        region: Region,
232        context: BadArityContext,
233        name: &'a str,
234        expected: usize,
235        actual: usize,
236    },
237    ExportNotFound {
238        region: Region,
239        kind: VarKind,
240        name: &'a str,
241        /// Names that could have been meant, like Elm's suggestion lists.
242        suggestions: &'a [&'a str],
243    },
244    ExportOpenAlias {
245        region: Region,
246        name: &'a str,
247    },
248    DuplicateDecl {
249        name: &'a str,
250        first: Region,
251        second: Region,
252    },
253    DuplicateType {
254        name: &'a str,
255        first: Region,
256        second: Region,
257    },
258    DuplicateCtor {
259        name: &'a str,
260        first: Region,
261        second: Region,
262    },
263    DuplicateBinop {
264        name: &'a str,
265        first: Region,
266        second: Region,
267    },
268    /// An `infix` declaration's function is not a top-level value of the
269    /// module. Elm never validates this (infix is kernel-only there, and
270    /// its interface extraction crashes on the missing annotation); Nash
271    /// allows user-defined operators, so it must be a real error.
272    BinopFunctionNotFound {
273        region: Region,
274        op: &'a str,
275        function: &'a str,
276    },
277    DuplicateUnionArg {
278        type_name: &'a str,
279        arg_name: &'a str,
280        first: Region,
281        second: Region,
282    },
283    DuplicateAliasArg {
284        type_name: &'a str,
285        arg_name: &'a str,
286        first: Region,
287        second: Region,
288    },
289    RecursiveAlias {
290        region: Region,
291        name: &'a str,
292        args: &'a [&'a str],
293        /// The alias's source type, used to suggest a `type` replacement.
294        typ: &'a Located<SourceType<'a>>,
295        others: &'a [&'a str],
296    },
297    TypeVarsUnboundInUnion {
298        region: Region,
299        name: &'a str,
300        args: &'a [&'a str],
301        unbound: (&'a str, Region),
302        more_unbound: &'a [(&'a str, Region)],
303    },
304    TypeVarsMessedUpInAlias {
305        region: Region,
306        name: &'a str,
307        args: &'a [&'a str],
308        unused: &'a [(&'a str, Region)],
309        unbound: &'a [(&'a str, Region)],
310    },
311    LabeledCtorMissingField {
312        region: Region,
313        ctor: &'a str,
314        field: &'a str,
315    },
316    LabeledCtorExtraField {
317        region: Region,
318        ctor: &'a str,
319        field: &'a str,
320    },
321    LabeledCtorUnknownField {
322        region: Region,
323        ctor: &'a str,
324        field: &'a str,
325    },
326    DuplicateField {
327        name: &'a str,
328        first: Region,
329        second: Region,
330    },
331    ExportDuplicate {
332        name: &'a str,
333        first: Region,
334        second: Region,
335    },
336    NotFoundCtor {
337        region: Region,
338        prefix: Option<&'a str>,
339        name: &'a str,
340        suggestions: PossibleNames<'a>,
341    },
342    AmbiguousCtor {
343        region: Region,
344        prefix: Option<&'a str>,
345        name: &'a str,
346        first_module: ModuleName<'a>,
347        other_modules: &'a [ModuleName<'a>],
348    },
349    PatternHasRecordCtor {
350        region: Region,
351        name: &'a str,
352    },
353    DuplicatePattern {
354        context: DuplicatePatternContext<'a>,
355        name: &'a str,
356        first: Region,
357        second: Region,
358    },
359
360    // --- Expression canonicalization errors ---
361    NotFoundVar {
362        region: Region,
363        prefix: Option<&'a str>,
364        name: &'a str,
365        suggestions: PossibleNames<'a>,
366    },
367    AmbiguousVar {
368        region: Region,
369        prefix: Option<&'a str>,
370        name: &'a str,
371        first_module: ModuleName<'a>,
372        other_modules: &'a [ModuleName<'a>],
373    },
374    NotFoundBinop {
375        region: Region,
376        name: &'a str,
377        available: &'a [&'a str],
378    },
379    AmbiguousBinop {
380        region: Region,
381        name: &'a str,
382        first_module: ModuleName<'a>,
383        other_modules: &'a [ModuleName<'a>],
384    },
385    BinopConflict {
386        region: Region,
387        op1: &'a str,
388        op2: &'a str,
389    },
390    Shadowing {
391        name: &'a str,
392        original: Region,
393        new: Region,
394    },
395    RecursiveLet {
396        name: &'a Located<&'a str>,
397        others: &'a [&'a str],
398    },
399    RecursiveDecl {
400        name: &'a Located<&'a str>,
401        others: &'a [&'a str],
402    },
403    AnnotationTooShort {
404        region: Region,
405        name: &'a str,
406        /// How many arguments the annotation accounts for.
407        index: usize,
408        /// How many definition arguments have no corresponding type.
409        leftovers: usize,
410    },
411
412    // --- Import validation errors ---
413    ImportExposingNotFound {
414        region: Region,
415        module: ModuleName<'a>,
416        name: &'a str,
417        available: &'a [&'a str],
418    },
419    ImportCtorByName {
420        region: Region,
421        name: &'a str,
422        type_name: &'a str,
423    },
424    ImportOpenAlias {
425        region: Region,
426        name: &'a str,
427    },
428}
429
430#[derive(Clone, Copy, Debug)]
431pub enum KindContext<'a> {
432    TypeAnnotation,
433    BigField {
434        union: &'a str,
435        ctor: &'a str,
436        index: u16,
437    },
438    LittleField {
439        union: &'a str,
440        ctor: &'a str,
441        index: u16,
442    },
443    RecordField {
444        alias: &'a str,
445        field: &'a str,
446        big: bool,
447    },
448    AliasCasing {
449        alias: &'a str,
450        big: bool,
451    },
452    Annotation {
453        name: &'a str,
454    },
455    /// The `index`th type in an `impl Trait T1 .. Tn` head (plans/03).
456    ImplHead {
457        trait_: QualifiedName<'a>,
458        index: u16,
459    },
460}
461
462#[derive(Clone, Copy, Debug)]
463pub enum BadHead {
464    BareVariable,
465    Function,
466    Record,
467    VariableApplication,
468}