Skip to content

Commit

Permalink
checker: disallow blank ident in const and global decl
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Oct 5, 2024
1 parent b6c9971 commit de7e92a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
12 changes: 11 additions & 1 deletion vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,13 @@ fn (mut c Checker) const_decl(mut node ast.ConstDecl) {
}
c.error('duplicate of a module name `${field.name}`', name_pos)
}
if const_name == '_' {
name_pos := token.Pos{
...field.pos
len: util.no_cur_mod(field.name, c.mod).len
}
c.error('cannot use `_` as a const name', name_pos)
}
c.const_names << field.name
}
for i, mut field in node.fields {
Expand Down Expand Up @@ -2317,7 +2324,7 @@ fn (mut c Checker) global_decl(mut node ast.GlobalDecl) {
if sym.kind == .placeholder {
c.error('unknown type `${sym.name}`', field.typ_pos)
}
if field.has_expr {
if field.has_expr && field.name != '_' {
if field.expr is ast.AnonFn && field.name == 'main' {
c.error('the `main` function is the program entry point, cannot redefine it',
field.pos)
Expand All @@ -2337,6 +2344,9 @@ fn (mut c Checker) global_decl(mut node ast.GlobalDecl) {
}
v.typ = ast.mktyp(field.typ)
}
if field.name == '_' {
c.error('cannot use `_` as a global name', field.pos)
}
}
c.global_names << field.name
}
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/checker/tests/const_blank_ident_as_name_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vlib/v/checker/tests/const_blank_ident_as_name_err.vv:1:7: error: cannot use `_` as a const name
1 | const _ = 23
| ^
1 change: 1 addition & 0 deletions vlib/v/checker/tests/const_blank_ident_as_name_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const _ = 23
18 changes: 18 additions & 0 deletions vlib/v/checker/tests/globals/global_blank_ident_as_name_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
vlib/v/checker/tests/globals/global_blank_ident_as_name_err.vv:2:5: error: cannot use `_` as a global name
1 | __global (
2 | _ = 23
| ^
3 | _ int
4 | )
vlib/v/checker/tests/globals/global_blank_ident_as_name_err.vv:3:5: error: duplicate global `_`
1 | __global (
2 | _ = 23
3 | _ int
| ^
4 | )
vlib/v/checker/tests/globals/global_blank_ident_as_name_err.vv:3:5: error: cannot use `_` as a global name
1 | __global (
2 | _ = 23
3 | _ int
| ^
4 | )
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__global (
_ = 23
_ int
)

0 comments on commit de7e92a

Please sign in to comment.