Skip to content

Commit fcccbcb

Browse files
committed
Finish conversion of error reporting within elaboration context
1 parent 03aae4b commit fcccbcb

File tree

6 files changed

+295
-245
lines changed

6 files changed

+295
-245
lines changed

crates/sml-core/src/check.rs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,21 @@ use sml_frontend::ast::*;
1414
use sml_util::diagnostics::Diagnostic;
1515
use sml_util::interner::*;
1616
use sml_util::span::Span;
17-
1817
use std::collections::{HashSet, VecDeque};
1918

2019
const BUILTIN_CONSTRUCTORS: [Symbol; 5] = [S_NIL, S_CONS, S_TRUE, S_FALSE, S_REF];
2120

22-
#[derive(Default, Debug)]
23-
pub struct Check {
21+
pub struct Check<'a> {
22+
interner: &'a Interner,
2423
pub diags: Vec<Diagnostic>,
2524
}
2625

27-
impl Check {
28-
pub fn check_program(program: &[Decl]) -> Vec<Diagnostic> {
29-
let mut check = Check { diags: Vec::new() };
30-
for d in program {
31-
check.check_decl(d);
26+
impl<'a> Check<'a> {
27+
pub fn new(interner: &'a Interner) -> Self {
28+
Self {
29+
interner,
30+
diags: Vec::new(),
3231
}
33-
check.diags
3432
}
3533

3634
fn check_pat(&mut self, pattern: &Pat) {
@@ -59,7 +57,10 @@ impl Check {
5957
self.diags.push(
6058
Diagnostic::error(
6159
pattern.span,
62-
format!("duplicate variable in pattern: '{:?}'", sym),
60+
format!(
61+
"duplicate variable in pattern: '{}'",
62+
self.interner.get(*sym).unwrap_or("?")
63+
),
6364
)
6465
.message(pat.span, "redefined here"),
6566
);
@@ -77,7 +78,10 @@ impl Check {
7778
if !set.insert(row.label) {
7879
self.diags.push(Diagnostic::error(
7980
row.span,
80-
format!("duplicate record label: '{:?}'", row.label),
81+
format!(
82+
"duplicate record label: '{}'",
83+
self.interner.get(row.label).unwrap_or("?")
84+
),
8185
));
8286
}
8387
f(self, &row.data);
@@ -171,7 +175,10 @@ impl Check {
171175
if !set.insert(tv) {
172176
self.diags.push(Diagnostic::error(
173177
sp,
174-
format!("type variable '{:?}' cannot be rebound", tv),
178+
format!(
179+
"type variable '{}' cannot be rebound",
180+
self.interner.get(*tv).unwrap_or("?")
181+
),
175182
));
176183
}
177184
}
@@ -188,8 +195,8 @@ impl Check {
188195
self.diags.push(Diagnostic::error(
189196
con.span,
190197
format!(
191-
"builtin data constructor '{:?}' cannot be rebound",
192-
con.label
198+
"builtin data constructor '{}' cannot be rebound",
199+
self.interner.get(con.label).unwrap_or("?")
193200
),
194201
));
195202
}
@@ -207,7 +214,10 @@ impl Check {
207214
if BUILTIN_CONSTRUCTORS.contains(&s) {
208215
self.diags.push(Diagnostic::error(
209216
pat.span,
210-
format!("builtin data constructor '{:?}' cannot be rebound", s),
217+
format!(
218+
"builtin data constructor '{}' cannot be rebound",
219+
self.interner.get(s).unwrap_or("?")
220+
),
211221
));
212222
}
213223
}
@@ -226,16 +236,17 @@ impl Check {
226236
self.diags.push(Diagnostic::error(
227237
fb.span,
228238
format!(
229-
"function clause with a different name; expected: {:?}, found {:?}",
230-
n, fb.name
239+
"function clause with a different name; expected: {}, found {}",
240+
self.interner.get(n).unwrap_or("?"),
241+
self.interner.get(fb.name).unwrap_or("?")
231242
),
232243
));
233244
}
234245
if a != fb.pats.len() {
235246
self.diags.push(Diagnostic::error(
236247
fb.span,
237248
format!(
238-
"function clause with a different number of args; expected: {:?}, found {:?}",
249+
"function clause with a different number of args; expected: {}, found {}",
239250
a, fb.pats.len()
240251
)
241252
));
@@ -245,8 +256,8 @@ impl Check {
245256
self.diags.push(Diagnostic::error(
246257
f.span,
247258
format!(
248-
"function '{:?}' was previously defined in function bindings",
249-
n
259+
"function '{}' was previously defined in function bindings",
260+
self.interner.get(n).unwrap_or("?")
250261
),
251262
));
252263
}

0 commit comments

Comments
 (0)