From 97684e8edcdb9184f6d28bacd779ac088cf0c096 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 25 Oct 2025 12:58:56 -0300 Subject: [PATCH] checker: fix if branch type nr_muls mismatch (fix #25556) (fix #25555) (#25571) --- vlib/v/checker/if.v | 6 ++++ vlib/v/checker/tests/if_mismatch_muls_err.out | 7 +++++ vlib/v/checker/tests/if_mismatch_muls_err.vv | 28 +++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 vlib/v/checker/tests/if_mismatch_muls_err.out create mode 100644 vlib/v/checker/tests/if_mismatch_muls_err.vv diff --git a/vlib/v/checker/if.v b/vlib/v/checker/if.v index c9b31dc73..b61e0b1d9 100644 --- a/vlib/v/checker/if.v +++ b/vlib/v/checker/if.v @@ -396,6 +396,12 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { c.error('mismatched types `${c.table.type_to_str(node.typ)}` and `${c.table.type_to_str(stmt.typ)}`', node.pos) } else { + if !node.typ.has_option_or_result() && !node.typ.has_flag(.shared_f) + && stmt.typ != ast.voidptr_type + && stmt.typ.nr_muls() != node.typ.nr_muls() { + c.error('mismatched types `${c.table.type_to_str(node.typ)}` and `${c.table.type_to_str(stmt.typ)}`', + node.pos) + } if node.is_expr == false && c.type_resolver.is_generic_param_var(stmt.expr) { // generic variable no yet type bounded node.is_expr = true diff --git a/vlib/v/checker/tests/if_mismatch_muls_err.out b/vlib/v/checker/tests/if_mismatch_muls_err.out new file mode 100644 index 000000000..a07132c7a --- /dev/null +++ b/vlib/v/checker/tests/if_mismatch_muls_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/if_mismatch_muls_err.vv:17:11: error: mismatched types `&&Dum` and `&Dum` + 15 | + 16 | fn main() { + 17 | dummy := if false { + | ~~ + 18 | res := &Dum{ + 19 | v: 2 diff --git a/vlib/v/checker/tests/if_mismatch_muls_err.vv b/vlib/v/checker/tests/if_mismatch_muls_err.vv new file mode 100644 index 000000000..21f1d4529 --- /dev/null +++ b/vlib/v/checker/tests/if_mismatch_muls_err.vv @@ -0,0 +1,28 @@ +module main + +struct Dummy { + v int +} + +fn get_dummy() &Dum { + d := Dum{ + v: 1 + } + return &d +} + +type Dum = Dummy + +fn main() { + dummy := if false { + res := &Dum{ + v: 2 + } + &res + } else { + r := get_dummy() + r + } + + println(dummy) +} -- 2.39.5