Skip to content

Commit bf4e813

Browse files
committed
Add duplicate function name error
1 parent cd5fbad commit bf4e813

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
fn foo() { return; }
2+
fn foo() { return; }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
fn foo() -> int {
22
return true;
33
}
4+
5+
fn bar() {
6+
return 1;
7+
}
8+
9+
fn baz() -> int {
10+
return;
11+
}
12+
13+

src/tc.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,50 @@ impl TypeChecker {
293293
// errs.push(TcErrorInner::new("function `main` not found", prog.loc));
294294
// }
295295

296+
let mut seen_funcs: HashMap<String, WithLoc<String>> = HashMap::new();
297+
296298
prog.iter_mut()
297-
.for_each(|fdef| self.tc_fdef(fdef));
299+
.for_each(|fdef| {
300+
if let Some(entry) = seen_funcs.get(fdef.name.as_str()) {
301+
let [color1, color2] = colors();
302+
303+
Report::build(ariadne::ReportKind::Error, &self.src_file, fdef.name.loc.start)
304+
.with_message::<&str>("duplicate function name")
305+
.with_label(
306+
Label::new(
307+
(&self.src_file, fdef.name.loc.range())
308+
)
309+
.with_message(
310+
format!("function {} redefined here", fdef.name.as_str().fg(color1))
311+
)
312+
.with_color(color1)
313+
)
314+
.with_label(
315+
Label::new(
316+
(&self.src_file, entry.loc.range())
317+
)
318+
.with_message(
319+
format!("first definition of function {} here", entry.as_str().fg(color2))
320+
)
321+
.with_color(color2)
322+
)
323+
.with_note(
324+
format!("function names must be distinct")
325+
)
326+
.finish()
327+
.print((&self.src_file, Source::from(self.src_content.clone())))
328+
.unwrap();
329+
330+
self.errors.add(
331+
format!("duplicate function name `{}`", entry.elem),
332+
fdef.name.loc
333+
);
334+
} else {
335+
seen_funcs.insert(fdef.name.elem.clone(), fdef.name.clone());
336+
}
337+
338+
self.tc_fdef(fdef);
339+
});
298340

299341
if !self.errors.is_empty() {
300342
return Err(self.errors.clone());

0 commit comments

Comments
 (0)