Skip to content

Commit 9f76a7b

Browse files
parser: simplify unused vars & add loop/if vars etc
1 parent e67bf67 commit 9f76a7b

File tree

15 files changed

+79
-82
lines changed

15 files changed

+79
-82
lines changed

vlib/builtin/string.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ fn (ar []string) contains(val string) bool {
832832

833833
// TODO generic
834834
fn (ar []int) contains(val int) bool {
835-
for i, s in ar {
835+
for s in ar {
836836
if s == val {
837837
return true
838838
}
@@ -1206,7 +1206,7 @@ pub fn (a []string) join(del string) string {
12061206
return ''
12071207
}
12081208
mut len := 0
1209-
for i, val in a {
1209+
for val in a {
12101210
len += val.len + del.len
12111211
}
12121212
len -= del.len

vlib/os/os.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ pub fn walk_ext(path, ext string) []string {
10611061
}
10621062
mut res := []string{}
10631063
separator := if path.ends_with(os.path_separator) { '' } else { os.path_separator }
1064-
for i, file in files {
1064+
for file in files {
10651065
if file.starts_with('.') {
10661066
continue
10671067
}

vlib/v/ast/ast.v

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,13 @@ pub struct Stmt {
270270
*/
271271
pub struct Var {
272272
pub:
273-
name string
274-
expr Expr
275-
is_mut bool
273+
name string
274+
expr Expr
275+
is_mut bool
276276
mut:
277-
typ table.Type
278-
pos token.Position
277+
typ table.Type
278+
pos token.Position
279+
is_used bool
279280
}
280281

281282
pub struct GlobalDecl {

vlib/v/ast/scope.v

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,9 @@ mut:
1212
children []&Scope
1313
start_pos int
1414
end_pos int
15-
unused_vars map[string]UnusedVar
1615
objects map[string]ScopeObject
1716
}
1817

19-
pub struct UnusedVar {
20-
name string
21-
pos token.Position
22-
}
23-
2418
pub fn new_scope(parent &Scope, start_pos int) &Scope {
2519
return &ast.Scope{
2620
parent: parent
@@ -63,25 +57,25 @@ pub fn (s &Scope) is_known(name string) bool {
6357
return false
6458
}
6559

66-
pub fn (s &Scope) find_var(name string) ?Var {
60+
pub fn (s &Scope) find_var(name string) ?&Var {
6761
if obj := s.find(name) {
6862
v := ScopeObject(obj)
6963
match v {
7064
Var {
71-
return *it
65+
return it
7266
}
7367
else {}
7468
}
7569
}
7670
return none
7771
}
7872

79-
pub fn (s &Scope) find_const(name string) ?ConstField {
73+
pub fn (s &Scope) find_const(name string) ?&ConstField {
8074
if obj := s.find(name) {
8175
cf := ScopeObject(obj)
8276
match cf {
8377
ConstField {
84-
return *it
78+
return it
8579
}
8680
else {}
8781
}
@@ -112,37 +106,13 @@ pub fn (s mut Scope) register(name string, obj ScopeObject) {
112106
if name == '_' {
113107
return
114108
}
115-
if x := s.find(name) {
109+
if _ := s.find(name) {
116110
// println('existing obect: $name')
117111
return
118112
}
119113
s.objects[name] = obj
120114
}
121115

122-
pub fn (s mut Scope) register_unused_var(name string, pos token.Position) {
123-
s.unused_vars[name] = UnusedVar{name, pos}
124-
}
125-
126-
pub fn (s mut Scope) remove_unused_var(name string) {
127-
mut sc := s
128-
for !isnil(sc) {
129-
sc.unused_vars.delete(name)
130-
sc = sc.parent
131-
}
132-
}
133-
134-
pub fn (s mut Scope) unused_vars() []UnusedVar {
135-
ret := []UnusedVar{}
136-
for _, v in s.unused_vars {
137-
ret << v
138-
}
139-
return ret
140-
}
141-
142-
pub fn (s mut Scope) clear_unused_vars() {
143-
s.unused_vars = map[string]UnusedVar
144-
}
145-
146116
pub fn (s &Scope) outermost() &Scope {
147117
mut sc := s
148118
for !isnil(sc.parent) {

vlib/v/checker/checker.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ pub fn (mut c Checker) struct_decl(decl ast.StructDecl) {
203203
}
204204
c.error('struct name must begin with capital letter', pos)
205205
}
206-
for fi, field in decl.fields {
206+
for field in decl.fields {
207207
sym := c.table.get_type_symbol(field.typ)
208208
if sym.kind == .placeholder && !decl.is_c && !sym.name.starts_with('C.') {
209209
c.error('unknown type `$sym.name`', field.pos)
@@ -554,7 +554,7 @@ pub fn (mut c Checker) call_method(call_expr mut ast.CallExpr) table.Type {
554554
mut scope := c.file.scope.innermost(call_expr.pos.pos)
555555
scope.update_var_type('it', array_info.elem_type)
556556
}
557-
for i, arg in call_expr.args {
557+
for arg in call_expr.args {
558558
c.expr(arg.expr)
559559
}
560560
// need to return `array_xxx` instead of `array`

vlib/v/gen/cgen.v

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ fn (mut g Gen) stmt(node ast.Stmt) {
480480
g.typedefs.writeln('typedef enum {')
481481
mut cur_enum_expr := ''
482482
mut cur_enum_offset := 0
483-
for j, field in it.fields {
483+
for field in it.fields {
484484
g.typedefs.write('\t${enum_name}_${field.name}')
485485
if field.has_expr {
486486
g.typedefs.write(' = ')
@@ -1918,7 +1918,7 @@ fn (mut g Gen) return_statement(node ast.Return) {
19181918
}
19191919

19201920
fn (mut g Gen) const_decl(node ast.ConstDecl) {
1921-
for i, field in node.fields {
1921+
for field in node.fields {
19221922
name := c_name(field.name)
19231923
// TODO hack. Cut the generated value and paste it into definitions.
19241924
pos := g.out.len
@@ -3061,7 +3061,7 @@ fn (mut g Gen) gen_str_for_enum(info table.Enum, styp, str_fn_name string) {
30613061
g.definitions.writeln('string ${str_fn_name}($styp it); // auto')
30623062
g.auto_str_funcs.writeln('string ${str_fn_name}($styp it) { /* gen_str_for_enum */')
30633063
g.auto_str_funcs.writeln('\tswitch(it) {')
3064-
for i, val in info.vals {
3064+
for val in info.vals {
30653065
g.auto_str_funcs.writeln('\t\tcase ${s}_$val: return tos3("$val");')
30663066
}
30673067
g.auto_str_funcs.writeln('\t\tdefault: return tos3("unknown enum value");')
@@ -3075,7 +3075,7 @@ fn (mut g Gen) gen_str_for_struct(info table.Struct, styp, str_fn_name string) {
30753075
mut fnames2strfunc := {
30763076
'': ''
30773077
} // map[string]string // TODO vfmt bug
3078-
for i, field in info.fields {
3078+
for field in info.fields {
30793079
sym := g.table.get_type_symbol(field.typ)
30803080
if sym.kind in [.struct_, .array, .array_fixed, .map, .enum_] {
30813081
field_styp := g.typ(field.typ)

vlib/v/gen/fn.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ fn (mut g Gen) call_args(args []ast.CallArg, expected_types []table.Type) {
426426
is_forwarding_varg := args.len > 0 && args[args.len - 1].typ.flag_is(.variadic)
427427
gen_vargs := is_variadic && !is_forwarding_varg
428428
mut arg_no := 0
429-
for i, arg in args {
429+
for arg in args {
430430
if gen_vargs && arg_no == expected_types.len - 1 {
431431
break
432432
}

vlib/v/gen/x64/gen.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ fn (mut g Gen) allocate_var(name string, size, initial_val int) {
600600

601601
fn (mut g Gen) assign_stmt(node ast.AssignStmt) {
602602
// `a := 1` | `a,b := 1,2`
603-
for i, ident in node.left {
603+
for ident in node.left {
604604
match node.right[0] {
605605
ast.IntegerLiteral {
606606
g.allocate_var(ident.name, 4, it.val.int())

vlib/v/parser/assign.v

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ fn (mut p Parser) assign_stmt() ast.Stmt {
3232
name: ident.name
3333
expr: exprs[i]
3434
is_mut: ident.is_mut || p.inside_for
35+
pos: ident.pos
3536
})
3637
} else {
3738
p.scope.register(ident.name, ast.Var{
3839
name: ident.name
3940
is_mut: ident.is_mut || p.inside_for
41+
pos: ident.pos
4042
})
4143
}
4244
}

vlib/v/parser/fn.v

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,14 @@ pub fn (mut p Parser) call_expr(is_c, is_js bool, mod string) ast.CallExpr {
3737
p.scope.register('err', ast.Var{
3838
name: 'err'
3939
typ: table.string_type
40+
pos: p.tok.position()
41+
is_used: true
4042
})
4143
p.scope.register('errcode', ast.Var{
4244
name: 'errcode'
4345
typ: table.int_type
46+
pos: p.tok.position()
47+
is_used: true
4448
})
4549
is_or_block_used = true
4650
or_stmts = p.parse_block_no_scope()
@@ -164,6 +168,8 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
164168
name: arg.name
165169
typ: arg.typ
166170
is_mut: arg.is_mut
171+
pos: p.tok.position()
172+
is_used: true
167173
})
168174
// Do not allow `mut` with simple types
169175
// TODO move to checker?
@@ -264,6 +270,8 @@ fn (mut p Parser) anon_fn() ast.AnonFn {
264270
p.scope.register(arg.name, ast.Var{
265271
name: arg.name
266272
typ: arg.typ
273+
pos: p.tok.position()
274+
is_used: true
267275
})
268276
}
269277
mut return_type := table.void_type

0 commit comments

Comments
 (0)