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
//! Error message generation.

#![warn(missing_docs)]
#![warn(clippy::missing_docs_in_private_items)]
#![warn(clippy::pedantic)]

use proc_macro2::Span;
use std::fmt::Display;

/// Repository of this project, to be displayed in error messages.
#[macro_export]
macro_rules! repo {
    () => {
        "https://github.com/Vanille-N/chandeliers"
    };
}

/// Generate an error message better than just "proc macro panicked".
#[macro_export]
macro_rules! panic {
    ($($err:tt)*) => {{
        std::panic!("
Chandeliers panicked: \x1b[1;31m{}.\x1b[0m
This error occured in \x1b[1;35m{}:{}:{}\x1b[0m

If you are not a developper of Chandeliers and you see this message then this is a bug.
I'd be grateful if you could report this error at \x1b[33m{}\x1b[0m
with the code that produced it and the version of Chandeliers you are using.
",
            format!($($err)*),
            file!(),
            line!(),
            column!(),
            ::chandeliers_err::repo!(),
        );
    }};
}

/// Special instance of `panic` for code that should be trivially unreachable.
#[macro_export]
macro_rules! unreachable {
    () => {{
        ::chandeliers_err::panic!("Entered unreachable code");
    }};
}

/// Anything that went wrong: a sequence of [Span] and associated message.
pub type Error = Vec<(String, Option<Span>)>;

/// Generate an [Error].
pub trait IntoError {
    /// Produce the sequence of spans and help messages.
    fn into_err(self) -> Error;
}

/// Result type with errors that can be emitted by Rustc.
pub type Result<T> = std::result::Result<T, Error>;

/// Objects that can be converted to spans.
pub trait TrySpan {
    /// Try to get a span from the object (by default we don't get any,
    /// but a wrapper might provide one)
    fn try_span(&self) -> Option<Span> {
        None
    }
}

/// Always [Some].
impl TrySpan for Span {
    fn try_span(&self) -> Option<Span> {
        Some(*self)
    }
}

/// Trivial projection.
impl<T: TrySpan> TrySpan for &T {
    fn try_span(&self) -> Option<Span> {
        (*self).try_span()
    }
}

/// Trivial projection.
impl<T: TrySpan> TrySpan for Option<T> {
    fn try_span(&self) -> Option<Span> {
        self.as_ref().and_then(TrySpan::try_span)
    }
}

/// An explicit error message with its span.
pub struct Basic {
    /// Error kind.
    pub msg: String,
    /// Error location.
    pub span: Span,
}

impl IntoError for Basic {
    fn into_err(self) -> Error {
        vec![(self.msg, Some(self.span))]
    }
}

/// Generate an error for incompatible types between a "left" and a "right" values.
pub struct TypeMismatch<Source, Left, Right, Msg> {
    /// Span of the entire error.
    pub source: Source,
    /// Left expression and span.
    pub left: Left,
    /// Right expression and span.
    pub right: Right,
    /// Extra message.
    pub msg: Msg,
}

impl<Source, Left, Right, Msg> IntoError for TypeMismatch<Source, Left, Right, Msg>
where
    Source: TrySpan,
    Msg: Display,
    Left: Display + TrySpan,
    Right: Display + TrySpan,
{
    fn into_err(self) -> Error {
        vec![
            (
                format!(
                    "Type mismatch between the left and right sides: {}",
                    self.msg
                ),
                self.source.try_span(),
            ),
            (
                format!("This element has type {}", self.left),
                self.left.try_span(),
            ),
            (
                format!("While this element has type {}", self.right),
                self.right.try_span(),
            ),
        ]
    }
}

/// Generate an error for a variable that was not declared yet.
pub struct VarNotFound<Var, Suggest1, Suggest2> {
    /// What is missing.
    pub var: Var,
    /// Local variable suggestions.
    pub suggest1: Suggest1,
    /// Global variable suggestions.
    pub suggest2: Suggest2,
}

impl<Var, Suggest1, S1, Suggest2, S2> IntoError for VarNotFound<Var, Suggest1, Suggest2>
where
    Var: Display + TrySpan,
    Suggest1: IntoIterator<Item = S1>,
    Suggest2: IntoIterator<Item = S2>,
    S1: Display,
    S2: Display,
{
    fn into_err(self) -> Vec<(String, Option<Span>)> {
        let mut suggest1 = self
            .suggest1
            .into_iter()
            .map(|v| format!("{v}"))
            .collect::<Vec<_>>();
        let mut suggest2 = self
            .suggest2
            .into_iter()
            .map(|v| format!("{v}"))
            .collect::<Vec<_>>();
        suggest1.sort();
        suggest2.sort();
        let suggest1 = if suggest1.is_empty() {
            String::from("(none declared)")
        } else {
            suggest1.join(", ")
        };
        let suggest2 = if suggest2.is_empty() {
            String::from("(none declared)")
        } else {
            suggest2.join(", ")
        };

        vec![
            (
                format!("Variable {} not found in the context.", self.var),
                self.var.try_span(),
            ),
            (
                format!("Perhaps you meant one of the local variables: {suggest1}"),
                None,
            ),
            (format!("or one of the global variables: {suggest2}"), None),
        ]
    }
}

/// Generate an erorr for an expression that is noot valid in a `const` declaration.
pub struct NotConst<Item, Site> {
    /// Description of the invalid expression constructor.
    pub what: Item,
    /// Location of the error.
    pub site: Site,
}

impl<Item, Site> IntoError for NotConst<Item, Site>
where
    Item: Display,
    Site: TrySpan,
{
    fn into_err(self) -> Error {
        vec![
            (
                format!("{} not valid in const contexts", self.what),
                self.site.try_span(),
            ),
            (
                String::from("You must put this definition inside a node"),
                None,
            ),
        ]
    }
}

/// Generate an error for a binary operator that expected arguments of a specific type.
pub struct BinopMismatch<Oper, Site, Expect, Left, Right> {
    /// Description of the operator.
    pub oper: Oper,
    /// Location of the error.
    pub site: Site,
    /// What we expected in place of the arguments.
    pub expect: Expect,
    /// Left hand side and span.
    pub left: Left,
    /// Right hand side and span.
    pub right: Right,
}

impl<Oper, Site, Expect, Left, Right> IntoError for BinopMismatch<Oper, Site, Expect, Left, Right>
where
    Oper: Display,
    Site: TrySpan,
    Expect: Display,
    Left: Display + TrySpan,
    Right: Display + TrySpan,
{
    fn into_err(self) -> Vec<(String, Option<Span>)> {
        vec![
            (
                format!(
                    "Binary operator `{}` expects arguments of {}",
                    self.oper, self.expect
                ),
                self.site.try_span(),
            ),
            (
                format!("The left-hand-side is found to be of type {}", self.left),
                self.left.try_span(),
            ),
            (
                format!("The right-hand-side is found to be of type {}", self.right),
                self.right.try_span(),
            ),
        ]
    }
}

/// Generate an error for a unary operator that expected an argument of a specific type.
pub struct UnopMismatch<Oper, Expect, Site, Inner> {
    /// Description of the operator.
    pub oper: Oper,
    /// What the operator expects.
    pub expect: Expect,
    /// Location of the error.
    pub site: Site,
    /// Invalid expression and span.
    pub inner: Inner,
}

impl<Oper, Expect, Site, Inner> IntoError for UnopMismatch<Oper, Expect, Site, Inner>
where
    Oper: Display,
    Expect: Display,
    Site: TrySpan,
    Inner: Display + TrySpan,
{
    fn into_err(self) -> Error {
        vec![
            (
                format!(
                    "Unary operator `{}` expects an argument of {}",
                    self.oper, self.expect
                ),
                self.site.try_span(),
            ),
            (
                format!("The inner value is found to be of type {}", self.inner),
                self.inner.try_span(),
            ),
        ]
    }
}

/// Generate an error for something that should have been a bool but isn't,
/// e.g. `if 1 then 0 else 1`.
pub struct BoolRequired<Type, Site, Inner> {
    /// Type that was found (should have been bool).
    pub actual: Type,
    /// Location of the error.
    pub site: Site,
    /// Location of the inner contents.
    pub inner: Inner,
}

impl<Type, Site, Inner> IntoError for BoolRequired<Type, Site, Inner>
where
    Type: Display,
    Site: TrySpan,
    Inner: Display + TrySpan,
{
    fn into_err(self) -> Error {
        vec![
            (
                format!("{} should be of type bool", self.actual),
                self.site.try_span(),
            ),
            (
                format!("The argument is found to be of type {}", self.inner),
                self.inner.try_span(),
            ),
        ]
    }
}

/// Generate an error for a cyclic definition.
pub struct Cycle<Items> {
    /// A (not necessarily ordered) cycle.
    pub items: Items,
}

impl<Items, Item> IntoError for Cycle<Items>
where
    Items: IntoIterator<Item = (Item, Option<Span>)>,
    Item: Display,
{
    fn into_err(self) -> Error {
        let mut v = vec![];
        for (i, (it, sp)) in self.items.into_iter().enumerate() {
            v.push((
                if i == 0 {
                    format!("{it} was found to be part of a dependency cycle")
                } else {
                    format!("The cycle also goes through {it}")
                },
                sp,
            ));
        }
        v
    }
}

/// Error message for an object that was defined twice when only one
/// declaration should exist.
pub struct GraphUnitDeclTwice<Unit, NewSite, Prior, PriorSite> {
    /// Display of the redefined object.
    pub unit: Unit,
    /// Location of the superfluous definition.
    pub new_site: NewSite,
    /// Item that defined the object previously.
    pub prior: Prior,
    /// Location of the first definition.
    pub prior_site: PriorSite,
}

impl<Unit, NewSite, Prior, PriorSite> IntoError
    for GraphUnitDeclTwice<Unit, NewSite, Prior, PriorSite>
where
    Unit: Display,
    Prior: Display,
    NewSite: TrySpan,
    PriorSite: TrySpan,
{
    fn into_err(self) -> Error {
        vec![
            (
                format!(
                    "Attempt to redefine {}, when {} already defines it",
                    self.unit, self.prior
                ),
                self.new_site.try_span(),
            ),
            (
                String::from("Already defined here"),
                self.prior_site.try_span(),
            ),
        ]
    }
}

/// Error for an object that should have been declared but was not.
pub struct GraphUnitUndeclared<Unit> {
    /// Missing object and site where usage was attempted.
    pub unit: Unit,
}

impl<Unit> IntoError for GraphUnitUndeclared<Unit>
where
    Unit: Display + TrySpan,
{
    fn into_err(self) -> Error {
        vec![(
            format!("No definition provided for {} which is required", self.unit),
            self.unit.try_span(),
        )]
    }
}

/// Special case of [Cycle]: custom message for an object that depends
/// specifically on itself directly.
pub struct GraphUnitDependsOnItself<Unit, Site1, Site2> {
    /// Object that loops.
    pub unit: Unit,
    /// Where it is defined.
    pub def_site: Site1,
    /// Where it is used (usually a subspan of `def_site`).
    pub usage: Site2,
}

impl<Unit, Site1, Site2> IntoError for GraphUnitDependsOnItself<Unit, Site1, Site2>
where
    Unit: Display,
    Site1: TrySpan,
    Site2: TrySpan,
{
    fn into_err(self) -> Error {
        vec![
            (
                format!("{} depends on itself", self.unit),
                self.def_site.try_span(),
            ),
            (
                String::from("used here within its own definition"),
                self.usage.try_span(),
            ),
        ]
    }
}

/// Error for when one tried to access too far into the past.
pub struct NotPositive<Var, Site> {
    /// Variable that is not deep enough.
    pub var: Var,
    /// Location of the error.
    pub site: Site,
    /// How deep we could have gone.
    pub available_depth: usize,
    /// How deep we actually tried to go.
    pub attempted_depth: usize,
}
impl<Var, Site> IntoError for NotPositive<Var, Site>
where
    Var: Display,
    Site: TrySpan,
{
    fn into_err(self) -> Error {
        vec![
            (
                format!("Variable {} is not positive at this depth", self.var),
                self.site.try_span(),
            ),
            (
                format!(
                    "tried to reach {} steps into the past, with only {} available",
                    self.attempted_depth, self.available_depth
                ),
                None,
            ),
            (
                String::from("Maybe add a `->` in front of the expression to increase the depth ?"),
                None,
            ),
        ]
    }
}

/// Error for a literal that is not supported.
///
/// Lustre only has `float`, `int`, and `bool` literals, so e.g. a `&str` will trigger this error.
pub struct UnhandledLitType<Site> {
    /// Location of the literal.
    pub site: Site,
}
impl<Site> IntoError for UnhandledLitType<Site>
where
    Site: TrySpan,
{
    fn into_err(self) -> Error {
        vec![(
            String::from("Lustre only accepts literals of type int, float, or bool"),
            self.site.try_span(),
        )]
    }
}

/// Error for when a comparison operator is used with associativity.
///
/// Since `a = b = c` is ambiguous (does it mean `(a = b) = c` or `a = (b = c)`
/// or `(a = b) and (b = c)`, we choose to reject all interpretations and
/// ask for explicit parentheses around comparison operators.
pub struct CmpNotAssociative<First, Oper1, Second, Oper2, Third, Site> {
    /// The `<` of `a < b > c`
    pub oper1: Oper1,
    /// The `a` of `a < b > c`
    pub first: First,
    /// The whole location of `a < b > c`
    pub site: Site,
    /// The `b` of `a < b > c`
    pub second: Second,
    /// The `c` of `a < b > c`
    pub third: Third,
    /// The `>` of `a < b > c`
    pub oper2: Oper2,
}
impl<First, Oper1, Second, Oper2, Third, Site> IntoError
    for CmpNotAssociative<First, Oper1, Second, Oper2, Third, Site>
where
    Oper1: Display,
    Oper2: Display,
    First: Display,
    Second: Display,
    Third: Display,
    Site: TrySpan,
{
    fn into_err(self) -> Error {
        let Self {
            first,
            oper1,
            second,
            oper2,
            third,
            site,
        } = &self;
        vec![(
            format!("Comparison operator {oper1} is not associative"),
            site.try_span(),
        ),(
            format!("Maybe replace `{first} {oper1} {second} {oper2} {third}` with `{first} {oper1} {second} and {second} {oper2} {third}` ?"),
            None,
        )]
    }
}