Skip to content

Commit 0028e55

Browse files
authored
checker: cleanup smartcast in checker.v (#13618)
1 parent 9a2df0d commit 0028e55

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

vlib/v/checker/checker.v

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,14 +1070,14 @@ fn (mut c Checker) fail_if_immutable(expr ast.Expr) (string, token.Pos) {
10701070
return '', pos
10711071
}
10721072
ast.Ident {
1073-
if expr.obj is ast.Var {
1074-
mut v := expr.obj as ast.Var
1075-
if !v.is_mut && !c.pref.translated && !c.file.is_translated && !c.inside_unsafe {
1073+
if mut expr.obj is ast.Var {
1074+
if !expr.obj.is_mut && !c.pref.translated && !c.file.is_translated
1075+
&& !c.inside_unsafe {
10761076
c.error('`$expr.name` is immutable, declare it with `mut` to make it mutable',
10771077
expr.pos)
10781078
}
1079-
v.is_changed = true
1080-
if v.typ.share() == .shared_t {
1079+
expr.obj.is_changed = true
1080+
if expr.obj.typ.share() == .shared_t {
10811081
if expr.name !in c.locked_names {
10821082
if c.locked_names.len > 0 || c.rlocked_names.len > 0 {
10831083
if expr.name in c.rlocked_names {
@@ -1764,11 +1764,8 @@ pub fn (mut c Checker) enum_decl(mut node ast.EnumDecl) {
17641764
}
17651765
// ast.ParExpr {} // TODO allow `.x = (1+2)`
17661766
else {
1767-
if field.expr is ast.Ident {
1768-
x := field.expr as ast.Ident
1769-
// TODO sum type bug, remove temp var
1770-
// if field.expr.language == .c {
1771-
if x.language == .c {
1767+
if mut field.expr is ast.Ident {
1768+
if field.expr.language == .c {
17721769
continue
17731770
}
17741771
}
@@ -1856,21 +1853,19 @@ fn (mut c Checker) stmt(node ast.Stmt) {
18561853
}
18571854
for i, ident in node.defer_vars {
18581855
mut id := ident
1859-
if id.info is ast.IdentVar {
1856+
if mut id.info is ast.IdentVar {
18601857
if id.comptime && id.name in checker.valid_comptime_not_user_defined {
18611858
node.defer_vars[i] = ast.Ident{
18621859
scope: 0
18631860
name: ''
18641861
}
18651862
continue
18661863
}
1867-
mut info := id.info as ast.IdentVar
18681864
typ := c.ident(mut id)
18691865
if typ == ast.error_type_idx {
18701866
continue
18711867
}
1872-
info.typ = typ
1873-
id.info = info
1868+
id.info.typ = typ
18741869
node.defer_vars[i] = id
18751870
}
18761871
}
@@ -2886,9 +2881,9 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
28862881

28872882
// checks on int literal to enum cast if the value represents a value on the enum
28882883
if to_sym.kind == .enum_ {
2889-
if node.expr is ast.IntegerLiteral {
2884+
if mut node.expr is ast.IntegerLiteral {
28902885
enum_typ_name := c.table.get_type_name(to_type)
2891-
node_val := (node.expr as ast.IntegerLiteral).val.int()
2886+
node_val := node.expr.val.int()
28922887

28932888
if enum_decl := c.table.enum_decls[to_sym.name] {
28942889
mut in_range := false
@@ -3414,8 +3409,8 @@ fn (mut c Checker) find_obj_definition(obj ast.ScopeObject) ?ast.Expr {
34143409
} else {
34153410
return error('`$name` is a global variable and is unknown at compile time')
34163411
}
3417-
if expr is ast.Ident {
3418-
return c.find_definition(expr as ast.Ident) // TODO: smartcast
3412+
if mut expr is ast.Ident {
3413+
return c.find_definition(expr)
34193414
}
34203415
if !expr.is_lit() {
34213416
return error('definition of `$name` is unknown at compile time')
@@ -3706,10 +3701,9 @@ pub fn (mut c Checker) index_expr(mut node ast.IndexExpr) ast.Type {
37063701
&& !node.left.is_auto_deref_var()) || typ.is_pointer()) {
37073702
mut is_ok := false
37083703
if mut node.left is ast.Ident {
3709-
if node.left.obj is ast.Var {
3710-
v := node.left.obj as ast.Var
3704+
if mut node.left.obj is ast.Var {
37113705
// `mut param []T` function parameter
3712-
is_ok = v.is_mut && v.is_arg && !typ.deref().is_ptr()
3706+
is_ok = node.left.obj.is_mut && node.left.obj.is_arg && !typ.deref().is_ptr()
37133707
}
37143708
}
37153709
if !is_ok && !c.pref.translated && !c.file.is_translated {

0 commit comments

Comments
 (0)