Skip to content

Commit 21d9a5d

Browse files
authored
parser,checker,cgen: remove unused err declaration or or { } blocks (#25034)
1 parent c5b6d2a commit 21d9a5d

15 files changed

Lines changed: 108 additions & 37 deletions

File tree

cmd/tools/vast/vast.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,6 +1646,7 @@ fn (t Tree) or_expr(node ast.OrExpr) &Node {
16461646
obj.add_terse('stmts', t.array_node_stmt(node.stmts))
16471647
obj.add_terse('kind', t.enum_node(node.kind))
16481648
obj.add('pos', t.pos(node.pos))
1649+
obj.add('scope', t.number_node(int(node.scope)))
16491650
return obj
16501651
}
16511652

vlib/builtin/sorted_map.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ module builtin
1414
// The number for `degree` has been picked through vigor-
1515
// ous benchmarking but can be changed to any number > 1.
1616
// `degree` determines the maximum length of each node.
17+
// TODO: the @[markused] tag here is needed as a workaround for
18+
// compilation with `-cc clang -usecache`; added in https://github.com/vlang/v/pull/25034
19+
20+
@[markused]
1721
const degree = 6
1822
const mid_index = degree - 1
1923
const max_len = 2 * degree - 1

vlib/v/ast/ast.v

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,7 @@ pub mut:
935935
is_tmp bool // for tmp for loop vars, so that autofree can skip them
936936
is_auto_heap bool // value whose address goes out of scope
937937
is_stack_obj bool // may be pointer to stack value (`mut` or `&` arg and not @[heap] struct)
938+
is_special bool // err, it, a, b vars (ignore not useds)
938939
}
939940

