Skip to content

Commit

Permalink
ast: improve error message for duplicated symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Dec 27, 2024
1 parent bfbc1b4 commit d4b0691
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 14 additions & 4 deletions compiler/ast/Scope.v
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,25 @@ pub fn (sc &Scope) derive() &Scope {
}

pub fn (mut sc Scope) add_symbol(sym Symbol) ! {
if _ := sc.lookup(sym.name) {
return error('duplicate ${sym.type_of()} `${sym.name}`')
if other := sc.lookup(sym.name) {
m := if other.type_of() == sym.type_of() {
'duplicate ${sym.type_of()} `${sym.name}`'
} else {
'another symbol exists with the same name as `${sym.name}`'
}
return error(m)
}
sc.syms << sym
}

pub fn (mut sc Scope) add_local_symbol(sym Symbol) ! {
if _ := sc.find(sym.name) {
return error('duplicate symbol `${sym.name}`')
if other := sc.find(sym.name) {
m := if other.type_of() == sym.type_of() {
'duplicate ${sym.type_of()} `${sym.name}`'
} else {
'another symbol exists with the same name as `${sym.name}`'
}
return error(m)
}
sc.syms << sym
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/sema/mod.v
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn (mut sema Sema) fn_stmt(mut stmt ast.FnStmt) {
sema.scope.add_local_symbol(stmt.sym) or { context.error(err.msg(), stmt.name_pos) }
sema.scope = stmt.scope
for arg in stmt.args {
sema.scope.add_symbol(ast.Variable{
sema.scope.add_local_symbol(ast.Variable{
name: arg.name
is_local: true
is_arg: true
Expand Down

0 comments on commit d4b0691

Please sign in to comment.