Skip to content

Commit c1550b3

Browse files
authored
ast, checker: change stmt(node_ ast.Stmt) to stmt(mut node ast.Stmt) (#18756)
1 parent ab258ae commit c1550b3

File tree

9 files changed

+68
-66
lines changed

9 files changed

+68
-66
lines changed

vlib/v/ast/ast.v

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,10 @@ pub const (
176176
// `{stmts}` or `unsafe {stmts}`
177177
pub struct Block {
178178
pub:
179-
stmts []Stmt
180179
is_unsafe bool
181180
pos token.Pos
181+
pub mut:
182+
stmts []Stmt
182183
}
183184

184185
// | IncDecStmt k
@@ -1166,12 +1167,12 @@ pub enum ComptimeForKind {
11661167
pub struct ComptimeFor {
11671168
pub:
11681169
val_var string
1169-
stmts []Stmt
11701170
kind ComptimeForKind
11711171
pos token.Pos
11721172
typ_pos token.Pos
11731173
pub mut:
1174-
typ Type
1174+
stmts []Stmt
1175+
typ Type
11751176
}
11761177

11771178
pub struct ForStmt {
@@ -1193,7 +1194,6 @@ pub:
11931194
val_var string
11941195
is_range bool
11951196
high Expr // `10` in `for i in 0..10 {`
1196-
stmts []Stmt
11971197
pos token.Pos
11981198
comments []Comment
11991199
val_is_mut bool // `for mut val in vals {` means that modifying `val` will modify the array
@@ -1208,6 +1208,7 @@ pub mut:
12081208
kind Kind // array/map/string
12091209
label string // `label: for {`
12101210
scope &Scope = unsafe { nil }
1211+
stmts []Stmt
12111212
}
12121213

12131214
pub struct ForCStmt {
@@ -1362,9 +1363,9 @@ pub:
13621363
[minify]
13631364
pub struct DeferStmt {
13641365
pub:
1365-
stmts []Stmt
1366-
pos token.Pos
1366+
pos token.Pos
13671367
pub mut:
1368+
stmts []Stmt
13681369
defer_vars []Ident
13691370
ifdef string
13701371
idx_in_fn int = -1 // index in FnDecl.defer_stmts
@@ -1717,9 +1718,10 @@ pub enum OrKind {
17171718
// `or { ... }`
17181719
pub struct OrExpr {
17191720
pub:
1721+
kind OrKind
1722+
pos token.Pos
1723+
pub mut:
17201724
stmts []Stmt
1721-
kind OrKind
1722-
pos token.Pos
17231725
}
17241726

17251727
/*
@@ -1854,7 +1856,6 @@ pub:
18541856
is_env bool
18551857
env_pos token.Pos
18561858
is_pkgconfig bool
1857-
or_block OrExpr
18581859
pub mut:
18591860
vweb_tmpl File
18601861
left_type Type
@@ -1863,6 +1864,7 @@ pub mut:
18631864
args_var string
18641865
args []CallArg
18651866
embed_file EmbeddedFile
1867+
or_block OrExpr
18661868
}
18671869

18681870
pub struct None {
@@ -1916,7 +1918,6 @@ pub:
19161918
is_array bool
19171919
// is_generated indicates a statement is generated by ORM for complex queries with related tables.
19181920
is_generated bool
1919-
or_expr OrExpr
19201921
pos token.Pos
19211922
pub mut:
19221923
typ Type
@@ -1928,6 +1929,7 @@ pub mut:
19281929
table_expr TypeNode
19291930
fields []StructField
19301931
sub_structs map[int]SqlExpr
1932+
or_expr OrExpr
19311933
}
19321934

19331935
pub struct NodeError {

vlib/v/checker/checker.v

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ pub fn (mut c Checker) check(mut ast_file ast.File) {
208208
for mut stmt in ast_file.stmts {
209209
if stmt in [ast.ConstDecl, ast.ExprStmt] {
210210
c.expr_level = 0
211-
c.stmt(stmt)
211+
c.stmt(mut stmt)
212212
}
213213
if c.should_abort {
214214
return
@@ -219,7 +219,7 @@ pub fn (mut c Checker) check(mut ast_file ast.File) {
219219
for mut stmt in ast_file.stmts {
220220
if stmt is ast.GlobalDecl {
221221
c.expr_level = 0
222-
c.stmt(stmt)
222+
c.stmt(mut stmt)
223223
}
224224
if c.should_abort {
225225
return
@@ -230,7 +230,7 @@ pub fn (mut c Checker) check(mut ast_file ast.File) {
230230
for mut stmt in ast_file.stmts {
231231
if stmt !in [ast.ConstDecl, ast.GlobalDecl, ast.ExprStmt] {
232232
c.expr_level = 0
233-
c.stmt(stmt)
233+
c.stmt(mut stmt)
234234
}
235235
if c.should_abort {
236236
return
@@ -267,8 +267,8 @@ pub fn (mut c Checker) check_scope_vars(sc &ast.Scope) {
267267
// not used right now
268268
pub fn (mut c Checker) check2(mut ast_file ast.File) []errors.Error {
269269
c.change_current_file(ast_file)
270-
for stmt in ast_file.stmts {
271-
c.stmt(stmt)
270+
for mut stmt in ast_file.stmts {
271+
c.stmt(mut stmt)
272272
}
273273
return c.errors
274274
}
@@ -1491,7 +1491,7 @@ fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type {
14911491
node.typ = field.typ
14921492
if node.or_block.kind == .block {
14931493
c.expected_or_type = node.typ.clear_flags(.option, .result)
1494-
c.stmts_ending_with_expression(node.or_block.stmts)
1494+
c.stmts_ending_with_expression(mut node.or_block.stmts)
14951495
c.check_or_expr(node.or_block, node.typ, c.expected_or_type, node)
14961496
c.expected_or_type = ast.void_type
14971497
}
@@ -1833,8 +1833,7 @@ fn (mut c Checker) check_loop_label(label string, pos token.Pos) {
18331833
c.loop_label = label
18341834
}
18351835

1836-
fn (mut c Checker) stmt(node_ ast.Stmt) {
1837-
mut node := unsafe { node_ }
1836+
fn (mut c Checker) stmt(mut node ast.Stmt) {
18381837
$if trace_checker ? {
18391838
ntype := typeof(node).replace('v.ast.', '')
18401839
eprintln('checking: ${c.file.path:-30} | pos: ${node.pos.line_str():-39} | node: ${ntype} | ${node}')
@@ -1858,13 +1857,13 @@ fn (mut c Checker) stmt(node_ ast.Stmt) {
18581857
c.assign_stmt(mut node)
18591858
}
18601859
ast.Block {
1861-
c.block(node)
1860+
c.block(mut node)
18621861
}
18631862
ast.BranchStmt {
18641863
c.branch_stmt(node)
18651864
}
18661865
ast.ComptimeFor {
1867-
c.comptime_for(node)
1866+
c.comptime_for(mut node)
18681867
}
18691868
ast.ConstDecl {
18701869
c.inside_const = true
@@ -1898,7 +1897,7 @@ fn (mut c Checker) stmt(node_ ast.Stmt) {
18981897
}
18991898
}
19001899
c.inside_defer = true
1901-
c.stmts(node.stmts)
1900+
c.stmts(mut node.stmts)
19021901
c.inside_defer = false
19031902
}
19041903
ast.EnumDecl {
@@ -1941,7 +1940,7 @@ fn (mut c Checker) stmt(node_ ast.Stmt) {
19411940
c.fn_decl(mut node)
19421941
}
19431942
ast.ForCStmt {
1944-
c.for_c_stmt(node)
1943+
c.for_c_stmt(mut node)
19451944
}
19461945
ast.ForInStmt {
19471946
c.for_in_stmt(mut node)
@@ -2011,14 +2010,14 @@ fn (mut c Checker) assert_stmt(node ast.AssertStmt) {
20112010
c.expected_type = cur_exp_typ
20122011
}
20132012

2014-
fn (mut c Checker) block(node ast.Block) {
2013+
fn (mut c Checker) block(mut node ast.Block) {
20152014
if node.is_unsafe {
20162015
prev_unsafe := c.inside_unsafe
20172016
c.inside_unsafe = true
2018-
c.stmts(node.stmts)
2017+
c.stmts(mut node.stmts)
20192018
c.inside_unsafe = prev_unsafe
20202019
} else {
2021-
c.stmts(node.stmts)
2020+
c.stmts(mut node.stmts)
20222021
}
20232022
}
20242023

@@ -2366,10 +2365,10 @@ fn (mut c Checker) import_stmt(node ast.Import) {
23662365
}
23672366

23682367
// stmts should be used for processing normal statement lists (fn bodies, for loop bodies etc).
2369-
fn (mut c Checker) stmts(stmts []ast.Stmt) {
2368+
fn (mut c Checker) stmts(mut stmts []ast.Stmt) {
23702369
old_stmt_level := c.stmt_level
23712370
c.stmt_level = 0
2372-
c.stmts_ending_with_expression(stmts)
2371+
c.stmts_ending_with_expression(mut stmts)
23732372
c.stmt_level = old_stmt_level
23742373
}
23752374

@@ -2378,7 +2377,7 @@ fn (mut c Checker) stmts(stmts []ast.Stmt) {
23782377
// `x := opt() or { stmt1 stmt2 ExprStmt }`,
23792378
// `x := if cond { stmt1 stmt2 ExprStmt } else { stmt2 stmt3 ExprStmt }`,
23802379
// `x := match expr { Type1 { stmt1 stmt2 ExprStmt } else { stmt2 stmt3 ExprStmt }`.
2381-
fn (mut c Checker) stmts_ending_with_expression(stmts []ast.Stmt) {
2380+
fn (mut c Checker) stmts_ending_with_expression(mut stmts []ast.Stmt) {
23822381
if stmts.len == 0 {
23832382
c.scope_returns = false
23842383
return
@@ -2392,14 +2391,14 @@ fn (mut c Checker) stmts_ending_with_expression(stmts []ast.Stmt) {
23922391
line_nr: -1
23932392
}
23942393
c.stmt_level++
2395-
for i, stmt in stmts {
2394+
for i, mut stmt in stmts {
23962395
c.is_last_stmt = i == stmts.len - 1
23972396
if c.scope_returns {
23982397
if unreachable.line_nr == -1 {
23992398
unreachable = stmt.pos
24002399
}
24012400
}
2402-
c.stmt(stmt)
2401+
c.stmt(mut stmt)
24032402
if stmt is ast.GotoLabel {
24042403
unreachable = token.Pos{
24052404
line_nr: -1
@@ -3297,7 +3296,7 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
32973296
}
32983297
unwrapped_typ := typ.clear_flags(.option, .result)
32993298
c.expected_or_type = unwrapped_typ
3300-
c.stmts_ending_with_expression(node.or_expr.stmts)
3299+
c.stmts_ending_with_expression(mut node.or_expr.stmts)
33013300
c.check_or_expr(node.or_expr, typ, c.expected_or_type, node)
33023301
return unwrapped_typ
33033302
}
@@ -3402,7 +3401,7 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
34023401
}
34033402
unwrapped_typ := typ.clear_flags(.option, .result)
34043403
c.expected_or_type = unwrapped_typ
3405-
c.stmts_ending_with_expression(node.or_expr.stmts)
3404+
c.stmts_ending_with_expression(mut node.or_expr.stmts)
34063405
c.check_or_expr(node.or_expr, typ, c.expected_or_type, node)
34073406
return unwrapped_typ
34083407
}
@@ -3655,9 +3654,9 @@ fn (mut c Checker) smartcast(expr_ ast.Expr, cur_type ast.Type, to_type_ ast.Typ
36553654
fn (mut c Checker) select_expr(mut node ast.SelectExpr) ast.Type {
36563655
node.is_expr = c.expected_type != ast.void_type
36573656
node.expected_type = c.expected_type
3658-
for branch in node.branches {
3659-
c.stmt(branch.stmt)
3660-
match branch.stmt {
3657+
for mut branch in node.branches {
3658+
c.stmt(mut branch.stmt)
3659+
match mut branch.stmt {
36613660
ast.ExprStmt {
36623661
if branch.is_timeout {
36633662
if !branch.stmt.typ.is_int() {
@@ -3666,7 +3665,7 @@ fn (mut c Checker) select_expr(mut node ast.SelectExpr) ast.Type {
36663665
branch.stmt.pos)
36673666
}
36683667
} else {
3669-
if branch.stmt.expr is ast.InfixExpr {
3668+
if mut branch.stmt.expr is ast.InfixExpr {
36703669
if branch.stmt.expr.left !in [ast.Ident, ast.SelectorExpr, ast.IndexExpr] {
36713670
c.error('channel in `select` key must be predefined', branch.stmt.expr.left.pos())
36723671
}
@@ -3708,7 +3707,7 @@ fn (mut c Checker) select_expr(mut node ast.SelectExpr) ast.Type {
37083707
}
37093708
}
37103709
}
3711-
c.stmts(branch.stmts)
3710+
c.stmts(mut branch.stmts)
37123711
}
37133712
return ast.bool_type
37143713
}
@@ -3737,7 +3736,7 @@ fn (mut c Checker) lock_expr(mut node ast.LockExpr) ast.Type {
37373736
c.locked_names << id_name
37383737
}
37393738
}
3740-
c.stmts(node.stmts)
3739+
c.stmts(mut node.stmts)
37413740
// handle `x := rlock a { a.getval() }`
37423741
mut ret_type := ast.void_type
37433742
if node.stmts.len > 0 {
@@ -4026,7 +4025,7 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type {
40264025
}
40274026
if node.op == .arrow {
40284027
if right_sym.kind == .chan {
4029-
c.stmts_ending_with_expression(node.or_block.stmts)
4028+
c.stmts_ending_with_expression(mut node.or_block.stmts)
40304029
return right_sym.chan_info().elem_type
40314030
}
40324031
c.type_error_for_operator('<-', '`chan`', right_sym.name, node.pos)
@@ -4209,7 +4208,7 @@ fn (mut c Checker) index_expr(mut node ast.IndexExpr) ast.Type {
42094208
if node.or_expr.stmts.len > 0 && node.or_expr.stmts.last() is ast.ExprStmt {
42104209
c.expected_or_type = typ
42114210
}
4212-
c.stmts_ending_with_expression(node.or_expr.stmts)
4211+
c.stmts_ending_with_expression(mut node.or_expr.stmts)
42134212
c.check_expr_opt_call(node, typ)
42144213
return typ
42154214
}

vlib/v/checker/comptime.v

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ fn (mut c Checker) comptime_call(mut node ast.ComptimeCall) ast.Type {
149149
// check each arg expression
150150
node.args[i].typ = c.expr(arg.expr)
151151
}
152-
c.stmts_ending_with_expression(node.or_block.stmts)
152+
c.stmts_ending_with_expression(mut node.or_block.stmts)
153153
// assume string for now
154154
return ast.string_type
155155
}
@@ -246,7 +246,7 @@ fn (mut c Checker) comptime_selector(mut node ast.ComptimeSelector) ast.Type {
246246
return ast.void_type
247247
}
248248

249-
fn (mut c Checker) comptime_for(node ast.ComptimeFor) {
249+
fn (mut c Checker) comptime_for(mut node ast.ComptimeFor) {
250250
typ := c.unwrap_generic(node.typ)
251251
sym := c.table.final_sym(typ)
252252
if sym.kind == .placeholder || typ.has_flag(.generic) {
@@ -274,7 +274,7 @@ fn (mut c Checker) comptime_for(node ast.ComptimeFor) {
274274
c.comptime_for_field_var = node.val_var
275275
c.comptime_fields_type[node.val_var] = node.typ
276276
c.comptime_fields_default_type = field.typ
277-
c.stmts(node.stmts)
277+
c.stmts(mut node.stmts)
278278

279279
unwrapped_expr_type := c.unwrap_generic(field.typ)
280280
tsym := c.table.sym(unwrapped_expr_type)
@@ -294,11 +294,11 @@ fn (mut c Checker) comptime_for(node ast.ComptimeFor) {
294294
c.comptime_enum_field_value = field
295295
c.comptime_for_field_var = node.val_var
296296
c.comptime_fields_type[node.val_var] = node.typ
297-
c.stmts(node.stmts)
297+
c.stmts(mut node.stmts)
298298
}
299299
}
300300
} else {
301-
c.stmts(node.stmts)
301+
c.stmts(mut node.stmts)
302302
}
303303
}
304304

vlib/v/checker/fn.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
379379
}
380380
}
381381
c.fn_scope = node.scope
382-
c.stmts(node.stmts)
382+
c.stmts(mut node.stmts)
383383
node_has_top_return := has_top_return(node.stmts)
384384
node.has_return = c.returns || node_has_top_return
385385
c.check_noreturn_fn_decl(mut node)
@@ -463,7 +463,7 @@ fn (mut c Checker) anon_fn(mut node ast.AnonFn) ast.Type {
463463
c.error('generic closure fn must specify type parameter, e.g. fn [foo] [T]()',
464464
node.decl.pos)
465465
}
466-
c.stmts(node.decl.stmts)
466+
c.stmts(mut node.decl.stmts)
467467
c.fn_decl(mut node.decl)
468468
return node.typ
469469
}
@@ -514,7 +514,7 @@ fn (mut c Checker) call_expr(mut node ast.CallExpr) ast.Type {
514514
}
515515
}
516516
c.expected_or_type = node.return_type.clear_flag(.result)
517-
c.stmts_ending_with_expression(node.or_block.stmts)
517+
c.stmts_ending_with_expression(mut node.or_block.stmts)
518518
c.expected_or_type = ast.void_type
519519

520520
if !c.inside_const && c.table.cur_fn != unsafe { nil } && !c.table.cur_fn.is_main

0 commit comments

Comments
 (0)