940941
// used for smartcasting only
@@ -1912,10 +1913,12 @@ pub enum OrKind {
19121913
// `or { ... }`
19131914
pub struct OrExpr {
19141915
pub:
1915-
kind OrKind
1916-
pos token.Pos
1916+
kind OrKind
1917+
pos token.Pos
1918+
scope &Scope = unsafe { nil }
19171919
pub mut:
1918-
stmts []Stmt
1920+
err_used bool
1921+
stmts []Stmt
19191922
}
19201923

19211924
/*

vlib/v/checker/checker.v

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ mut:
113113
type_level int // to avoid infinite recursion segfaults due to compiler bugs in ensure_type_exists
114114
ensure_generic_type_level int // to avoid infinite recursion segfaults in ensure_generic_type_specify_type_names
115115
cur_orm_ts ast.TypeSymbol
116+
cur_or_expr &ast.OrExpr = unsafe { nil }
116117
cur_anon_fn &ast.AnonFn = unsafe { nil }
117118
vmod_file_content string // needed for @VMOD_FILE, contents of the file, *NOT its path**
118119
loop_labels []string // filled, when inside labelled for loops: `a_label: for x in 0..10 {`
@@ -299,7 +300,7 @@ pub fn (mut c Checker) check_scope_vars(sc &ast.Scope) {
299300
for _, obj in sc.objects {
300301
match obj {
301302
ast.Var {
302-
if !obj.is_used && obj.name[0] != `_` {
303+
if !obj.is_special && !obj.is_used && obj.name[0] != `_` {
303304
if !c.pref.translated && !c.file.is_translated {
304305
if obj.is_arg {
305306
if c.pref.show_unused_params {
@@ -310,11 +311,11 @@ pub fn (mut c Checker) check_scope_vars(sc &ast.Scope) {
310311
}
311312
}
312313
}
313-
if obj.is_mut && !obj.is_changed && !c.is_builtin_mod && obj.name != 'it' {
314-
// if obj.is_mut && !obj.is_changed && !c.is_builtin { //TODO C error bad field not checked
315-
// c.warn('`$obj.name` is declared as mutable, but it was never changed',
316-
// obj.pos)
317-
}
314+
// if obj.is_mut && !obj.is_changed && !c.is_builtin_mod && obj.name != 'it' {
315+
// if obj.is_mut && !obj.is_changed && !c.is_builtin { //TODO C error bad field not checked
316+
// c.warn('`$obj.name` is declared as mutable, but it was never changed',
317+
// obj.pos)
318+
// }
318319
}
319320
else {}
320321
}
@@ -1338,7 +1339,10 @@ fn (mut c Checker) check_expr_option_or_result_call(expr ast.Expr, ret_type ast.
13381339
}
13391340
}
13401341
} else {
1342+
last_cur_or_expr := c.cur_or_expr
1343+
c.cur_or_expr = &expr.or_block
13411344
c.check_or_expr(expr.or_block, ret_type, expr_ret_type, expr)
1345+
c.cur_or_expr = last_cur_or_expr
13421346
}
13431347
return ret_type.clear_flag(.result)
13441348
} else {
@@ -1365,7 +1369,10 @@ fn (mut c Checker) check_expr_option_or_result_call(expr ast.Expr, ret_type ast.
13651369
}
13661370
} else {
13671371
if expr.or_block.kind != .absent {
1372+
last_cur_or_expr := c.cur_or_expr
1373+
c.cur_or_expr = &expr.or_block
13681374
c.check_or_expr(expr.or_block, ret_type, expr.typ, expr)
1375+
c.cur_or_expr = last_cur_or_expr
13691376
}
13701377
}
13711378
return ret_type.clear_flag(.result)
@@ -1389,8 +1396,11 @@ fn (mut c Checker) check_expr_option_or_result_call(expr ast.Expr, ret_type ast.
13891396
if return_none_or_error {
13901397
c.check_expr_option_or_result_call(expr.or_expr, c.table.cur_fn.return_type)
13911398
} else {
1399+
last_cur_or_expr := c.cur_or_expr
1400+
c.cur_or_expr = &expr.or_expr
13921401
c.check_or_expr(expr.or_expr, ret_type, ret_type.set_flag(.result),
13931402
expr)
1403+
c.cur_or_expr = last_cur_or_expr
13941404
}
13951405
} else if expr.left is ast.SelectorExpr && expr.left_type.has_option_or_result() {
13961406
with_modifier_kind := if expr.left_type.has_flag(.option) {
@@ -1464,6 +1474,10 @@ fn (mut c Checker) check_or_expr(node ast.OrExpr, ret_type ast.Type, expr_return
14641474
}
14651475
// allow `f() or {}`
14661476
return
1477+
} else if !node.err_used {
1478+
if err_var := node.scope.find_var('err') {
1479+
c.cur_or_expr.err_used = err_var.is_used
1480+
}
14671481
}
14681482
mut valid_stmts := node.stmts.filter(it !is ast.SemicolonStmt)
14691483
mut last_stmt := if valid_stmts.len > 0 { valid_stmts.last() } else { node.stmts.last() }
@@ -1809,7 +1823,10 @@ fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type {
18091823
unwrapped_typ := c.unwrap_generic(node.typ)
18101824
c.expected_or_type = unwrapped_typ.clear_option_and_result()
18111825
c.stmts_ending_with_expression(mut node.or_block.stmts, c.expected_or_type)
1826+
last_cur_or_expr := c.cur_or_expr
1827+
c.cur_or_expr = &node.or_block
18121828
c.check_or_expr(node.or_block, unwrapped_typ, c.expected_or_type, node)
1829+
c.cur_or_expr = last_cur_or_expr
18131830
c.expected_or_type = ast.void_type
18141831
}
18151832
return field.typ
@@ -4079,7 +4096,10 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
40794096
unwrapped_typ := typ.clear_option_and_result()
40804097
c.expected_or_type = unwrapped_typ
40814098
c.stmts_ending_with_expression(mut node.or_expr.stmts, c.expected_or_type)
4099+
last_cur_or_expr := c.cur_or_expr
4100+
c.cur_or_expr = &node.or_expr
40824101
c.check_or_expr(node.or_expr, typ, c.expected_or_type, node)
4102+
c.cur_or_expr = last_cur_or_expr
40834103
return unwrapped_typ
40844104
}
40854105
return typ
@@ -4194,7 +4214,10 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
41944214
unwrapped_typ := typ.clear_option_and_result()
41954215
c.expected_or_type = unwrapped_typ
41964216
c.stmts_ending_with_expression(mut node.or_expr.stmts, c.expected_or_type)
4217+
last_cur_or_expr := c.cur_or_expr
4218+
c.cur_or_expr = &node.or_expr
41974219
c.check_or_expr(node.or_expr, typ, c.expected_or_type, node)
4220+
c.cur_or_expr = last_cur_or_expr
41984221
return unwrapped_typ
41994222
}
42004223
return typ
@@ -4256,7 +4279,10 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
42564279
unwrapped_typ := typ.clear_option_and_result()
42574280
c.expected_or_type = unwrapped_typ
42584281
c.stmts_ending_with_expression(mut node.or_expr.stmts, c.expected_or_type)
4282+
last_cur_or_expr := c.cur_or_expr
4283+
c.cur_or_expr = &node.or_expr
42594284
c.check_or_expr(node.or_expr, typ, c.expected_or_type, node)
4285+
c.cur_or_expr = last_cur_or_expr
42604286
}
42614287
return typ
42624288
}

vlib/v/checker/fn.v

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,10 @@ fn (mut c Checker) call_expr(mut node ast.CallExpr) ast.Type {
783783
if node.or_block.kind == .block {
784784
old_inside_or_block_value := c.inside_or_block_value
785785
c.inside_or_block_value = true
786+
last_cur_or_expr := c.cur_or_expr
787+
c.cur_or_expr = &node.or_block
786788
c.check_or_expr(node.or_block, typ, c.expected_or_type, node)
789+
c.cur_or_expr = last_cur_or_expr
787790
c.inside_or_block_value = old_inside_or_block_value
788791
}
789792
c.expected_or_type = old_expected_or_type
@@ -3819,10 +3822,11 @@ fn scope_register_a_b(mut s ast.Scope, pos token.Pos, typ ast.Type) {
38193822

38203823
fn scope_register_var_name(mut s ast.Scope, pos token.Pos, typ ast.Type, name string) {
38213824
s.register(ast.Var{
3822-
name: name
3823-
pos: pos
3824-
typ: typ
3825-
is_used: true
3825+
name: name
3826+
pos: pos
3827+
typ: typ
3828+
is_used: false
3829+
is_special: true
38263830
})
38273831
}
38283832

vlib/v/checker/orm.v

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,10 @@ fn (mut c Checker) sql_expr(mut node ast.SqlExpr) ast.Type {
192192
if node.is_insert {
193193
node.typ = ast.int_type
194194
}
195-
195+
last_cur_or_expr := c.cur_or_expr
196+
c.cur_or_expr = &node.or_expr
196197
c.check_orm_or_expr(mut node)
198+
c.cur_or_expr = last_cur_or_expr
197199

198200
if node.is_insert {
199201
return ast.int_type
@@ -211,8 +213,10 @@ fn (mut c Checker) sql_stmt(mut node ast.SqlStmt) ast.Type {
211213
for mut line in node.lines {
212214
c.sql_stmt_line(mut line)
213215
}
214-
216+
last_cur_or_expr := c.cur_or_expr
217+
c.cur_or_expr = &node.or_expr
215218
c.check_orm_or_expr(mut node)
219+
c.cur_or_expr = last_cur_or_expr
216220

217221
return ast.void_type
218222
}
@@ -605,7 +609,6 @@ fn (mut c Checker) check_orm_or_expr(mut expr ORMExpr) {
605609
return
606610
}
607611
}
608-
609612
return_type := if mut expr is ast.SqlExpr {
610613
expr.typ
611614
} else {

vlib/v/gen/c/cgen.v

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7177,7 +7177,10 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type ast.Ty
71777177
}
71787178
if or_block.kind == .block {
71797179
g.or_expr_return_type = return_type.clear_option_and_result()
7180-
g.writeln('\tIError err = ${cvar_name}${tmp_op}err;')
7180+
if !g.pref.skip_unused || or_block.err_used
7181+
|| (g.fn_decl != unsafe { nil } && (g.fn_decl.is_main || g.fn_decl.is_test)) {
7182+
g.writeln('\tIError err = ${cvar_name}${tmp_op}err;')
7183+
}
71817184

71827185
g.inside_or_block = true
71837186
defer {

vlib/v/gen/c/if.v

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,12 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
310310
g.writeln('{')
311311
// define `err` for the last branch after a `if val := opt {...}' guard
312312
if is_guard && guard_idx == i - 1 {
313-
cvar_name := guard_vars[guard_idx]
314-
g.writeln('\tIError err = ${cvar_name}.err;')
313+
if err_var := branch.scope.find_var('err') {
314+
if !g.pref.skip_unused || err_var.is_used {
315+
cvar_name := guard_vars[guard_idx]
316+
g.writeln('\tIError err = ${cvar_name}.err;')
317+
}
318+
}
315319
}
316320
} else if branch.cond is ast.IfGuardExpr {
317321
mut var_name := guard_vars[i]

vlib/v/gen/c/orm.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ fn (mut g Gen) write_orm_select(node ast.SqlExpr, connection_var_name string, re
11241124
field_var := '${tmp}.${c_name(field.name)}'
11251125
field_c_typ := g.styp(final_field_typ)
11261126
if sym.kind == .struct && sym.name != 'time.Time' {
1127-
mut sub := node.sub_structs[int(final_field_typ)]
1127+
mut sub := node.sub_structs[int(final_field_typ)] or { continue }
11281128
mut where_expr := sub.where_expr as ast.InfixExpr
11291129
mut ident := where_expr.right as ast.Ident
11301130
primitive_type_index := g.table.find_type('orm.Primitive')
@@ -1161,7 +1161,7 @@ fn (mut g Gen) write_orm_select(node ast.SqlExpr, connection_var_name string, re
11611161
} else {
11621162
verror('missing fkey attribute')
11631163
}
1164-
sub := node.sub_structs[final_field_typ]
1164+
sub := node.sub_structs[final_field_typ] or { continue }
11651165
if sub.has_where {
11661166
mut where_expr := sub.where_expr as ast.InfixExpr
11671167
mut left_where_expr := where_expr.left as ast.Ident

vlib/v/parser/comptime.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,10 +536,11 @@ fn (mut p Parser) comptime_selector(left ast.Expr) ast.Expr {
536536
mut or_kind := ast.OrKind.absent
537537
mut or_pos := p.tok.pos()
538538
mut or_stmts := []ast.Stmt{}
539+
mut or_scope := &ast.Scope(unsafe { nil })
539540
if p.tok.kind == .key_orelse {
540541
// `$method() or {}``
541542
or_kind = .block
542-
or_stmts, or_pos = p.or_block(.with_err_var)
543+
or_stmts, or_pos, or_scope = p.or_block(.with_err_var)
543544
}
544545
return ast.ComptimeCall{
545546
left: left
@@ -554,6 +555,7 @@ fn (mut p Parser) comptime_selector(left ast.Expr) ast.Expr {
554555
stmts: or_stmts
555556
kind: or_kind
556557
pos: or_pos
558+
scope: or_scope
557559
}
558560
}
559561
}

0 commit comments

Comments
 (0)