@@ -2348,35 +2348,7 @@ fn (mut c Checker) stmt(mut node ast.Stmt) {
23482348 c.inside_const = false
23492349 }
23502350 ast.DeferStmt {
2351- c.inside_defer = true
2352- if node.idx_in_fn < 0 && c.table.cur_fn != unsafe { nil } {
2353- node.idx_in_fn = c.table.cur_fn.defer_stmts.len
2354- c.table.cur_fn.defer_stmts << unsafe { & node }
2355- }
2356- if c.locked_names.len != 0 || c.rlocked_names.len != 0 {
2357- c.error ('defers are not allowed in lock statements' , node.pos)
2358- }
2359- for i, ident in node.defer_vars {
2360- mut id := ident
2361- if mut id.info is ast.IdentVar {
2362- if id.comptime && (id.tok_kind == .question
2363- || id.name in ast.valid_comptime_not_user_defined) {
2364- node.defer_vars[i] = ast.Ident{
2365- scope: unsafe { nil }
2366- name: ''
2367- }
2368- continue
2369- }
2370- typ := c.ident (mut id)
2371- if typ == ast.error_type_idx {
2372- continue
2373- }
2374- id.info.typ = typ
2375- node.defer_vars[i] = id
2376- }
2377- }
2378- c.stmts (mut node.stmts)
2379- c.inside_defer = false
2351+ c.defer_stmt (mut node)
23802352 }
23812353 ast.EnumDecl {
23822354 c.enum_decl (mut node)
@@ -2449,6 +2421,44 @@ fn (mut c Checker) stmt(mut node ast.Stmt) {
24492421 }
24502422}
24512423
2424+ fn (mut c Checker) defer_stmt (mut node ast.DeferStmt) {
2425+ c.inside_defer = true
2426+ if node.idx_in_fn < 0 && c.table.cur_fn != unsafe { nil } {
2427+ node.idx_in_fn = c.table.cur_fn.defer_stmts.len
2428+ c.table.cur_fn.defer_stmts << unsafe { & node }
2429+ }
2430+ if node.mode == .function {
2431+ if ! isnil (c.fn_scope) && node.scope == c.fn_scope {
2432+ c.warn ('`defer` is already in function scope; just use `defer {` instead' ,
2433+ node.pos)
2434+ }
2435+ if c.locked_names.len != 0 || c.rlocked_names.len != 0 {
2436+ c.error ('`defer(fn)`s are not allowed in lock statements' , node.pos)
2437+ }
2438+ }
2439+ for i, ident in node.defer_vars {
2440+ mut id := ident
2441+ if mut id.info is ast.IdentVar {
2442+ if id.comptime
2443+ && (id.tok_kind == .question || id.name in ast.valid_comptime_not_user_defined) {
2444+ node.defer_vars[i] = ast.Ident{
2445+ scope: unsafe { nil }
2446+ name: ''
2447+ }
2448+ continue
2449+ }
2450+ typ := c.ident (mut id)
2451+ if typ == ast.error_type_idx {
2452+ continue
2453+ }
2454+ id.info.typ = typ
2455+ node.defer_vars[i] = id
2456+ }
2457+ }
2458+ c.stmts (mut node.stmts)
2459+ c.inside_defer = false
2460+ }
2461+
24522462fn (mut c Checker) assert_stmt (mut node ast.AssertStmt) {
24532463 cur_exp_typ := c.expected_type
24542464 c.expected_type = ast.bool_type
0 commit comments