Skip to content

Commit e14853d

Browse files
authored
v: cleanup if smartcasts in the compiler (#24734)
1 parent e7905ea commit e14853d

13 files changed

Lines changed: 89 additions & 133 deletions

File tree

‎vlib/v/ast/ast.v‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2594,10 +2594,8 @@ pub fn (mut lx IndexExpr) recursive_arraymap_set_is_setter() {
25942594
lx.is_setter = true
25952595
if mut lx.left is IndexExpr {
25962596
lx.left.recursive_arraymap_set_is_setter()
2597-
} else if mut lx.left is SelectorExpr {
2598-
if mut lx.left.expr is IndexExpr {
2599-
lx.left.expr.recursive_arraymap_set_is_setter()
2600-
}
2597+
} else if mut lx.left is SelectorExpr && lx.left.expr is IndexExpr {
2598+
lx.left.expr.recursive_arraymap_set_is_setter()
26012599
}
26022600
}
26032601

‎vlib/v/checker/assign.v‎

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,8 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
103103
c.error('cannot assign a `none` value to a variable', right.pos)
104104
}
105105
// Handle `left_name := unsafe { none }`
106-
if mut right is ast.UnsafeExpr {
107-
if mut right.expr is ast.None {
108-
c.error('cannot use `none` in `unsafe` blocks', right.expr.pos)
109-
}
106+
if mut right is ast.UnsafeExpr && right.expr is ast.None {
107+
c.error('cannot use `none` in `unsafe` blocks', right.expr.pos)
110108
}
111109
if mut right is ast.AnonFn {
112110
if right.decl.generic_names.len > 0 {
@@ -159,11 +157,9 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
159157
c.error('cannot dereference a function call on the left side of an assignment, use a temporary variable',
160158
left.pos)
161159
}
162-
} else if mut left is ast.IndexExpr {
163-
if left.index is ast.RangeExpr {
164-
c.error('cannot reassign using range expression on the left side of an assignment',
165-
left.pos)
166-
}
160+
} else if mut left is ast.IndexExpr && left.index is ast.RangeExpr {
161+
c.error('cannot reassign using range expression on the left side of an assignment',
162+
left.pos)
167163
} else if mut left is ast.Ident && node.op == .decl_assign {
168164
if left.name in c.global_names {
169165
c.note('the global variable named `${left.name}` already exists', left.pos)
@@ -199,10 +195,8 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
199195
}
200196
if node.right_types.len < node.left.len { // first type or multi return types added above
201197
old_inside_ref_lit := c.inside_ref_lit
202-
if mut left is ast.Ident {
203-
if mut left.info is ast.IdentVar {
204-
c.inside_ref_lit = c.inside_ref_lit || left.info.share == .shared_t
205-
}
198+
if mut left is ast.Ident && left.info is ast.IdentVar {
199+
c.inside_ref_lit = c.inside_ref_lit || left.info.share == .shared_t
206200
}
207201
c.inside_decl_rhs = is_decl
208202
mut expr := node.right[i]
@@ -666,11 +660,9 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
666660
}
667661
}
668662
}
669-
if mut left is ast.Ident {
670-
if mut left.info is ast.IdentVar {
671-
if left.info.is_static && right_sym.kind == .map {
672-
c.error('maps cannot be static', left.pos)
673-
}
663+
if mut left is ast.Ident && left.info is ast.IdentVar {
664+
if left.info.is_static && right_sym.kind == .map {
665+
c.error('maps cannot be static', left.pos)
674666
}
675667
}
676668
// Single side check

‎vlib/v/checker/checker.v‎

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -911,10 +911,8 @@ fn (mut c Checker) fail_if_immutable(mut expr ast.Expr) (string, token.Pos) {
911911
}
912912
ast.ComptimeSelector {
913913
mut expr_left := expr.left
914-
if mut expr.left is ast.Ident {
915-
if mut expr.left.obj is ast.Var {
916-
c.fail_if_immutable(mut expr_left)
917-
}
914+
if mut expr.left is ast.Ident && expr.left.obj is ast.Var {
915+
c.fail_if_immutable(mut expr_left)
918916
}
919917
return '', expr.pos
920918
}
@@ -1117,6 +1115,9 @@ fn (mut c Checker) fail_if_immutable(mut expr ast.Expr) (string, token.Pos) {
11171115
}
11181116
return '', expr.pos
11191117
}
1118+
ast.AsCast {
1119+
to_lock, pos = c.fail_if_immutable(mut expr.expr)
1120+
}
11201121
else {
11211122
if !expr.is_pure_literal() {
11221123
c.error('unexpected expression `${expr.type_name()}`', expr.pos())
@@ -1678,10 +1679,8 @@ fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type {
16781679
c.inside_selector_expr = true
16791680
mut typ := c.expr(mut node.expr)
16801681
if node.expr.is_auto_deref_var() {
1681-
if mut node.expr is ast.Ident {
1682-
if mut node.expr.obj is ast.Var {
1683-
typ = node.expr.obj.typ
1684-
}
1682+
if mut node.expr is ast.Ident && node.expr.obj is ast.Var {
1683+
typ = node.expr.obj.typ
16851684
}
16861685
}
16871686
c.inside_selector_expr = old_selector_expr
@@ -4417,10 +4416,8 @@ fn (mut c Checker) smartcast(mut expr ast.Expr, cur_type ast.Type, to_type_ ast.
44174416
}
44184417
}
44194418
mut expr_str := expr.expr.str()
4420-
if mut expr.expr is ast.ParExpr {
4421-
if mut expr.expr.expr is ast.AsCast {
4422-
expr_str = expr.expr.expr.expr.str()
4423-
}
4419+
if mut expr.expr is ast.ParExpr && expr.expr.expr is ast.AsCast {
4420+
expr_str = expr.expr.expr.expr.str()
44244421
}
44254422
field := scope.find_struct_field(expr_str, expr.expr_type, expr.field_name)
44264423
if field != unsafe { nil } {
@@ -5058,15 +5055,12 @@ fn (mut c Checker) index_expr(mut node ast.IndexExpr) ast.Type {
50585055
|| typ.is_pointer() {
50595056
mut is_ok := false
50605057
mut is_mut_struct := false
5061-
if mut node.left is ast.Ident {
5062-
if mut node.left.obj is ast.Var {
5063-
// `mut param []T` function parameter
5064-
is_ok = node.left.obj.is_mut && node.left.obj.is_arg && !typ.deref().is_ptr()
5065-
&& typ_sym.kind != .struct
5066-
// `mut param Struct`
5067-
is_mut_struct = node.left.obj.is_mut && node.left.obj.is_arg
5068-
&& typ_sym.kind == .struct
5069-
}
5058+
if mut node.left is ast.Ident && node.left.obj is ast.Var {
5059+
// `mut param []T` function parameter
5060+
is_ok = node.left.obj.is_mut && node.left.obj.is_arg && !typ.deref().is_ptr()
5061+
&& typ_sym.kind != .struct
5062+
// `mut param Struct`
5063+
is_mut_struct = node.left.obj.is_mut && node.left.obj.is_arg && typ_sym.kind == .struct
50705064
}
50715065
if !is_ok && node.index is ast.RangeExpr {
50725066
s := c.table.type_to_str(typ)

‎vlib/v/checker/containers.v‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -799,10 +799,8 @@ fn (mut c Checker) check_append(mut node ast.InfixExpr, left_type ast.Type, righ
799799
right := node.right
800800
if right is ast.PrefixExpr && right.op == .amp {
801801
mut expr2 := right.right
802-
if mut expr2 is ast.Ident {
803-
if !node.left.is_blank_ident() && expr2.obj is ast.ConstField {
804-
c.error('cannot have mutable reference to const `${expr2.name}`', expr2.pos)
805-
}
802+
if mut expr2 is ast.Ident && !node.left.is_blank_ident() && expr2.obj is ast.ConstField {
803+
c.error('cannot have mutable reference to const `${expr2.name}`', expr2.pos)
806804
}
807805
}
808806
if left_value_sym.kind == .interface {

‎vlib/v/checker/fn.v‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,10 +1520,8 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
15201520
}
15211521
node.args[i].typ = arg_typ
15221522
if c.comptime.comptime_for_field_var != '' {
1523-
if mut call_arg.expr is ast.Ident {
1524-
if mut call_arg.expr.obj is ast.Var {
1525-
node.args[i].typ = call_arg.expr.obj.typ
1526-
}
1523+
if mut call_arg.expr is ast.Ident && call_arg.expr.obj is ast.Var {
1524+
node.args[i].typ = call_arg.expr.obj.typ
15271525
}
15281526
}
15291527
arg_typ_sym := c.table.sym(arg_typ)

‎vlib/v/checker/for.v‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,11 @@ fn (mut c Checker) for_stmt(mut node ast.ForStmt) {
318318
c.error('non-bool used as for condition', node.pos)
319319
}
320320
}
321-
if mut node.cond is ast.InfixExpr {
322-
if node.cond.op == .key_is {
323-
if node.cond.right is ast.TypeNode && node.cond.left in [ast.Ident, ast.SelectorExpr] {
324-
if c.table.type_kind(node.cond.left_type) in [.sum_type, .interface] {
325-
c.smartcast(mut node.cond.left, node.cond.left_type, node.cond.right_type, mut
326-
node.scope, false, false)
327-
}
321+
if mut node.cond is ast.InfixExpr && node.cond.op == .key_is {
322+
if node.cond.right is ast.TypeNode && node.cond.left in [ast.Ident, ast.SelectorExpr] {
323+
if c.table.type_kind(node.cond.left_type) in [.sum_type, .interface] {
324+
c.smartcast(mut node.cond.left, node.cond.left_type, node.cond.right_type, mut
325+
node.scope, false, false)
328326
}
329327
}
330328
}

‎vlib/v/checker/match.v‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,8 @@ fn (mut c Checker) get_comptime_number_value(mut expr ast.Expr) ?i64 {
308308
if mut expr is ast.IntegerLiteral {
309309
return expr.val.i64()
310310
}
311-
if mut expr is ast.CastExpr {
312-
if mut expr.expr is ast.IntegerLiteral {
313-
return expr.expr.val.i64()
314-
}
311+
if mut expr is ast.CastExpr && expr.expr is ast.IntegerLiteral {
312+
return expr.expr.val.i64()
315313
}
316314
if mut expr is ast.Ident {
317315
if mut obj := c.table.global_scope.find_const(expr.full_name()) {

‎vlib/v/checker/return.v‎

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,8 @@ fn (mut c Checker) return_stmt(mut node ast.Return) {
8282
return
8383
}
8484
// Handle `return unsafe { none }`
85-
if mut expr is ast.UnsafeExpr {
86-
if mut expr.expr is ast.None {
87-
c.error('cannot return `none` in unsafe block', expr.expr.pos)
88-
}
85+
if mut expr is ast.UnsafeExpr && expr.expr is ast.None {
86+
c.error('cannot return `none` in unsafe block', expr.expr.pos)
8987
}
9088
if typ == ast.void_type {
9189
c.error('`${expr}` used as value', node.pos)
@@ -102,18 +100,16 @@ fn (mut c Checker) return_stmt(mut node ast.Return) {
102100
expr_idxs << i
103101
}
104102
} else {
105-
if mut expr is ast.Ident {
106-
if mut expr.obj is ast.Var {
107-
if expr.obj.smartcasts.len > 0 {
108-
typ = c.unwrap_generic(expr.obj.smartcasts.last())
109-
}
110-
if expr.obj.ct_type_var != .no_comptime {
111-
typ = c.type_resolver.get_type_or_default(expr, typ)
112-
}
113-
if mut expr.obj.expr is ast.IfGuardExpr {
114-
if var := expr.scope.find_var(expr.name) {
115-
typ = var.typ
116-
}
103+
if mut expr is ast.Ident && expr.obj is ast.Var {
104+
if expr.obj.smartcasts.len > 0 {
105+
typ = c.unwrap_generic(expr.obj.smartcasts.last())
106+
}
107+
if expr.obj.ct_type_var != .no_comptime {
108+
typ = c.type_resolver.get_type_or_default(expr, typ)
109+
}
110+
if mut expr.obj.expr is ast.IfGuardExpr {
111+
if var := expr.scope.find_var(expr.name) {
112+
typ = var.typ
117113
}
118114
}
119115
}

‎vlib/v/gen/c/assert.v‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,13 @@ fn (mut g Gen) gen_assert_single_expr(expr ast.Expr, typ ast.Type) {
231231
&& expr in [ast.IndexExpr, ast.CallExpr, ast.StringLiteral, ast.StringInterLiteral] {
232232
should_clone = false
233233
}
234-
if expr is ast.CTempVar {
235-
if expr.orig is ast.CallExpr {
236-
should_clone = false
237-
if expr.orig.or_block.kind == .propagate_option {
238-
should_clone = true
239-
}
240-
if expr.orig.is_method && expr.orig.args.len == 0
241-
&& expr.orig.name == 'type_name' {
242-
should_clone = true
243-
}
234+
if expr is ast.CTempVar && expr.orig is ast.CallExpr {
235+
should_clone = false
236+
if expr.orig.or_block.kind == .propagate_option {
237+
should_clone = true
238+
}
239+
if expr.orig.is_method && expr.orig.args.len == 0 && expr.orig.name == 'type_name' {
240+
should_clone = true
244241
}
245242
}
246243
if should_clone {

‎vlib/v/gen/c/dumpexpr.v‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,11 @@ fn (mut g Gen) dump_expr(node ast.DumpExpr) {
3434
}
3535
// var.$(field.name)
3636
if node.expr is ast.ComptimeSelector && node.expr.is_name {
37-
if node.expr.field_expr is ast.SelectorExpr {
38-
if node.expr.field_expr.expr is ast.Ident {
39-
if node.expr.field_expr.expr.name == g.comptime.comptime_for_field_var {
40-
field, _ := g.type_resolver.get_comptime_selector_var_type(node.expr)
41-
name = g.styp(g.unwrap_generic(field.typ.clear_flags(.shared_f, .result)))
42-
expr_type = field.typ
43-
}
37+
if node.expr.field_expr is ast.SelectorExpr && node.expr.field_expr.expr is ast.Ident {
38+
if node.expr.field_expr.expr.name == g.comptime.comptime_for_field_var {
39+
field, _ := g.type_resolver.get_comptime_selector_var_type(node.expr)
40+
name = g.styp(g.unwrap_generic(field.typ.clear_flags(.shared_f, .result)))
41+
expr_type = field.typ
4442
}
4543
}
4644
} else if node.expr is ast.Ident && node.expr.ct_expr {

0 commit comments

Comments
 (0)