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
use std::{fmt, ops::Range};

use indoc::formatdoc;

#[derive(Debug, Clone)]
pub struct Diagnostic {
    kind: DiagnosticKind,
    span: Range<usize>,
}

impl Diagnostic {
    pub fn new(kind: DiagnosticKind, span: Range<usize>) -> Self {
        Self { kind, span }
    }

    pub fn kind(&self) -> &DiagnosticKind {
        &self.kind
    }

    pub fn span(&self) -> &Range<usize> {
        &self.span
    }

    pub fn is_error(&self) -> bool {
        matches!(self.kind, DiagnosticKind::Error(_))
    }

    pub fn is_warning(&self) -> bool {
        matches!(self.kind, DiagnosticKind::Warning(_))
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum DiagnosticKind {
    Warning(WarningKind),
    Error(ErrorKind),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum WarningKind {
    UnusedFunction(String),
    UnusedInlineFunction(String),
    UnusedParameter(String),
    UnusedConst(String),
    UnusedInlineConst(String),
    UnusedLet(String),
    UnusedGenericType(String),
    UnusedEnum(String),
    UnusedEnumVariant(String),
    UnusedStruct(String),
    UnusedTypeAlias(String),
    RedundantNullableType(String),
    RedundantTypeCheck(String),
}

impl fmt::Display for WarningKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let message = match self {
            Self::UnusedFunction(name) => format!("Unused function `{name}`."),
            Self::UnusedInlineFunction(name) => format!("Unused inline function `{name}`."),
            Self::UnusedParameter(name) => format!("Unused parameter `{name}`."),
            Self::UnusedConst(name) => format!("Unused constant `{name}`."),
            Self::UnusedInlineConst(name) => format!("Unused inline constant `{name}`."),
            Self::UnusedLet(name) => format!("Unused let binding `{name}`."),
            Self::UnusedGenericType(name) => format!("Unused generic type `{name}`."),
            Self::UnusedEnum(name) => format!("Unused enum `{name}`."),
            Self::UnusedEnumVariant(name) => format!("Unused enum variant `{name}`."),
            Self::UnusedStruct(name) => format!("Unused struct `{name}`."),
            Self::UnusedTypeAlias(name) => format!("Unused type alias `{name}`."),
            Self::RedundantNullableType(ty) => {
                format!("This has no effect, since `{ty}` is already a nullable type.")
            }
            Self::RedundantTypeCheck(ty) => format!(
                "It's redundant to guard against `{ty}`, since the value already has that type."
            ),
        };
        write!(f, "{}", message.trim())
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ErrorKind {
    // Duplicate definitions.
    DuplicateType(String),
    DuplicateSymbol(String),
    ModuleNameTakenByEnum(String),
    EnumNameTakenByModule(String),

    // Invalid symbol references.
    UnknownSymbol(String),
    UnknownType(String),
    InlineFunctionReference(String),
    ModuleReference(String),

    // Types.
    RecursiveTypeAlias(String),
    TypeMismatch(String, String),
    CastMismatch(String, String),
    CannotInferType,

    // Function calls.
    UncallableType(String),
    ArgumentMismatch(usize, usize),
    ArgumentMismatchSpread(usize, usize),
    ArgumentMismatchOptional(usize, usize),

    // Field initialization.
    UninitializableType(String),
    InvalidEnumVariantInitializer(String),
    InvalidEnumVariantReference(String),
    DuplicateInitializerField(String),
    UnknownInitializerField(String),
    MissingInitializerFields(Vec<String>),

    // Field access.
    UnknownField(String),
    InvalidFieldAccess(String, String),
    InvalidIndexAccess(String),

    // Spread and optional.
    InvalidSpreadItem,
    InvalidSpreadArgument,
    InvalidSpreadParameter,
    InvalidSpreadField,
    InvalidOptionalParameter,
    InvalidOptionalField,
    OptionalParameterSpread,
    OptionalFieldSpread,
    UnsupportedFunctionSpread,
    RequiredFunctionSpread,

    // Enum variant definitions.
    DuplicateEnumVariant(String),
    DuplicateEnumDiscriminant(String),
    EnumDiscriminantTooLarge,

    // Paths.
    UnknownEnumVariantPath(String),
    UnknownModulePath(String),
    PrivateSymbol(String),
    PrivateType(String),
    InvalidTypePath(String),
    InvalidSymbolPath(Option<String>),
    ExpectedTypePath(String),
    ExpectedSymbolPath(String),

    // Type guards.
    UnsupportedTypeGuard(String, String),
    NonAnyPairTypeGuard,
    NonListPairTypeGuard,
    InvalidExistanceCheck(String),

    // Blocks.
    ImplicitReturnInIf,
    ExplicitReturnInExpr,
    EmptyBlock,

    // Atoms.
    NonAtomEquality(String),
    IntegerTooLarge,

    // Recursive constants.
    RecursiveConstantReference,
    RecursiveInlineConstantReference,
    RecursiveInlineFunctionCall,
}

impl fmt::Display for ErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let message = match self {
            // Duplicate definitions.
            Self::DuplicateType(name) => format!("There is already a type named `{name}` in this scope."),
            Self::DuplicateSymbol(name) => format!("There is already a symbol named `{name}` in this scope."),
            Self::ModuleNameTakenByEnum(name) => formatdoc!("
                There is already an enum type named `{name}` in this scope. \
                This isn't allowed to prevent ambiguity when referencing items.
            "),
            Self::EnumNameTakenByModule(name) => formatdoc!("
                There is already a module named `{name}` in this scope. \
                This isn't allowed to prevent ambiguity when referencing items.
            "),

            // Invalid symbol references.
            Self::UnknownSymbol(name) => format!("Reference to unknown symbol `{name}`."),
            Self::UnknownType(name) => format!("Reference to unknown type `{name}`."),
            Self::InlineFunctionReference(name) => formatdoc!("
                Cannot reference inline function `{name}`, since it is not a value. \
                Inline functions must be resolved at compile time. \
                Try calling the function instead.
            "),
            Self::ModuleReference(name) => formatdoc!("
                Cannot reference module `{name}`, since it is not a value. \
                Perhaps you meant to use the `::` operator to access a symbol in the module?
            "),

            // Types.
            Self::RecursiveTypeAlias(name) => formatdoc!("
                Cycle detected when resolving type alias `{name}`. \
                Type aliases cannot reference themselves.
            "),
            Self::TypeMismatch(found, expected) => format!("Expected type `{expected}`, but found `{found}`."),
            Self::CastMismatch(found, expected) => format!("Cannot cast type `{found}` to `{expected}`."),
            Self::CannotInferType => "Lambda parameter type could not be inferred.".to_string(),

            // Function calls.
            Self::UncallableType(ty) => format!("Expression with type `{ty}` cannot be called, since it is not a function."),
            Self::ArgumentMismatch(found, expected) => {
                format!(
                    "Expected {expected} argument{}, but found {found}.",
                    if *expected == 1 { "" } else { "s" }
                )
            }
            Self::ArgumentMismatchSpread (found, expected) => {
                format!(
                    "Expected at least {expected} argument{}, but found {found}.",
                    if *expected == 1 { "" } else { "s" }
                )
            }
            Self::ArgumentMismatchOptional (found, expected)=> {
                format!("Expected either {} or {expected} arguments, but found {found}.", expected - 1)
            }

            // Field initialization.
            Self::UninitializableType(ty) => formatdoc!("
                Cannot initializable type `{ty}`. \
                Only structs and enum variants with fields can be initialized.
            "),
            Self::InvalidEnumVariantReference(name) => formatdoc!("
                Cannot reference enum variant `{name}`. \
                Enum variants with fields cannot be referenced directly. \
                Consider initializing the enum variant instead.
            "),
            Self::InvalidEnumVariantInitializer(name) => formatdoc!("
                Cannot initialize enum variant `{name}`. \
                Enum variants without fields cannot be initialized. \
                Consider referencing the enum variant directly.
            "),
            Self::DuplicateInitializerField(name) => format!("Duplicate field `{name}` specified in initializer."),
            Self::UnknownInitializerField(name) => format!("Unknown field `{name}` specified in initializer."),
            Self::MissingInitializerFields(fields) => format!("Missing fields in initializer: {}.", join_names(fields)),

            // Field access.
            Self::UnknownField(name) => format!("Cannot reference unknown field `{name}`."),
            Self::InvalidFieldAccess(field, ty) => format!("Cannot reference field `{field}` of type `{ty}`."),
            Self::InvalidIndexAccess(ty) => format!("Cannot index into type `{ty}`."),

            // Spread and optional.
            Self::InvalidSpreadItem => formatdoc!("
                The spread operator can only be used on the last item in a list. \
                This is because it requires recursion at runtime to concatenate lists together. \
                By only allowing it on the last item by default, this additional complexity and runtime cost is avoided.
            "),
            Self::InvalidSpreadArgument => formatdoc!("
                The spread operator can only be used on the last argument in a function call. \
                This is because it requires recursion at runtime to concatenate lists together. \
                By only allowing it on the last item by default, this additional complexity and runtime cost is avoided.
            "),
            Self::InvalidSpreadParameter => formatdoc!("
                The spread operator can only be used on the last parameter in a function. \
                Otherwise, it would be ambiguous where the parameter should start and end.
            "),
            Self::InvalidSpreadField => formatdoc!("
                The spread operator can only be used on the last field. \
                Otherwise, it would be ambiguous where the field should start and end.
            "),
            Self::InvalidOptionalParameter => formatdoc!("
                Only the last parameter in a function can be optional. \
                Otherwise, it would be ambiguous which optional parameter was specified.
            "),
            Self::InvalidOptionalField => formatdoc!("
                Only the last field can be optional. \
                Otherwise, it would be ambiguous which optional field was specified.
            "),
            Self::OptionalParameterSpread => "The spread operator cannot be used on optional parameters.".to_string(),
            Self::OptionalFieldSpread => "The spread operator cannot be used on optional fields.".to_string(),
            Self::UnsupportedFunctionSpread => "This function does not support the spread operator on its last argument.".to_string(),
            Self::RequiredFunctionSpread => "This function requires the spread operator on its last argument.".to_string(),

            // Enum variant definitions.
            Self::DuplicateEnumVariant(name) => format!("Duplicate enum variant `{name}` specified."),
            Self::DuplicateEnumDiscriminant(discriminant) => format!("Duplicate enum discriminant `{discriminant}` specified."),
            Self::EnumDiscriminantTooLarge => "Enum discriminant is too large to allocate in CLVM.".to_string(),

            // Paths.
            Self::UnknownEnumVariantPath(name) => format!("Unknown enum variant `{name}`."),
            Self::UnknownModulePath(name) => format!("Could not resolve `{name}` in module."),
            Self::PrivateSymbol(name) => format!("Cannot access private symbol `{name}` in module."),
            Self::PrivateType(name) => format!("Cannot access private type `{name}` in module."),
            Self::InvalidTypePath(ty) => format!("Cannot path into type `{ty}`."),
            Self::InvalidSymbolPath(name) => if let Some(name) = name {
                format!("Cannot path into symbol `{name}`.")
            } else {
                "Cannot path into symbol.".to_string()
            },
            Self::ExpectedTypePath(name) => format!("Expected type, but found symbol `{name}` instead."),
            Self::ExpectedSymbolPath(name) => format!("Expected symbol, but found type `{name}` instead."),

            // Type guards.
            Self::UnsupportedTypeGuard(from, to) => format!("Cannot check type `{from}` against `{to}`."),
            Self::NonAnyPairTypeGuard => "Cannot check `Any` against pair types other than `(Any, Any)`.".to_string(),
            Self::NonListPairTypeGuard => "Cannot check `T[]` against pair types other than `(T, T[])`.".to_string(),
            Self::InvalidExistanceCheck(ty) => format!("Cannot check existence of value with type `{ty}`, since it can't be undefined."),

            // Blocks.
            Self::ImplicitReturnInIf => formatdoc!("
                Implicit returns are not allowed in if statements. \
                Either use an explicit return statement at the end of the block, \
                or raise an error.
            "),
            Self::ExplicitReturnInExpr => "Explicit return is not allowed within expressions.".to_string(),
            Self::EmptyBlock => "Blocks must either return an expression or raise an error.".to_string(),

            // Atoms.
            Self::NonAtomEquality(ty) => format!("Cannot check equality on non-atom type `{ty}`."),
            Self::IntegerTooLarge => "Integer literal is too large to allocate in CLVM.".to_string(),

            // Recursive constants.
            Self::RecursiveConstantReference => "Cannot recursively reference constant.".to_string(),
            Self::RecursiveInlineConstantReference => "Cannot recursively reference inline constant.".to_string(),
            Self::RecursiveInlineFunctionCall => "Cannot recursively call inline function.".to_string(),
        };
        write!(f, "{}", message.trim())
    }
}

/// Join a list of names into a string, wrapped in backticks.
fn join_names(kinds: &[String]) -> String {
    let names: Vec<String> = kinds.iter().map(|kind| format!("`{kind}`")).collect();
    names.join(", ")
}