Skip to content

Commit 97684e8

Browse files
authored
checker: fix if branch type nr_muls mismatch (fix #25556) (fix #25555) (#25571)
1 parent 92dd1a5 commit 97684e8

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

vlib/v/checker/if.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,12 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
396396
c.error('mismatched types `${c.table.type_to_str(node.typ)}` and `${c.table.type_to_str(stmt.typ)}`',
397397
node.pos)
398398
} else {
399+
if !node.typ.has_option_or_result() && !node.typ.has_flag(.shared_f)
400+
&& stmt.typ != ast.voidptr_type
401+
&& stmt.typ.nr_muls() != node.typ.nr_muls() {
402+
c.error('mismatched types `${c.table.type_to_str(node.typ)}` and `${c.table.type_to_str(stmt.typ)}`',
403+
node.pos)
404+
}
399405
if node.is_expr == false && c.type_resolver.is_generic_param_var(stmt.expr) {
400406
// generic variable no yet type bounded
401407
node.is_expr = true
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/checker/tests/if_mismatch_muls_err.vv:17:11: error: mismatched types `&&Dum` and `&Dum`
2+
15 |
3+
16 | fn main() {
4+
17 | dummy := if false {
5+
| ~~
6+
18 | res := &Dum{
7+
19 | v: 2
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module main
2+
3+
struct Dummy {
4+
v int
5+
}
6+
7+
fn get_dummy() &Dum {
8+
d := Dum{
9+
v: 1
10+
}
11+
return &d
12+
}
13+
14+
type Dum = Dummy
15+
16+
fn main() {
17+
dummy := if false {
18+
res := &Dum{
19+
v: 2
20+
}
21+
&res
22+
} else {
23+
r := get_dummy()
24+
r
25+
}
26+
27+
println(dummy)
28+
}

0 commit comments

Comments
 (0)