@@ -44,6 +44,10 @@ fn (mut g Gen) gen_comptime_selector(expr ast.ComptimeSelector) string {
4444}
4545
4646fn (mut g Gen) comptime_call (mut node ast.ComptimeCall) {
47+ if node.kind == .compile_error || node.kind == .compile_warn {
48+ // handled by checker, this branch was not taken
49+ return
50+ }
4751 if node.kind == .embed_file {
4852 // $embed_file('/path/to/file')
4953 g.gen_embed_file_init (mut node)
@@ -368,12 +372,27 @@ fn (mut g Gen) gen_branch_context_string() string {
368372
369373fn (mut g Gen) comptime_if (node ast.IfExpr) {
370374 tmp_var := g.new_tmp_var ()
371- is_opt_or_result := node.typ.has_option_or_result ()
372- is_array_fixed := g.table.final_sym (node.typ).kind == .array_fixed
373- line := if node.is_expr {
375+ mut inferred_typ := node.typ
376+ if node.is_expr && node.typ == ast.void_type && node.branches.len > 0 {
377+ for branch in node.branches {
378+ if branch.stmts.len > 0 {
379+ last_stmt := branch.stmts.last ()
380+ if last_stmt is ast.ExprStmt {
381+ expr_typ := g.type_resolver.get_type_or_default (last_stmt.expr, last_stmt.typ)
382+ if expr_typ != ast.void_type && ! expr_typ.has_flag (.generic) {
383+ inferred_typ = expr_typ
384+ break
385+ }
386+ }
387+ }
388+ }
389+ }
390+ is_opt_or_result := inferred_typ.has_option_or_result ()
391+ is_array_fixed := g.table.final_sym (inferred_typ).kind == .array_fixed
392+ line := if node.is_expr && inferred_typ != ast.void_type {
374393 stmt_str := g.go_before_last_stmt ()
375394 g.write (util.tabs (g.indent))
376- styp := g.styp (node.typ )
395+ styp := g.styp (inferred_typ )
377396 g.writeln ('${styp } ${tmp_var };' )
378397 stmt_str
379398 } else {
@@ -446,9 +465,9 @@ fn (mut g Gen) comptime_if(node ast.IfExpr) {
446465 g.skip_stmt_pos = true
447466 if is_opt_or_result {
448467 tmp_var2 := g.new_tmp_var ()
449- g.write ('{ ${g .base_type (node . typ )} ${tmp_var2 } = ' )
468+ g.write ('{ ${g .base_type (inferred_typ )} ${tmp_var2 } = ' )
450469 g.stmt (last)
451- g.writeln ('builtin___result_ok(&(${g .base_type (node . typ )}[]) { ${tmp_var2 } }, (_result*)(&${tmp_var }), sizeof(${g .base_type (node . typ )}));' )
470+ g.writeln ('builtin___result_ok(&(${g .base_type (inferred_typ )}[]) { ${tmp_var2 } }, (_result*)(&${tmp_var }), sizeof(${g .base_type (inferred_typ )}));' )
452471 g.writeln ('}' )
453472 } else {
454473 g.write ('\t ${tmp_var } = ' )
@@ -465,14 +484,14 @@ fn (mut g Gen) comptime_if(node ast.IfExpr) {
465484 g.skip_stmt_pos = true
466485 if is_opt_or_result {
467486 tmp_var2 := g.new_tmp_var ()
468- base_styp := g.base_type (node.typ )
487+ base_styp := g.base_type (inferred_typ )
469488 g.write ('{ ${base_styp } ${tmp_var2 } = ' )
470489 g.stmt (last)
471490 g.writeln ('builtin___result_ok(&(${base_styp }[]) { ${tmp_var2 } }, (_result*)(&${tmp_var }), sizeof(${base_styp }));' )
472491 g.writeln ('}' )
473492 } else if is_array_fixed {
474493 tmp_var2 := g.new_tmp_var ()
475- base_styp := g.base_type (node.typ )
494+ base_styp := g.base_type (inferred_typ )
476495 g.write ('{ ${base_styp } ${tmp_var2 } = ' )
477496 g.stmt (last)
478497 if g.out.last_n (2 ).contains (';' ) {
@@ -976,11 +995,28 @@ fn (mut g Gen) comptime_selector_type(node ast.SelectorExpr) ast.Type {
976995
977996fn (mut g Gen) comptime_match (node ast.MatchExpr) {
978997 tmp_var := g.new_tmp_var ()
979- is_opt_or_result := node.return_type.has_option_or_result ()
980- line := if node.is_expr {
998+ mut inferred_typ := node.return_type
999+ if node.is_expr && (node.return_type == ast.void_type || node.return_type.idx () == 0 )
1000+ && node.branches.len > 0 {
1001+ for branch in node.branches {
1002+ if branch.stmts.len > 0 {
1003+ last_stmt := branch.stmts.last ()
1004+ if last_stmt is ast.ExprStmt {
1005+ expr_typ := g.type_resolver.get_type_or_default (last_stmt.expr, last_stmt.typ)
1006+ if expr_typ != ast.void_type && expr_typ.idx () != 0
1007+ && ! expr_typ.has_flag (.generic) {
1008+ inferred_typ = expr_typ
1009+ break
1010+ }
1011+ }
1012+ }
1013+ }
1014+ }
1015+ is_opt_or_result := inferred_typ.has_option_or_result ()
1016+ line := if node.is_expr && inferred_typ != ast.void_type && inferred_typ.idx () != 0 {
9811017 stmt_str := g.go_before_last_stmt ()
9821018 g.write (util.tabs (g.indent))
983- styp := g.styp (node.return_type )
1019+ styp := g.styp (inferred_typ )
9841020 g.writeln ('${styp } ${tmp_var };' )
9851021 stmt_str
9861022 } else {
@@ -1034,9 +1070,9 @@ fn (mut g Gen) comptime_match(node ast.MatchExpr) {
10341070 g.skip_stmt_pos = true
10351071 if is_opt_or_result {
10361072 tmp_var2 := g.new_tmp_var ()
1037- g.write ('{ ${g .base_type (node . return_type )} ${tmp_var2 } = ' )
1073+ g.write ('{ ${g .base_type (inferred_typ )} ${tmp_var2 } = ' )
10381074 g.stmt (last)
1039- g.writeln ('builtin___result_ok(&(${g .base_type (node . return_type )}[]) { ${tmp_var2 } }, (_result*)(&${tmp_var }), sizeof(${g .base_type (node . return_type )}));' )
1075+ g.writeln ('builtin___result_ok(&(${g .base_type (inferred_typ )}[]) { ${tmp_var2 } }, (_result*)(&${tmp_var }), sizeof(${g .base_type (inferred_typ )}));' )
10401076 g.writeln ('}' )
10411077 } else {
10421078 g.write ('\t ${tmp_var } = ' )
@@ -1054,9 +1090,9 @@ fn (mut g Gen) comptime_match(node ast.MatchExpr) {
10541090 g.skip_stmt_pos = true
10551091 if is_opt_or_result {
10561092 tmp_var2 := g.new_tmp_var ()
1057- g.write ('{ ${g .base_type (node . return_type )} ${tmp_var2 } = ' )
1093+ g.write ('{ ${g .base_type (inferred_typ )} ${tmp_var2 } = ' )
10581094 g.stmt (last)
1059- g.writeln ('builtin___result_ok(&(${g .base_type (node . return_type )}[]) { ${tmp_var2 } }, (_result*)(&${tmp_var }), sizeof(${g .base_type (node . return_type )}));' )
1095+ g.writeln ('builtin___result_ok(&(${g .base_type (inferred_typ )}[]) { ${tmp_var2 } }, (_result*)(&${tmp_var }), sizeof(${g .base_type (inferred_typ )}));' )
10601096 g.writeln ('}' )
10611097 } else {
10621098 g.write ('${tmp_var } = ' )
0 commit comments