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
use std::ops::Range;

use ariadne::{ColorGenerator, Label, Report, ReportKind};
use edlang_lowering::errors::LoweringError;
use edlang_session::Session;

/// Creates a report from a lowering error.
pub fn lowering_error_to_report(
    error: LoweringError,
    session: &Session,
) -> Report<(String, Range<usize>)> {
    let mut colors = ColorGenerator::new();
    colors.next();
    match error {
        LoweringError::ModuleNotFound {
            span,
            module,
            file_id,
        } => {
            let path = session.file_paths[file_id].display().to_string();
            let offset = span.lo;
            Report::build(ReportKind::Error, path.clone(), offset)
                .with_code("E1")
                .with_label(
                    Label::new((path, span.into()))
                        .with_message(format!("Module {module:?} not found."))
                        .with_color(colors.next()),
                )
                .with_message("Unresolved import.")
                .finish()
        }
        LoweringError::FunctionNotFound {
            span,
            function,
            file_id,
        } => {
            let path = session.file_paths[file_id].display().to_string();
            Report::build(ReportKind::Error, path.clone(), span.lo)
                .with_code("EFNNOTFOUND")
                .with_label(
                    Label::new((path, span.into()))
                        .with_message(format!("Function {function:?} not found."))
                        .with_color(colors.next()),
                )
                .finish()
        }
        LoweringError::ImportNotFound {
            import_span,
            module_span,
            symbol,
            file_id,
        } => {
            let path = session.file_paths[file_id].display().to_string();
            let offset = symbol.span.lo;
            Report::build(ReportKind::Error, path.clone(), offset)
                .with_code("E2")
                .with_label(
                    Label::new((path.clone(), module_span.into()))
                        .with_message("In module this module."),
                )
                .with_label(
                    Label::new((path.clone(), import_span.into()))
                        .with_message("In this import statement"),
                )
                .with_label(
                    Label::new((path, symbol.span.into()))
                        .with_message(format!("Failed to find symbol {:?}", symbol.name))
                        .with_color(colors.next()),
                )
                .with_message("Unresolved import.")
                .finish()
        }
        LoweringError::BorrowNotMutable {
            span,
            name,
            type_span,
            file_id,
        } => {
            let path = session.file_paths[file_id].display().to_string();
            let mut labels = vec![Label::new((path.clone(), span.into()))
                .with_message(format!(
                    "Can't mutate {name:?} because it's behind a immutable borrow"
                ))
                .with_color(colors.next())];

            if let Some(type_span) = type_span {
                labels.push(
                    Label::new((path.clone(), type_span.into()))
                        .with_message(format!("Variable {name:?} has this type"))
                        .with_color(colors.next()),
                );
            }

            Report::build(ReportKind::Error, path.clone(), span.lo)
                .with_code("EREFMUT")
                .with_labels(labels)
                .finish()
        }
        LoweringError::UnrecognizedType {
            span,
            name,
            file_id,
        } => {
            let path = session.file_paths[file_id].display().to_string();
            Report::build(ReportKind::Error, path.clone(), span.lo)
                .with_code("E3")
                .with_label(
                    Label::new((path, span.into()))
                        .with_message(format!("Failed to find type {:?}", name))
                        .with_color(colors.next()),
                )
                .with_message(format!("Unresolved type {:?}.", name))
                .finish()
        }
        LoweringError::UnexpectedType {
            span,
            found,
            expected,
            file_id,
        } => {
            let path = session.file_paths[file_id].display().to_string();
            let mut labels = vec![Label::new((path.clone(), span.into()))
                .with_message(format!(
                    "Unexpected type '{}', expected '{}'",
                    found, expected.kind
                ))
                .with_color(colors.next())];

            if let Some(span) = expected.span {
                labels.push(
                    Label::new((path.clone(), span.into()))
                        .with_message(format!("expected '{}' due to this type", expected.kind))
                        .with_color(colors.next()),
                );
            }

            Report::build(ReportKind::Error, path.clone(), span.lo)
                .with_code("E3")
                .with_labels(labels)
                .with_message(format!("expected type {}.", expected.kind))
                .finish()
        }
        LoweringError::IdNotFound { span, id, file_id } => {
            let path = session.file_paths[file_id].display().to_string();
            Report::build(ReportKind::Error, path.clone(), span.lo)
                .with_code("E_ID")
                .with_label(
                    Label::new((path, span.into()))
                        .with_message("Failed to definition id")
                        .with_color(colors.next()),
                )
                .with_message(format!("Failed to find definition id {id:?}, this is most likely a compiler bug or a unimplemented lowering"))
                .finish()
        }
        LoweringError::NotYetImplemented {
            span,
            message,
            file_id,
        } => {
            let path = session.file_paths[file_id].display().to_string();
            Report::build(ReportKind::Error, path.clone(), span.lo)
                .with_code("TODO")
                .with_label(
                    Label::new((path, span.into()))
                        .with_message(message)
                        .with_color(colors.next()),
                )
                .finish()
        }
        LoweringError::UseOfUndeclaredVariable {
            span,
            name,
            file_id,
        } => {
            let path = session.file_paths[file_id].display().to_string();
            Report::build(ReportKind::Error, path.clone(), span.lo)
                .with_code("UseOfUndeclaredVariable")
                .with_label(
                    Label::new((path, span.into()))
                        .with_message(format!("use of undeclared variable {:?}", name))
                        .with_color(colors.next()),
                )
                .finish()
        }
        LoweringError::ParamCountMismatch {
            span,
            has_args,
            needs,
            file_id,
        } => {
            let path = session.file_paths[file_id].display().to_string();
            Report::build(ReportKind::Error, path.clone(), span.lo)
                .with_code("ParamCountMismatch")
                .with_label(
                    Label::new((path, span.into()))
                        .with_message(format!(
                            "function call parameter count mismatch: has {}, needs {}.",
                            has_args, needs
                        ))
                        .with_color(colors.next()),
                )
                .finish()
        }
        LoweringError::NotMutable {
            span,
            declare_span,
            file_id,
        } => {
            let path = session.file_paths[file_id].display().to_string();
            let mut report = Report::build(ReportKind::Error, path.clone(), span.lo)
                .with_code("NotMutable")
                .with_label(
                    Label::new((path.clone(), span.into()))
                        .with_message("can't mutate this variable because it's not mutable")
                        .with_color(colors.next()),
                );

            if let Some(declare_span) = declare_span {
                report = report.with_label(
                    Label::new((path, declare_span.into()))
                        .with_message("variable declared here")
                        .with_color(colors.next()),
                );
            }
            report.finish()
        }
        LoweringError::NotMutableSelf {
            span,
            path_span,
            file_id,
        } => {
            let path = session.file_paths[file_id].display().to_string();
            Report::build(ReportKind::Error, path.clone(), span.lo)
                .with_code("NotMutableSelf")
                .with_label(
                    Label::new((path.clone(), path_span.into()))
                        .with_message("this value is not declared mutable")
                        .with_color(colors.next()),
                )
                .with_label(
                    Label::new((path, span.into()))
                        .with_message("this method requires a mutable 'self'")
                        .with_color(colors.next()),
                )
                .finish()
        }
        LoweringError::CantTakeMutableBorrow {
            span,
            declare_span,
            file_id,
        } => {
            let path = session.file_paths[file_id].display().to_string();
            let mut report = Report::build(ReportKind::Error, path.clone(), span.lo)
                .with_code("CantTakeMutableBorrow")
                .with_label(
                    Label::new((path.clone(), span.into()))
                        .with_message("can't take a mutate borrow to this variable because it's not declared mutable")
                        .with_color(colors.next()),
                );

            if let Some(declare_span) = declare_span {
                report = report.with_label(
                    Label::new((path, declare_span.into()))
                        .with_message("variable declared here")
                        .with_color(colors.next()),
                );
            }
            report.finish()
        }
    }
}